<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Carr</title>
	<atom:link href="http://blog.james-carr.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.james-carr.org</link>
	<description>Rants and Musings of an Agile Developer</description>
	<lastBuildDate>Sun, 29 Jan 2012 16:51:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>NPM: Install Packages Not Yet Published</title>
		<link>http://blog.james-carr.org/2012/01/29/npm-install-packages-not-yet-published/</link>
		<comments>http://blog.james-carr.org/2012/01/29/npm-install-packages-not-yet-published/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 16:51:41 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1350</guid>
		<description><![CDATA[This morning I was working on a project and one of the modules I depended on had a small bug in it. As I was about to log an issue on the project&#8217;s github page I discovered that it was already fixed, just not yet released. I really wanted to push my changes out to [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I was working on a project and one of the modules I depended on had a small bug in it. As I was about to log an issue on the project&#8217;s github page I discovered that it was already fixed, just not yet released. I really wanted to push my changes out to our staging server and my build process relies on npm gathering all the dependencies my project needs, so I looked for ways to install through npm without much modification to my build process.</p>
<p>What I discovered could be considered an abuse of npm&#8217;s preinstall hook, but it works. </p>
<p><script src="https://gist.github.com/1699602.js"> </script></p>
<p>This brings in not just the module, but also all of the module&#8217;s transitive dependencies as well. This trick worked for me, but I&#8217;m still a little doubtful that this is considered the &#8220;right&#8221; way around the problem. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2012/01/29/npm-install-packages-not-yet-published/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blog Rolling with MongoDB, Node.js and Coffeescript</title>
		<link>http://blog.james-carr.org/2012/01/16/blog-rolling-with-mongodb-node-js-and-coffeescript/</link>
		<comments>http://blog.james-carr.org/2012/01/16/blog-rolling-with-mongodb-node-js-and-coffeescript/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 18:54:26 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1329</guid>
		<description><![CDATA[This morning I woke up with a lingering thought on my mind that was left over from recent conversations. In the technical community we often get so invested in our work that rather than talk about the simple building blocks that build our success we talk about the huge breakthroughs we make. The problem however [...]]]></description>
			<content:encoded><![CDATA[<p>This morning I woke up with a lingering thought on my mind that was left over from recent conversations. In the technical community we often get so invested in our work that rather than talk about the simple building blocks that build our success we talk about the huge breakthroughs we make. The problem however is that our breakthroughs most often aren&#8217;t accessible to someone who wants to just get started. So today I will give an intro tutorial to using node.js, coffeescript and mongodb to build a simple blog. It builds off the concept in a <a href="http://howtonode.org/express-mongodb">tutorial</a> I first used to learn node.js more than a year ago, but with a completely from scratch approach. In this tutorial I will also cover practicing <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behavior Driven Development</a> using Mocha. </p>
<p><span id="more-1329"></span></p>
<h4>Getting Started</h4>
<p>Quite obviously, you&#8217;re going to need node.js and mongodb installed. I recommend downloading and installing from the <a href="http://nodejs.org/">node.js website</a> and following the instructions. I will note that this tutorial covers <strong>0.6.7</strong> so if you come across this post a year from now the API might have changed significantly since then. You will also need mongodb installed, you can <a href="http://www.mongodb.org/downloads">download it here</a>. If you use Ubuntu (or some other Debian derivative) you should consider installing from their <a href="http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages">apt repository</a>. OSX? No problem, you can also <a href="http://www.mongodb.org/display/DOCS/Quickstart+OS+X">install it via homebrew or macports</a>. </p>
<p>Finally, since we&#8217;ll be using coffeescript for this tutorial, run <strong><code>npm -g install coffee-script</code></strong> (you might need to sudo) to install coffeescript. Run coffee from the commandline to access the coffeescript <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a>. If all works well, install these additional packages listed below via npm that we&#8217;ll be using throughout the tutorial.</p>
<ul>
<li>express</li>
<li>mocha</li>
</ul>
<p>Now let&#8217;s bootstrap our project structure. Type <strong><code>express coffeepress</code></strong> to generate a skeleton express project structure. You should see output similar to the following:</p>
<p><a href="http://blog.james-carr.org/wp-content/uploads/2012/01/express.png"><img src="http://blog.james-carr.org/wp-content/uploads/2012/01/express.png" alt="" title="express" width="400" height="306" class="aligncenter size-full wp-image-1330" /></a></p>
<p>Notice how at the end it says to cd to the directory and type <strong><code>npm install</code></strong>? Let&#8217;s follow those instructions. Let&#8217;s run what we have so far by typing <strong><code>node app.js</code></strong> and navigating to <a href="http://localhost:3000">http://localhost:3000</a>. This is the default structure that gives a good starting point.  Feel free to investigate the files under the directory before moving on. I even suggest poking around by changing the view a bit and changing the title from express to &#8220;My Coffeepress Blog&#8221;. </p>
<h4>Porting to Coffeescript</h4>
<p>At this point, let&#8217;s port our backend to coffeescript. I used to copy and paste files into the <a href="http://js2coffee.org/">js2coffee</a> website but you can also install js2coffee via npm. So run the following:</p>
<p><script src="https://gist.github.com/1621238.js"> </script></p>
<p>Now you can run <strong><code>coffee app.coffee</code></strong> to run the same app, but now in coffeescript. Take a look at the resulting files to get a feel for what has changed. New to coffeescript? Then I recommend taking a gander at <a href="http://coffeescript.org/">coffeescript.org</a> before moving on. Here is the <a href="https://github.com/jamescarr/nodejs-mongodb-blog/zipball/convert-to-coffee">project structure</a> so far.</p>
<h4>Basic Navigation</h4>
<p>I like to try and work my way from the outside in while developing a site or feature, materializing components into existence as I need them. So let&#8217;s start by working on the initial navigation of the site with some simple in-memory storage of blog posts. This is a good time to get our test framework setup and write a few simple tests against our routes. Normally I prefer to not write tests against my routes, shoving logic into heavy models or services. However I have come to learn that untested components of a system serve as a gravity well for untested code that eventually leads to clients calling you about broken applications. What follows serves as both an introduction to <a href="http://visionmedia.github.com/mocha/">Mocha</a> as well as <a href="http://expressjs.com/guide.html#route-middleware">express&#8217; routing mechanism</a>.</p>
<p>Let&#8217;s edit our package.json to include our test framework dependencies.<br />
<script src="https://gist.github.com/1621332.js"> </script></p>
<p>We include <a href="https://github.com/visionmedia/should.js">should</a> so that we can use BDD style assertions (more on this in a bit). Write a simple test case located at test/routes-test.coffee with the following code to get us started with mocha</p>
<p><script src="https://gist.github.com/1621352.js"> </script></p>
<p>Now run this by typing <strong><code>mocha</code></strong> from the root of the project directory. It should pass. Let&#8217;s go ahead and make it fail by changing 4 to 5 and rerunning it. Hopefully this gives you a good feel for our test framework before we move on and change this test to reflect our existing index route. Swap the code in this test out with the following.</p>
<p><script src="https://gist.github.com/1621377.js"> </script></p>
<p>Here we fake our requests and response in order to capture what is passed into the response. We fake the render method and verify that our rendered view is &#8220;index&#8221; and that the variable title is equal to what we expect to be passed in. Run the tests and make changes to your route to make it pass.</p>
<p>Now let&#8217;s add a post variable that will be an array of posts we&#8217;ll display on the front page. Add the following assertion right after the title assertion:</p>
<p><script src="https://gist.github.com/1621402.js"> </script></p>
<p>Run the tests to see it fail and change the route to have a posts array variable available in the template.</p>
<p><script src="https://gist.github.com/1621406.js"> </script></p>
<p>Unfortunately you&#8217;ll notice that the test fails. This is due to a subtle difference between <strong><code>equal</code></strong> and <strong><code>eql</code></strong>. The former enforces strict equality while the latter is a bit looser, so we change our assertion to use <code>eql</code>. Take a look at the <a href="https://github.com/visionmedia/should.js">should documentation</a> for more information.</p>
<p>Next let&#8217;s write tests for the &#8220;new post&#8221; route.<br />
<script src="https://gist.github.com/1621461.js"> </script><br />
Run it, see the failure, and rework our routes.coffee file to include the route (with no implementation yet)</p>
<p><script src="https://gist.github.com/1621470.js"> </script></p>
<p>You&#8217;ll notice our test passes. That&#8217;s not good. Why? Because we put our assertion in our req.render callback, which never gets executed. Doh! How can we make absolutely sure it gets called during our test run? Old school thinking would have you assign a local variable outside the scope of the callback that gets assigned during execution and then can be verified against later on. However we have no guarantee that the routing logic will be synchronous!</p>
<p>Thankfully mocha has a feature that allows for easy testing in these situations. In the method declaration of our test specify a parameter named <strong><code>done</code></strong>. This is a callback that we can call anywhere to indicate the test is done. Basically the test will wait up to a default of 2000ms for it to be called. With this in mind, let&#8217;s modify our tests with the following:</p>
<p><script src="https://gist.github.com/1621503.js"> </script> </p>
<p>If we run this via mocha now we&#8217;ll notice that we have one failure. Let&#8217;s go ahead and implement the route and connect it into our router.</p>
<p><script src="https://gist.github.com/1621520.js"> </script></p>
<p>And connecting it up in the app.coffee</p>
<p><script src="https://gist.github.com/1621528.js"> </script></p>
<h4>Modifying the Views</h4>
<p>This code is useless without views, so let&#8217;s modify our views a bit. Let&#8217;s modify our layout.jade to link to the new posts page. This layout also makes use of twitter bootstrap because I&#8217;m too lazy to design something for this tutorial. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><script src="https://gist.github.com/1621542.js"> </script></p>
<p>And create our add_post view at <strong><code>views/add_post.jade</code></strong>. An interesting thing to note here that I&#8217;ll touch on in a bit is that I prefix the input names with post. </p>
<p><script src="https://gist.github.com/1621769.js"> </script></p>
<p>Now let&#8217;s add another route to handle the post. This time I&#8217;m going to kind of skip delving into the details of writing the test, but you can look at what I have so far <a href="https://github.com/jamescarr/nodejs-mongodb-blog/zipball/fully-functional-in-memory">here</a> if you&#8217;d like to see it. </p>
<p><script src="https://gist.github.com/1621806.js"> </script></p>
<p>For now, we&#8217;re just going to store each post in an array. Nothing fancy yet. We also add a new route to app.coffee. We could refactor or use some express-mvc plugin to reduce adding each route by hand, but I think it&#8217;s good to do it like this to get a feel for express&#8217; low level routing mechanisms. </p>
<p><script src="https://gist.github.com/1621816.js"> </script></p>
<p>Finally, we&#8217;ll add one last view for viewing a single post:</p>
<p><script src="https://gist.github.com/1622209.js"> </script></p>
<p>Go ahead and start the application up and navigate to <a href="http://localhost:3000">http://localhost:3000</a>. Post a few posts and play around a bit. You can see the finished application we have so far <a href="https://github.com/jamescarr/nodejs-mongodb-blog/tree/fully-functional-in-memory">here</a>.</p>
<h4>Mongoose</h4>
<p>Whew. I hope I haven&#8217;t lost you yet. Especially with the tests against the routes&#8230; I know those are always a bit painful! Now that we have functional blog let&#8217;s make it work by storing posts in mongodb using <a href="http://mongoosejs.com/">Mongoose</a>.</p>
<p>Let&#8217;s add a dependency on mongoose to our project and freeze it at version 2.4.10. As always, run npm install to bring it in. Now we&#8217;ll create an initial test to just test mongoose out. </p>
<p><script src="https://gist.github.com/1622017.js"> </script></p>
<p>Here we import both mongoose and the model object that we&#8217;re going to create. Since we want our test to start with a clean slate, we use the before hook (which runs once before anything else in the test runs) to both connect to the database and then remove all of the Post objects from mongodb. We pass the done callback to the remove call so that tests don&#8217;t run until all Posts have been removed. </p>
<p>Now we create a new Post instance. You can pass an object literal in to set properties on the model, so we do that here. Finally, in our post.save callback we look the post back up and verify certain attributes have been set. It&#8217;s a dumb test (and in fact I rarely test mongoose&#8217;s behavior like this), but it does verify that we&#8217;ve configured our model correctly.</p>
<p>Now let&#8217;s implement our model to make the test pass.</p>
<p><script src="https://gist.github.com/1622036.js"> </script></p>
<p>Pretty simple. Now let&#8217;s refit our routes to use the Post model instead of an in memory array.</p>
<p><script src="https://gist.github.com/1622093.js"> </script></p>
<p>That&#8217;s all good and dandy, but one last hiccup is our tests for our routes now fail. Chalk this one up to not having any abstraction or dependency injection in place, but that is fine for now, we&#8217;ll live with it and change the tests.</p>
<p><script src="https://gist.github.com/1622106.js"> </script></p>
<p>Finally we need our app to actually connect to mongoose when we run it. I like to do this based on the express configuration. This is immensely important if you have mongodb running on servers separated from your application.  For this example we&#8217;ll just use the databases coffeepress-dev and coffeepress-prod. </p>
<p><script src="https://gist.github.com/1622312.js"> </script></p>
<p>Run it and write a few posts. Restart the app and you&#8217;ll see the posts still there. Woot!</p>
<h4>Conclusion</h4>
<p>Well, that about wraps it up&#8230; you can see this tutorial in it&#8217;s finished glory on the <a href="https://github.com/jamescarr/nodejs-mongodb-blog/tree/fully-functional-in-memory">finished</a> branch of the repository. There&#8217;s a bit missing out here that we&#8217;d implement in the real world. Obviously some kind of authentication would be in order if we took this further, possibly using mongoose-auth. We&#8217;d also want to add some validation when posting.  These are all excellent topics for future posts but for now I hope this was enough to help you get going! <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2012/01/16/blog-rolling-with-mongodb-node-js-and-coffeescript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Streaming Files from MongoDB GridFS</title>
		<link>http://blog.james-carr.org/2012/01/09/streaming-files-from-mongodb-gridfs/</link>
		<comments>http://blog.james-carr.org/2012/01/09/streaming-files-from-mongodb-gridfs/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 19:44:13 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1324</guid>
		<description><![CDATA[Not too long ago I tweeted what I felt was a small triumph on my latest project, streaming files from MongoDB GridFS for downloads (rather than pulling the whole file into memory and then serving it up). I promised to blog about this but unfortunately my specific usage was a little coupled to the domain [...]]]></description>
			<content:encoded><![CDATA[<p>Not too long ago I tweeted what I felt was a small triumph on my latest project, streaming files from <a href="http://www.mongodb.org/display/DOCS/GridFS">MongoDB GridFS</a> for downloads (rather than pulling the whole file into memory and then serving it up). I promised to blog about this but unfortunately my specific usage was a little coupled to the domain on my project so I couldn&#8217;t just show it off as is. So I&#8217;ve put together an <a href="https://github.com/jamescarr/nodejs-mongodb-streaming">example node.js+GridFS application</a> and shared it on github and will use this post to explain how I accomplished it. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>GridFS module</h4>
<p>First off, special props go to <a href="https://twitter.com/#!/tjholowaychuk">tjholowaychuk</a> who responded in the #node.js irc channel when I asked if anyone has had luck with using GridFS from <a href="http://mongoosejs.com/">mongoose</a>. A lot of my resulting code is derived from an gist he shared with me. Anyway, to the code. I&#8217;ll describe how I&#8217;m using gridfs and after setting the ground work illustrate how simple it is to stream files from GridFS.</p>
<p>I created a gridfs module that basically accesses GridStore through mongoose (which I use throughout my application) that can also share the db connection created when connecting mongoose to the mongodb server.</p>
<p><script src="https://gist.github.com/1584410.js"> </script></p>
<p>We can&#8217;t get files from mongodb if we cannot put anything into it, so let&#8217;s create a putFile operation.</p>
<p><script src="https://gist.github.com/1584399.js"> </script></p>
<p>This really just delegates to the putFile operation that exists in GridStore as part of the mongodb module. I also have a little logic in place to parse options, providing defaults if none were provided. One interesting feature to note is that I store the filename in the metadata because at the time I ran into a funny issue where files retrieved from gridFS had the id as the filename (even though a look in mongo reveals that the filename is in fact in the database).</p>
<p>Now the get operation. The original implementation of this simply passed the contents as a buffer to the provided callback by calling store.readBuffer(), but this is now changed to pass the resulting store object to the callback. The value in this is that the caller can use the store object to access metadata, contentType, and other details. The user can also determine how they want to read the file (either into memory or using a ReadableStream).</p>
<p><script src="https://gist.github.com/1584440.js"> </script></p>
<p>This code just has a small blight in that it checks to see if the filename and fileId are equal. If they are, it then checks to see if metadata.filename is set and sets store.filename to the value found there. I&#8217;ve tabled the issue to investigate further later. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>The Model</h4>
<p>In my specific instance, I wanted to attach files to a model. In this example, let&#8217;s pretend that we have an Application for something (job, a loan application, etc) that we can attach any number of files to. Think of tax receipts, a completed application, other scanned documents.</p>
<p><script src="https://gist.github.com/1584475.js"> </script></p>
<p>Here I define files as an array of Mixed object types (meaning they can be anything) and a method addFile which basically takes an object that at least contains a path and filename attribute. It uses this to save the file to gridfs and stores the resulting gridstore file object in the files array (this contains stuff like an id, uploadDate, contentType, name, size, etc).</p>
<h4>Handling Requests</h4>
<p>This all plugs in to the request handler to handle form submissions to <code>/new</code>. All this entails is creating an Application model instance, adding the uploaded file from the request (in this case we named the file field &#8220;file&#8221;, hence <code>req.files.file</code>) and saving it.</p>
<p><script src="https://gist.github.com/1584525.js"> </script></p>
<p>Now the sum of all this work allows us to reap the rewards by making it super simple to download a requested file from gridFS.</p>
<p><script src="https://gist.github.com/1584540.js"> </script></p>
<p>Here we simply look up a file by id and use the resulting file object to set Content-Type and Content-Disposition fields and finally make use of <code>ReadableStream::pipe</code> to write the file out to the response object (which is an instance of WritableStream). This is the piece of magic that streams data from MongoDB to the client side.</p>
<h4>Ideas</h4>
<p>This is just a humble beginning. Other ideas include completely encapsulating gridfs within the model. Taking things further we could even turn the gridfs model into a mongoose plugin to allow completely blackboxed usage of gridfs. </p>
<p>Feel free to check the <a href="https://github.com/jamescarr/nodejs-mongodb-streaming">project</a> out and let me know if you have ideas to take it even further. Fork away! <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2012/01/09/streaming-files-from-mongodb-gridfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enabling JMX in Gradle&#8217;s jetty Plugin</title>
		<link>http://blog.james-carr.org/2011/12/20/enabling-jmx-in-gradles-jetty-plugin/</link>
		<comments>http://blog.james-carr.org/2011/12/20/enabling-jmx-in-gradles-jetty-plugin/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 22:38:12 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[gradle]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1316</guid>
		<description><![CDATA[It&#8217;s another day, which means another gradle tip. I have been experimenting with JMX lately and using MBeanExporter to export spring beans so that I can interact with them over JMX (specifically, stopping and starting rabbitMQ consumers). I can get this working on any container easily enough but I really wanted to get it working [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s another day, which means another gradle tip. I have been experimenting with JMX lately and using MBeanExporter to export spring beans so that I can interact with them over JMX (specifically, stopping and starting rabbitMQ consumers). I can get this working on any container easily enough but I really wanted to get it working with my locally running jetty instance launched by gradle.</p>
<p>First you&#8217;ll set a jettyConfig for the jettyRun task. I usually do this for both jettyRun and jettyRunWar: </p>
<p><script src="https://gist.github.com/1503601.js"> </script></p>
<p>The additionalRuntimeJars is needed because of a transitive dependency on mx4j. I don&#8217;t know why this is, but it is required. I add mx4j as a providedRuntime dependency along with jetty-management:</p>
<p><script src="https://gist.github.com/1503603.js"> </script></p>
<p>Finally you need to setup your jetty configuration to startup a JMX server. There&#8217;s a bit of freedom here with what you can do but here is one that I stole shamelessly from the jetty website:</p>
<p><script src="https://gist.github.com/1503624.js"> </script></p>
<p>Now run <code>gradle jettyRun</code> and have jconsole open a remote connection to service:jmx:rmi://localhost:2100/jndi/rmi://localhost:2099/jmxrmi and go do whatever you want to do with JMX. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/12/20/enabling-jmx-in-gradles-jetty-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gradle Tip: Start/Stop Embedded Jetty for System Tests</title>
		<link>http://blog.james-carr.org/2011/12/06/gradle-tip-startstop-embedded-jetty-for-system-tests/</link>
		<comments>http://blog.james-carr.org/2011/12/06/gradle-tip-startstop-embedded-jetty-for-system-tests/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 23:18:31 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gradle jetty java groovy test automation]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1306</guid>
		<description><![CDATA[I thought I&#8217;d share another feature of gradle that i have found extremely useful, starting and stopping an embedded jetty server when my tests run. This is really useful for projects that host web services as it allows me to hit them and very the correct results plus it verifies the full stack is configured [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I&#8217;d share another feature of <a href="http://gradle.org">gradle</a> that i have found extremely useful, starting and stopping an embedded jetty server when my tests run. This is really useful for projects that host web services as it allows me to hit them and very the correct results plus it verifies the full stack is configured correctly. One could quite possibly also use this setup on web projects and have <a href="http://www.gebish.org/">Geb</a> based tests run against their project.</p>
<p>Given a project setup using the jetty plugin as I described in my <a href="http://blog.james-carr.org/2011/11/27/gradle-using-jndi-with-the-jetty-plugin/">previous post</a>, all you need to do is hook jetty into run before and after the test task:</p>
<p><script src="https://gist.github.com/1440530.js"> </script></p>
<p>And that&#8217;s it. Now whenever you run <code>gradle test</code> the embedded jetty server will run along with your tests. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/12/06/gradle-tip-startstop-embedded-jetty-for-system-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gradle: Using JNDI with the Jetty Plugin</title>
		<link>http://blog.james-carr.org/2011/11/27/gradle-using-jndi-with-the-jetty-plugin/</link>
		<comments>http://blog.james-carr.org/2011/11/27/gradle-using-jndi-with-the-jetty-plugin/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 17:06:16 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1309</guid>
		<description><![CDATA[I use gradle a lot at work and I believe one discovery that was a true win was discovering how to fake JNDI when using the jettyRun task for local development. Originally googling and searching the documentation didn&#8217;t yield anything so I thought I&#8217;d write a quick post detailing how to do it in case [...]]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://gradle.org">gradle</a> a lot at work and I believe one discovery that was a true win was discovering how to fake JNDI when using the jettyRun task for local development.  Originally googling and searching the documentation didn&#8217;t yield anything so I thought I&#8217;d write a quick post detailing how to do it in case you&#8217;re like me and googling for the same thing.</p>
<p>First off you need to create a <a href="http://docs.codehaus.org/display/JETTY/jetty-env.xml">jetty-env configuration</a> file and put it somewhere in your project (I prefer src/test/resources). Here&#8217;s a sample of one I use that uses H2 for the dataSource:</p>
<p><script src="https://gist.github.com/1397818.js"> </script></p>
<p>This uses an already running H2 instance and runs an init script located in src/test/resources for populating the database with some tables. From our gradle script we need to reference the file from the jettyRun task (I also add it to the jettyRunWar task as well).</p>
<p><script src="https://gist.github.com/1397822.js"> </script></p>
<p>Finally, to complete this example, we want H2 running before jetty kicks off. So we add the h2 dependency to our build script and run the main method of <code>org.h2.tools.Server</code>.</p>
<p><script src="https://gist.github.com/1397828.js"> </script></p>
<p>I&#8217;ve created a <a href="https://github.com/jamescarr/gradle-jetty-h2">sample spring MVC 3 project</a> that makes use of all the above for local development. Just clone it and run <code>gradle jettyRun</code> to see it in action!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/11/27/gradle-using-jndi-with-the-jetty-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Big Company vs. Small Company</title>
		<link>http://blog.james-carr.org/2011/11/25/big-company-vs-small-company/</link>
		<comments>http://blog.james-carr.org/2011/11/25/big-company-vs-small-company/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 16:35:34 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1304</guid>
		<description><![CDATA[The other day I was having lunch with a friend of mine who works for a medium sized company (by medium sized I mean large, but not Fortune 500 large). Our discussions touched a variety of topics by one that caught my attention was when he voiced his frustration on his current project. &#8220;We&#8217;re not [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was having lunch with a friend of mine who works for a medium sized company (by medium sized I mean large, but not Fortune 500 large). Our discussions touched a variety of topics by one that caught my attention was when he voiced his frustration on his current project. &#8220;We&#8217;re not doing much programming right now,&#8221; he quiped, &#8220;for the most part we&#8217;re doing static content management and updating pages that is basically a &#8216;Recent News&#8217; section for the organization.&#8221; With all respect to my friend (who is a really good programmer) this discussion really reminded me of what I dislike about companies with large company mindsets. </p>
<p>It&#8217;s hard for me to put my finger on it, but the gist of it is that developers paid upwards of $60,000+ a year were doing static content management while a $15 an hour developer fresh out of college would most likely setup Drupal or a WordPress blog and let them update it while he focused on more important things. I&#8217;d know&#8230; I was once that $15 an hour developer. </p>
<p>It&#8217;s a common problem I&#8217;ve seen in large companies in my experiences&#8230; problem A is easily solved by tool B but tool B is written in language C while the company has embraced language D (and its derivatives). No good equivalent to tool B exists in language D so the developers will spend lots of time doing manual work, in which case you have one to five developers with degrees updating text (I&#8217;ve seen it). The worst case (and common) scenario is the company will possibly spend upwards of a quarter million or more developing a poor imitation of tool B in language D that will only work in house and will never be able to be used outside the company.</p>
<p>Why is this? Why is it so hard to just use tool B and keep focusing on the more important tasks at hand? From what I&#8217;ve observed in my career is that companies with large company mindsets rarely can consider using languages outside of their core language choice. It requires server provisioning, training, hiring developers or server administrators experienced with language C&#8230; and quite possibly the hiring of consultants who specialize in tool B. The prep time for such a task could easily take up to six months or more and in the end it probably will be decided that tool B isn&#8217;t up to the task. </p>
<p>Compare this to a more fluid development environment. The developer probably uses language D too but knows tool B would be the best choice to get the job done. He&#8217;d probably have the company drop 40 stones on a rackspace instance and install tool B and language C on it and integrate it with the current site written in language D. I&#8217;ve seen this done in days, if not hours. </p>
<p>Notice I really try to emphasize &#8220;big company mindset&#8221; over &#8220;big companies&#8221; as the guilty party of this tomfoolery. Just because you&#8217;re big doesn&#8217;t mean you have to act like this and I&#8217;ve seen small companies engage in this behavior as well. &#8220;Large Company Minded&#8221; companies tend to prefer a great deal of process in the way they do business and identify success with solid, foolproof process that can be adhered to by anyone. Don&#8217;t get me wrong, I&#8217;m not saying absolute chaos of no processes are a good thing but I believe that a business should have a certain degree of fluidity to be successful. Why not just do a quick cost analysis over how much it would cost to have a single server (probably even one already running some other application server) to run language C and tool B rather than redevelop tool B in language D? Why not just determine that that would be the best option and just do it instead of lollygagging around and wasting your company&#8217;s money? </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/11/25/big-company-vs-small-company/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing a Connect Session From Socket.IO</title>
		<link>http://blog.james-carr.org/2011/10/13/accessing-a-connect-session-from-socket-io/</link>
		<comments>http://blog.james-carr.org/2011/10/13/accessing-a-connect-session-from-socket-io/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 03:46:34 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1301</guid>
		<description><![CDATA[UPDATE 11/28/2011: After talking with TJ Hallowaychuck I discovered I was doing it wrong&#8230; there are better ways to do this then the hack I had come up with. Using connect to parse the cooie you can use this instead: Which is much cleaner than what I originally posted. The original post is intact for [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 11/28/2011:</strong> After talking with TJ Hallowaychuck I discovered I was doing it wrong&#8230; there are better ways to do this then the hack I had come up with. Using connect to parse the cooie you can use this instead:</p>
<p><script src="https://gist.github.com/1397838.js"> </script></p>
<p>Which is much cleaner than what I originally posted. The original post is intact for historical reasons. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I thought I&#8217;d post on a technique I&#8217;ve been using to associate the users session with a socket.io server. Although this technique was done in a pure node.js app, it&#8217;s probably possible to do the same to grab the session id from your PHP app or Grails app that is utilizing socket.io.</p>
<p>Anyhow, here&#8217;s what I&#8217;m rolling with:</p>
<p><script src="https://gist.github.com/1286189.js"> </script></p>
<p>Yep, it&#8217;s sneaky. I sniff the sid out and simply use redisClient (since redis is my backing session store) to look the session up. Now on all socket requests I can access the session directly. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/10/13/accessing-a-connect-session-from-socket-io/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Current Java Workflow</title>
		<link>http://blog.james-carr.org/2011/09/09/my-current-java-workflow/</link>
		<comments>http://blog.james-carr.org/2011/09/09/my-current-java-workflow/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 20:00:43 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[gradle]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1027</guid>
		<description><![CDATA[I thought I&#8217;d share the current workflow I have been using at work and why I think it&#8217;s been a pretty decent scheme so far. For this post I&#8217;m only covering jar deployment but I could see it easily working the same for gems or node.js npm packages. Just for a bit of background, here&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I thought I&#8217;d share the current workflow I have been using at work and why I think it&#8217;s been a pretty decent scheme so far. For this post I&#8217;m only covering jar deployment but I could see it easily working the same for gems or node.js npm packages.</p>
<p>Just for a bit of background, here&#8217;s some of the tooling we&#8217;re using:</p>
<ul>
<li><a href="http://gradle.org">Gradle</a> for build scripts</a></li>
<li><a href="http://www.jfrog.com/products.php">Artifactory</a> serving up artifacts</a></li>
<li><a href="https://github.com/">Github</a> for Revision Control</a></li>
<li><a href="http://jenkins-ci.org/">Jenkins</a> for Continuous Deployment</a></li>
<li><a href="https://github.com/nvie/gitflow">Git Flow</a> for high level git operations based on Vincent Driessen&#8217;s <a href="http://nvie.com/posts/a-successful-git-branching-model/">Git Branching Model</a></li>
</ul>
<p>For this example, let&#8217;s create a jar that is really just a simple wrapper to Joiner.join in Google Guava. Yes this is pretty lame and has no use in the real world but we don&#8217;t want to detract from the point of this post, setting up a pragmatic build system. </p>
<p>So to start, we create a new project and create the various files needed for our project. Create a directory for the module (for this project we&#8217;re just calling it module-example). cd into and type &#8220;git flow init&#8221;. You&#8217;ll be prompted for branch names but I always just accept the defaults. If you type git branch you&#8217;ll see that you have two branches, develop and master, with develop checked out. </p>
<p>Now it&#8217;s time to create our initial project structure. I create build.gradle, settings.gradle, and gradle.properties with the following contents:</p>
<p><script src="https://gist.github.com/1207093.js"> </script><br />
All this does is specify what gradle plugins we&#8217;ll be using, dependencies, and where to resolve our dependencies from. The javadocJar and sourcesJar tasks are just to produce javadoc and sources jars as part of the build task.</p>
<p>I&#8217;ll explain the maven plugin in better detail later.</p>
<p><script src="https://gist.github.com/1207081.js"> </script><br />
This just specifies the groupId and the initial integration version. Nuff said.</p>
<p><script src="https://gist.github.com/1207083.js"> </script><br />
Unfortunately gradle uses the folder name as the module name, which will be workspace be default in jenkins. You can change this in jenkins, but I prefer setting the module name explicitly to ensure the right name is always used regardless of the dir name.</p>
<p><script src="https://gist.github.com/1207102.js"> </script><br />
This is my .gitignore to ignore eclipse and gradle generated artifacts. I&#8217;ve learned that lots of problems get caused by committing IDE files and you also don&#8217;t want to commit your compiled jars/classes/test-reports/etc. from gradle.</p>
<p>With everything done, I do a &#8220;git add -A&#8221; and &#8220;git commit -am &#8216;initial commit&#8217;&#8221; to start tracking files. Finally, I do a &#8220;git push &#8211;all&#8221; to push all branches to github. You can see my initial project structure <a href="https://github.com/james-carr/module-example/tree/initial-commit">here</a>.</p>
<h4>Creating the First Feature</h4>
<p>To start with our first feature we use git flow to create a new feature branch.</p>
<pre>
git flow feature start the-combiner
</pre>
<p>This creates and checks out a new feature branch from develop. Now I just create a simple class named Combiner that looks like the following:</p>
<p><script src="https://gist.github.com/1207126.js"> </script></p>
<p>With the feature finished we add the untracked file, commit and type &#8220;git flow feature finish&#8221; to merge our changes back into develop. We also push the development branch back to github.</p>
<h4>Now Would Be a Good Time to Setup Jenkins</h4>
<p>I&#8217;m going to assume Jenkins is already setup. You&#8217;ll need these plugins installed:</p>
<ul>
<li>Jenkins Artifactory Plugin</li>
<li>Jenkins Gradle plugin</li>
<li>Jenkins GIT plugin</li>
</ul>
<p>I then setup a Jenkins job called &#8220;module-example snapshot&#8221;. This checks out any pushes to the develop branch, runs the gradle build task on it (which runs tests and produces artifacts on successful test passes) and then pushes a snapshot release to our in house artifactory server. This means any push to develop will trigger a build that releases a snapshot jar of that module that others could use for their development. For this I use the following artifactory plugin settings: </p>
<p><a href="http://blog.james-carr.org/wp-content/uploads/2011/09/artifactory-settings.png"><img src="http://blog.james-carr.org/wp-content/uploads/2011/09/artifactory-settings.png" alt="" title="artifactory-settings" width="676" height="578" class="aligncenter size-full wp-image-1028" /></a></p>
<p>Next, I create a job named &#8220;module-example release&#8221; which does the same as above except it watches and checks out the master branch and publishes a release artifact rather than a snapshot. </p>
<p>Finally, I have a job named &#8220;module-example post-release&#8221; that checks develop out and bumps the version number up to the next integration release (1.0.1-SNAPSHOT) and pushes it back to the develop branch. I have not found an easy way to do this in jenkins so I simply have a task like the following in my scripts to bump the snapshot version:</p>
<p><script src="https://gist.github.com/1207161.js"> </script></p>
<p>If we push to develop now, we should see a new jar with the version 1.0.0-SNAPSHOT show up in our artifactory snapshots repository. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Let&#8217;s do a Release</h4>
<p>Doing a release with git flow is pretty easy. We&#8217;re ready to deploy version 1.0.0 of our new module so we type </p>
<pre>
git flow release start 1.0.0
</pre>
<p>We now edit our gradle.properties file and take the -SNAPSHOT off the version number and do any other pre-release activities that might be needed. </p>
<p>Once we&#8217;re ready we run </p>
<pre>
git flow release finish 1.0.0 &#038;&#038; git push --all
</pre>
<p>And sit back and watch the magic happen&#8230; 1.0.0 of our module is released to our artifactory server for others to use and when we fetch the latest upstream the next version is 1.0.1-SNAPSHOT in the develop branch. </p>
<h4>Thoughts</h4>
<p>Although the initial setup requires a little investment up front, the flow of actually working and releasing pays off in huge dividends IMHO. Our build script knows next to nothing about what SCM we use and where artifacts get published to&#8230; it&#8217;s only concerned about what it is ment for: building. Releasing is just a matter of running two commands and pushing master. Another nicety is that master ALWAYS reflects something that could be released&#8230; there&#8217;s no confusion on whether or not what is in master could be released. </p>
<p>In the near future I&#8217;ll follow up more with our evolving process of deploying JEE web applications using this process. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/09/09/my-current-java-workflow/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Playing To Win</title>
		<link>http://blog.james-carr.org/2011/08/21/playing-to-win/</link>
		<comments>http://blog.james-carr.org/2011/08/21/playing-to-win/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 04:44:58 +0000</pubDate>
		<dc:creator>James Carr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.james-carr.org/?p=1023</guid>
		<description><![CDATA[Previously I had posted about the sad drudgery developers often have to deal with in large companies. I&#8217;ve decided to follow up that post due to two epic milestones the past week: taking RabbitMQ live within the company and getting github blessed by legal and infrastructure for internal use. My previous post was a rant [...]]]></description>
			<content:encoded><![CDATA[<p>Previously I had posted about the sad drudgery developers often have to deal with in large companies. I&#8217;ve decided to follow up that post due to two epic milestones the past week: taking RabbitMQ live within the company and getting github blessed by legal and infrastructure for internal use. My previous post was a rant in response to port 22 being blocked and preventing us developers from using github so I thought I&#8217;d detail how I got past the resistance and how you too as a corporate rebel can help bring change to your company as well. <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First, let me set the stage. It has been my experience that larger companies tend to err on the side of caution when it comes to introducing new technologies/ideas/practices. This can be due to being burned in the past as well as a combination of getting more process or control in place due to social scalability issues. The later just boils down to it being easier to trust the devs you work with or near you versus 200 or so developers of which sadly a percentage have probably downloaded viruses or other silly things. There&#8217;s always a lot of resistance within these type of companies as adopting something new poses a risk that quite frankly some people might rather do without. So, given my successes in the last couple weeks I&#8217;ve decided to share some of my techniques for battling this resistance. </p>
<ol>
<li><strong>Research. </strong> Do a lot of experimentation and research on what it is you are wanting to bring in. Sometimes this involves just using it on personal or side projects. I&#8217;ve also found that writing articles, blog posts, and presenting at usergroups about the topic is a GREAT way to learn more about it. </li>
<li><strong>Share Information.</strong> The best way to have a new idea gain traction is to share information about it. Brown bag lunches and internal presentations are the gold standard here, just give presentations and demos internally to those who are interested without trying to imply you&#8217;re hoping the company will adopt it. Not only will this expose people to it but it will also help you gain insight into what kind of concerns people might have. In addition to presentations, also share links from time to time on case studies, tutorials, and the like. Extra bonus: you might be a catalyst that will ignite someone else to help bring the idea forward.</li>
<li><strong>Find an ally.</strong> With my situation with RabbitMQ, I found allies in different departments and even on the business side. These &#8220;alliances&#8221; came in strong for the win in the 11th hour, especially when the dreaded &#8220;what is the business justification for&#8230;&#8221; resistance tactic came in. When that question came up, my product owner was able to pipe up and make a strong case for why he needed what I was proposing. Not only that, but he was able to help coordinate with managers in other departments to ensure we succeeded.</li>
<li><strong>Be there.</strong> Every one hates 1am production support issues. Remember that what you&#8217;re trying to bring to the company will undoubtedly lead to someone&#8217;s lost sleep sooner or later and they know it. Be ready to take the fallout from the risk that your change will bring and be ready to respond to production support calls. The first night we took RabbitMQ live I had to bust my butt and head down to the data center at two in the morning to respond to &#8220;queue depth limit exceeded&#8221; alerts because message consumers weren&#8217;t able to stay caught up with the heavy load from publishers. The moral is, be ready to be available and respond to those situations. Make sure others know that you are taking the responsibility for the risks that you are bringing. Bonus points in outlining those risks and how you will counter them. </li>
<li><strong>Never give up.</strong> If you&#8217;re convinced that something will improve your company and make a change for the better, stand behind it 100%. No matter how many times you fail keep pushing it, push until it breaks. There are a lot of times I&#8217;ve seen people talk about things like &#8220;it&#8217;d be a lot better if we used something like Hadoop instead of our homebrew framework&#8221; and not do squat about it. They might even self defeat themselves by making an excuse for why it probably won&#8217;t do well in our environment. Don&#8217;t let that happen! Throw your full weight behind it don&#8217;t give up.</li>
</ol>
<p>Hope that helps for all of you other change agents out there! <img src='http://blog.james-carr.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.james-carr.org/2011/08/21/playing-to-win/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

