<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Dev Notes</title>
	
	<link>http://www.connorgarvey.com/blog</link>
	<description>Connor Garvey's application development notes</description>
	<lastBuildDate>Sat, 07 Apr 2012 02:12:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/connorgarvey/devnotes" /><feedburner:info uri="connorgarvey/devnotes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>A New Gradle Grails Plugin</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/CgYlbOPJRmE/</link>
		<comments>http://www.connorgarvey.com/blog/?p=271#comments</comments>
		<pubDate>Sat, 07 Apr 2012 02:11:57 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Gradle]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=271</guid>
		<description><![CDATA[My team wanted to use Gradle to build our Grails project.  We had some trouble with version compatibility problems between Grails and the Grails build plugin, though.  Instead of using the plugin, we tried writing some code that called the &#8216;grails&#8217; command directly, but that caused problems with the CI server because it required that [...]]]></description>
			<content:encoded><![CDATA[<p>My team wanted to use <a href="http://www.gradle.org">Gradle</a> to build our <a href="http://www.grails.org">Grails</a> project.  We had some trouble with version compatibility problems between Grails and the Grails build plugin, though.  Instead of using the plugin, we tried writing some code that called the &#8216;grails&#8217; command directly, but that caused problems with the CI server because it required that the correct version of Grails be preinstalled on all build servers and exist in a known location.  I decided to try my hand at writing a simpler Grails build plugin and this is what I came up with.  It doesn&#8217;t require Grails to be pre-installed on the machine and, since it just executes the &#8216;grails&#8217; command, it works with any version.</p>
<p><a href="http://connorwgarvey.github.com/gradle-grails-wrapper/">Fork it!  Or just use it <img src='http://www.connorgarvey.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </a></p>
<p><a href="http://connorwgarvey.github.com/gradle-grails-wrapper/">Gradle Grails Wrapper</a></p>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/CgYlbOPJRmE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=271</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=271</feedburner:origLink></item>
		<item>
		<title>Formatting JSON and XML in Gedit</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/Z2wrNa-BRu0/</link>
		<comments>http://www.connorgarvey.com/blog/?p=264#comments</comments>
		<pubDate>Tue, 22 Nov 2011 19:45:28 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[gedit]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=264</guid>
		<description><![CDATA[Here&#8217;s how to add commands to Gedit to format JSON and XML documents. Ensure that you have an up-to-date version of Python.  It&#8217;s included with nearly every Linux distribution. Ensure that the External Tools plugin is installed Click Edit -&#62; Preferences Select the Plugins tab Check the box next to External Tools Click Close Add [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to add commands to Gedit to format JSON and XML documents.</p>
<ol>
<li>Ensure that you have an up-to-date version of Python.  It&#8217;s included with nearly every Linux distribution.</li>
<li>Ensure that the External Tools plugin is installed</li>
<ol>
<li>Click <em>Edit</em> -&gt; <em>Preferences</em></li>
<li>Select the <em>Plugins</em> tab</li>
<li>Check the box next to External Tools</li>
<li>Click Close</li>
</ol>
<li>Add the <em>Format JSON</em> command</li>
<ol>
<li>Click <em>Tools</em> -&gt; <em>Manage External Tools&#8230;</em></li>
<li>Click <em>New</em> (bottom left, looks like a piece of paper with a plus sign)</li>
<li>Enter a name (<em>Format JSON</em>)</li>
<li>Paste this text into the text window on the right<br />
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="co1">#! /usr/bin/env python</span>
&nbsp;
<span class="kw1">import</span> json
<span class="kw1">import</span> <span class="kw3">sys</span>
&nbsp;
j = json.<span class="me1">load</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">stdin</span><span class="br0">&#41;</span>
<span class="kw1">print</span> json.<span class="me1">dumps</span><span class="br0">&#40;</span>j, sort_keys=<span class="kw2">True</span>, indent=<span class="nu0">2</span><span class="br0">&#41;</span></pre></div></div></li>
<li>Set <em>Input</em> to <em>Current document</em></li>
<li>Set <em>Output</em> to <em>Replace current document</em></li>
</ol>
<li>Add the <em>Format XML</em> command</li>
<ol>
<li>Install lxml (on Ubuntu, sudo apt-get install python-lxml)</li>
<ol>
<li>Python&#8217;s included XML modules either don&#8217;t support pretty printing or are buggy</li>
</ol>
<li>Create a new external tool configuration as above (<em>Format XML</em>)</li>
<li>Paste this text into the text window on the right<br />
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="co1">#! /usr/bin/env python</span>
&nbsp;
<span class="kw1">import</span> <span class="kw3">sys</span>
<span class="kw1">import</span> lxml.<span class="me1">etree</span> <span class="kw1">as</span> etree
<span class="kw1">import</span> <span class="kw3">traceback</span>
&nbsp;
result = <span class="st0">''</span>
<span class="kw1">for</span> line <span class="kw1">in</span> <span class="kw3">sys</span>.<span class="me1">stdin</span>:
  result += line
<span class="kw1">try</span>:
  x = etree.<span class="me1">fromstring</span><span class="br0">&#40;</span>result<span class="br0">&#41;</span>
  result = etree.<span class="me1">tostring</span><span class="br0">&#40;</span>x, pretty_print=<span class="kw2">True</span><span class="br0">&#41;</span>
<span class="kw1">except</span>:
  etype, evalue, etraceback = <span class="kw3">sys</span>.<span class="me1">exc_info</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="kw3">traceback</span>.<span class="me1">print_exception</span><span class="br0">&#40;</span>etype, evalue, etraceback, <span class="kw2">file</span>=<span class="kw3">sys</span>.<span class="me1">stderr</span><span class="br0">&#41;</span>
<span class="kw1">print</span> result</pre></div></div>&nbsp;</li>
<li>Set <em>Input</em> to <em>Current document</em></li>
<li>Set <em>Output</em> to <em>Replace current document</em></li>
</ol>
</ol>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/Z2wrNa-BRu0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=264</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=264</feedburner:origLink></item>
		<item>
		<title>A Yammer App Using Gaelyk for Google App Engine</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/AYg7T8Sp3JU/</link>
		<comments>http://www.connorgarvey.com/blog/?p=255#comments</comments>
		<pubDate>Fri, 19 Aug 2011 23:22:21 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Gaelyk]]></category>
		<category><![CDATA[Google App Engine]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=255</guid>
		<description><![CDATA[I recently attended a presentation by Tim Berglund about Gaelyk at Uberconf.  The technology of Gaelyk is pretty straightforward and doesn&#8217;t need much of an introduction, especially with the excellent tutorial available on its web site.  Watching the presentation, I was struck by how little there is to Gaelyk and wondered why I was there.  [...]]]></description>
			<content:encoded><![CDATA[<p>I recently attended a presentation by <a href="http://www.augusttechgroup.com/tim/about/">Tim Berglund</a> about <a href="http://gaelyk.appspot.com/">Gaelyk</a> at <a href="http://uberconf.com/">Uberconf</a>.  The technology of Gaelyk is pretty straightforward and doesn&#8217;t need much of an introduction, especially with <a href="http://gaelyk.appspot.com/tutorial">the excellent tutorial available on its web site</a>.  Watching the presentation, I was struck by how little there is to Gaelyk and wondered why I was there.  Sitting in a room of developers and coding a working, useful application in just over an hour, though, reminded me about how much fun web programming can be.  Many of us have become so accustomed to frameworks like Struts, WebMVC, GWT and Grails that we&#8217;ve forgotten the simpler times, when you could slap together some Servlets and JSPs and have fun putting together a small, useful app.  Gaelyk brings back those simpler times and makes them even more enjoyable by adding the conveniences of Groovy and App Engine.</p>
<p>I picked an app that I wanted to code, something that would tell Yammer users what their companies are talking about.  It&#8217;s available for anyone to use at <a href="http://whatyammer.appspot.com">http://whatyammer.appspot.com</a>.</p>
<h1>Data access</h1>
<p>Want to save a record to the database?  Forget about messing around with bean and data access classes, database connections and ORM.  No need to create tables.  You&#8217;re coding for fun!</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;"><span class="kw2">def</span> user <span class="sy0">=</span> <span class="kw2">new</span> <span class="kw3">Entity</span><span class="br0">&#40;</span><span class="st0">'user'</span><span class="br0">&#41;</span>
user.<span class="me1">email</span> <span class="sy0">=</span> params.<span class="me1">email</span>
user.<span class="me1">name</span> <span class="sy0">=</span> params.<span class="me1">name</span>
user.<span class="me1">phone</span> <span class="sy0">=</span> params.<span class="me1">phone</span>
user.<span class="me1">save</span><span class="br0">&#40;</span><span class="br0">&#41;</span></pre></div></div>
<p>That&#8217;s all.  To get that record back out <div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;">datastore.<span class="kw9">execute</span> <span class="br0">&#123;</span>
  select single from user
  where email <span class="sy0">==</span> params.<span class="me1">email</span>
<span class="br0">&#125;</span></pre></div></div></p>
<h1>Forwarding objects to a view</h1>
<p>Gaelyk views are coded in gtpl files, which are much like JSPs. The controller, echo.groovy <div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">echo.groovy</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;">request.<span class="me1">users</span> <span class="sy0">=</span> datastore.<span class="kw9">execute</span> <span class="br0">&#123;</span> select all from user where email <span class="sy0">==</span> params.<span class="me1">email</span> <span class="br0">&#125;</span>
forward <span class="st0">'showusers.gtpl'</span></pre></div></div> The view, showusers.gtpl <div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">showusers.gtpl</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;">Adventure <span class="kw3">Time</span> with <span class="sy0">&lt;%=</span> <span class="br0">&#40;</span>request.<span class="me1">users</span><span class="sy0">*</span>.<span class="me1">name</span><span class="br0">&#41;</span>.<span class="kw5">join</span><span class="br0">&#40;</span><span class="st0">' and '</span><span class="br0">&#41;</span> <span class="sy0">%&gt;</span></pre></div></div> That prints the names of all users, with the word and between each one.  That is just a small taste of what Gaelyk makes available.  So simple!  So much fun!  Single-command deployment!  Google will host 10 apps for free, so there isn&#8217;t any reason not to <a href="http://gaelyk.appspot.com/download">try it out</a>.  Just note that before you deploy, you have to visit <a href="http://appengine.google.com">http://appengine.google.com</a> and register your new app or Google will give you a very unhelpful error message.</p>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/AYg7T8Sp3JU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=255</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=255</feedburner:origLink></item>
		<item>
		<title>Functional Test and Integration Test Targets in Gradle</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/01mfN5COX1Y/</link>
		<comments>http://www.connorgarvey.com/blog/?p=243#comments</comments>
		<pubDate>Tue, 11 Jan 2011 15:17:14 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[Gradle]]></category>
		<category><![CDATA[Groovy]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=243</guid>
		<description><![CDATA[* Update February 13th, 2012: Thanks to Ben Ripkens for updates to match the new Gradle API. While searching online, I found many suggestions for how to add a new test target to a Gradle script. Most of them were wrong and others didn&#8217;t properly separate the integration test target from standard targets. After not [...]]]></description>
			<content:encoded><![CDATA[<p><em>* Update February 13th, 2012: Thanks to Ben Ripkens for updates to match the new Gradle API.</em></p>
<p>While searching online, I found many suggestions for how to add a new test target to a Gradle script. Most of them were wrong and others didn&#8217;t properly separate the integration test target from standard targets. After not finding a solution, I came up with one on my own.</p>
<p>This example sets up integration tests for Groovy .</p>
<h2>Create a source set</h2>
<p>This will separate the integration test code from other code, allowing it to be built separately.</p>
<ul>
<li>The classpath in the example gives integration tests access to all application and test classes</li>
<li>The source location will be src/integrationTest/groovy</li>
</ul>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;">sourceSets <span class="br0">&#123;</span>
  integrationTest <span class="br0">&#123;</span>
    compileClasspath <span class="sy0">=</span> sourceSets.<span class="me1">main</span>.<span class="me1">output</span> <span class="sy0">+</span> configurations.<span class="me1">testRuntime</span>
    runtimeClasspath <span class="sy0">=</span> output <span class="sy0">+</span> sourceSets.<span class="me1">main</span>.<span class="me1">output</span> <span class="sy0">+</span> configurations.<span class="me1">testRuntime</span>
    groovy <span class="br0">&#123;</span>
      srcDir <span class="st0">'src/integrationTest/groovy'</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<h2>Add the target</h2>
<p>&nbsp;</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="groovy" style="font-family:monospace;">task integrationTest<span class="br0">&#40;</span>type: Test<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  testClassesDir <span class="sy0">=</span> sourceSets.<span class="me1">integrationTest</span>.<span class="me1">output</span>.<span class="me1">classesDir</span>
  classpath <span class="sy0">=</span> sourceSets.<span class="me1">integrationTest</span>.<span class="me1">runtimeClasspath</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/01mfN5COX1Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=243</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=243</feedburner:origLink></item>
		<item>
		<title>Antcall vs Depends</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/OzZwdANsnfI/</link>
		<comments>http://www.connorgarvey.com/blog/?p=227#comments</comments>
		<pubDate>Fri, 17 Sep 2010 22:23:41 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[antcall]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[depends]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=227</guid>
		<description><![CDATA[Occasionally, I see issues pop up on Java projects about builds not working properly.  They usually look like, &#8220;The build worked yesterday, but it doesn&#8217;t work today.  I was working on it, but I didn&#8217;t change anything related to the part that is failing.&#8221; or &#8220;I can&#8217;t figure out why, when I run this by [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally, I see issues pop up on Java projects about builds not working properly.  They usually look like, &#8220;The build worked yesterday, but it doesn&#8217;t work today.  I was working on it, but I didn&#8217;t change anything related to the part that is failing.&#8221; or &#8220;I can&#8217;t figure out why, when I run this by itself, it passes.  When I run it as a part of a full build, it fails.&#8221;  Build errors are almost always caused by a misunderstanding of the function of &#8220;depends&#8221;.</p>
<h1>How Not to Use Depends</h1>
<p>This probably works, but only by chance.</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;clean,compile,compile-test,test,service-test&quot;</span> <span class="re2">/&gt;</span></span></pre></div></div>
<p>Depends is not a list of tasks to be executed.  It&#8217;s a list of dependencies that must be satisfied before a target can be completed.  Execution is not guaranteed and side effects are common.  Take a close look at what each of the  tasks in the following script is doing.</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;functional-test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;start-server,run-tests,stop-server&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;service-test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;start-server,run-service-tests,stop-server&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;clean,compile,compile-test,functional-test,service-test&quot;</span> <span class="re2">/&gt;</span></span></pre></div></div>
<p>To a human, the intent is obvious.</p>
<ol>
<li>Clean and compile</li>
<li>Start server</li>
<li>Run functional tests</li>
<li>Stop server</li>
<li>Start server</li>
<li>Run service tests</li>
<li>Stop server</li>
</ol>
<p>But Ant will not execute those tasks.  Since they were declared as dependencies, Ant evaluates them as such.  The functional tests will run as you would expect.  When it runs the service-test target, though, it will see the start and stop server tasks <em>again</em>.  As far as it&#8217;s concerned, those dependencies have already been satisfied, so it will skip start-server and stop-server.  Ant is evaluating the script correctly, but the script is wrong.</p>
<p>Dependencies are not tasks to be executed.  They are dependencies.  Once a dependency has been satisfied, it will not be executed again.</p>
<h1>Antcall Is Not Evil</h1>
<p>It exists for a reason.  Use it when it is useful.  Dependencies are dependencies, not task lists.  In this case, it would be impractical to try to write the script to use depends attributes to cause events to happen correctly.  It can be fixed very easily by using antcall.  Each antcall is evaluated separately, so you don&#8217;t have to worry about side effects like the script above.  It&#8217;s also easier to read.  You can see what&#8217;s intended to happen before this target and you can see what this target is intended to do.</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;functional-test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;start-server,run-tests,stop-server&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;service-test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;start-server,run-service-tests,stop-server&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;target</span> <span class="re0">name</span>=<span class="st0">&quot;test&quot;</span> <span class="re0">depends</span>=<span class="st0">&quot;clean,compile,compile-test&quot;</span><span class="re2">&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;functional-test&quot;</span> <span class="re2">/&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;antcall</span> <span class="re0">target</span>=<span class="st0">&quot;service-test&quot;</span> <span class="re2">/&gt;</span></span>
<span class="sc3"><span class="re1">&lt;/target<span class="re2">&gt;</span></span></span></pre></div></div>
<h1>Use Your Own Judgement</h1>
<p>Obviously, antcall should not be used instead of depends.  Depends is far more useful.  But don&#8217;t throw a tool like antcall away just because it doesn&#8217;t seem as cool or concise or elegant.</p>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/OzZwdANsnfI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=227</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=227</feedburner:origLink></item>
		<item>
		<title>A threaded Go language application with semaphores</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/6RWvo3iLJgE/</link>
		<comments>http://www.connorgarvey.com/blog/?p=221#comments</comments>
		<pubDate>Fri, 21 May 2010 13:54:34 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Go]]></category>
		<category><![CDATA[go language]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=221</guid>
		<description><![CDATA[After hearing about Go and its love of parallelism, I wanted to give it a try. I couldn&#8217;t think of any small projects that needed to be threaded until recently.  My employer uses an RPM system, but I&#8217;m using Ubuntu, which requires deb files.  Searching through the RPMs in a browser is a pain, so [...]]]></description>
			<content:encoded><![CDATA[<p>After hearing about <a href="http://golang.org/">Go</a> and its <a href="http://golang.org/doc/effective_go.html#concurrency">love of parallelism</a>, I wanted to give it a try.  I couldn&#8217;t think of any small projects that needed to be threaded until recently.  My employer uses an RPM system, but I&#8217;m using Ubuntu, which requires deb files.  Searching through the RPMs in a browser is a pain, so I wanted a small script that would search for me.  Here&#8217;s the script with server names changed.</p>
<pre style="padding-left: 30px;">package main

import (
  "bufio"
  "fmt"
  "http"
  "os"
  "regexp"
  "strings"
)

var servers = []string {
  "http://www.google.com/search?client=ubuntu&amp;channel=fs&amp;q=go+language&amp;ie=utf-8&amp;oe=utf-8",
  "http://golang.org/",
  "http://golang.org/doc/go_tutorial.html"}

var maxOpenRequests int = 3

var rpmRegexp = regexp.MustCompile("href=\"[^\"]+\"")

func main() {
  search := getSearchTerm()
  out := make(chan string)
  done := make(chan int)
  go printer(out)
  for _, url := range servers {
    go searchURL(search, url, out, done)
  }
  for i := 0; i &lt; len(servers); i++ {
    &lt;- done
  }
}

func getSearchTerm() string {
  args := os.Args
  if len(args) != 2 {
    die("Please enter one search term")
  }
  return args[1]
}

func die(message string) {
  fmt.Println(message)
  os.Exit(1)
}

func printer(out chan string) {
  for {
    fmt.Println(&lt;- out)
  }
}

var requestSemaphore = make(chan int, maxOpenRequests) // Integer chanel with a maximum queue size

func searchURL(search string, url string, out chan string, done chan int) {
  requestSemaphore &lt;- 1 // Block until put in the semaphore queue
  response, realURL, err := http.Get(url)
  if err == nil {
    bufferedReader := bufio.NewReader(response.Body)
    err = searchAll(search, bufferedReader, url, out)
    response.Body.Close()
  }
  if err != nil {
    die("Could not read from " + realURL + ":" + err.String())
  }
  &lt;- requestSemaphore // Dequeue from the semaphore
  done &lt;- 1 // Signal that function is done
}

func searchAll(search string, reader *bufio.Reader, httpRoot string, out chan string) (os.Error) {
  var error os.Error = nil;
  for {
    var line, err = reader.ReadString('\n')
    if err != nil {
      if err == os.EOF {
        break
      }
      return error
    }
    var hrefs = rpmRegexp.AllMatchesStringIter(line, 0)
    for href := range hrefs {
      start := strings.Index(href, "\"") + 1
      end := len(href) - 1
      packageFile := href[start : end]
      if strings.Index(packageFile, search) != -1 {
        out &lt;- httpRoot + packageFile
      }
    }
  }
  return error
}
</pre>
<h1>A quick overview</h1>
<p>Go uses &#8220;channels&#8221; for communication among threads.</p>
<ol>
<li>The main thread creates two channels, one to receive search output and one to receive &#8220;done&#8221; responses when a child thread finishes.</li>
<li>The main thread attaches the output channel to a simple function that prints to standard out.</li>
<li>The main thread spawns one child thread per server, giving each the output and done channels.</li>
<li>The main thread loops over the done channel once for each server, waiting until all requests are complete.</li>
<li>Each child blocks immediately until space is available in a semaphore.</li>
<li>The child performs the HTTP get, then searches the response for anything matching a regular expression.  Any matches are signaled back to the output channel.</li>
<li>The child opens a space in the semaphore and signals to the main thread that it is done.</li>
</ol>
<p>There isn&#8217;t much more to say.  The <a href="http://golang.org/">Go web site</a> has <a href="http://golang.org/doc/go_tutorial.html">great tutorials</a>, so I don&#8217;t yet feel the need to write one.</p>
<p>By the way, if you&#8217;re searching for Go resources, be sure to search for &#8220;go language&#8221; and not &#8220;go&#8221;.  But then, that&#8217;s probably how you found this page <img src='http://www.connorgarvey.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">package main</p>
<p>import (<br />
&#8220;bufio&#8221;<br />
&#8220;fmt&#8221;<br />
&#8220;http&#8221;<br />
&#8220;os&#8221;<br />
&#8220;regexp&#8221;<br />
&#8220;strings&#8221;<br />
)</p>
<p>var servers = []string {<br />
&#8220;http://apt.duncllc.com/dist/WS4.0/RedHat/RPMS/&#8221;,<br />
&#8220;http://apt.duncllc.com/dist/WS4.0/RedHat/RPMS.extras/&#8221;,<br />
&#8220;http://apt.duncllc.com/dist/WS4.0/RedHat/RPMS.java/&#8221;}</p>
<p>var maxOpenRequests int = 3</p>
<p>var rpmRegexp = regexp.MustCompile(&#8220;href=\&#8221;[^\"]+\&#8221;")</p>
<p>func main() {<br />
search := getSearchTerm()<br />
out := make(chan string)<br />
done := make(chan int)<br />
go printer(out)<br />
for _, url := range servers {<br />
go searchURL(search, url, out, done)<br />
}<br />
for i := 0; i &lt; len(servers); i++ {<br />
&lt;- done<br />
}<br />
}</p>
<p>func getSearchTerm() string {<br />
args := os.Args<br />
if len(args) != 2 {<br />
die(&#8220;Please enter one search term&#8221;)<br />
}<br />
return args[1]<br />
}</p>
<p>func die(message string) {<br />
fmt.Println(message)<br />
os.Exit(1)<br />
}</p>
<p>func printer(out chan string) {<br />
for {<br />
fmt.Println(&lt;- out)<br />
}<br />
}</p>
<p>var requestSemaphore = make(chan int, maxOpenRequests) // Integer chanel with a maximum queue size</p>
<p>func searchURL(search string, url string, out chan string, done chan int) {<br />
requestSemaphore &lt;- 1 // Block until put in the semaphore queue<br />
response, realURL, err := http.Get(url)<br />
if err == nil {<br />
bufferedReader := bufio.NewReader(response.Body)<br />
err = searchAll(search, bufferedReader, url, out)<br />
response.Body.Close()<br />
}<br />
if err != nil {<br />
die(&#8220;Could not read from &#8221; + realURL + &#8220;:&#8221; + err.String())<br />
}<br />
&lt;- requestSemaphore // Dequeue from the semaphore<br />
done &lt;- 1 // Signal that function is done<br />
}</p>
<p>func searchAll(search string, reader *bufio.Reader, httpRoot string, out chan string) (os.Error) {<br />
var error os.Error = nil;<br />
for {<br />
var line, err = reader.ReadString(&#8216;\n&#8217;)<br />
if err != nil {<br />
if err == os.EOF {<br />
break<br />
}<br />
return error<br />
}<br />
var hrefs = rpmRegexp.AllMatchesStringIter(line, 0)<br />
for href := range hrefs {<br />
start := strings.Index(href, &#8220;\&#8221;") + 1<br />
end := len(href) &#8211; 1<br />
packageFile := href[start : end]<br />
if strings.Index(packageFile, search) != -1 {<br />
out &lt;- httpRoot + packageFile<br />
}<br />
}<br />
}<br />
return error<br />
}</p>
</div>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/6RWvo3iLJgE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=221</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=221</feedburner:origLink></item>
		<item>
		<title>Python Package Structure with Unit Testing, Injection and Mocking</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/HLPacpHPOrY/</link>
		<comments>http://www.connorgarvey.com/blog/?p=184#comments</comments>
		<pubDate>Sun, 07 Feb 2010 03:25:15 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=184</guid>
		<description><![CDATA[Python is a language that some people like and use.  I recently was given a small Python application and had to refactor it so that I could implement unit and functional tests.  It took me a while to find the resources and learn how to implement them, so I thought I&#8217;d write it up here. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.python.org/">Python</a> is a language that some people like and use.  I recently was given a small Python application and had to refactor it so that I could implement unit and functional tests.  It took me a while to find the resources and learn how to implement them, so I thought I&#8217;d write it up here.</p>
<p><em>* Please note that this is not a unit testing tutorial.  Those are plentiful and can be found elsewhere.  This describes setting up a professional Python project with a real package structure, injection and tests.</em></p>
<h1>Python Project Structure</h1>
<p>Python was conceived as a scripting language.  Unlike compiled languages like C or Java, Python files are designed to be directly runnable.</p>
<ul>
<li><code>RunMe.java -p someargument</code> <strong>&lt; <span style="color: #800000;">Not ok!</span></strong></li>
<li><code>RunMe.py -p someargument</code> <strong>&lt; <span style="color: #339966;">Ok!</span></strong></li>
</ul>
<p>This is convenient for small scripts, but doesn&#8217;t help applications.  Since you are intended to run <code>.py</code> files directly, Python projects should not have source (<code>src</code>) or binary (<code>bin</code>) folders.  Having a source folder makes a program difficult to run.  The root of the Python package structure should be the root of the project.</p>
<p>For the same reason, the <code>test</code> folder should not be separate.  It should be a package in the main project.  Since the root folder is the root package, you won&#8217;t have much choice.</p>
<h2>Example</h2>
<ul>
<li><code><span style="color: #008080;">MyProject</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code><span style="color: #008080;">model</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code>user.py</code></li>
</ul>
</li>
<li><code><span style="color: #008080;">service</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code>user_service.py</code></li>
<li><code>ldap_user_service.py</code></li>
</ul>
</li>
<li><code><span style="color: #008080;">test</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code><span style="color: #008080;">unit</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code><span style="color: #008080;">service</span></code>
<ul>
<li><code>__init__.py</code></li>
<li><code>user_service_test.py</code></li>
</ul>
</li>
</ul>
</li>
<li><code><span style="color: #008080;">functional</span></code>
<ul>
<li><code><span style="font-family: monospace;">__init__.py</span></code></li>
<li><code><span style="color: #008080;">service</span></code>
<ul>
<li><code><span style="font-family: monospace;">__init__.py</span></code></li>
<li><code><span style="font-family: monospace;">user_service_test.py</span></code></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>If you&#8217;re new to Python, please note that those <code>__init__.py</code>s are required.  They tell Python which folders contain Python files.  This is Python&#8217;s way of allowing you to have other folders, such as config, that don&#8217;t contain source files.</p>
<table style="border-collapse: collapse; width: 100%; background-color: #edfcef; border: 1px solid #a1f6a3;" border="1" cellpadding="3" align="right">
<tbody>
<tr>
<td><span style="color: #008000;"><strong>Note</strong></span></p>
<p>You should specify exactly one class per file.  It makes your code easier to find and read.</td>
</tr>
</tbody>
</table>
<h1>Injection with snake-guice</h1>
<p>Injection is essential for unit tests.  In the example above, <code>LDAPUserService</code> may connect to an LDAP server to retrieve information, something we don&#8217;t want to happen in unit tests.</p>
<p>I&#8217;ve been using <a href="http://code.google.com/p/snake-guice/">snake-guice</a> and highly recommend it.</p>
<h2>Install snake-guice, Mock and nose</h2>
<p>If you don&#8217;t have it, <a href="http://pypi.python.org/pypi/setuptools">grab a copy of easy install</a>.  Then, <code>$ sudo easy_install.py snake-guice Mock nose</code>.  That installs packages from <a href="http://pypi.python.org/pypi">PyPI, the Python Package Index</a>.  For those used to non-scripting languages, this may not seem like a great way to use external libraries.  Since Python is a scripting language, however, it&#8217;s the only way that makes sense.</p>
<h2>Create a module and use it to instantiate your application</h2>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">class</span> ExampleModule:
  <span class="kw1">def</span> configure<span class="br0">&#40;</span><span class="kw2">self</span>, binder<span class="br0">&#41;</span>
    binder.<span class="me1">bind</span><span class="br0">&#40;</span>UserService, to=LDAPUserService<span class="br0">&#41;</span></pre></div></div>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">class</span> ExampleRunner:
  user_service = <span class="kw2">None</span>
&nbsp;
  @inject<span class="br0">&#40;</span>user_service=UserService<span class="br0">&#41;</span>
  <span class="kw1">def</span> <span class="kw4">__init__</span><span class="br0">&#40;</span><span class="kw2">self</span>, user_service<span class="br0">&#41;</span>:
    <span class="kw2">self</span>.<span class="me1">user_service</span> = user_service
&nbsp;
  <span class="kw1">def</span> main<span class="br0">&#40;</span>user_service<span class="br0">&#41;</span>
    <span class="kw2">self</span>.<span class="me1">user_service</span>.<span class="me1">login</span><span class="br0">&#40;</span><span class="st0">&quot;name&quot;</span>, <span class="st0">&quot;password&quot;</span><span class="br0">&#41;</span></pre></div></div>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;">injector = Injector<span class="br0">&#40;</span>ExampleModule<span class="br0">&#41;</span>
runner = injector.<span class="me1">get_instance</span><span class="br0">&#40;</span>ExampleRunner<span class="br0">&#41;</span></pre></div></div>
<p>When <code>get_instance</code> is called, <code>injector</code> will use the <code>ExampleModule</code> to discover bindings.  Then, it will create an <code>ExampleRunner</code>, passing in arguments specified by <code>@inject</code>.</p>
<p>The <code>setUp</code> method of <a href="http://pyunit.sourceforge.net/">PyUnit</a> tests should create an injector with a <code>TestExampleModule</code>, which should be configured to return mocks.</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">class</span> TestExampleModule:
  <span class="kw1">def</span> user_service = Mock<span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp;
  <span class="kw1">def</span> configure<span class="br0">&#40;</span><span class="kw2">self</span>, binder<span class="br0">&#41;</span>
    binder.<span class="me1">bind</span><span class="br0">&#40;</span>UserService, to=user_service<span class="br0">&#41;</span></pre></div></div>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">class</span> ExampleTest<span class="br0">&#40;</span>TestCase<span class="br0">&#41;</span>:
  application = <span class="kw2">None</span>
&nbsp;
  <span class="kw1">def</span> setUp<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
    Injector = Injector<span class="br0">&#40;</span>TestExampleModule<span class="br0">&#41;</span>
    <span class="kw2">self</span>.<span class="me1">application</span> = injector.<span class="me1">get_instance</span><span class="br0">&#40;</span>ExampleApplication<span class="br0">&#41;</span>
&nbsp;
  <span class="kw1">def</span> test_something<span class="br0">&#40;</span><span class="kw2">self</span><span class="br0">&#41;</span>:
    application.<span class="me1">user_service</span>.<span class="me1">search</span>.<span class="me1">return_value</span> = <span class="st0">&quot;expected&quot;</span>
    actual = application.<span class="me1">user_service</span>.<span class="me1">search</span><span class="br0">&#40;</span><span class="st0">&quot;any&quot;</span><span class="br0">&#41;</span>
    <span class="kw2">self</span>.<span class="me1">assertEquals</span><span class="br0">&#40;</span>expected, actual<span class="br0">&#41;</span></pre></div></div>
<p>Since <code>setUp</code> is called before each test, each test method gets a fresh mock that it can configure any way it likes.</p>
<p>Similarly, a functional test module can be created that connects to a local or testing server.</p>
<h1>Mocking with Mock</h1>
<p>I recommend using <a href="http://www.voidspace.org.uk/python/mock/">Mock</a> for mocking, which is why I had you install it earlier.  Be careful when searching for it because there appear to be two projects called Mock.</p>
<p>The Mock package is well documented.  Please <a href="http://www.voidspace.org.uk/python/mock/getting-started.html">go to the Mock site for more information</a>.</p>
<h1>Running Tests</h1>
<p>For running tests, I recommend <a href="http://code.google.com/p/python-nose/">Nose</a>.  To run your tests, open a command line and change to your root project folder.  Then, nosetests.  Nose will take care of adding the packages you installed earlier, snake-guice and Mock, to the Python path.  It will also find all tests, run them and report the results.  Handy!</p>
<p><code>$ cd ~/exampleproject<br />
$ nosetests</code></p>
<p>To run only unit or functional tests, use the -w argument.</p>
<p><code>$ nosetests -w ./ ./test/unit<br />
$ nosetests -w ./ ./test/functional</code></p>
<h1>Is that all?</h1>
<p>To those who have not used injection or mocks for testing, this may seem like a lot of work.  I hope you&#8217;ll try it, though, because it&#8217;s a one-time setup.  Once these good practices are in place, you&#8217;ll find that all of your code organization and testing becomes simple and almost automatic.  If I missed something or if you have any suggestions, please post below.</p>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/HLPacpHPOrY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=184</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=184</feedburner:origLink></item>
		<item>
		<title>HTC T-Mobile G1 vs Motorola CLIQ</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/04fwEaGjmgM/</link>
		<comments>http://www.connorgarvey.com/blog/?p=169#comments</comments>
		<pubDate>Tue, 22 Dec 2009 15:57:29 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[CLIQ]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[G1]]></category>
		<category><![CDATA[HTC]]></category>
		<category><![CDATA[Motorola]]></category>
		<category><![CDATA[phone]]></category>
		<category><![CDATA[review]]></category>
		<category><![CDATA[T-Mobile]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=169</guid>
		<description><![CDATA[I owned a G1 for just over a year and it was lost or stolen.  Having used Android for so long, I couldn&#8217;t live without it for 7 months until I was eligible for an upgrade.  I stopped by the T-Mobile store to get a replacement G1, but was pleasantly surprised to find that the [...]]]></description>
			<content:encoded><![CDATA[<p>I owned a G1 for just over a year and it was lost or stolen.  Having used Android for so long, I couldn&#8217;t live without it for 7 months until I was eligible for an upgrade.  I stopped by the T-Mobile store to get a replacement G1, but was pleasantly surprised to find that the CLIQ was the same price.  I knew it was faster and had a headphone jack (yes!), so I picked it up.</p>
<p><iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=devnot-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B002TX76E4" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe> <iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=devnot-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B001JT1U1E" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></p>
<p>It always takes time to get used to a new phone, but even after a month of use, I wasn&#8217;t blown away by the CLIQ.</p>
<ul>
<li>The basics, things that you can read in the phone specs
<ul>
<li>The G1 has a larger screen.  It&#8217;s not a big difference on paper, but is quite noticeable in use.</li>
<li>The CLIQ has a headphone jack, meaning you can listen to music with standard earbuds or headphones or plug it in to your car&#8217;s stereo and charge it at the same time without having to buy and tote around a custom dongle.</li>
</ul>
</li>
<li>Speed &#8211; <strong>CLIQ</strong>
<ul>
<li>Although they have the same processor, the CLIQ wins here pretty handily.  Even after all of the updates between Android 1.0 and 1.6, the G1 was always sluggish.  Some applications slowed it down so much that it became unusable and had to be rebooted.</li>
</ul>
</li>
<li>Battery &#8211; <strong>CLIQ</strong>
<ul>
<li>The G1 had to be recharged at least once a day.  During a day of heavy use, it needed more than one charge.  The CLIQ won&#8217;t go for days on a charge like some non-smart phones, but it easily outlasts the G1.</li>
</ul>
</li>
<li>Keyboard &#8211; <strong>G1</strong>
<ul>
<li>People may complain about the G1&#8242;s rotate-out-slider feature, but it allows the G1 a gigantic, easy-to-use keyboard by phone standards.  Traditional sliders, like the CLIQ&#8217;s, can only go so far up before they become wobbly and prone to breaking.  The G1&#8242;s screen is completely separate from the main body except for the connection at the bottom (or right), allowing it to slide much farther up.  The G1&#8242;s keys are also better designed.  They&#8217;re just as responsive as the CLIQ&#8217;s, but without the annoying, well, clicks made by the &#8230; CLIQ.</li>
</ul>
</li>
<li>D-pad / Trackball &#8211; <strong>G1</strong>
<ul>
<li>The CLIQ&#8217;s directional pad works just as well as the G1&#8242;s trackball.  Each design has it&#8217;s benefits.  The trackball lets you zoom through text or selections, but it&#8217;s difficult to press without accidentally rotating it at the same time.  I&#8217;ll never figure out why the CLIQ&#8217;s designers decided to hide the d-pad under the screen, though.  It&#8217;s really a pain to have to slide the keyboard out and rotate the screen  just to get access to it.  One major score for the G1.</li>
</ul>
</li>
<li>UI &#8211; <strong>G1</strong>
<ul>
<li>Phone manufacturers can never leave an operating system alone.  Imagine if Windows were customized so that it worked completely differently on a Sony than on an HP. Luckily, Motorola didn&#8217;t go as far as others (like Samsung).  Still, the stock Android OS looks much better than Motorola&#8217;s.</li>
</ul>
</li>
<li>OS &#8211; <strong>G1</strong>
<ul>
<li>Here, too, Motorola&#8217;s decision to customize Android is a problem.  HTC can update Android on the G1 by adding device-specific code.  For a CLIQ update, Motorola has to merge the entire Android codebase with their own <a href="http://en.wikipedia.org/wiki/Fork_%28software_development%29">fork</a>.  This means CLIQ updates lag behind other devices.  The G1 has had Android 1.6 for a while.  The CLIQ is still on 1.5 and won&#8217;t be updated until Motorola releases 2.1 for it.  <em>Maybe</em>.</li>
</ul>
</li>
<li>Touchability &#8211; <strong>G1</strong>
<ul>
<li>Many users (including me) have had problems with the CLIQ&#8217;s capacitive touch screen.  Sometimes it works flawlessly and sometimes it doesn&#8217;t.  It&#8217;s especially annoying when playing games.</li>
</ul>
</li>
<li>Exterior appearance &#8211; <strong>CLIQ</strong>
<ul>
<li>The CLIQ&#8217;s bold lines and attractive coloring outshine the boring G1.  I highly recommend the titanium white version.</li>
</ul>
</li>
<li>Sturdiness &#8211; <strong>CLIQ</strong>
<ul>
<li>With a more solid construction and a responsive slider mechanism, the CLIQ feels much better in the hand.</li>
</ul>
</li>
</ul>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/04fwEaGjmgM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=169</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=169</feedburner:origLink></item>
		<item>
		<title>Guice BlazeDS’ MessageBroker</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/zKUmk2wGxRg/</link>
		<comments>http://www.connorgarvey.com/blog/?p=163#comments</comments>
		<pubDate>Tue, 20 Oct 2009 19:10:46 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Guice]]></category>
		<category><![CDATA[blazeds]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=163</guid>
		<description><![CDATA[In a previous post (http://www.connorgarvey.com/blog/?p=132), I wrote about how to use Guice injection for Flex services.  I used web.xml to configure the MessageBrokerServlet and configured each Flex service to use Guice as a factory.  Since then, on this project, we&#8217;ve had to introduce new servlets.  Rather than continue to use web.xml&#8217;s verbose and fully-qualified-path based [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.connorgarvey.com/blog/?p=132">a previous post (http://www.connorgarvey.com/blog/?p=132)</a>, I wrote about how to use Guice injection for Flex services.  I used web.xml to configure the MessageBrokerServlet and configured each Flex service to use Guice as a factory.  Since then, on this project, we&#8217;ve had to introduce new servlets.  Rather than continue to use web.xml&#8217;s verbose and fully-qualified-path based configuration, we moved to using Guice&#8217;s ServletModule class.  Here are the steps we followed.</p>
<ol>
<li>Ensure the guice-servlet.jar is included in your project and is deployed with your build.</li>
<li>Add the Guice filter to web.xml.<br />
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;filter<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filter-name<span class="re2">&gt;</span></span></span>guiceFilter<span class="sc3"><span class="re1">&lt;/filter-name<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filter-class<span class="re2">&gt;</span></span></span>com.google.inject.servlet.GuiceFilter<span class="sc3"><span class="re1">&lt;/filter-class<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/filter<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;filter-mapping<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;filter-name<span class="re2">&gt;</span></span></span>guiceFilter<span class="sc3"><span class="re1">&lt;/filter-name<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;url-pattern<span class="re2">&gt;</span></span></span>/*<span class="sc3"><span class="re1">&lt;/url-pattern<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/filter-mapping<span class="re2">&gt;</span></span></span></pre></div></div></li>
<li>Create a servlet module, a class extending com.google.inject.servlet.ServletModule.</li>
<li>Override the configureServlets() method of ServletModule and add the message broker servlet configuration.<br />
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="java5" style="font-family:monospace;"><span class="kw2">this</span>.<span class="me1">bind</span><span class="br0">&#40;</span>MessageBrokerServlet.<span class="kw2">class</span><span class="br0">&#41;</span>.<span class="me1">in</span><span class="br0">&#40;</span>Scopes.<span class="me1">SINGLETON</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw2">final</span> Map<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span> params = <span class="kw2">new</span> TreeMap<span class="sy0">&lt;</span>String, String<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
params.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;services.configuration.file&quot;</span>, <span class="kw2">this</span>.<span class="me1">context</span>
    .<span class="me1">getRealPath</span><span class="br0">&#40;</span><span class="st0">&quot;WEB-INF/config/flex/services-config.xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw2">this</span>.<span class="me1">serve</span><span class="br0">&#40;</span><span class="st0">&quot;/messagebroker/*&quot;</span><span class="br0">&#41;</span>.<span class="me1">with</span><span class="br0">&#40;</span>MessageBrokerServlet.<span class="kw2">class</span>, params<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div></p>
<ol>
<li>Normally, servlets configured in Guice are tagged with @Singleton.  Since the MessageBrokerServlet is third party, it&#8217;s marked as a singleton here, in the module.</li>
</ol>
</li>
<li>Add the new module to the Guice servlet context listener, which should already be configured in web.xml.</li>
<li>Remove the servlet and servlet-mapping from web.xml.</li>
</ol>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/zKUmk2wGxRg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=163</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=163</feedburner:origLink></item>
		<item>
		<title>Using Guice with Flex remoting for BlazeDS</title>
		<link>http://feedproxy.google.com/~r/connorgarvey/devnotes/~3/cYTR7KmEBxE/</link>
		<comments>http://www.connorgarvey.com/blog/?p=132#comments</comments>
		<pubDate>Sat, 20 Jun 2009 04:30:55 +0000</pubDate>
		<dc:creator>Connor Garvey</dc:creator>
				<category><![CDATA[Guice]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[blazeds]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[injection]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Redpoint]]></category>
		<category><![CDATA[source]]></category>

		<guid isPermaLink="false">http://www.connorgarvey.com/blog/?p=132</guid>
		<description><![CDATA[I just finished putting a server together that uses Google&#8217;s Guice and Adobe&#8217;s BlazeDS.  Here&#8217;s how you can quickly get a project working using the same configuration. * Update: A new post describes using Guice&#8217;s ServletModule to configure the MessageBrokerServlet. Download the libraries Download Guice and BlazeDS.  Use the project configured in the BlazeDS war [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished putting a server together that uses <a href="http://code.google.com/p/google-guice/">Google&#8217;s Guice</a> and <a href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/">Adobe&#8217;s BlazeDS</a>.  Here&#8217;s how you can quickly get a project working using the same configuration.</p>
<p>* <em>Update:</em> <a href="http://www.connorgarvey.com/blog/?p=163">A new post describes using Guice&#8217;s ServletModule to configure the MessageBrokerServlet</a>.</p>
<h3>Download the libraries</h3>
<p>Download <a href="http://code.google.com/p/google-guice/downloads/list">Guice</a> and <a href="http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/">BlazeDS</a>.  Use the project configured in the BlazeDS war to get started.</p>
<h3>Create the Guice factory</h3>
<p>Since BlazeDS provides the servlet, you won&#8217;t be able to configure your classes using Guice unless you have a Flex factory.  This code will inject itself with Guice to get instances of Flex services.</p>
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="java" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.connorgarvey.guiceblazeds.servlet</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">import</span> <span class="co2">java.util.HashMap</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">java.util.Map</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">com.google.inject.Injector</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">flex.messaging.FactoryInstance</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">flex.messaging.FlexContext</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">flex.messaging.FlexFactory</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">flex.messaging.config.ConfigMap</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">flex.messaging.services.ServiceException</span><span class="sy0">;</span>
&nbsp;
<span class="co3">/**
 * &lt;p&gt;A Flex factory that retrieves instances of Flex services from Guice&lt;/p&gt;
 * &lt;p&gt;This is based on a similar factory built for Spring by Jeff Vroom&lt;/p&gt;
 * @author Connor Garvey
 * @created May 27, 2009 1:09:49 PM
 * @version 1.0.0
 * @since 1.0.0
 */</span>
<span class="kw1">public</span> <span class="kw1">class</span> GuiceFactory <span class="kw1">implements</span> FlexFactory <span class="br0">&#123;</span>
  <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> SOURCE <span class="sy0">=</span> <span class="st0">&quot;source&quot;</span><span class="sy0">;</span>
&nbsp;
  <span class="co3">/**
   * @see flex.messaging.FlexFactory#createFactoryInstance(java.lang.String, flex.messaging.config.ConfigMap)
   */</span>
  <span class="kw1">public</span> FactoryInstance createFactoryInstance<span class="br0">&#40;</span><span class="kw1">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> id, <span class="kw1">final</span> ConfigMap properties<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">final</span> GuiceFactoryInstance instance <span class="sy0">=</span> <span class="kw1">new</span> GuiceFactoryInstance<span class="br0">&#40;</span><span class="kw1">this</span>, id, properties<span class="br0">&#41;</span><span class="sy0">;</span>
    instance.<span class="me1">setSource</span><span class="br0">&#40;</span>properties.<span class="me1">getPropertyAsString</span><span class="br0">&#40;</span>SOURCE, instance.<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">return</span> instance<span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co3">/**
   * @see flex.messaging.FlexConfigurable#initialize(java.lang.String, flex.messaging.config.ConfigMap)
   */</span>
  <span class="kw1">public</span> <span class="kw4">void</span> initialize<span class="br0">&#40;</span><span class="kw1">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> id, <span class="kw1">final</span> ConfigMap configMap<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co3">/**
   * @see flex.messaging.FlexFactory#lookup(flex.messaging.FactoryInstance)
   */</span>
  <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> lookup<span class="br0">&#40;</span><span class="kw1">final</span> FactoryInstance inst<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> inst.<span class="me1">lookup</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">static</span> <span class="kw1">class</span> GuiceFactoryInstance <span class="kw1">extends</span> FactoryInstance <span class="br0">&#123;</span>
    <span class="kw1">private</span> Map<span class="sy0">&lt;</span>String, Class<span class="sy0">&lt;?&gt;&gt;</span> classes <span class="sy0">=</span> <span class="kw1">new</span> HashMap<span class="sy0">&lt;</span>String, Class<span class="sy0">&lt;?&gt;&gt;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    GuiceFactoryInstance<span class="br0">&#40;</span><span class="kw1">final</span> GuiceFactory factory, <span class="kw1">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> id, <span class="kw1">final</span> ConfigMap properties<span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">super</span><span class="br0">&#40;</span>factory, id, properties<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    @Override
    <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Object</span></a> lookup<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">final</span> Injector injector <span class="sy0">=</span> <span class="br0">&#40;</span>Injector<span class="br0">&#41;</span>FlexContext.<span class="me1">getServletContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getAttribute</span><span class="br0">&#40;</span>
          GuiceServletContextListener.<span class="me1">KEY</span><span class="br0">&#41;</span><span class="sy0">;</span>
      injector.<span class="me1">injectMembers</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> className <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">getSource</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
      Class<span class="sy0">&lt;?&gt;</span> clazz <span class="sy0">=</span> <span class="kw1">this</span>.<span class="me1">classes</span>.<span class="me1">get</span><span class="br0">&#40;</span>className<span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="kw1">if</span> <span class="br0">&#40;</span>clazz <span class="sy0">==</span> <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">try</span> <span class="br0">&#123;</span>
          clazz <span class="sy0">=</span> <span class="kw1">Class</span>.<span class="me1">forName</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">getSource</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
          <span class="kw1">this</span>.<span class="me1">classes</span>.<span class="me1">put</span><span class="br0">&#40;</span>className, clazz<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <span class="kw1">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aclassnotfoundexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ClassNotFoundException</span></a> ex<span class="br0">&#41;</span> <span class="br0">&#123;</span>
          ServiceException throwing <span class="sy0">=</span> <span class="kw1">new</span> ServiceException<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
          throwing.<span class="me1">setMessage</span><span class="br0">&#40;</span><span class="st0">&quot;Could not find remote service class '&quot;</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">getSource</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;'&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
          throwing.<span class="me1">setRootCause</span><span class="br0">&#40;</span>ex<span class="br0">&#41;</span><span class="sy0">;</span>
          throwing.<span class="me1">setCode</span><span class="br0">&#40;</span><span class="st0">&quot;Server.Processing&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
          <span class="kw1">throw</span> throwing<span class="sy0">;</span>
        <span class="br0">&#125;</span>
      <span class="br0">&#125;</span>
      <span class="kw1">return</span> injector.<span class="me1">getInstance</span><span class="br0">&#40;</span>clazz<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
    @Override
    <span class="kw1">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> toString<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span class="kw1">return</span> <span class="st0">&quot;Guice factory &lt;id='&quot;</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;',source='&quot;</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">getSource</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span>
          <span class="st0">&quot;',scope='&quot;</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">getScope</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;'&gt;&quot;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<h3>Create a context listener</h3>
<p>The Guice servlet jar contains a ServletContextListener, but I haven&#8217;t taken the time to integrate it into this solution.  For simplicity, you can use this one, which works with the factory above.</p>
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="java" style="font-family:monospace;"><span class="kw1">package</span> <span class="co2">com.connorgarvey.guiceblazeds.servlet</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">import</span> <span class="co2">java.util.List</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletContext</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletContextEvent</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">javax.servlet.ServletContextListener</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">com.google.inject.Guice</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">com.google.inject.Injector</span><span class="sy0">;</span>
<span class="kw1">import</span> <span class="co2">com.google.inject.Module</span><span class="sy0">;</span>
&nbsp;
<span class="co3">/**
 * Prepares Guice on application startup
 * @author Connor Garvey
 * @created May 27, 2009 8:37:26 AM
 * @version 1.0.0
 * @since 1.0.0
 */</span>
<span class="kw1">public</span> <span class="kw1">abstract</span> <span class="kw1">class</span> GuiceServletContextListener <span class="kw1">implements</span> ServletContextListener <span class="br0">&#123;</span>
  <span class="co3">/**
   * The key of the servlet context attribute holding the injector
   */</span>
  <span class="kw1">public</span> <span class="kw1">static</span> <span class="kw1">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> KEY <span class="sy0">=</span> Injector.<span class="kw1">class</span>.<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
  <span class="co3">/**
   * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
   */</span>
  <span class="kw1">public</span> <span class="kw4">void</span> contextDestroyed<span class="br0">&#40;</span><span class="kw1">final</span> ServletContextEvent servletContextEvent<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    servletContextEvent.<span class="me1">getServletContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">removeAttribute</span><span class="br0">&#40;</span>KEY<span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co3">/**
   * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
   */</span>
  <span class="kw1">public</span> <span class="kw4">void</span> contextInitialized<span class="br0">&#40;</span><span class="kw1">final</span> ServletContextEvent servletContextEvent<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    servletContextEvent.<span class="me1">getServletContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">setAttribute</span><span class="br0">&#40;</span>KEY,
        <span class="kw1">this</span>.<span class="me1">getInjector</span><span class="br0">&#40;</span>servletContextEvent.<span class="me1">getServletContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">private</span> Injector getInjector<span class="br0">&#40;</span><span class="kw1">final</span> ServletContext servletContext<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> Guice.<span class="me1">createInjector</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">getModules</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co3">/**
   * Gets the modules used by the application
   * @return the modules
   */</span>
  <span class="kw1">protected</span> <span class="kw1">abstract</span> List<span class="sy0">&lt;?</span> <span class="kw1">extends</span> Module<span class="sy0">&gt;</span> getModules<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<h3>Extend GuiceContextListener</h3>
<p>Create a concret version of the class above.  It should return all modules needed for the application.</p>
<h3>Modify web.xml</h3>
<p>Now, prepare web.xml with the normal BlazeDS settings plus some extras for Guice.</p>
<div id="wpshdo_21" class="wp-synhighlighter-outer"><div id="wpshdt_21" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_21"></a><a id="wpshat_21" class="wp-synhighlighter-title" href="#codesyntax_21"  onClick="javascript:wpsh_toggleBlock(21)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_21" onClick="javascript:wpsh_code(21)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_21" onClick="javascript:wpsh_print(21)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_21" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;UTF-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc0">&lt;!DOCTYPE web-app PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot; &quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;&gt;</span>
<span class="sc3"><span class="re1">&lt;web-app<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;display-name<span class="re2">&gt;</span></span></span>Guice BlazeDS<span class="sc3"><span class="re1">&lt;/display-name<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>Guice BlazeDS<span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;listener<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;listener-class<span class="re2">&gt;</span></span></span>flex.messaging.HttpFlexSession<span class="sc3"><span class="re1">&lt;/listener-class<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/listener<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;listener<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;listener-class<span class="re2">&gt;</span></span></span>com.connorgarvey.guiceblazeds.servlet.MyCustomGuiceServletContextListener<span class="sc3"><span class="re1">&lt;/listener-class<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/listener<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;servlet<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>MessageBrokerServlet<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;display-name<span class="re2">&gt;</span></span></span>MessageBrokerServlet<span class="sc3"><span class="re1">&lt;/display-name<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;servlet-class<span class="re2">&gt;</span></span></span>flex.messaging.MessageBrokerServlet<span class="sc3"><span class="re1">&lt;/servlet-class<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;init-param<span class="re2">&gt;</span></span></span>
      <span class="sc3"><span class="re1">&lt;param-name<span class="re2">&gt;</span></span></span>services.configuration.file<span class="sc3"><span class="re1">&lt;/param-name<span class="re2">&gt;</span></span></span>
      <span class="sc3"><span class="re1">&lt;param-value<span class="re2">&gt;</span></span></span>/WEB-INF/config/flex/services-config.xml<span class="sc3"><span class="re1">&lt;/param-value<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/init-param<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;load-on-startup<span class="re2">&gt;</span></span></span>1<span class="sc3"><span class="re1">&lt;/load-on-startup<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/servlet<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;servlet-mapping<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;servlet-name<span class="re2">&gt;</span></span></span>MessageBrokerServlet<span class="sc3"><span class="re1">&lt;/servlet-name<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;url-pattern<span class="re2">&gt;</span></span></span>/messagebroker/*<span class="sc3"><span class="re1">&lt;/url-pattern<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/servlet-mapping<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;welcome-file-list<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;welcome-file<span class="re2">&gt;</span></span></span>index.html<span class="sc3"><span class="re1">&lt;/welcome-file<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;welcome-file<span class="re2">&gt;</span></span></span>index.htm<span class="sc3"><span class="re1">&lt;/welcome-file<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;/welcome-file-list<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/web-app<span class="re2">&gt;</span></span></span></pre></div></div>
<p>This way, BlazeDS will start at server startup, followed by Guice.</p>
<h3>Modify BlazeDS config</h3>
<p>Open services-config.xml and add this at the top of the file.  Point it to the Guice factory.</p>
<div id="wpshdo_22" class="wp-synhighlighter-outer"><div id="wpshdt_22" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_22"></a><a id="wpshat_22" class="wp-synhighlighter-title" href="#codesyntax_22"  onClick="javascript:wpsh_toggleBlock(22)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_22" onClick="javascript:wpsh_code(22)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_22" onClick="javascript:wpsh_print(22)" title="Print code"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.connorgarvey.com/blog/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_22" class="wp-synhighlighter-inner" style="display: block;"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;UTF-8&quot;</span><span class="re2">?&gt;</span></span>
<span class="sc3"><span class="re1">&lt;services-config<span class="re2">&gt;</span></span></span>
  <span class="sc3"><span class="re1">&lt;factories<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;factory</span> <span class="re0">id</span>=<span class="st0">&quot;guice&quot;</span> <span class="re0">class</span>=<span class="st0">&quot;com.connorgarvey.guiceblazeds.servlet.GuiceFactory&quot;</span> <span class="re2">/&gt;</span></span>
  <span class="sc3"><span class="re1">&lt;/factories<span class="re2">&gt;</span></span></span></pre></div></div>
<h3>Set the factory of all remoting destinations</h3>
<p>In remoting-config.xml, be sure to set the factory of all destinations to the name specified in services-config.xml above.</p>
<pre>&lt;destination id="SomeService"&gt;
  &lt;properties&gt;
    &lt;factory&gt;guice&lt;/factory&gt;
    &lt;source&gt;com.connorgarvey.guiceblazeds.service.flex.SomeFlexService&lt;/source&gt;
  &lt;/properties&gt;
&lt;/destination&gt;</pre>
<h3>How this all works</h3>
<ol>
<li>When the server starts, BlazeDS and Guice are initialized by the servlet listeners.</li>
<li>When BlazeDS receives a request, to a service, it&#8217;ll see that it&#8217;s supposed to retrieve it from the &#8220;guice&#8221; factory.</li>
<li>The Guice factory will return an instance of the class specified in the source of the service configuration from Guice to BlazeDS for use in completing the request.</li>
</ol>
<img src="http://feeds.feedburner.com/~r/connorgarvey/devnotes/~4/cYTR7KmEBxE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.connorgarvey.com/blog/?feed=rss2&amp;p=132</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.connorgarvey.com/blog/?p=132</feedburner:origLink></item>
	</channel>
</rss>

