tag:blogger.com,1999:blog-90227855512822617932024-02-07T02:06:20.313-06:00My Brain Is OpenAnonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-9022785551282261793.post-49860924460310859052014-04-14T16:19:00.000-05:002014-04-15T19:45:49.393-05:00Javascript Series for Beginners - Objects<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="docText" style="background-color: white; margin-bottom: 12px; margin-top: 16px; padding: 0px;">
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">Introduction:</span></h2>
<div style="font-size: 13px;">
<span style="font-family: Verdana, sans-serif;">In this post we will talk about how to handle javascript objects.</span></div>
<div style="font-size: 13px;">
<span style="font-family: Verdana, sans-serif;">On the bottom of this post you will find a jsbin that you can use to play with the code snippets below.</span><br />
<span style="font-family: Verdana, sans-serif;">This post was inspired by this book: "<a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742" target="_blank">Javascript: The good parts</a>" by Douglas Crockford</span></div>
<div style="font-size: 13px;">
<span style="font-family: Verdana, sans-serif;"><br />
</span></div>
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">How to create new objects:</span>
</h2>
<div style="font-size: 13px;">
</div>
</div>
<script src="https://gist.github.com/adilmezghouti/10690138.js"></script></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">How to use inheritance:</span>
</h2>
<div style="font-size: 13px;">
</div>
<span style="font-family: Verdana, sans-serif;">Prototype is the mechanism by which objects in javascript inherit properties from other objects.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">The prototype link is only used in retrieval. If we look up a property that does not exist in the current object, the prototype link will be used to go up the chain looking if any of the parents has it.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Adding a new property to the parent object will cause to appear in the children but not the other way around.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">Reflection and property deletion:</span>
</h2>
<div style="font-size: 13px;">
</div>
<span style="font-family: Verdana, sans-serif;">Deleting a property from the child does not impact the parent but it is true the other way around.</span><br />
<span style="font-family: Verdana, sans-serif;">Also if you delete a property from the child and the parent has the same property, the parent's value will shine through.</span><br />
<script src="https://gist.github.com/adilmezghouti/10690673.js"></script>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">Global scope problem:</span>
</h2>
<div style="font-size: 13px;">
</div>
<span style="font-family: Verdana, sans-serif;">One of the big downsides of javascript is that it throws all the objects in the global scope, one way to work around this, is to create a variable that will offer a new scope for your objects and prevent them from conflicting with objects from other applications and librairies.</span><br />
<script src="https://gist.github.com/adilmezghouti/7500130ca2b37de9890d.js"></script>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h2 style="text-align: left;">
<span style="font-family: Verdana, sans-serif; font-size: small;">Your Lab: </span>
</h2>
<div style="font-size: 13px;">
</div>
<span style="font-family: Verdana, sans-serif;">Feel free to play with the code here.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><a class="jsbin-embed" href="http://jsbin.com/quyul/2/embed?js,console">JS Bin</a></span><script src="http://static.jsbin.com/js/embed.js"></script>
</div>
<br /><br />
<script type="text/javascript"><!--
amazon_ad_tag = "mybraisope-20"; amazon_ad_width = "728"; amazon_ad_height = "90";//--></script>
<script type="text/javascript" src="http://ir-na.amazon-adsystem.com/s/ads.js"></script>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-41018286335794358562014-04-03T18:52:00.001-05:002014-04-03T18:52:41.965-05:00Useful unix commands<div dir="ltr" style="text-align: left;" trbidi="on">
This is a gist where I save useful unix commands as I come across them.<script src="https://gist.github.com/adilmezghouti/9932354.js"></script>
</div>
Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-13440890874614359932014-03-04T02:32:00.004-06:002014-04-15T19:47:11.490-05:00How to Deploy a Sharded MongoDB Cluster<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<span style="background-color: transparent; color: black; font-family: Arial; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="font-size: large; font-weight: normal;">Introduction</span></span></h2>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">In this tutorial we will show how to deploy a sharded MongoDb cluster. We assume that you already have MongoDB and Node.js installed.</span></div>
<b id="docs-internal-guid-08166539-8c29-aac2-0f90-1c7dac7ab46b" style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Before moving forward, let's explain some key concepts that will make this tutorial easier to follow:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- Sharding: It is the fact of adding more machines to support data growth and greater demand of read and write operations.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- Cluster: It is a set of connected servers that can be viewed as one system.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Our cluster will be composed of the following components:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- 2 shards: these are just regular mongodb databases to store the data </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- 1 query router: also named mongos, it interfaces with client applications and direct operations to the appropriate shard.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">- 1 config server: this is a mongodb database that stores the cluster's metadata including the mapping of the cluster's data set to the shards. The query router uses this metadata to route requests to shards.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<h2 style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt; text-align: left;">
<span style="background-color: transparent; color: black; font-family: Arial; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="font-size: large; font-weight: normal;">Installation</span></span></h2>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Let's start by creating our 2 shards.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The shard needs a folder that will house its files. You can create it like so (-p is to create all the nested folders in the path if they don't exist):</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mkdir -p /data/db1</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Now you can start the first shard by issuing this command:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongod --shardsvr --port 27016 --dbpath /data/db1 </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Let's repeat the same steps for the second shard:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mkdir -p /data/db2</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongod --shardsvr --port 27017 --dbpath /data/db2 </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Now let's configure the config server.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Issue the following command to create the folder that will house the files of the config server:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mkdir -p /data/configdb</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">then start the config server like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongod --configsvr --dbpath /data/configdb --port 27018</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The only component left is the query router server (mongos). You can start it by typing the following command:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos --configdb localhost:27018 --port 27019 </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">All the components of our cluster are now installed and running. Let's move to the next step and configure our system to use sharding.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<h2 style="text-align: left;">
<span style="background-color: transparent; color: black; font-family: Arial; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="font-size: large; font-weight: normal;">Sharding configuration</span></span></h2>
<h2 style="text-align: left;">
<span style="font-family: Arial; font-size: 13px; font-weight: normal; line-height: 1.15; white-space: pre-wrap;">For this step, we will be using the "test" database.</span></h2>
</div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">In order to start the configuration, we need to connect to the mongos instance. The connection can be established like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongo --host localhost --port 27019 </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Add the first shard to the cluster by typing this command from the mongos console prompt:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> sh.addShard("localhost:27016")</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">You should see an output matching this line:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">{ "shardAdded" : "shard0000", "ok" : 1 }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Add the second shard:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> sh.addShard("localhost:27017")</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The output should match this:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">{ "shardAdded" : "shard0001", "ok" : 1 }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">To verify that the two shards have been created, type the following command:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> sh.status()</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">You should see two shards listed in the output like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">chunks:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> shard0000 <span class="Apple-tab-span" style="white-space: pre;"> </span>1</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> shard0001 <span class="Apple-tab-span" style="white-space: pre;"> </span>1</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">To enable sharding for the db, you can type the following command:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> </span><span style="background-color: whitesmoke; color: #232323; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">sh.enableSharding("test")</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The output should be something like this:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: whitesmoke; color: #232323; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">output: </span><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">{ "ok" : 1 }</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">To enable sharding for a collection, issue this command (the collection will be created if it does not exist):</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> sh.shardCollection("test.location", { "zipcode": 1} ) </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">You should see this output:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">{ "collectionsharded" : "test.location", "ok" : 1 } </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<h2 style="text-align: left;">
<span style="background-color: transparent; color: black; font-family: Arial; font-style: normal; font-variant: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"><span style="font-size: large; font-weight: normal;">Verification and Testing</span></span></h2>
<h2 style="text-align: left;">
<span style="font-family: Arial; font-size: 13px; font-weight: normal; line-height: 1.15; white-space: pre-wrap;">Now it is time to test our setup and verify that the whole system works as expected. To achieve that, we need to write some code to insert 300000 records into the database and then verify that this data has actually been distributed over the two shards we have.</span></h2>
</div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">For our test, let's create a file called "sharded_cluster_test.js" and add the below Node.js snippet to it. This code will insert some data into our sharded cluster, :</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">var MongoClient = require('mongodb').MongoClient</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>, format = require('util').format;</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">MongoClient.connect('mongodb://127.0.0.1:27019/test', function(err, db) {</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>if(err) throw err;</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>var collection = db.collection('location');</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>for(i=0;i < 300000;i++){</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>collection.insert({ "zipcode" : i}, function(){});</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>}</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> <span class="Apple-tab-span" style="white-space: pre;"> </span>db.close();</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">});</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Run this file like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">node sharded_cluster_test.js</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Verify that the data made it to the database:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> use test</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongos> db.location.count() </span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The above command should return: 300000</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Let's now verify it has been distributed over our two shards.</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Log into the first shard like so:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongo --host localhost --port 27016</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">and then count the items that have been added:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">db.location.count()</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Repeat the same steps for the second shard:</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">mongo --host localhost --port 27017</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">db.location.count()</span></div>
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">~~~~</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">The total of the items added to both shards should be equal to 300000</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Congratulations! you have successfully deployed your first MongoDb sharded cluster.</span></div>
<b style="font-weight: normal;"><br /><span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"></span></b>
<br />
<div dir="ltr" style="line-height: 1.15; margin-bottom: 0pt; margin-top: 0pt;">
<span style="background-color: transparent; color: black; font-family: Arial; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">Resources</span></div>
<a href="http://docs.mongodb.org/manual/sharding/" style="text-decoration: none;"><span style="color: #1155cc; font-family: Arial; font-size: 13px; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap;">http://docs.mongodb.org/manual/sharding/</span></a></div>
<br /><br />
<script type="text/javascript"><!--
amazon_ad_tag = "mybraisope-20"; amazon_ad_width = "728"; amazon_ad_height = "90";//--></script>
<script type="text/javascript" src="http://ir-na.amazon-adsystem.com/s/ads.js"></script>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0Morocco31.791702 -7.092620000000010831.791702 -7.0926200000000108 31.791702 -7.0926200000000108tag:blogger.com,1999:blog-9022785551282261793.post-51672569832984400132011-09-18T22:15:00.001-05:002014-04-15T19:46:31.617-05:00How to execute tasks in separate threads in Objective C<div dir="ltr" style="text-align: left;" trbidi="on">
This code snippet shows how you can execute tasks in two separate threads:<br />
<br />
<br />
<div class="p1">
<span class="s1">dispatch_queue_t</span> download_queue = <span class="s2">dispatch_queue_create</span>(<span class="s3">"Home View Controller"</span>, <span class="s4">NULL</span>);</div>
<div class="p1">
//first thread</div>
<div class="p1">
<span class="s2">dispatch_async</span>(download_queue, ^{ </div>
<div class="p1">
<span class="s1">NSError</span> *error = <span class="s4">nil</span>;</div>
<div class="p1">
<span class="s1">NSArray</span> *array = [[<span class="s5">NetworkService</span> <span class="s6">getThingsByAccountId</span>:<span class="s5">accountId</span> <span class="s6">error</span>:&error] <span class="s6">positions</span>]; </div>
<div class="p1">
//second thread, we use the main thread to run ui related activities</div>
<div class="p2">
<span class="s7"> </span><span class="s2">dispatch_async</span><span class="s7">(</span>dispatch_get_main_queue<span class="s7">(),^{</span></div>
<div class="p1">
[<span class="s5">spinner</span> <span class="s2">stopAnimating</span>];</div>
<div class="p1">
<span class="s5">ViewController</span> *controller = [[<span class="s5">ViewController</span> <span class="s2">alloc</span>] <span class="s6">initWithNibName</span>:<span class="s3">@"ViewController" array:array</span>];</div>
<div class="p1">
[<span class="s4">self</span>.<span class="s1">navigationController</span> <span class="s2">pushViewController</span>:controller <span class="s2">animated</span>:<span class="s4">YES</span>];</div>
<div class="p1">
[controller <span class="s2">release</span>]; </div>
<div class="p1">
});</div>
<div class="p1">
});</div>
<div class="p1">
<span class="s2">dispatch_release</span>(download_queue); </div>
</div>
<br />
<br />
<script type="text/javascript"><!--
amazon_ad_tag = "mybraisope-20"; amazon_ad_width = "728"; amazon_ad_height = "90";//--></script>
<script type="text/javascript" src="http://ir-na.amazon-adsystem.com/s/ads.js"></script>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-15341525598557571282011-01-12T20:53:00.000-06:002011-01-12T20:53:12.251-06:00How to fix blank screen due to undetected graphics card on ubuntuI recently encountered an issue while installing Ubuntu 10.04 Lucid Lynx on a HP Proliant Desktop with an Nvidia display card. After the installation finished and i boot up the system, i got a blank screen.<div><br />
<div>Here is how this issue can be fixed:</div><div>- Boot up the system using an Ubuntu Live CD and press any key when you see a human and a little keyboard at the bottom of the screen</div><div>- press F6 and then Esc.</div><div>- Append "xforcevesa" at the end of the command line.</div><div>- Select try out Ubuntu</div><div>- Mount the hard drive (in the Places).</div><div>- Edit this file "/etc/default/grub" and locate GRUB_CMDLINE_LINUX_DEFAULT</div><div>- Change it to be like so if you have an nvidia display card: GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"</div><div>- Edit this file "/boot/grub/grub.cfg" in the hard drive</div><div>- Locate the "linux" tag and add "nomodeset" at the end of the command with "quiet splash"</div><div>- Reboot the system without the Live CD.</div><div>- Done</div></div>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-87483170473335972542010-12-22T23:24:00.000-06:002010-12-22T23:24:30.708-06:00How Garbage Collection works in Java<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Garbage collection is one of the features that made Java very popular, it saved developers the burden of having to deal with pointers. This convenience came at a price though; developers had to give up control over the objects' cleanup, only the JVM can decide when to collect objects.</span><br />
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><br />
</span><br />
<b><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Heap memory arrangement:</span></b><br />
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">The heap is divided into 3 main spaces:</span><br />
<br />
<ol><li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Young: contains the newly created objects. It usually contains 3 spaces; Eden and 2 survivor spaces that active objects get copied in between.</span></li>
<li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Tenured: contains old objects.</span></li>
<li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Perm: it holds data needed by the virtual machine.</span></li>
</ol><div><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">The JVM comes with a default arrangement of generations, but you can always change their sizes to fit the requirements of your system.</span></div><div><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><br />
</span></div><div><b><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Collectors:</span></b></div><br />
<span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">The collectors mentioned below are stop-the-world "tracing collectors"<b> </b>in which a collector starts from the root reference and follows all the references until all reachable objects have been examined. </span><br />
<ul><li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>Mark-sweep: </b>It marks all the nodes it visits and after it is done, it collects the non-marked ones. It is used to collect objects in the old generation region of the heap.</span></li>
<li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><b>Copying Collectors: </b>When the space of memory dedicated to active objects fills up this collector runs and copies the live objects to another space of the heap and then switch the roles of these two spaces. This type of collectors is used to collect objects in the young generation region of the heap.</span></li>
</ul><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">Developers have the option of running the collectors above concurrently by using the following flags:</span><br />
<br />
<ul><li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">-XX:+UseConcMarkSweepGC : enables concurrent collector</span></li>
<li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">-XX:+UseParNewGC : enables parallel copying collector</span></li>
<li><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;">XX:UseParallelGC : enables parallel scavenge collector </span></li>
</ul><br />
For more details, check out these links:<br />
<a href="http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html">http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html</a><br />
<a href="http://www.javaperformancetuning.com/news/qotm026.shtml">http://www.javaperformancetuning.com/news/qotm026.shtml</a><br />
<a href="http://www.ibm.com/developerworks/java/library/j-jtp10283/">http://www.ibm.com/developerworks/java/library/j-jtp10283/</a>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-15109695279297674542010-11-16T23:11:00.000-06:002010-11-16T23:11:39.557-06:00Cloud ComputingWith all the hype going on around cloud computing and the attraction it is gaining day after day, i thought it is time for me to put my head in the cloud as well.<br />
So I did a little research, put together some material and wrote a powerpoint presentation that you can access <a href="https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B6628Yt0LVHSNWQwMjM3NWYtMTBmMC00NmY3LWJjYzYtMTAzOWQ5Mjg0OWRk&hl=en">here</a>.<br />
Feel free to leave any feedback or thoughts about the presentation.Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-71560607702567259132010-11-14T20:28:00.004-06:002010-11-14T20:41:38.475-06:00How to parse xml using Objective C<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" /> <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script> <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript"></script> <script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript"></script><br />
<br />
There will always be a need to parse xml, especially if you are dealing with web services. In this post i will show how to use NSXMLParser to parse the xml you get back from Amazon Web Service when you attach a device to a running EC2 instance.<br />
Here is what the xml response looks like:<br />
<pre class="brush:xml"><AttachVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/">
<volumeId>vol-4d826724</volumeId>
<instanceId>i-6058a509</instanceId>
<device>/dev/sdh</device>
<status>attaching</status>
<attachTime>2008-05-07T11:51:50.000Z</attachTime>
</AttachVolumeResponse>
</pre><br />
To do the parsing , we need to use <span class="Apple-style-span" style="font-family: Menlo; font-size: 11px;"><b>NSXMLParserDelegate</b> </span>protocol and we will implement three of its methods:<br />
<ul><li>– parser:didStartElement:namespaceURI:qualifiedName:attributes:</li>
<li>– parser:didEndElement:namespaceURI:qualifiedName:</li>
<li>– parser:foundCharacters:</li>
</ul><div>Here is what the header file looks like:</div><div><title></title> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #743fa4}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4b8186}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c02d9d}
span.s1 {color: #c02d9d}
span.s2 {color: #000000}
span.s3 {color: #4b8186}
span.s4 {color: #743fa4}
span.Apple-tab-span {white-space:pre}
</style> <br />
<div class="p1"><span class="s1">@interface</span> AttachVolumeParser : NSObject <nsxmlparserdelegate> {</nsxmlparserdelegate></div><div class="p2"><span class="s2"> </span>NSMutableArray<span class="s2"> *</span><span class="s3">items</span><span class="s2">;</span></div><div class="p3"><span class="s2"> </span>AttachVolumeInfo<span class="s2"> *</span>volumeInfo<span class="s2">;</span></div><div class="p3"><span class="s2"> </span><span class="s4">NSString</span><span class="s2"> *</span>keyInProgress<span class="s2">;</span></div><div class="p2"><span class="s2"> </span>NSMutableString<span class="s2"> *</span><span class="s3">textInProgress</span><span class="s2">;</span></div><div class="p3"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s1">bool</span><span class="s2"> </span>isItemInProgress<span class="s2">;</span></div><div class="p1">}</div><div class="p4"><br />
</div><div class="p5">@property<span class="s2">(</span>nonatomic<span class="s2">, </span>assign<span class="s2">) </span>bool<span class="s2"> isItemInProgress;</span></div><div class="p4"><br />
</div><div class="p1">- (<span class="s1">BOOL</span>)parseData:(<span class="s4">NSData</span> *)d;</div><div class="p1">- (<span class="s4">NSArray</span> *)items;</div><div class="p5">@end</div><div class="p5"><br />
</div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">items is the array that will hold the objects resulting from parsing the xml.</span></span></span></div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">you need to create an object that corresponds to the xml above and that i refer to in above code snippet as AttachVolumeInfo.</span></span></span></div><div class="p5"><span class="Apple-style-span" style="color: black;"><br />
</span></div><div class="p5"><span class="Apple-style-span" style="color: black; font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">And the implementation looks something like this:</span></span></div><div class="p5"><title></title> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d62c24}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4b8186}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c02d9d}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a482f}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008324}
span.s1 {color: #7a482f}
span.s2 {color: #c02d9d}
span.s3 {color: #000000}
span.s4 {color: #4b8186}
span.s5 {color: #743fa4}
span.s6 {color: #40207c}
span.s7 {color: #d62c24}
span.s8 {color: #2e595d}
span.Apple-tab-span {white-space:pre}
</style> </div><div class="p1" style="font-family: Times, 'Times New Roman', serif; font-size: medium;"><span class="Apple-style-span" style="color: #7a482f;"> <title></title> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d62c24}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4b8186}
p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #c02d9d}
p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #7a482f}
p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008324}
span.s1 {color: #7a482f}
span.s2 {color: #c02d9d}
span.s3 {color: #000000}
span.s4 {color: #4b8186}
span.s5 {color: #743fa4}
span.s6 {color: #40207c}
span.s7 {color: #d62c24}
span.s8 {color: #2e595d}
span.Apple-tab-span {white-space:pre}
</style> </span></div><div class="p1"><span class="Apple-style-span" style="color: #7a482f;"><span class="s1">#import </span>"AttachVolumeParser.h"</span></div><span class="Apple-style-span" style="color: #7a482f;"> </span><br />
<div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"><span class="s2">static</span> NSSet *interestingKeys;</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"><span class="s2">@implementation</span> AttachVolumeParser</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"><span class="s2">@synthesize</span> isItemInProgress;</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">+ (<span class="s2">void</span>)initialize</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span><span class="s2">if</span><span class="s3"> (!</span>interestingKeys<span class="s3">) {</span></span></div><div class="p1"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span><span class="s4">interestingKeys</span><span class="s3"> = [[</span><span class="s5">NSSet</span><span class="s3"> </span><span class="s6">alloc</span><span class="s3">] </span><span class="s6">initWithObjects</span><span class="s3">:</span>@"volumeId"<span class="s3">, </span>@"instanceId"<span class="s3">, </span>@"device"<span class="s3">, </span>@"status"<span class="s3">, </span>@"attachTime"<span class="s3">, </span><span class="s2">nil</span><span class="s3">];</span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s2">void</span>)dealloc</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [<span class="s4">items</span> <span class="s6">release</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [<span class="s2">super</span> <span class="s6">dealloc</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s2">BOOL</span>)parseData:(<span class="s5">NSData</span> *)d</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [<span class="s4">items</span> <span class="s6">release</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s4">items</span> = [[<span class="s5">NSMutableArray</span> <span class="s6">alloc</span>] <span class="s6">init</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s5">NSXMLParser</span> *parser = [[<span class="s5">NSXMLParser</span> <span class="s6">alloc</span>] <span class="s6">initWithData</span>:d];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [parser <span class="s6">setDelegate</span>:<span class="s2">self</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [parser <span class="s6">parse</span>];</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [parser <span class="s6">release</span>];</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span></span></div><div class="p5"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span>return<span class="s3"> </span>YES<span class="s3">;</span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s5">NSArray</span> *)items</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">return</span> <span class="s4">items</span>;</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p6"><span class="Apple-style-span" style="color: #7a482f;">#pragma mark Delegate calls</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s2">void</span>)parser:(<span class="s5">NSXMLParser</span> *)parser</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">didStartElement:(<span class="s5">NSString</span> *)elementName</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> namespaceURI:(<span class="s5">NSString</span> *)namespaceURI</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> qualifiedName:(<span class="s5">NSString</span> *)qName</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> attributes:(<span class="s5">NSDictionary</span> *)attributeDict</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">if</span> ([elementName <span class="s6">isEqual</span>:<span class="s7">@"AttachVolumeResponse"</span>]) {</span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>isItemInProgress<span class="s3"> = </span><span class="s1">true</span><span class="s3">;</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>volumeInfo<span class="s3"> = [[</span>AttachVolumeInfo<span class="s3"> </span><span class="s6">alloc</span><span class="s3">] </span><span class="s6">init</span><span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">return</span>;</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span></span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">if</span> ([<span class="s4">interestingKeys</span> <span class="s6">containsObject</span>:elementName]) {</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s4">keyInProgress</span> = [elementName <span class="s6">copy</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s4">textInProgress</span> = [[<span class="s5">NSMutableString</span> <span class="s6">alloc</span>] <span class="s6">init</span>];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s2">void</span>)parser:(<span class="s5">NSXMLParser</span> *)parser</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> didEndElement:(<span class="s5">NSString</span> *)elementName</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> namespaceURI:(<span class="s5">NSString</span> *)namespaceURI</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> qualifiedName:(<span class="s5">NSString</span> *)qName</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">if</span> ([elementName <span class="s6">isEqual</span>:<span class="s7">@"AttachVolumeResponse"</span>]) {</span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[</span>items<span class="s3"> </span><span class="s6">addObject</span><span class="s3">:</span>volumeInfo<span class="s3">];</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[</span>volumeInfo<span class="s3"> </span><span class="s6">release</span><span class="s3">];</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>volumeInfo<span class="s3"> = </span><span class="s2">nil</span><span class="s3">;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>isItemInProgress<span class="s3"> = </span><span class="s1">false</span><span class="s3">;</span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">return</span>;</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span></span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span></span></div><div class="p7"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span>// Is the current key complete?</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">if</span> ([elementName <span class="s6">isEqual</span>:<span class="s4">keyInProgress</span>]) {</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> <span class="s2">if</span>([elementName <span class="s6">isEqual</span>:<span class="s7">@"volumeId"</span>]) {</span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>volumeInfo<span class="s3"> </span><span class="s8">setVolumeId</span><span class="s3">:</span>textInProgress<span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> } <span class="s2">else</span> <span class="s2">if</span>([elementName <span class="s6">isEqual</span>:<span class="s7">@"instanceId"</span>]) {<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>volumeInfo<span class="s3"> </span><span class="s8">setInstanceId</span><span class="s3">:</span>textInProgress<span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> } <span class="s2">else</span> <span class="s2">if</span>([elementName <span class="s6">isEqual</span>:<span class="s7">@"device"</span>]) {<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>volumeInfo<span class="s3"> </span><span class="s8">setDevice</span><span class="s3">:</span>textInProgress<span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> } <span class="s2">else</span> <span class="s2">if</span>([elementName <span class="s6">isEqual</span>:<span class="s7">@"status"</span>]) {<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>volumeInfo<span class="s3"> </span><span class="s8">setStatus</span><span class="s3">:</span>textInProgress<span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> } <span class="s2">else</span> <span class="s2">if</span>([elementName <span class="s6">isEqual</span>:<span class="s7">@"attachTime"</span>]) {<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>volumeInfo<span class="s3"> </span><span class="s8">setAttachTime</span><span class="s3">:</span>textInProgress<span class="s3">];<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></div><div class="p7"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span>// Clear the text and key</span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>textInProgress<span class="s3"> </span><span class="s6">release</span><span class="s3">];</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span>textInProgress<span class="s3"> = </span><span class="s2">nil</span><span class="s3">;</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> [</span>keyInProgress<span class="s3"> </span><span class="s6">release</span><span class="s3">];</span></span></div><div class="p4"><span class="Apple-style-span" style="color: #7a482f;"><span class="s3"> </span>keyInProgress<span class="s3"> = </span><span class="s2">nil</span><span class="s3">;</span></span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> }</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p7"><span class="Apple-style-span" style="color: #7a482f;">// This method can get called multiple times for the</span></div><div class="p7"><span class="Apple-style-span" style="color: #7a482f;">// text in a single element</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">- (<span class="s2">void</span>)parser:(<span class="s5">NSXMLParser</span> *)parser</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">foundCharacters:(<span class="s5">NSString</span> *)string</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">{</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;"> [<span class="s4">textInProgress</span> <span class="s6">appendString</span>:string];</span></div><div class="p3"><span class="Apple-style-span" style="color: #7a482f;">}</span></div><div class="p2"><span class="Apple-style-span" style="color: #7a482f;"><br />
</span></div><div class="p5"><span class="Apple-style-span" style="color: #7a482f;">@end</span></div><br />
<div class="p5" style="color: black; font-family: Times, 'Times New Roman', serif; font-size: medium;"><br />
</div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">What i like about this method is that it is fast due to its event-based nature, but on the other hand i need to write a new parser every time i have a new xml schema. </span></span></span></div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">I have already tried to use ObjectiveResource to parse amazon xml schemas but it was a little bit challenging, hopefully i will find some time soon to grab the source code and modify it to do that.</span></span></span></div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;"><br />
</span></span></span></div><div class="p5"><span class="Apple-style-span" style="color: black;"><span class="Apple-style-span" style="font-family: Times, 'Times New Roman', serif;"><span class="Apple-style-span" style="font-size: medium;">If you have done any amazon xml parsing using a better method, you are welcome to share.</span></span></span></div></div><br />
<script type="text/javascript">
SyntaxHighlighter.all()
</script>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-58932080893398527242010-11-08T13:35:00.000-06:002010-11-08T13:35:53.144-06:00How to use hmac-md5 to encrypt data using Objective CI am including a code snippet that will show you how to do that.<br />
<br />
<title></title> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1e81}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a}
span.s1 {color: #703daa}
span.s2 {color: #000000}
span.s3 {color: #ba2da2}
span.s4 {color: #3d1e81}
span.s5 {color: #d12e1b}
span.Apple-tab-span {white-space:pre}
</style> <br />
<div class="p1"> <title></title> <style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1e81}
p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px}
p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a}
span.s1 {color: #703daa}
span.s2 {color: #000000}
span.s3 {color: #ba2da2}
span.s4 {color: #3d1e81}
span.Apple-tab-span {white-space:pre}
</style> <br />
<div class="p1">+(<span class="s1">NSData</span>*) calculateHMACMD5WithKey:(<span class="s1">NSString</span>*) key andData:(<span class="s1">NSString</span>*) data{</div><div class="p2"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">const</span><span class="s2"> </span><span class="s3">char</span><span class="s2"> *cKey = [key </span>cStringUsingEncoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">];</span></div><div class="p2"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">const</span><span class="s2"> </span><span class="s3">char</span><span class="s2"> *cData = [data </span>cStringUsingEncoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">];</span></div><div class="p3"><span class="Apple-tab-span"> </span></div><div class="p4"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">unsigned</span><span class="s2"> </span><span class="s3">char</span><span class="s2"> cHMAC[</span>CC_MD5_DIGEST_LENGTH<span class="s2">];</span></div><div class="p3"><span class="Apple-tab-span"> </span></div><div class="p1"><span class="Apple-tab-span"> </span><span class="s4">CCHmac</span>(<span class="s4">kCCHmacAlgMD5</span>, cKey, <span class="s4">strlen</span>(cKey), cData, <span class="s4">strlen</span>(cData), cHMAC);</div><div class="p3"><span class="Apple-tab-span"> </span></div><div class="p1"><span class="Apple-tab-span"> </span><span class="s3">return</span> [[<span class="s1">NSData</span> <span class="s4">alloc</span>] <span class="s4">initWithBytes</span>:cHMAC <span class="s4">length</span>:<span class="s3">sizeof</span>(cHMAC)];</div><div class="p3"><span class="Apple-tab-span"> </span></div><div class="p1">}</div></div>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-88886529958223683312010-11-06T13:09:00.005-05:002010-11-06T13:20:45.544-05:00How to create a UIImage with rounded cornersIn this post, i am including a code snippet that can be used to create a UIImage with rounded corners.
</div><div><meta equiv="Content-Type" content="text/html; charset=UTF-8"> <meta equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> <meta name="CocoaVersion" content="1038.32"> <style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1e81} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12e1b} span.s1 {color: #000000} span.s2 {color: #ba2da2} span.s3 {color: #703daa} span.s4 {color: #252bd8} span.s5 {color: #3d1e81} span.s6 {color: #31595e} span.Apple-tab-span {white-space:pre} </style> <p class="p1">@implementation<span class="s1"> ImageUtils</span></p> <p class="p2">
</p> <p class="p3"><span class="s2">void</span> addRoundedRectToPath(<span class="s3">CGContextRef</span> context, <span class="s3">CGRect</span> rect, <span class="s2">float</span> ovalWidth, <span class="s2">float</span> ovalHeight)</p> <p class="p3">{</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">float</span> fw, fh;</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">if</span> (ovalWidth == <span class="s4">0</span> || ovalHeight == <span class="s4">0</span>) {</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">CGContextAddRect</span>(context, rect);</p> <p class="p1"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>return<span class="s1">;</span></p> <p class="p3"><span class="Apple-tab-span"> </span>}</p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextSaveGState<span class="s1">(context);</span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextTranslateCTM<span class="s1"> (context, </span>CGRectGetMinX<span class="s1">(rect), </span>CGRectGetMinY<span class="s1">(rect));</span></p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextScaleCTM</span> (context, ovalWidth, ovalHeight);</p> <p class="p3"><span class="Apple-tab-span"> </span>fw = <span class="s5">CGRectGetWidth</span> (rect) / ovalWidth;</p> <p class="p3"><span class="Apple-tab-span"> </span>fh = <span class="s5">CGRectGetHeight</span> (rect) / ovalHeight;</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextMoveToPoint</span>(context, fw, fh/<span class="s4">2</span>);</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextAddArcToPoint</span>(context, fw, fh, fw/<span class="s4">2</span>, fh, <span class="s4">1</span>);</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextAddArcToPoint</span>(context, <span class="s4">0</span>, fh, <span class="s4">0</span>, fh/<span class="s4">2</span>, <span class="s4">1</span>);</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextAddArcToPoint</span>(context, <span class="s4">0</span>, <span class="s4">0</span>, fw/<span class="s4">2</span>, <span class="s4">0</span>, <span class="s4">1</span>);</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextAddArcToPoint</span>(context, fw, <span class="s4">0</span>, fw, fh/<span class="s4">2</span>, <span class="s4">1</span>);</p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextClosePath<span class="s1">(context);</span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextRestoreGState<span class="s1">(context);</span></p> <p class="p3">}</p> <p class="p2">
</p> <p class="p3">+ (<span class="s3">UIImage</span> *)roundCornersOfImage:(<span class="s3">UIImage</span> *)source;</p> <p class="p3">{</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">int</span> w = source.<span class="s3">size</span>.<span class="s3">width</span>;</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">int</span> h = source.<span class="s3">size</span>.<span class="s3">height</span>;</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s5">NSLog</span><span class="s1">(</span>@"width: %d height: %d"<span class="s1">, w, h);</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s3">CGColorSpaceRef</span><span class="s1"> colorSpace = </span>CGColorSpaceCreateDeviceRGB<span class="s1">();</span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span><span class="s3">CGContextRef</span><span class="s1"> context = </span>CGBitmapContextCreate<span class="s1">(</span><span class="s2">NULL</span><span class="s1">, w, h, </span><span class="s4">8</span><span class="s1">, </span><span class="s4">4</span><span class="s1"> * w, colorSpace, </span>kCGImageAlphaPremultipliedFirst<span class="s1">);</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextBeginPath<span class="s1">(context);</span></p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s3">CGRect</span> rect = <span class="s5">CGRectMake</span>(<span class="s4">0</span>, <span class="s4">0</span>, w, h);</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s6">addRoundedRectToPath</span>(context, rect, <span class="s4">5</span>, <span class="s4">5</span>);</p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextClosePath<span class="s1">(context);</span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextClip<span class="s1">(context);</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s5">CGContextDrawImage</span>(context, <span class="s5">CGRectMake</span>(<span class="s4">0</span>, <span class="s4">0</span>, w, h), source.<span class="s3">CGImage</span>);</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s3">CGImageRef</span> imageMasked = <span class="s5">CGBitmapContextCreateImage</span>(context);</p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGContextRelease<span class="s1">(context);</span></p> <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>CGColorSpaceRelease<span class="s1">(colorSpace);</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p3"><span class="Apple-tab-span"> </span><span class="s2">return</span> [<span class="s3">UIImage</span> <span class="s5">imageWithCGImage</span>:imageMasked]; </p> <p class="p3">}</p> <p class="p2">
</p> <p class="p1">@end</p>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-14857680935551420642010-11-05T23:18:00.003-05:002010-11-06T13:21:51.898-05:00How to do base64 encoding using Objective CI am including in this post a code snippet that can be used to do base64 encoding.
<div>
</div><div><meta equiv="Content-Type" content="text/html; charset=UTF-8"> <meta equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> <meta name="CocoaVersion" content="1038.32"> <style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12e1b} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187} p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1e81} span.s1 {color: #78492a} span.s2 {color: #000000} span.s3 {color: #ba2da2} span.s4 {color: #252bd8} span.s5 {color: #4f8187} span.s6 {color: #3d1e81} span.s7 {color: #703daa} span.s8 {color: #31595e} span.s9 {color: #008400} span.Apple-tab-span {white-space:pre} </style> <p class="p1"><span class="s1">#import </span>"Base64Encoder.h"</p> <p class="p2">#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))</p> <p class="p3">
</p> <p class="p4">@implementation<span class="s2"> Base64Encoder</span></p> <p class="p3">
</p> <p class="p1"><span class="s3">static</span><span class="s2"> </span><span class="s3">char</span><span class="s2"> encodingTable[] = </span>"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"<span class="s2">;</span></p> <p class="p5"><span class="s3">static</span> <span class="s3">char</span> decodingTable[<span class="s4">128</span>];</p> <p class="p3">
</p> <p class="p5">+ (<span class="s3">void</span>) initialize {</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">if</span> (<span class="s3">self</span> == [<span class="s5">Base64Encoder</span> <span class="s6">class</span>]) {</p> <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s1">memset</span><span class="s2">(</span>decodingTable<span class="s2">, </span><span class="s4">0</span><span class="s2">, </span><span class="s1">ArrayLength</span><span class="s2">(</span>decodingTable<span class="s2">));</span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">for</span> (<span class="s7">NSInteger</span> i = <span class="s4">0</span>; i < <span class="s1">ArrayLength</span>(<span class="s5">encodingTable</span>); i++) {</p> <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>decodingTable<span class="s2">[</span>encodingTable<span class="s2">[i]] = i;</span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}</p> <p class="p5"><span class="Apple-tab-span"> </span>}</p> <p class="p5">}</p> <p class="p3">
</p> <p class="p3">
</p> <p class="p5">+ (<span class="s7">NSString</span>*) encode:(<span class="s3">const</span> <span class="s7">uint8_t</span>*) input length:(<span class="s7">NSInteger</span>) length {</p> <p class="p5"> <span class="s7">NSMutableData</span>* data = [<span class="s7">NSMutableData</span> <span class="s6">dataWithLength</span>:((length + <span class="s4">2</span>) / <span class="s4">3</span>) * <span class="s4">4</span>];</p> <p class="p5"> <span class="s7">uint8_t</span>* output = (<span class="s7">uint8_t</span>*)data.mutableBytes;</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p5"> <span class="s3">for</span> (<span class="s7">NSInteger</span> i = <span class="s4">0</span>; i < class="s4">3</span>) {</p> <p class="p5"> <span class="s7">NSInteger</span> value = <span class="s4">0</span>;</p> <p class="p5"> <span class="s3">for</span> (<span class="s7">NSInteger</span> j = i; j < (i + <span class="s4">3</span>); j++) {</p> <p class="p5"> value <<= <span class="s4">8</span>;</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p> <p class="p5"> <span class="s3">if</span> (j <> <p class="p5"> value |= (<span class="s4">0xFF</span> & input[j]);</p> <p class="p5"> }</p> <p class="p5"> }</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p> <p class="p5"> <span class="s7">NSInteger</span> index = (i / <span class="s4">3</span>) * <span class="s4">4</span>;</p> <p class="p5"> output[index + <span class="s4">0</span>] = <span class="s5">encodingTable</span>[(value >> <span class="s4">18</span>) & <span class="s4">0x3F</span>];</p> <p class="p5"> output[index + <span class="s4">1</span>] = <span class="s5">encodingTable</span>[(value >> <span class="s4">12</span>) & <span class="s4">0x3F</span>];</p> <p class="p5"> output[index + <span class="s4">2</span>] = (i + <span class="s4">1</span>) < class="s5">encodingTable</span>[(value >> <span class="s4">6</span>) & <span class="s4">0x3F</span>] : <span class="s4">'='</span>;</p> <p class="p5"> output[index + <span class="s4">3</span>] = (i + <span class="s4">2</span>) < class="s5">encodingTable</span>[(value >> <span class="s4">0</span>) & <span class="s4">0x3F</span>] : <span class="s4">'='</span>;</p> <p class="p5"> }</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p7"><span class="s2"> </span><span class="s3">return</span><span class="s2"> [[[</span><span class="s7">NSString</span><span class="s2"> </span>alloc<span class="s2">] </span>initWithData<span class="s2">:data</span></p> <p class="p7"><span class="s2"> </span>encoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">] </span>autorelease<span class="s2">];</span></p> <p class="p5">}</p> <p class="p3">
</p> <p class="p3">
</p> <p class="p5">+ (<span class="s7">NSString</span>*) encode:(<span class="s7">NSData</span>*) rawBytes {</p> <p class="p5"> <span class="s3">return</span> [<span class="s3">self</span> <span class="s8">encode</span>:(<span class="s3">const</span> <span class="s7">uint8_t</span>*) rawBytes.<span class="s7">bytes</span> <span class="s8">length</span>:rawBytes.<span class="s6">length</span>];</p> <p class="p5">}</p> <p class="p3">
</p> <p class="p3">
</p> <p class="p5">+ (<span class="s7">NSData</span>*) decode:(<span class="s3">const</span> <span class="s3">char</span>*) string length:(<span class="s7">NSInteger</span>) inputLength {</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">if</span> ((string == <span class="s3">NULL</span>) || (inputLength % <span class="s4">4</span> != <span class="s4">0</span>)) {</p> <p class="p4"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>return<span class="s2"> </span>nil<span class="s2">;</span></p> <p class="p5"><span class="Apple-tab-span"> </span>}</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">while</span> (inputLength > <span class="s4">0</span> && string[inputLength - <span class="s4">1</span>] == <span class="s4">'='</span>) {</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>inputLength--;</p> <p class="p5"><span class="Apple-tab-span"> </span>}</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">NSInteger</span> outputLength = inputLength * <span class="s4">3</span> / <span class="s4">4</span>;</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">NSMutableData</span>* data = [<span class="s7">NSMutableData</span> <span class="s6">dataWithLength</span>:outputLength];</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">uint8_t</span>* output = data.mutableBytes;</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">NSInteger</span> inputPoint = <span class="s4">0</span>;</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s7">NSInteger</span> outputPoint = <span class="s4">0</span>;</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">while</span> (inputPoint <> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">char</span> i0 = string[inputPoint++];</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">char</span> i1 = string[inputPoint++];</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">char</span> i2 = inputPoint < class="s4">'A'</span>; <span class="s9">/* 'A' will decode to \0 */</span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">char</span> i3 = inputPoint < class="s4">'A'</span>;</p> <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>output[outputPoint++] = (<span class="s5">decodingTable</span>[i0] << <span class="s4">2</span>) | (<span class="s5">decodingTable</span>[i1] >> <span class="s4">4</span>);</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">if</span> (outputPoint <> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>output[outputPoint++] = ((<span class="s5">decodingTable</span>[i1] & <span class="s4">0xf</span>) << <span class="s4">4</span>) | (<span class="s5">decodingTable</span>[i2] >> <span class="s4">2</span>);</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s3">if</span> (outputPoint <> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>output[outputPoint++] = ((<span class="s5">decodingTable</span>[i2] & <span class="s4">0x3</span>) << <span class="s4">6</span>) | <span class="s5">decodingTable</span>[i3];</p> <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>}</p> <p class="p5"><span class="Apple-tab-span"> </span>}</p> <p class="p3"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="Apple-tab-span"> </span><span class="s3">return</span> data;</p> <p class="p5">}</p> <p class="p3">
</p> <p class="p3">
</p> <p class="p5">+ (<span class="s7">NSData</span>*) decode:(<span class="s7">NSString</span>*) string {</p> <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">return</span><span class="s2"> [</span><span class="s3">self</span><span class="s2"> </span><span class="s8">decode</span><span class="s2">:[string </span>cStringUsingEncoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">] </span><span class="s8">length</span><span class="s2">:string.</span>length<span class="s2">];</span></p> <p class="p5">}</p> <p class="p3">
</p> <p class="p4">@end</p>
</div>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-61271082188865411282010-11-04T18:09:00.008-05:002010-11-04T19:13:03.186-05:00How to configure Fail-over using MySql, Hibernate and C3P0<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript"></script>
Here is the hibernate configuration i used to implement the fail-over for MySql.
<pre class="brush:xml">
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost,host-address/database-name</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.use_second_level_cache">false </property>
</pre>
<script type="text/javascript">
SyntaxHighlighter.all()
</script>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-22057042937427137122010-11-03T09:24:00.003-05:002010-11-03T09:37:40.116-05:00How to use NSDate with Amazon Query APIIn this post i am including a snippet that shows how to format an NSDate so it can be used as a value for the "Expires" key in a query that wil be sent to the Amazon web service.
The standard format for the date is like so: "YYYY-MM-DDThh:mm:ssZ", the "Z" stands for the time zone designator (Z or +hh:mm or -hh:mm).
Snippet:
<div>
</div><div> <style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #40207c} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} span.s1 {color: #743fa4} span.s2 {color: #000000} span.s3 {color: #2f2fd0} span.s4 {color: #40207c} span.s5 {color: #d62c24} span.Apple-tab-span {white-space:pre} </style> <p class="p1"><span class="s1">NSDate</span><span class="s2"> *date = [[[</span><span class="s1">NSDate</span><span class="s2"> </span>alloc<span class="s2">] </span>init<span class="s2">] </span>autorelease<span class="s2">];</span></p> <p class="p1"><span class="s2">date = [date </span>dateByAddingTimeInterval<span class="s2">:</span><span class="s3">120</span><span class="s2">];</span></p><p class="p1"><span class="s2"></span><span class="Apple-style-span" style="color: rgb(0, 0, 0); "><span class="s1">NSDateFormatter</span> *dateFormatter = [[[<span class="s1">NSDateFormatter</span> <span class="s4">alloc</span>] <span class="s4">init</span>] <span class="s4">autorelease</span>];</span></p> <p class="p2">[dateFormatter <span class="s4">setDateFormat</span>:<span class="s5">@"yyyy-MM-dd"</span>];</p> <p class="p2"><span class="s1">NSString</span> *date1 = [dateFormatter <span class="s4">stringFromDate</span>:date];</p> <p class="p2">[dateFormatter <span class="s4">setDateFormat</span>:<span class="s5">@"HH:mm:ss"</span>];</p> <p class="p2"><span class="s1">NSString</span> *date2 = [dateFormatter <span class="s4">stringFromDate</span>:date];</p><p class="p2">
</p><p class="p2"> <style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} span.s1 {color: #743fa4} span.s2 {color: #40207c} span.s3 {color: #d62c24} </style> </p><p class="p1">[<span class="s1">NSString</span> <span class="s2">stringWithFormat</span>:<span class="s3">@"%@T%@-07:00"</span>, date1, date2]</p><p></p></div>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-59436125795192931362010-11-01T00:17:00.009-05:002010-11-01T01:11:44.435-05:00How to construct an Amazon Query based url<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript"></script>
I am currently writing an iPhone app to manage Amazon Cloud infrastructure and thought it might be helpful to share some of the code with the community. <div>In this post i will include two methods, one to url encode a string and the second to construct a Query based url.
<pre>
</div><div></div><div>
<p class="p1"> <meta equiv="Content-Style-Type" content="text/css"> <title></title> <meta name="Generator" content="Cocoa HTML Writer"> <meta name="CocoaVersion" content="1038.32"> <style type="text/css"> p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12e1b} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #ba2da2} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1e81} p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #78492a} p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187} p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa} span.s1 {color: #78492a} span.s2 {color: #000000} span.s3 {color: #703daa} span.s4 {color: #ba2da2} span.s5 {color: #3d1e81} span.s6 {color: #31595e} span.s7 {color: #d12e1b} span.s8 {color: #4f8187} span.Apple-tab-span {white-space:pre} </style> </p><p class="p1"><span class="s1">#import </span>"AWSUtils.h"</p> <p class="p1"><span class="s1">#include <</span>CommonCrypto/CommonHMAC.h></p> <p class="p1"><span class="s1">#import </span>"Base64Encoder.h"</p> <p class="p2">
</p> <p class="p3">@implementation<span class="s2"> AWSUtils</span></p> <p class="p2">
</p> <p class="p4">+(<span class="s3">NSString</span>*) calculateHMACWithKey:(<span class="s3">NSString</span>*) key andData:(<span class="s3">NSString</span>*) data{</p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s4">const</span><span class="s2"> </span><span class="s4">char</span><span class="s2"> *cKey = [key </span>cStringUsingEncoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">];</span></p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s4">const</span><span class="s2"> </span><span class="s4">char</span><span class="s2"> *cData = [data </span>cStringUsingEncoding<span class="s2">:</span>NSASCIIStringEncoding<span class="s2">];</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p6"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s4">unsigned</span><span class="s2"> </span><span class="s4">char</span><span class="s2"> cHMAC[</span>CC_SHA256_DIGEST_LENGTH<span class="s2">];</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s5">CCHmac</span>(<span class="s5">kCCHmacAlgSHA256</span>, cKey, <span class="s5">strlen</span>(cKey), cData, <span class="s5">strlen</span>(cData), cHMAC);</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s3">NSData</span> *hmac = [[<span class="s3">NSData</span> <span class="s5">alloc</span>] <span class="s5">initWithBytes</span>:cHMAC</p> <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span> <span class="s5">length</span>:<span class="s4">sizeof</span>(cHMAC)];</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p7"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s4">return</span><span class="s2"> [</span>Base64Encoder<span class="s2"> </span><span class="s6">encode</span><span class="s2">:hmac];</span></p> <p class="p2">
</p> <p class="p4">}</p> <p class="p2">
</p> <p class="p4">+(<span class="s3">NSString</span>*) urlEncode:(<span class="s3">NSString</span>*) url {</p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s4">return</span><span class="s2"> (</span><span class="s3">NSString</span><span class="s2"> *)</span>CFURLCreateStringByAddingPercentEscapes<span class="s2">(</span><span class="s4">NULL</span><span class="s2">,</span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>(<span class="s3">CFStringRef</span>)url,</p> <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s4">NULL</span>,</p> <p class="p1"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>(</span><span class="s3">CFStringRef</span><span class="s2">)</span>@"!*'();:@+$,/?%#[]"<span class="s2">,</span></p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>kCFStringEncodingUTF8<span class="s2"> );</span></p> <p class="p4">}</p> <p class="p2">
</p> <p class="p4">+(<span class="s3">NSString</span>*) getQueryString:(<span class="s3">NSMutableDictionary</span>*) dict{</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">NSEnumerator</span><span class="s2"> *enumerator = [[[dict </span>allKeys<span class="s2">] </span>sortedArrayUsingSelector<span class="s2">:</span><span class="s4">@selector</span><span class="s2">(</span>caseInsensitiveCompare<span class="s2">:)] </span>objectEnumerator<span class="s2">];</span></p> <p class="p8"><span class="s2"><span class="Apple-tab-span"> </span></span>NSString<span class="s2"> *key;</span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s3">NSString</span> *queryString = [[[<span class="s3">NSString</span> <span class="s5">alloc</span>] <span class="s5">init</span>] <span class="s5">autorelease</span>];</p> <p class="p5"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">NSMutableArray</span><span class="s2"> *queryArray = [[[</span><span class="s3">NSMutableArray</span><span class="s2"> </span>alloc<span class="s2">] </span>initWithCapacity<span class="s2">:[dict </span>count<span class="s2">]] </span>autorelease<span class="s2">];</span></p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s4">while</span> (key = [enumerator <span class="s5">nextObject</span>]) {</p> <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>[queryArray <span class="s5">addObject</span>:[<span class="s3">NSString</span> <span class="s5">stringWithFormat</span>:<span class="s7">@"%@=%@"</span>,[<span class="s8">AWSUtils</span> <span class="s6">urlEncode</span>:key], [<span class="s8">AWSUtils</span> <span class="s6">urlEncode</span>:(<span class="s3">NSString</span>*)[dict <span class="s5">valueForKey</span>:key]]]];</p> <p class="p4"><span class="Apple-tab-span"> </span>}</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p4"><span class="Apple-tab-span"> </span>queryString = [queryArray <span class="s5">componentsJoinedByString</span>:<span class="s7">@"&"</span>];</p> <p class="p2"><span class="Apple-tab-span"> </span></p> <p class="p1"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">NSString</span><span class="s2"> *signedUrl = [</span><span class="s8">AWSUtils</span><span class="s2"> </span><span class="s6">calculateHMACWithKey</span><span class="s2">:</span>@"private key"<span class="s2"> </span><span class="s6">andData</span><span class="s2">:[</span><span class="s3">NSString</span><span class="s2"> </span><span class="s5">stringWithFormat</span><span class="s2">:</span>@"GET\nec2.amazonaws.com\n/\nAWSAccessKeyId=accessKeyId&%@"<span class="s2">,queryString]];</span></p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s3">NSString</span> *encodedAndSignedUrl = [<span class="s8">AWSUtils</span> <span class="s6">urlEncode</span>:signedUrl];</p> <p class="p2">
</p> <p class="p1"><span class="s2"><span class="Apple-tab-span"> </span></span><span class="s3">NSString</span><span class="s2"> *urlToUse = [</span><span class="s3">NSString</span><span class="s2"> </span><span class="s5">stringWithFormat</span><span class="s2">:</span>@"https://ec2.amazonaws.com/?%@&Signature=%@&AWSAccessKeyId=accesskeyId"<span class="s2">,queryString, encodedAndSignedUrl];</span></p> <p class="p2">
</p> <p class="p4"><span class="Apple-tab-span"> </span><span class="s4">return</span> urlToUse;</p> <p class="p4">}</p> <p class="p2">
</p> <p class="p3">@end</p><p class="p3">
</p><p class="p3">
</p><p></p></div>
</pre>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-19857032812095474932009-08-23T23:29:00.002-05:002009-08-23T23:36:44.830-05:00Spring-ws and WebsphereDevelopers have been arguing about the use of servlet containers like Tomcat vs the use of JEE application servers like Websphere for a long time now. And from what i've read, almost everybody seems to agree that application servers lack a great deal of flexibility and sometimes they may be an overhead, especially if you are working on a small to medium application that does not need any of the big features like security, transaction management,ejb...
Putting all the arguing aside, in this post i will talk about the challenges i faced when migrating an application that was initially working on Tomcat to Websphere.
The application in question was developed using spring-ws and oracle.
Here is a list of all the challenges and how i solved them:
<ol><li>DataSource problem: I was getting a message like this one "Cannot convert value of type [com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found". I solved it by using PARENT_LAST setting and excluding the Database driver from the ear file.</li><li>SAAJ problem: i was getting this message "Could not create message from InputStream: Unable to internalize message; nested exception is com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to internalize message". This was happening because spring depends on a class that exists in the SUN JDK and not in the IBM JDK. I solved it by using AxiomSoapMessageFactory instead of SaajSoapMessageFactory.</li><li>Schema problem: i was getting this message "rcase-Recurse.2: There is not a complete functional mapping between the particles.". The same schema was working on Tomcat but not on Websphere and that was because Tomcat is a kind of forgiving when it comes to handling xsds. One of the elements was extending another element with a sequence inside but the child was not respecting the sequence order in the parent.</li></ol>These are all the things i had to do to get my app working.Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com2tag:blogger.com,1999:blog-9022785551282261793.post-51673038197113569692009-02-28T12:07:00.002-06:002009-02-28T12:26:40.606-06:00Maven assembly localesIn the last few days, i was working on a project that involves taring files which include spanish characters. The problem i was facing is that the spanish characters don't get properly encoded on the unix environment when i use maven to do the build.
The solution was to add the spanish locale to the plugin configuration that generates the tar:
<pre name="code" class="xml">
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<configuration>
<locales>
<locale>en</locale>
<locale>es</locale>
</locales>
<descriptor>src/main/assembly/src.xml</descriptor>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</pre>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-11659908651857702642008-11-08T08:38:00.001-06:002008-11-08T10:46:44.181-06:00Mock your worldI have been always fascinated by this neat idea of mocking in the context of the testing world. So as a topic for this post, i thought writing about this concept may give it more clarity and strength in my mind and hopefully it will do the same for the community.
<div style="text-align: left;"><font style="font-weight: bold;">
Definition:</font>
</div>Mocking is replacing all the collaborators and dependencies of the class under test by mocks or fake objects so you can easily and efficiently unit test it.
<font style="font-weight: bold;">Why do you need Mocking for?</font>
<ul><li>It gives you the ability to focus your test on the functionality of a code unit.</li><li>It removes your dependency on the different components of your app, such as database...</li><li>It makes your tests run faster.</li><li>It allows you to have a clean and better design (interface driven design).
</li></ul><font style="font-weight: bold;">Some tips on the implementation:</font>
In order to make mocking an easy task, you need to take in consideration the following factors:
<ul><li>Base your design on interfaces instead of inheriting from a class, because of the fact that classes may sometimes be final or have static or final methods and therefore will not allow you to override them.<div id="p38b" style="padding: 1em 0pt; text-align: left;"><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw_W6I2NURmzo0476rxqn0Rn1eSHzaDiXESnnq0c_5bBOjjcpl8rdvOoDlRHl7lZnJJyW2Tm1O_eN6dqubB8SrEr0BQAQvuXdJjc_vBgIy8YVrqNpc6oCHhtjOhm0EEGVpoBswObTIpKqw/s1600-h/interface.jpeg"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 183px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiw_W6I2NURmzo0476rxqn0Rn1eSHzaDiXESnnq0c_5bBOjjcpl8rdvOoDlRHl7lZnJJyW2Tm1O_eN6dqubB8SrEr0BQAQvuXdJjc_vBgIy8YVrqNpc6oCHhtjOhm0EEGVpoBswObTIpKqw/s400/interface.jpeg" alt="" id="BLOGGER_PHOTO_ID_5266319236159512578" border="0"></a></div>In the diagram above, we used the interface implemented by one of the collaborators of the class under test to create our mock.
<div id="hgah" style="padding: 1em 0pt; text-align: left;"><div style="text-align: center;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL8kO_1R0eDrsmmKhDsyE7XtZhqICPBlu9YF-GdGGgP-ZnR4NmSYzMPo9gcpu5F3YjIF9pOU7_gTVB9dSslpOd8EFE2_yg7e7xlgE2TD5c60jd-y0F0cKgVHgXf9FO1hxrPXDNIOtzGPKE/s1600-h/class.jpeg"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 281px; height: 274px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL8kO_1R0eDrsmmKhDsyE7XtZhqICPBlu9YF-GdGGgP-ZnR4NmSYzMPo9gcpu5F3YjIF9pOU7_gTVB9dSslpOd8EFE2_yg7e7xlgE2TD5c60jd-y0F0cKgVHgXf9FO1hxrPXDNIOtzGPKE/s400/class.jpeg" alt="" id="BLOGGER_PHOTO_ID_5266324391291596482" border="0"></a></div>
As showed in the diagram above, we tried to mock User by extending it, but since it has the method findAllUsers() marked as final we could not override it, which means that the mocking operation was a failure.
</div></div></li><li>make your objects as small as possible so you can easily mock them (divide and conquer principle).</li>
<li>remove the instantiation of the collaborators from within the class under test so you can mock them.</li></ul><font style="font-weight: bold;">Projects:</font>
If you don't like creating mocks manually, here are some open source projects that can help you do that:
<ul><li>http://www.jmock.org</li><li>http://www.easymock.org</li><li>http://code.google.com/p/mockito
</li></ul>Please don't hesitate to enrich this post by your comments.Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-13514245051716621052008-10-21T07:10:00.000-05:002008-11-08T10:46:01.198-06:00Interface your worldThe notion of interface in the Object Oriented Programming world has been always very confusing for developers.
In this post I am reflecting about this very important notion and the role it plays in the Object Oriented philosophy.
In a nutshell an interface is a contract that describes some behavior pieces and every developer wiling to use this interface is required to implement all the pieces.
Here is what a contract looks like in the Java world.
example1
<pre name='code' class='java'>
public interface Vehicle {
public void start();
public void drive();
public void park();
}
</pre>
But, what do we need this interface for?
Well, if you take sometime to think about it, you’ll find that it has many advantages.
• It gives you control over your design because all the classes implementing a given interface are required to implement all the methods of your interface.
• It makes your design flexible and loosely coupled.
Below is an example of a method that can accept all the classes implementing List:
example2:
<pre name='code' class='java'>
List<String> list = new ArrayList<String>();
List<String> vector = new Vector<String>();
public void displayItems(List<String> list){
for(String item:list){
System.out.println(item);
}
}
</pre>
We can call the method above like displayItems(list) or displayItems(vector).
• It makes your code testable, through using mocks for example
• It separates the concerns, because interface is used mainly to implement behavior and abstract class to provide properties. For example, we can have the Vehicle interface in example1 provide the behavior and have the abstract class below provide the properties:
example3:
<pre name='code' class='java'>
public abstract class VehicleDescription {
public abstract String getColor();
public abstract String getMake();
}
</pre>
This class gets its behavior and proeprties from the interface and abstract classes described above.
<pre name='code' class='java'>
public class Camry extends VehicleDescription implements Vehicle {
private String color;
private String make;
public void start(){
//implementation
}
public void drive(){
//implementation
}
public void park(){
//implementation
}
public String getColor() {
return null;
}
public void setColor() {
//implementation
}
public String getMake() {
return null;
}
public void setMake() {
//implementation
}
}
</pre>
Please feel free to speak your mind and add anything you see that i missed in this post to the comments.Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-83068125203056295282008-10-14T21:16:00.000-05:002008-11-08T10:46:19.938-06:00Formatting code snippetsWhen i started writing this blog, i did not find any functionality in Blogger to format the code snippets the i would like to include in my posts. So i did some research and found a <a href="http://code.google.com/p/syntaxhighlighter/">project</a> hosted in google that is addressing this topic.
I wrote some code that makes use of this very nice project to format my code snippets.
If you are looking to do the same thing, you can use the code below in your blog if you like. The Javascript libraries are served from my server.
<pre name="code" class="xml">
<link type="text/css" rel="stylesheet" src="http://www.javageek.net/SyntaxHighlighter/Styles/SyntaxHighlighter.css"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shCore.js"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shBrushJava.js"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shBrushXml.js"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shBrushJScript.js"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shBrushCss.js"/>
<script type="text/javascript" src="http://www.javageek.net/SyntaxHighlighter/Scripts/shBrushCSharp.js"/>
<script type="text/javascript">
dp.SyntaxHighlighter.ClipboardSwf = 'clipboard.swf';
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script>
</pre>Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0tag:blogger.com,1999:blog-9022785551282261793.post-86207756939449940442008-10-03T16:36:00.000-05:002008-11-08T10:46:32.389-06:00How to develop your first maven plugin<span style="color: rgb(0, 0, 0);">Few weeks ago, i participated in a hackathon and my project was to write my first maven plugin ever. Unfortunately, i was not able to finish my hack in the same day and that was because i missed some small steps in the process, so i thought it would be worthwile to share what i discovered with the community.</span>
<span style="color: rgb(0, 0, 0);">Here are the steps that i followed to create my first maven plugin.</span>
<ol><li><span style="color: rgb(0, 0, 0);">creating the folder structure for our project by typing this maven comand:</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >mvn archetype:create \</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > -DgroupId=sample.plugin \</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > -DartifactId=maven-hello-plugin \</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > -DarchetypeGroupId=org.apache.maven.archetypes \</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > -DarchetypeArtifactId=maven-archetype-mojo</span>
</li><li><span style="color: rgb(0, 0, 0);">creating the project:</span>
<span style="color: rgb(0, 0, 0);">cd to the folder that was generated by the command above and type this command to generate th project files.</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >mvn idea:idea</span>
or
<span style="color: rgb(0, 0, 153);font-family:courier new;" >mvn eclipse:eclipse</span></li><li><span style="color: rgb(0, 0, 153);font-family:courier new;" ><span style="font-family:arial;"><span style="color: rgb(0, 0, 0);"><span style="color: rgb(0, 0, 0);">open the project using your favorite IDE and then replace Mymojo.java content by the following code:</span><span style="font-size:100%;"><span style="color: rgb(0, 0, 153);font-family:courier new;" >
package sample.plugin;</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >import org.apache.maven.plugin.AbstractMojo;</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >import org.apache.maven.plugin.MojoExecutionException;</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >import java.io.IOException;</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >/**</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * Goal which displays a hello message.</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > *</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * @goal hello-message</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * </span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * @phase package</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > */</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >public class MyMojo</span><span style="color: rgb(0, 0, 153);font-family:courier new;" > extends AbstractMojo</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >{</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > /**</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * the hello message.</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * @parameter </span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > * @required</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > */</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > private String message;</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > public void execute()</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > throws MojoExecutionException</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > {</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > getLog().info("Hello from :" + message);</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" > }</span>
<span style="color: rgb(0, 0, 153);font-family:courier new;" >}</span></span></span></span></span></li><li><span style="color: rgb(0, 0, 153);font-family:courier new;" ><span style="font-family:arial;"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;"><span style="color: rgb(0, 0, 153);font-family:courier new;" ></span>installing the plugin:
<span style="color: rgb(0, 0, 153);font-family:courier new;" >mvn clean install</span></span></span></span></span></li><li><span style="color: rgb(0, 0, 153);font-family:courier new;" ><span style="font-family:arial;"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;">executing the plugin:
create another pom with different name, we'll call it here pom1.xml and then put this content </span></span></span></span><span style="color: rgb(0, 0, 153);font-family:courier new;" ><span style="font-family:arial;"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;">in there.
<!-- code formatted by http://manoli.net/csharpformat/ -->
<style type="text/css"><br />.csharpcode, .csharpcode pre<br />{<br /> font-size: small;<br /> color: black;<br /> font-family: Consolas, "Courier New", Courier, Monospace;<br /> background-color: #ffffff;<br /> /*white-space: pre;*/<br />}<br /><br />.csharpcode pre { margin: 0em; }<br /><br />.csharpcode .rem { color: #008000; }<br /><br />.csharpcode .kwrd { color: #0000ff; }<br /><br />.csharpcode .str { color: #006080; }<br /><br />.csharpcode .op { color: #0000c0; }<br /><br />.csharpcode .preproc { color: #cc6633; }<br /><br />.csharpcode .asp { background-color: #ffff00; }<br /><br />.csharpcode .html { color: #800000; }<br /><br />.csharpcode .attr { color: #ff0000; }<br /><br />.csharpcode .alt <br />{<br /> background-color: #f4f4f4;<br /> width: 100%;<br /> margin: 0em;<br />}<br /><br />.csharpcode .lnum { color: #606060; }<br /></style><div class="csharpcode"><pre><span class="lnum"> 1: </span><span class="kwrd"><</span><span class="html">project</span> <span class="attr">xmlns</span><span class="kwrd">="http://maven.apache.org/POM/4.0.0"</span> <span class="attr">xmlns:xsi</span><span class="kwrd">="http://www.w3.org/2001/XMLSchema-instance"</span></pre><pre><span class="lnum"> 2: </span> <span class="attr">xsi:schemaLocation</span><span class="kwrd">="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span><span class="kwrd">></span></pre><pre><span class="lnum"> 3: </span> <span class="kwrd"><</span><span class="html">modelVersion</span><span class="kwrd">></span>4.0.0<span class="kwrd"></</span><span class="html">modelVersion</span><span class="kwrd">></span></pre><pre><span class="lnum"> 4: </span> <span class="kwrd"><</span><span class="html">groupId</span><span class="kwrd">></span>sample.plugin<span class="kwrd"></</span><span class="html">groupId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 5: </span> <span class="kwrd"><</span><span class="html">artifactId</span><span class="kwrd">></span>maven-hello-plugin<span class="kwrd"></</span><span class="html">artifactId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 6: </span> <span class="kwrd"><</span><span class="html">packaging</span><span class="kwrd">></span>maven-plugin<span class="kwrd"></</span><span class="html">packaging</span><span class="kwrd">></span></pre><pre><span class="lnum"> 7: </span> <span class="kwrd"><</span><span class="html">version</span><span class="kwrd">></span>1.0-SNAPSHOT<span class="kwrd"></</span><span class="html">version</span><span class="kwrd">></span></pre><pre><span class="lnum"> 8: </span> <span class="kwrd"><</span><span class="html">name</span><span class="kwrd">></span>maven-hello-plugin Maven Mojo<span class="kwrd"></</span><span class="html">name</span><span class="kwrd">></span></pre><pre><span class="lnum"> 9: </span> <span class="kwrd"><</span><span class="html">url</span><span class="kwrd">></span>http://maven.apache.org<span class="kwrd"></</span><span class="html">url</span><span class="kwrd">></span></pre><pre><span class="lnum"> 10: </span> <span class="kwrd"><</span><span class="html">build</span><span class="kwrd">></span></pre><pre><span class="lnum"> 11: </span> <span class="kwrd"><</span><span class="html">plugins</span><span class="kwrd">></span></pre><pre><span class="lnum"> 12: </span> <span class="kwrd"><</span><span class="html">plugin</span><span class="kwrd">></span></pre><pre><span class="lnum"> 13: </span> <span class="kwrd"><</span><span class="html">groupId</span><span class="kwrd">></span>sample.plugin<span class="kwrd"></</span><span class="html">groupId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 14: </span> <span class="kwrd"><</span><span class="html">artifactId</span><span class="kwrd">></span>maven-hello-plugin<span class="kwrd"></</span><span class="html">artifactId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 15: </span> <span class="kwrd"><</span><span class="html">version</span><span class="kwrd">></span>1.0-SNAPSHOT<span class="kwrd"></</span><span class="html">version</span><span class="kwrd">></span></pre><pre><span class="lnum"> 16: </span> <span class="kwrd"><</span><span class="html">executions</span><span class="kwrd">></span></pre><pre><span class="lnum"> 17: </span> <span class="kwrd"><</span><span class="html">execution</span><span class="kwrd">></span></pre><pre><span class="lnum"> 18: </span> <span class="kwrd"><</span><span class="html">phase</span><span class="kwrd">></span>package<span class="kwrd"></</span><span class="html">phase</span><span class="kwrd">></span></pre><pre><span class="lnum"> 19: </span> <span class="kwrd"><</span><span class="html">configuration</span><span class="kwrd">></span></pre><pre><span class="lnum"> 20: </span> <span class="kwrd"><</span><span class="html">message</span><span class="kwrd">></span>Jupiter<span class="kwrd"></</span><span class="html">message</span><span class="kwrd">></span></pre><pre><span class="lnum"> 21: </span> <span class="kwrd"></</span><span class="html">configuration</span><span class="kwrd">></span></pre><pre><span class="lnum"> 22: </span> <span class="kwrd"><</span><span class="html">goals</span><span class="kwrd">></span></pre><pre><span class="lnum"> 23: </span> <span class="kwrd"><</span><span class="html">goal</span><span class="kwrd">></span></pre><pre><span class="lnum"> 24: </span> hello-message</pre><pre><span class="lnum"> 25: </span> <span class="kwrd"></</span><span class="html">goal</span><span class="kwrd">></span></pre><pre><span class="lnum"> 26: </span> <span class="kwrd"></</span><span class="html">goals</span><span class="kwrd">></span></pre><pre><span class="lnum"> 27: </span> <span class="kwrd"></</span><span class="html">execution</span><span class="kwrd">></span></pre><pre><span class="lnum"> 28: </span> <span class="kwrd"></</span><span class="html">executions</span><span class="kwrd">></span></pre><pre><span class="lnum"> 29: </span> <span class="kwrd"></</span><span class="html">plugin</span><span class="kwrd">></span></pre><pre><span class="lnum"> 30: </span> <span class="kwrd"></</span><span class="html">plugins</span><span class="kwrd">></span></pre><pre><span class="lnum"> 31: </span> <span class="kwrd"></</span><span class="html">build</span><span class="kwrd">></span></pre><pre><span class="lnum"> 32: </span>
</pre><pre><span class="lnum"> 33: </span>
</pre><pre><span class="lnum"> 34: </span>
</pre><pre><span class="lnum"> 35: </span> <span class="kwrd"><</span><span class="html">dependencies</span><span class="kwrd">></span></pre><pre><span class="lnum"> 36: </span> <span class="kwrd"><</span><span class="html">dependency</span><span class="kwrd">></span></pre><pre><span class="lnum"> 37: </span> <span class="kwrd"><</span><span class="html">groupId</span><span class="kwrd">></span>org.apache.maven<span class="kwrd"></</span><span class="html">groupId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 38: </span> <span class="kwrd"><</span><span class="html">artifactId</span><span class="kwrd">></span>maven-plugin-api<span class="kwrd"></</span><span class="html">artifactId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 39: </span> <span class="kwrd"><</span><span class="html">version</span><span class="kwrd">></span>2.0<span class="kwrd"></</span><span class="html">version</span><span class="kwrd">></span></pre><pre><span class="lnum"> 40: </span> <span class="kwrd"></</span><span class="html">dependency</span><span class="kwrd">></span></pre><pre><span class="lnum"> 41: </span> <span class="kwrd"><</span><span class="html">dependency</span><span class="kwrd">></span></pre><pre><span class="lnum"> 42: </span> <span class="kwrd"><</span><span class="html">groupId</span><span class="kwrd">></span>junit<span class="kwrd"></</span><span class="html">groupId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 43: </span> <span class="kwrd"><</span><span class="html">artifactId</span><span class="kwrd">></span>junit<span class="kwrd"></</span><span class="html">artifactId</span><span class="kwrd">></span></pre><pre><span class="lnum"> 44: </span> <span class="kwrd"><</span><span class="html">version</span><span class="kwrd">></span>3.8.1<span class="kwrd"></</span><span class="html">version</span><span class="kwrd">></span></pre><pre><span class="lnum"> 45: </span> <span class="kwrd"><</span><span class="html">scope</span><span class="kwrd">></span>test<span class="kwrd"></</span><span class="html">scope</span><span class="kwrd">></span></pre><pre><span class="lnum"> 46: </span> <span class="kwrd"></</span><span class="html">dependency</span><span class="kwrd">></span></pre><pre><span class="lnum"> 47: </span> <span class="kwrd"></</span><span class="html">dependencies</span><span class="kwrd">></span></pre><pre><span class="lnum"> 48: </span><span class="kwrd"></</span><span class="html">project</span><span class="kwrd">></span></pre></div></span></span></span></span><span style="color: rgb(0, 0, 153);font-family:courier new;" ><span style="font-family:arial;"><span style="color: rgb(0, 0, 0);"><span style="font-size:100%;">
and then execute it:
<span style="color: rgb(0, 0, 153);font-family:courier new;" >mvn -f pom1.xml package</span>
you'll see the message that we put in pom1.xml displayed in the logs.</span></span></span></span></li></ol>You can download the project <a href="http://www.javageek.net/maven-hello-plugin.zip">here</a>.Anonymoushttp://www.blogger.com/profile/15526304075331461830noreply@blogger.com0