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

<channel>
	<title>Source Allies Blog &#187; Development</title>
	<atom:link href="http://blogs.sourceallies.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Mon, 26 Jul 2010 15:01:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Google Analytics Data Export API &#8211; Part 1</title>
		<link>http://blogs.sourceallies.com/2010/03/google-analytics-data-export-api-part-1/</link>
		<comments>http://blogs.sourceallies.com/2010/03/google-analytics-data-export-api-part-1/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 13:10:45 +0000</pubDate>
		<dc:creator>Octavian Covalschi</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[Google API]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1461</guid>
		<description><![CDATA[When Google exposed its Data Export API, it endeared itself closer to developers and to customers of Google Analytics. Data Export API allows us to develop client applications to retrieve data from existing analytics profiles of authorized users.
How does it work?
The Data Export API provides read-only access to all available analytics data. Any data that [...]]]></description>
			<content:encoded><![CDATA[<p>When Google exposed its Data Export API, it endeared itself closer to developers and to customers of Google Analytics. Data Export API allows us to develop client applications to retrieve data from existing analytics profiles of authorized users.</p>
<p><strong>How does it work?</strong><br />
The Data Export API provides read-only access to all available analytics data. Any data that is displayed in the analytics web interface can be accessed through this API. Nice isn&#8217;t it? You can get all your analytics data, for all your monitored websites and use it as you please.</p>
<p><span id="more-1461"></span></p>
<p><strong>Getting started</strong><br />
To get started basically you need very little &#8212; a valid Google Analytics account and a client that would do all the low level work(sending HTTP requests with corresponding headers) or you could write your own small client that fits your needs. For example you could write shell scripts that use cURL under the hood. Anyway, it doesn&#8217;t matter, the important thing here is to find a good way to communicate with the Google Analytics servers.</p>
<p>At this point, Google offers 2 solutions: a Java and a JavaScript client. Both are easy to use, but may not fit your needs. That&#8217;s why there are also other similar clients in different languages like PHP, Perl,  and Ruby.</p>
<p>The entire process of getting Google Analytics data and doing something useful with it can be split into 3 steps:</p>
<ul>
<li>Authorization</li>
<li>Data feed</li>
<li>Parse XML feed and perform your computations.</li>
</ul>
<p>In Part 2 I&#8217;ll describe several ways of authorizations and when you would use each. But before that I&#8217;d like to mention some limitations. Basically Google has several quota policies, which currently are:</p>
<ul>
<li>10,000 requests per 24 hours (this limit applies to a single web resource)</li>
<li>10 requests in any given 1-second period (this limit applies across all queries an application makes to the API)</li>
<li>4 pending requests at any given time. Be advised that this limit is new and is subject to change</li>
<li>A query is also limited to pagination limits of 10,000 entries per feed, with a default response of 1,000 entries</li>
</ul>
<p>Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/google-analytics-data-export-api-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Musings of a SpringOne 2009 Attendee – Day 4</title>
		<link>http://blogs.sourceallies.com/2010/03/musings-of-a-springone-2009-attendee-%e2%80%93-day-4/</link>
		<comments>http://blogs.sourceallies.com/2010/03/musings-of-a-springone-2009-attendee-%e2%80%93-day-4/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 16:00:53 +0000</pubDate>
		<dc:creator>Sudhakar Ramasamy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Conference]]></category>
		<category><![CDATA[SpringOne]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=535</guid>
		<description><![CDATA[This is the last and final part on my SpringOne 2009 experience. It&#8217;s late catching up to the 3 earlier posts but it&#8217;s here now. This post summarizes the sessions I attended from day 4 and wraps up with a summary of my take aways. If you want to catch up here are the three [...]]]></description>
			<content:encoded><![CDATA[<p>This is the last and final part on my SpringOne 2009 experience. It&#8217;s late catching up to the 3 earlier posts but it&#8217;s here now. This post summarizes the sessions I attended from day 4 and wraps up with a summary of my take aways. If you want to catch up here are the three earlier posts:</p>
<ol>
<li><a href="http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-1">Musings of a SpringOne 2009 Attendee Day 1</a></li>
<li><a href="http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-2">Musings of a SpringOne 2009 Attendee Day 2</a></li>
<li><a href="http://blogs.sourceallies.com/2009/10/musings-of-a-springone-2009-attendee-day-3">Musings of a SpringOne 2009 Attendee Day 3</a></li>
</ol>
<p>Read on for day 4.</p>
<p><span id="more-535"></span></p>
<h2><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15408">OSGi and Groovy Jump Start</a></h2>
<p>The presentation started with a <a href="http://www.youtube.com/watch?v=eTRM5n3khw8">5 minute youtube video</a> preview which I thought was pretty innovative.</p>
<p>The first half was around what is OSGi, some of which I&#8217;ve had experience with. I did pick up on a few key concepts that I didn&#8217;t know i wasn&#8217;t communicating earlier in my own presentations on OSGi.</p>
<p>We covered how a groovy POGO has groovy types in the public API. So if your groovy bundle has the groovylib in it&#8217;s classpath then it has to export the groovy package. Also when using Groovy in OSGi, if possible use POJOs for your applications type system since POGOs force you to expose couple groovy types in the bundle&#8217;s API.</p>
<p>The examples were done using <a href="http://www.gradle.org/">gradle</a> and this was my first live example of using gradle. This is a key benefit of conferences &#8212; being exposed to technologies you may otherwise not have the time to take a peek at.</p>
<p>I liked how the presenter took the path of not making things look complex but rather simplifying the concepts. He did this with concepts such as the  OSGi bundle lifecycle.</p>
<p>Some of the tools covered in this session were:</p>
<ul>
<li>bnd to create a groovy manifest</li>
<li>osgi bundle for gradle uses bnd</li>
<li>junit4osgi</li>
</ul>
<h2><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=16462">Expand your business with Groovy &#8211; Case Study</a></h2>
<p>This session was a case study on how a monolithic application was extended to provide customizations using groovy. A major customer had approached  HypericQ wanting a custom dashboard.</p>
<p>HypericQ developed a architecture around attach point&#8217;s which are places within the UI where plugins can connect to.</p>
<p>Custom UI was provided by gsp templates for customer specific dashboards.</p>
<p>Groovy templates was used as the templating framework that allowed clients to build their own using GSP  pages.</p>
<p>A Web Services API was provided as part of the core platform that enabled several benefits:<br />
- hqapi command line interaction<br />
- allows integration testing of the application<br />
- embedding a groovy console inside the application<br />
- console gives you a HQL way to query the domain and not worry about the end customers database environment</p>
<p>The talk was an interesting exercise in how a dynamic plugin architecture could be implemented. <a href="http://docs.codehaus.org/display/GROOVY/Groovy+Categories">Groovy categories</a> was heavily used to enable this.</p>
<h2><a href="http://www.springone2gx.com/conference/new_orleans/2009/10/session?id=15779">Re-factoring a Spring Application for SOA using Spring technologies in 40 min</a></h2>
<p>The current focus of SOA is really software architecture. SOA is neither static nor predictable. The architectural pattern of any application must be reflective of the business. SOA is preparing the stuff that is critical to your business to stand the test of time. </p>
<p>Architecture involves the courage to be wrong while minimizing the impact of being wrong. It is about making educated decisions between speed and perfection. The longer it takes to deliver the software the higher the client&#8217;s expectations of that software is.</p>
<p>The foundation of SOA is highly cohesive, loosely coupled modules. One must practice</p>
<ol>
<li>strategy first programming</li>
<li>process life-cycle dynamics</li>
<li>process collaboration</li>
<li>loose coupling promotes a network based architecture over a hierarchical one &#8211; composition over inheritance</li>
</ol>
<p>SOA and OSGi enable embracing change by making versioning a runtime concept.</p>
<p>There is an argument to be made for deploying a process as a whole in BPEL. But what about flexible orchestration via simple messaging. We were given the American football analogy. Each play is scripted, but between plays we need to adjust strategy to meet the overall goal. </p>
<p>Modern event driven architecture was mentioned as a composite process linked together in a linear fashion through the use of rule based pathing.</p>
<p>The second half of this talk was the Spring booking demo from Spring Web Flow project. There are 3 OSGi bundles:</p>
<ol>
<li>war bundle</li>
<li>service bundle</li>
<li>datasource bundle</li>
</ol>
<p>The demo was using STS and it&#8217;s DM Server tooling. The entire development workflow was very seamless with SpringSource having done an awesome job of the tooling.</p>
<p>We saw an example of a transparent failover. So there is a need to take down the primary database for maintenance work without taking the application down. A backup datasource bundle is started and the primary datasource bundle is stopped. The datasource bundle is a simple datasource definition and an osgi service with ranking. Because of a higher rank the backup datasource bundle takes precendence and the primary datasource bundle can be safely stopped. Bundles can also take advantage of filtering besides ranking. </p>
<p>Spring Integration builds on pipes and filters. You can expose OSGi services as pipes. It provides the ability to change the behavior of a modularized application by changing how the modules are hooked up in the XML as opposed to modifying source code or in cases where the source code is not available.</p>
<h2>Conference Wrap Up</h2>
<p>Here are my key take aways:</p>
<ul>
<li>It is time for Grails</li>
<li>Need to investigate Spring Faces</li>
<li><a href="http://www.owasp.org/index.php/Category:OWASP_WebGoat_Project">Webgoat</a></li>
<li>Spring Web Flow</li>
<li>Spring Integration</li>
<li>Spring DM Server</li>
</ul>
<p>Overall it was a very productive conference and I would recommend it for anyone looking to attend it next year.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/musings-of-a-springone-2009-attendee-%e2%80%93-day-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rendering Global t:messages After Redirect</title>
		<link>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/</link>
		<comments>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:24:46 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Facelets]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[JSF messages]]></category>
		<category><![CDATA[tomahawk]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1219</guid>
		<description><![CDATA[A common problem when working with JSF is getting global info messages  via &#60;t:messages globalOnly="true"&#62; or &#60;f:messages globalOnly="true"&#62; to display messages set in the previous request when you have a &#60;redirect/&#62; in your faces-config for a particular page You will not see your &#60;t:messages&#62; that are set on the previous page.
The Problem
For instance, say you [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem when working with JSF is getting global info messages  via <code>&lt;t:messages globalOnly="true"&gt;</code> or <code>&lt;f:messages globalOnly="true"&gt;</code> to display messages set in the previous request when you have a <code>&lt;redirect/&gt;</code> in your faces-config for a particular page You will not see your <code>&lt;t:messages&gt;</code> that are set on the previous page.</p>
<h4><span style="text-decoration: underline"><strong>The Problem</strong></span></h4>
<p>For instance, say you have two pages &#8211; page1.xhtml and page2.xhtml. In your faces-config.xml, you will have 2 entries.</p>
<p><span id="more-1219"></span></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page1.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>page2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/from-outcome<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/pages/page2.xhtml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/to-view-id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;redirect</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/navigation-case<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Let&#8217;s say page1Bean is called in page1 and you want it to display messages in page2.  So your bean would have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> page1Bean <span style="color: #009900;">&#123;</span>
	...
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> page1Action<span style="color: #009900;">&#123;</span>
		...
		<span style="color: #008000; font-style: italic; font-weight: bold;">/** Add message */</span>
		FacesMessage facesMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FacesMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #006633;">FacesContext</span>.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, facesMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		...
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;page2&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Your page2.xhtml has like the following somewhere in it.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;t:messages globalOnly=&quot;true&quot;&gt;</pre></div></div>

<p>One would expect that this would display the message on the next page, however these messages are request scoped and so are not available in this second request and you do not see your message unless you remove the <code>&lt;redirect/&gt;</code> on page2.</p>
<p>Fortunately, there is a nifty and quick solution.  Basically, it is a phase listener that saves the messages from the previous request and then restores them just before the RENDER_RESPONSE phase of the second request.</p>
<h4><span style="text-decoration: underline"><strong>The Solution</strong></span></h4>
<p>To solve the problem, save the following class into some package where your faces-config will be able to access it</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.myproject.web.jsf.phaselistener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.application.FacesMessage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.context.FacesContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseId</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.event.PhaseListener</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Enables messages to be rendered on different pages from which they were set.
 * To produce this behaviour, this class acts as a &lt;code&gt;PhaseListener&lt;/code&gt;.
 *
 * This is performed by moving the FacesMessage objects:
 *
	&lt;li&gt;After each phase where messages may be added, this moves the messages from
 * the page-scoped FacesContext to the session-scoped session map.
 *&lt;/li&gt;
	&lt;li&gt;Before messages are rendered, this moves the messages from the session-scoped
 * session map back to the page-scoped FacesContext.
 *&lt;/li&gt;
 * Only messages that are not associated with a particular component are ever
 * moved. These are the only messages that can be rendered on a page that is different
 * from where they originated.
 *
 * To enable this behaviour, add a &lt;code&gt;lifecycle&lt;/code&gt; block to your
 * faces-config.xml file. That block should contain a single &lt;code&gt;phase-listener&lt;/code&gt;
 * block containing the fully-qualified classname of this file.
 *
 * EDIT: This code was minimally modified by Max Kuipers to address some of the Java 1.6
 * compiler warnings.  All code was originally written by Jesse Wilson.
 *
 * @author &lt;a href=&quot;mailto:jesse@odel.on.ca&quot;&gt;Jesse Wilson&lt;/a&gt;
 * @author &lt;a href=&quot;mailto:mkuipers@sourceallies.com&quot;&gt;Max Kuipers&lt;/a&gt;
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MessageHandler <span style="color: #000000; font-weight: bold;">implements</span> PhaseListener <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * a name to save messages in the session under
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> sessionToken <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MULTI_PAGE_MESSAGES_SUPPORT&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Return the identifier of the request processing phase during which this
	 * listener is interested in processing PhaseEvent events.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PhaseId getPhaseId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> PhaseId.<span style="color: #006633;">ANY_PHASE</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase of the
	 * request processing lifecycle is about to begin.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> beforePhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">RENDER_RESPONSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			restoreMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Handle a notification that the processing for a particular phase has just
	 * been completed.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPhase<span style="color: #009900;">&#40;</span>PhaseEvent event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">APPLY_REQUEST_VALUES</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">PROCESS_VALIDATIONS</span> <span style="color: #339933;">||</span>
				event.<span style="color: #006633;">getPhaseId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> PhaseId.<span style="color: #006633;">INVOKE_APPLICATION</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
			FacesContext facesContext <span style="color: #339933;">=</span> event.<span style="color: #006633;">getFacesContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			saveMessages<span style="color: #009900;">&#40;</span>facesContext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the faces context and store them to the user's session.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> saveMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the context</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getMessages</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			messages.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			i.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// store them in the session</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// if there already are messages</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> existingMessages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span> sessionMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>existingMessages <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			existingMessages.<span style="color: #006633;">addAll</span><span style="color: #009900;">&#40;</span>messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			sessionMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>sessionToken, messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// if these are the first messages</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remove the messages that are not associated with any particular component
	 * from the user's session and add them to the faces context.
	 *
	 * @return the number of removed messages.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> restoreMessages<span style="color: #009900;">&#40;</span>FacesContext facesContext<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// remove messages from the session</span>
		<span style="color: #003399;">Map</span> sessionMap <span style="color: #339933;">=</span> facesContext.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSessionMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #003399;">List</span> messages <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span><span style="color: #009900;">&#41;</span>sessionMap.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>sessionToken<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// store them in the context</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>messages <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">int</span> restoredCount <span style="color: #339933;">=</span> messages.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Iterator</span> i <span style="color: #339933;">=</span> messages.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			facesContext.<span style="color: #006633;">addMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span>, i.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> restoredCount<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then add something like the following in your faces-config.xml. Be sure to replace the example package location with your own.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.myproject.web.jsf.phaselistener.MessageHandler<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase-listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lifecycle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The forum post that documented this solution is <a href="http://forums.sun.com/thread.jspa?forumID=427&amp;threadID=657727">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/rendering-global-tmessages-after-redirect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Developing a multithreaded test harness</title>
		<link>http://blogs.sourceallies.com/2010/03/developing-a-multithreaded-test-harness/</link>
		<comments>http://blogs.sourceallies.com/2010/03/developing-a-multithreaded-test-harness/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 17:19:19 +0000</pubDate>
		<dc:creator>Tim Bierbaum</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1263</guid>
		<description><![CDATA[You can&#8217;t ignore the fact that web servers are multithreaded.  We can hide as much as we want, but sooner or later you&#8217;ll find yourself in the situation where your application works fine during development and testing; but once it hits production you start hearing about &#8220;funny&#8221; things happening.  While there are plenty [...]]]></description>
			<content:encoded><![CDATA[<p>You can&#8217;t ignore the fact that web servers are multithreaded.  We can hide as much as we want, but sooner or later you&#8217;ll find yourself in the situation where your application works fine during development and testing; but once it hits production you start hearing about &#8220;funny&#8221; things happening.  While there are plenty of tools that can be used to simulate multiple users, they aren&#8217;t always the easiest to run locally and they seem to take to much time to modify while hot on the trail of a multithreading bug.  Here I&#8217;ll discuss the approach I took when I was recently faced with this situation.<br />
<span id="more-1263"></span></p>
<p>It was the end of the quarter and teachers were complaining that the test scoring application was returning incorrect results.  The application had passed all development tests, passed through qa with flying colors, and has been deployed without any issue for a little while now.  To make matters worse, all of the teachers were under lots of pressure to get all of their tests scored before the end of the quarter.  When I heard that, a little light went off in my head.  With a little digging, we were able to determine that the system had been serving more concurrent users than planned for.</p>
<p>The first thing I try to do when faced with a bug report is find a way to consistently reproduce it.  This particular issue is a little harder since it only seems to be happening with multiple users.  Luckily, Java makes writing multithreaded code very easy.  Since most of the complaints seem to be related to test scoring, I start focusing my attention on that.  The plan of attack is to:</p>
<ol>
<li>Get a list of test ID&#8217;s.</li>
<li>Score each test and save it&#8217;s result.</li>
<li>Create a separate thread for each ID.</li>
<li>Each thread will score the test again, and compare it to the score from step 2.  If it doesn&#8217;t match, increment a failure count.</li>
<li>Perform step 4 a couple of times.</li>
<li>Once all of the threads exit, print out the number of failures.</li>
</ol>
<p>The plan of attack would then be to run this until either a failure occurs or until I feel fairly certain that there isn&#8217;t a problem with the scoring service.</p>
<p>Here is the the initial version of the test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.multithread</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScoringServiceTest <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> runCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> scoreFailureCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">List</span> skipFailureList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ScoringService scoringService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScoringService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> incrementRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    runCount<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> incrementScoreFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    scoreFailureCount<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> getRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> runCount<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> getScoreFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> scoreFailureCount<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> compareScores<span style="color: #009900;">&#40;</span>ScoringResult a, ScoringResult b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> a.<span style="color: #006633;">getNumerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>b.<span style="color: #006633;">getNumerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
    a.<span style="color: #006633;">getDenominator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>b.<span style="color: #006633;">getDenominator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> runTest<span style="color: #009900;">&#40;</span><span style="color: #003399;">List</span> testIdList,  <span style="color: #000066; font-weight: bold;">int</span> iterationCount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">long</span> start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">List</span> threadList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> testId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> testIdList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      testId <span style="color: #339933;">=</span> testIdList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      ScoringResult score <span style="color: #339933;">=</span> scoringService.<span style="color: #006633;">scoreTest</span><span style="color: #009900;">&#40;</span>testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">Runnable</span> testRunner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TestRunner<span style="color: #009900;">&#40;</span>iterationCount, testId, score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">Thread</span> thread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>testRunner, testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      threadList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>thread<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> exc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error with ID: &quot;</span> <span style="color: #339933;">+</span> testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exc.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Starting threads&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span> t <span style="color: #339933;">:</span> threadList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      t.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Waiting for threads to finish&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span> t <span style="color: #339933;">:</span> threadList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        t.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> exc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Interrupted while waiting for &quot;</span> <span style="color: #339933;">+</span> t.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Number of IDs: &quot;</span> <span style="color: #339933;">+</span> testIdList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Iterations: &quot;</span> <span style="color: #339933;">+</span> iterationCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Total: &quot;</span> <span style="color: #339933;">+</span> getRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Elapsed Time: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;# Score Failures: &quot;</span> <span style="color: #339933;">+</span> getScoreFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> TestRunner <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> testId<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> ScoringResult expectedScore<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> iterations<span style="color: #339933;">;</span>
&nbsp;
    TestRunner<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> iters, <span style="color: #003399;">String</span> tId, ScoringResult score<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      testId <span style="color: #339933;">=</span> tId<span style="color: #339933;">;</span>
      expectedScore <span style="color: #339933;">=</span> score<span style="color: #339933;">;</span>
      iterations <span style="color: #339933;">=</span> iters<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> iterations<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ScoringServiceTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">incrementRunCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ScoringResult scoringResult <span style="color: #339933;">=</span> ScoringServiceTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">scoringService</span>.<span style="color: #006633;">scoreTest</span><span style="color: #009900;">&#40;</span>testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>compareScores<span style="color: #009900;">&#40;</span>scoringResult, expectedScore<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          ScoringServiceTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">incrementScoreFailureCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ScoringServiceTest test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScoringServiceTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">List</span> idList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Math1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Science1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Math2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Science2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;English1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    test.<span style="color: #006633;">runTest</span><span style="color: #009900;">&#40;</span>idList, <span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While this did the job at first, it&#8217;s usefulness was very short lived.  Before long, I was wanting to change the test logic and didn&#8217;t want the threading logic getting in the way.  The approach I took was to extract the threading code out into a test harness and then create an interface for the testing logic by using the Command pattern.  This resulted in the following code, which I have been able to reuse across multiple test scenarios.</p>
<p>The test harness class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.multithread</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MultithreadedTest <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> runCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> failureCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> incrementRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    runCount<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> incrementFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    failureCount<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> runCount<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span>  <span style="color: #000066; font-weight: bold;">int</span> getFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> failureCount<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> runTest<span style="color: #009900;">&#40;</span><span style="color: #003399;">Collection</span> commands,  <span style="color: #000066; font-weight: bold;">int</span> iterationCount<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">long</span> start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">List</span> threadList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Loading expected results&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>TestCommand command <span style="color: #339933;">:</span> commands<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">Object</span> result <span style="color: #339933;">=</span> command.<span style="color: #006633;">doTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      TestRunner testRunner <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TestRunner<span style="color: #009900;">&#40;</span>iterationCount, command, result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">Thread</span> thread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Thread</span><span style="color: #009900;">&#40;</span>testRunner<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      threadList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>thread<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Starting threads&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span> t <span style="color: #339933;">:</span> threadList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      t.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Waiting for threads to finish&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Thread</span> t <span style="color: #339933;">:</span> threadList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        t.<span style="color: #006633;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> exc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Interrupted while waiting for &quot;</span> <span style="color: #339933;">+</span> t.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Number of Tests: &quot;</span> <span style="color: #339933;">+</span> commands.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Iterations: &quot;</span> <span style="color: #339933;">+</span> iterationCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Total: &quot;</span> <span style="color: #339933;">+</span> getRunCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Elapsed Time: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;# Failures: &quot;</span> <span style="color: #339933;">+</span> getFailureCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> TestRunner <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> iterations<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> TestCommand command<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Object</span> expectedResult<span style="color: #339933;">;</span>
&nbsp;
    TestRunner<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> iters, TestCommand command, <span style="color: #003399;">Object</span> expectedResult<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">command</span> <span style="color: #339933;">=</span> command<span style="color: #339933;">;</span>
      iterations <span style="color: #339933;">=</span> iters<span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">expectedResult</span> <span style="color: #339933;">=</span> expectedResult<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> iterations<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MultithreadedTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">incrementRunCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003399;">Object</span> result <span style="color: #339933;">=</span> command.<span style="color: #006633;">doTest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>command.<span style="color: #006633;">compareResults</span><span style="color: #009900;">&#40;</span>result, expectedResult<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            MultithreadedTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">incrementFailureCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> exc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          exc.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          MultithreadedTest.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">incrementFailureCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The test command:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.multithread</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> TestCommand <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> doTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> compareResults<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> a, <span style="color: #003399;">Object</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and finally, the test command for my scenario:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.multithread</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ScoringServiceTestCommand <span style="color: #000000; font-weight: bold;">implements</span> TestCommand <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> testId<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ScoringService scoringService<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ScoringServiceTestCommand<span style="color: #009900;">&#40;</span>ScoringService service, <span style="color: #003399;">String</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    scoringService <span style="color: #339933;">=</span> service<span style="color: #339933;">;</span>
    testId <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> doTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> scoringService.<span style="color: #006633;">scoreTest</span><span style="color: #009900;">&#40;</span>testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> compareResults<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> expectedResult, <span style="color: #003399;">Object</span> result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ScoringResult sr1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ScoringResult<span style="color: #009900;">&#41;</span>expectedResult<span style="color: #339933;">;</span>
    ScoringResult sr2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ScoringResult<span style="color: #009900;">&#41;</span>result<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">boolean</span> match <span style="color: #339933;">=</span> sr1.<span style="color: #006633;">getNumerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>sr2.<span style="color: #006633;">getNumerator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
        sr1.<span style="color: #006633;">getDenominator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>sr2.<span style="color: #006633;">getDenominator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> match<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">List</span> idList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Math1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Science1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Math2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Science2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    idList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;English1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    ScoringService scoringService <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ScoringService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">List</span> commandList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> idList.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      commandList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ScoringServiceTestCommand<span style="color: #009900;">&#40;</span>scoringService, idList.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    MultithreadedTest mtest <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MultithreadedTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    mtest.<span style="color: #006633;">runTest</span><span style="color: #009900;">&#40;</span>commandList, <span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/developing-a-multithreaded-test-harness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Subversion Branching and Merging</title>
		<link>http://blogs.sourceallies.com/2010/03/simple-subversion-branching-and-merging/</link>
		<comments>http://blogs.sourceallies.com/2010/03/simple-subversion-branching-and-merging/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 16:00:42 +0000</pubDate>
		<dc:creator>Zach  Cox</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1064</guid>
		<description><![CDATA[Branching and merging in Subversion is a great way to work on large new features without disrupting mainline development on trunk.  However, it has a reputation for being so difficult that many developers never take advantage of it.  In this post I&#8217;ll show just how easy it really is thanks to some newer features in [...]]]></description>
			<content:encoded><![CDATA[<p>Branching and merging in Subversion is a great way to work on large new features without disrupting mainline development on trunk.  However, it has a reputation for being so difficult that many developers never take advantage of it.  In this post I&#8217;ll show just how easy it really is thanks to some newer features in Subversion and Subclipse (a Subversion plug-in for Eclipse).<br />
<span id="more-1064"></span><br />
You should be using <a href="http://www.eclipse.org/downloads/">Eclipse 3.5</a>, <a href="http://subclipse.tigris.org/servlets/ProjectProcess?pageID=p4wYuA">Subclipse 1.6.x</a> and <a href="http://subversion.apache.org/packages.html">Subversion 1.6.x</a>.  Instructions on this page were inspired by the <a href="http://svnbook.red-bean.com/nightly/en/svn.branchmerge.html">Subversion Red Book</a>.</p>
<h3>Overview</h3>
<p>The discussion below assumes that you have a Subversion repository that uses the standard trunk/branches/tags structure.  In other words, a repository at http://site.com/project with trunk, branches, and tags subdirectories.</p>
<p>I&#8217;ll also assume that you have already checked out a local working copy of trunk, and that you&#8217;ve  already created an Eclipse project using this working copy.</p>
<p>When you want to work on a new branch you&#8217;ll follow these general steps using Subclipse, which are described in more detail below:</p>
<ul>
<li>Create a new branch and switch to it</li>
<li>Develop in your branch, periodically merging the latest changes from trunk into your branch</li>
<li>When your development is complete, merge changes from your branch back into trunk</li>
<li>Delete your branch</li>
</ul>
<h3>Switch</h3>
<p>Your local working copy reflects some directory in the remote Subversion repository.  If you initially checkout trunk, then your working copy reflects trunk.  If you initially checkout a branch, it reflects that branch.  Using the svn switch command you can have Subversion modify your working copy to reflect another directory in the repository.  Switching may not sound that exciting but it&#8217;s key to branching and merging, as you&#8217;ll soon see.</p>
<p>First off, ensure that your working copy has no local changes.  Either commit your changes or revert them.</p>
<p>Next, right-click your project in Eclipse&#8217;s Project Explorer, select the Team submenu, and click the Switch to another Branch/Tag/Revision&#8230; item.  All of Subclipse&#8217;s actions are in this Team menu, so from now on I&#8217;ll refer to specific actions like Team &gt; Switch to another Branch/Tag/Revision.</p>
<p>In the To URL field, you specify the URL of the repository directory you want to switch your local working copy to.  If your working copy initially reflects http://site.com/project/trunk, you could specify a branch as http://site.com/project/branches/somebranch.  Generally you&#8217;ll want to leave all of the other options in this dialog box as they are.</p>
<h3>Create a new Branch</h3>
<p>Creating a new branch is almost trivial.  First, switch your local copy to trunk (if it&#8217;s not already trunk).  And again, make sure you have no local changes.</p>
<p>Next select Team &gt; Branch/Tag&#8230; and specify http://site.com/project/branches/yourbranch in the Copy to URL field.  This will create a branch named yourbranch.</p>
<p>Click Next and leave HEAD revision selected.  Click Next again, enter a commit message like &#8220;Created the yourbranch branch&#8221;, check the &#8220;Switch working copy to new branch/tag&#8221; checkbox, and click Finish.</p>
<p>That&#8217;s it!  You&#8217;ve created your new branch in the repository and switched your working copy to it.  Now you can implement your major new features in your branch and commit them to subversion without affecting trunk.</p>
<h3>Merge Trunk into a Branch</h3>
<p>While you&#8217;re working on your branch, you should periodically merge the latest version of trunk into your branch to make sure you&#8217;re not straying too far away.  Eventually, you&#8217;ll also need to do this as the last step before merging your branch back into trunk, to ensure you won&#8217;t cause any conflicts with trunk.</p>
<p>First you&#8217;ll need to switch your working copy to your branch, if necessary.  Subversion will merge the changes from trunk into your working copy, and then you&#8217;ll commit your working copy to your branch to fully integrate the changes from trunk.  So it&#8217;s vital that you start with your working copy as a mirror of your branch.  And again, ensure you have no local changes.</p>
<p>Click Team &gt; Merge&#8230; and select the &#8220;Merge a range of revisions&#8221; option.  Make sure &#8220;Perform pre-merge best practices&#8221; is checked and click Next.  On the next page, if it asks you to perform any actions, like updating your working copy, perform them and click Next.</p>
<p>On the next page, make sure you&#8217;re merging from trunk and getting all eligible revisions, and click Next.  Leave all of the defaults for conflict handling and click Finish.</p>
<p>After the merge, the results screen will show you what happened.  If you got any conflicts just resolve them as usual.  Then commit any changes to your branch, and you&#8217;re done!</p>
<h3>Merge a Branch into Trunk</h3>
<p>When you&#8217;ve finished development in your branch, you need to merge your branch changes back into trunk, so everyone else can share in the glory of your awesome new feature.  This is usually where developers turn pale and run away, but fear not; Subclipse will guide you through.</p>
<p>Make sure you only perform this merge when you are completely finished with your branch!  After merging your branch into trunk, you will no longer be able to use your branch.  In fact, you should delete your branch after merging it into trunk (see next section).</p>
<p>First, merge trunk into your branch and commit any changes to your branch, as described in the previous section.  We need to make sure your branch is consistent with trunk before merging it back.</p>
<p>Next, switch your working copy to trunk.  Remember that Subversion merges changes into your working copy first, and then you commit them back to the repository.  So Subversion will apply your branch changes to your working copy of trunk, and then you commit those changes to trunk.</p>
<p>Click Team &gt; Merge&#8230; and select the &#8220;Reintegrate a branch&#8221; option.  Make sure &#8220;Perform pre-merge best practices&#8221; is checked and click Next.  On the next page, if it asks you to perform any actions, like updating your working copy, perform them and click Next.</p>
<p>On the next page, make sure you&#8217;re merging from your branch and getting all eligible revisions, and click Next.  Leave all of the defaults for conflict handling and click Finish.</p>
<p>After the merge, the results screen will show you what happened.  If you got any conflicts just resolve them as usual.  Then commit any changes to trunk, and you&#8217;re done!</p>
<h3>Delete a Branch</h3>
<p>As previously mentioned, after merging a branch into trunk, the branch can no longer be used.  Subversion keeps track of merges using the svn:mergeinfo property.  Because of the way it uses this property, once a branch is merged into trunk it can no longer be involved in merges properly.  For the full story, please consult the last few paragraphs of <a href="http://svnbook.red-bean.com/nightly/en/svn.branchmerge.basicmerging.html#svn.branchemerge.basicmerging.stayinsync">this section on branching and merging</a>.</p>
<p>Subclipse does not provide a way to delete an entire branch, so you&#8217;ll need to just run this command manually:</p>
<p><code>svn delete http://site.com/project/branches/yourbranch -m "Removing completed branch"</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/03/simple-subversion-branching-and-merging/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Java EE 6 and Scala</title>
		<link>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/</link>
		<comments>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 17:36:31 +0000</pubDate>
		<dc:creator>Zach  Cox</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[JSF]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1057</guid>
		<description><![CDATA[Last weekend while pondering the question &#8220;Is Scala ready for the enterprise?&#8221; I decided to write a simple Java EE 6 app entirely in Scala, without using any Java. I had three main reasons for doing this: one was just to see how easy/difficult it would be to write everything in Scala (it was easy).  [...]]]></description>
			<content:encoded><![CDATA[<p>Last weekend while pondering the question &#8220;<a href="http://www.google.ca/search?q=is+scala+ready+for+the+enterprise%3F">Is Scala ready for the enterprise?</a>&#8221; I decided to write a simple <a href="http://java.sun.com/javaee/technologies/">Java EE 6</a> app entirely in <a href="http://www.scala-lang.org">Scala</a>, without using any Java. I had three main reasons for doing this: one was just to see how easy/difficult it would be to write everything in Scala (it was easy).  Another was to document the process for others journeying down the same road (<a href="http://github.com/zcox/javaee6-books-scala">the entire project is on github</a>).  Finally, I wanted to identify advantages of using Scala instead of Java that are specific to Java EE apps (I found several).<br />
<span id="more-1057"></span></p>
<h2>Background</h2>
<p>The specific app I created was an adaptation of the Books example from Chapter 10 of <a href="http://apress.com/book/view/1430219548">Beginning Java™ EE 6 Platform with GlassFish™ 3</a>. It&#8217;s a simple web app that displays a list of books in a database and lets you add new books. Although it&#8217;s a pretty trivial app, it does touch on several important Java EE 6 technologies: JPA 2.0, EJB 3.1 and JSF 2.0.</p>
<h2>Results</h2>
<p>As a baseline, I first created the example from the Book using Java (<a href="http://github.com/zcox/javaee6-books">and put it on github</a>). The three Java classes are mostly identical to the book: <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/model/Book.java">Book.java</a>, <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/ejb/BookEjb.java">BookEjb.java</a> and <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/java/com/sourceallies/controller/BookController.java">BookController.java</a>. Aside from those, I refactored common elements of the two JSF views into a <a href="http://github.com/zcox/javaee6-books/blob/master/src/main/webapp/template.xhtml">template</a>, included the <a href="http://blueprintcss.org/">Blueprint CSS</a> framework and created a better Maven <a href="http://github.com/zcox/javaee6-books/blob/master/pom.xml">pom.xml</a>.</p>
<p>Next I rewrote the Java parts in Scala (<a href="http://github.com/zcox/javaee6-books-scala">and put it on github</a>). Initially I did a one-to-one mapping of the Java classes to their Scala counterparts. Once that was complete I did a bit of refactoring to Book.scala and BookEjb.scala, so the versions you&#8217;ll see on github now look a bit different.  During this process I did use some hints from a few other <a href="http://vikasrao.wordpress.com/2009/02/03/scala-with-jpa/">related</a> <a href="http://www.hars.de/2009/03/jpa-with-scala.html">blog</a> <a href="http://vikasrao.wordpress.com/2009/06/">posts</a>.</p>
<h3>JPA</h3>
<p>First up, here&#8217;s Book.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.model</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.*</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #003399;">Book</span>.<span style="color: #006633;">FindAllBooks</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT b FROM Book b&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Book</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> FindAllBooks <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;findAllBooks&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">IDENTITY</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
&nbsp;
	@Column<span style="color: #009900;">&#40;</span>nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> title<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Float</span> price<span style="color: #339933;">;</span>
&nbsp;
	@Column<span style="color: #009900;">&#40;</span>length <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Boolean</span> illustrations<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTitle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> title<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTitle<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> title<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">title</span> <span style="color: #339933;">=</span> title<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Float</span> getPrice<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> price<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setPrice<span style="color: #009900;">&#40;</span><span style="color: #003399;">Float</span> price<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">price</span> <span style="color: #339933;">=</span> price<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescription<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> description<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescription<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getIsbn<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIsbn<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> isbn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">isbn</span> <span style="color: #339933;">=</span> isbn<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getNbOfPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNbOfPage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> nbOfPage<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nbOfPage</span> <span style="color: #339933;">=</span> nbOfPage<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> getIllustrations<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> illustrations<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIllustrations<span style="color: #009900;">&#40;</span><span style="color: #003399;">Boolean</span> illustrations<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">illustrations</span> <span style="color: #339933;">=</span> illustrations<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getFindallbooks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> FindAllBooks<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Java version has 58 lines of completely useless getters &amp; setters.  You need at least 9 lines per field and more for additional annotations.  IDEs like Eclipse can generate these getters/setters for you, but they still take up space and must be maintained over time.</p>
<p>Now let&#8217;s look at the Book entity in Scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">persistence</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">reflect</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>Entity
<span style="color: #000080;">@</span>NamedQuery<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> name <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;findAllBook&quot;</span>, <span style="color: #0000ff; font-weight: bold;">val</span> query <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;SELECT b FROM Book b&quot;</span><span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> Book <span style="color: #0000ff; font-weight: bold;">extends</span> Id <span style="color: #0000ff; font-weight: bold;">with</span> Description <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Column<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> nullable <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> title<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> price<span style="color: #000080;">:</span> Float <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> isbn<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> nbOfPage<span style="color: #000080;">:</span> Int <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> illustrations<span style="color: #000080;">:</span> Boolean <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Defines an id field for entities to use as a primary key. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Id <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>javax.<span style="color: #000000;">persistence</span>.<span style="color: #000000;">Id</span>
  <span style="color: #000080;">@</span>GeneratedValue<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> strategy <span style="color: #000080;">=</span> GenerationType.<span style="color: #000000;">IDENTITY</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> id<span style="color: #000080;">:</span> Long <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Defines a description field for entities. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Description <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>Column<span style="color: #F78811;">&#123;</span><span style="color: #0000ff; font-weight: bold;">val</span> length <span style="color: #000080;">=</span> <span style="color: #F78811;">2000</span><span style="color: #F78811;">&#125;</span>
  <span style="color: #000080;">@</span>BeanProperty
  <span style="color: #0000ff; font-weight: bold;">var</span> description<span style="color: #000080;">:</span> String <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>In the code base these are actually three separate files: <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Book.scala">Book.scala</a>, <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Id.scala">Id.scala</a> and <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/model/Description.scala">Description.scala</a>, I just included them all in one place here for convenience.</p>
<p>The Scala version needs only two lines per field: one for the @BeanProperty annotation to generate getters/setters in the compiled class and one to declare the field itself.  This is seven lines less than the Java version <em>per field</em>.</p>
<p>Fields are declared as vars so they are public by default, mutable and every var that is a non-private member of some object implicitly defines a getter and a setter method with it.  So &#8220;var x&#8221; defines a getter just named &#8220;x&#8221; and a setter named &#8220;x_=&#8221;.  This lets you get the field using object.x and set it using object.x = y.</p>
<p>If JPA supported Scala-style accessors &amp; mutators, we&#8217;d be done, but it requires the use of get/set methods.  This is why we use <a href="http://www.scala-lang.org/docu/files/api/scala/reflect/BeanProperty.html">@BeanProperty</a> on each field.  This instructs the Scala compiler to generate Java-style get/set methods in the compiled class file so it looks just like a Java entity to JPA.</p>
<p>You may also be wondering why each field is assigned a value of _ in its declaration.  This is just the default value in Scala, just like omitting an initializer in Java: 0 for numeric types, false for booleans, and null for reference types.</p>
<p>The annotations also look a bit different; in Java they&#8217;re @Column(nullable = false) but in Scala are @Column{val nullable = false}.  This is just how you do it in Scala.</p>
<p>I also pulled the id and description fields out into their own traits. This makes the entity class shorter and makes common fields reusable and more consistent across entities.  I could have pulled all fields out into their own traits, but in practice there&#8217;s probably a limit to how much you&#8217;d want to do this, some fields will be specific to a single entity class and not reusable.</p>
<h3>EJB</h3>
<p>Now let&#8217;s look at BookEjb.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.ejb</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.model.*</span><span style="color: #339933;">;</span>
&nbsp;
@Stateless
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookEjb <span style="color: #009900;">&#123;</span>
	@PersistenceContext<span style="color: #009900;">&#40;</span>unitName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;book&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> EntityManager manager<span style="color: #339933;">;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> findBooks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> manager.<span style="color: #006633;">createNamedQuery</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span>.<span style="color: #006633;">FindAllBooks</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResultList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Book</span> createBook<span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		manager.<span style="color: #006633;">persist</span><span style="color: #009900;">&#40;</span>book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Java version is very simple: it basically just provides two Book-specific CRUD methods using a JPA EntityManager.</p>
<p>And here is the Scala version:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">ejb</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">persistence</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>Stateless
<span style="color: #000080;">@</span>LocalBean
<span style="color: #0000ff; font-weight: bold;">class</span> BookEjb <span style="color: #0000ff; font-weight: bold;">extends</span> CrudEjb<span style="color: #F78811;">&#91;</span>Book<span style="color: #F78811;">&#93;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/** Provides basic CRUD support using an injected JPA entity manager. */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> CrudEjb<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>PersistenceContext
  <span style="color: #0000ff; font-weight: bold;">protected</span> <span style="color: #0000ff; font-weight: bold;">var</span> manager<span style="color: #000080;">:</span> EntityManager <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> create<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    manager persist entity
    entity
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> readAll<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> m<span style="color: #000080;">:</span> Manifest<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> manager createNamedQuery <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;findAll&quot;</span> + m.<span style="color: #000000;">erasure</span>.<span style="color: #000000;">getSimpleName</span><span style="color: #F78811;">&#41;</span> getResultList
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> read<span style="color: #F78811;">&#40;</span>id<span style="color: #000080;">:</span> Long<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> manifest<span style="color: #000080;">:</span> Manifest<span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> manager.<span style="color: #000000;">find</span><span style="color: #F78811;">&#40;</span>manifest.<span style="color: #000000;">erasure</span>, id<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">asInstanceOf</span><span style="color: #F78811;">&#91;</span>E<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> update<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> E <span style="color: #000080;">=</span> manager merge entity
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> delete<span style="color: #F78811;">&#40;</span>entity<span style="color: #000080;">:</span> E<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span> manager remove entity <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Again, these are two separate files in the actual code base: <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/ejb/BookEjb.scala">BookEjb.scala</a> and <a href="http://github.com/zcox/javaee6-books-scala/blob/master/src/main/scala/com/sourceallies/ejb/CrudEjb.scala">CrudEjb.scala</a>.</p>
<p>In the Scala version I created the CrudEjb trait that provides full CRUD support for any JPA entity, so the concrete EJB can be defined simply as BookEjb extends CrudEjb[Book].  In several places we need the Class object of the entity, which is difficult to obtain due to Java&#8217;s erasure of generic types at runtime.  Scala&#8217;s <a href="http://daily-scala.blogspot.com/2009/11/manifests-s.html">Manifests</a> are a great solution to this problem.</p>
<p>The Scala version requires the @LocalBean annotation while the Java version does not (not sure why).  Also the EntityManager is defined as a var without the Java-style getters and setters.  The container is able to inject the EntityManager using the Scala-style mutator (bean.manager = x) but it does log an error about it not having a set method.</p>
<h3>JSF</h3>
<p>And now the BookController.java:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.controller</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.faces.bean.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.ejb.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.model.*</span><span style="color: #339933;">;</span>
&nbsp;
@ManagedBean
@RequestScoped
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookController <span style="color: #009900;">&#123;</span>
	@EJB
	<span style="color: #000000; font-weight: bold;">private</span> BookEjb ejb<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Book</span> book<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> bookList<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Book</span> getBook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>book <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			book <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Book</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBook<span style="color: #009900;">&#40;</span><span style="color: #003399;">Book</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">book</span> <span style="color: #339933;">=</span> book<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> getBookList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bookList <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
			bookList <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">findBooks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> bookList<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBookList<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Book<span style="color: #339933;">&gt;</span> bookList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bookList</span> <span style="color: #339933;">=</span> bookList<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doNew<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;newBook.xhtml&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> doCreateBook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		book <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">createBook</span><span style="color: #009900;">&#40;</span>book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		bookList <span style="color: #339933;">=</span> ejb.<span style="color: #006633;">findBooks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;listBooks.xhtml&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This Java version is simply a Book-specific CRUD wrapper around BookEjb, and provides Book and List&lt;Book&gt; objects to the JSF views.</p>
<p>And finally, BookController.scala:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">package</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">controller</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> javax.<span style="color: #000000;">faces</span>.<span style="color: #000000;">bean</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">model</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> com.<span style="color: #000000;">sourceallies</span>.<span style="color: #000000;">ejb</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #000080;">@</span>ManagedBean
<span style="color: #000080;">@</span>RequestScoped
<span style="color: #0000ff; font-weight: bold;">class</span> BookController <span style="color: #F78811;">&#123;</span>
  <span style="color: #000080;">@</span>EJB
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">var</span> ejb<span style="color: #000080;">:</span> BookEjb <span style="color: #000080;">=</span> <span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">val</span> book <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Book
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> getBook <span style="color: #000080;">=</span> book
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> getBookList <span style="color: #000080;">=</span> ejb.<span style="color: #000000;">readAll</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> doNew<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;newBook.xhtml&quot;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> doCreateBook<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    ejb create book
    <span style="color: #6666FF;">&quot;listBooks.xhtml&quot;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The Scala version is basically just the Java version converted to Scala, no major changes, just looks a bit cleaner due to Scala&#8217;s concise syntax.  As with the EJB, we get an error logged about not having a set method for injecting BookEjb, but the container does actually inject an instance properly.</p>
<h2>Conclusion</h2>
<p>Even though this was a simple example, I think it shows a few advantages of using Scala instead of Java in Java EE apps.  You can forget about useless getters/setters in JPA entity classes, use traits to reuse common JPA entity field definitions and mixin common behavior in EJBs (example: BookEjb could also mixin Logging trait and other traits).  And advanced Scala features like Manifests help you write better code faster.</p>
<p>This example only scratched the surface of what Scala can do, and didn&#8217;t even use things like closures, case classes, pattern matching or Scala&#8217;s functional programming capabilities (using Scala collections of JPA entities would help a lot).</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/java-ee-6-and-scala/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Running a Technical Book Club &#8211; Take 1</title>
		<link>http://blogs.sourceallies.com/2010/02/running-a-technical-book-club-take-1/</link>
		<comments>http://blogs.sourceallies.com/2010/02/running-a-technical-book-club-take-1/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 16:00:48 +0000</pubDate>
		<dc:creator>Sudhakar Ramasamy</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book club]]></category>
		<category><![CDATA[continuous learning]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=1044</guid>
		<description><![CDATA[Last year I coordinated a technical book club here at Source Allies. This was my first experience doing one and I wanted to share my experience for the benefit of others who may be looking at starting one.
The fact that we even started a book club was a big positive because it is one great [...]]]></description>
			<content:encoded><![CDATA[<p>Last year I coordinated a technical book club here at Source Allies. This was my first experience doing one and I wanted to share my experience for the benefit of others who may be looking at starting one.</p>
<p>The fact that we even started a book club was a big positive because it is one great way to geek out with very smart people. You get the opportunity to voice your opinion on a certain topic and hear counter-points or similar views that expand your own perspective.<span id="more-1044"></span></p>
<p>We went through 2 books in the span of about 6 months. The first book was <a href="http://search.barnesandnoble.com/Test-Driven-Development/Kent-Beck/e/9780321146533">Test Driven Development: By Example</a> by Kent Beck. It&#8217;s a very good book by itself. But we found it didn&#8217;t make for very good book club discussion. The material didn&#8217;t really provide much fodder for critical thinking and exploration in my opinion. But it was a start.</p>
<p>We then picked up <a href="http://java.sun.com/docs/books/effective/">Effective Java</a> by Joshua Bloch as our second book. Java is currently my bread and butter and this book was very appropriate with it&#8217;s tip based approach. The material was a little dense in some areas, but overall an extremely good book for a book club discussion because it provided enough room for critical thinking and discussion. Unfortunately though well intentioned I thought we fell off the wagon due to a few different reasons:</p>
<ul>
<li>We met in a cafeteria location which was noisy and had it&#8217;s own share of distractions making it difficult to have any sustained discussion</li>
<li>Not a whole lot of discussion around the topic because each of us read a different chapter in the book and we didn&#8217;t really have common ground</li>
<li>We had participants in the discussion who hadn&#8217;t read the book and would understandably drive the conversations off topic</li>
<li>We didn&#8217;t have a standing meeting time which we were committed to. Sometimes I felt we were scrambling just to get a common meeting time organized. We did get better about this towards the later half of last year.</li>
</ul>
<p><strong>Going forward into 2010</strong><br />
Learning from last year, we are making some changes to derive more out of the book club this year. </p>
<ul>
<li>Meet in a quiet location &#8211; This year we will be meeting in a conference room with little distractions</li>
<li>A once a week standing meeting &#8211; This way everyone in the group is aware of the weekly commitment and can be prepared</li>
<li>Be more pro-active about our book selection &#8211; Maintain a <a href="http://www.goodreads.com/review/list/2958611-sud?shelf=bookclub">list</a> of potentially interesting books and select books at the regular meeting</li>
<li>More structured discussion format &#8211; We all have views and opinions on what we&#8217;ve read. So all of us must have the opportunity to voice our thoughts</li>
<li>Book review &#8211; This year we are going to attempt to publish a blog post reviewing each book that we discussed at the book club</li>
<li>Limit the book club to a maximum of 6 members &#8211; any more and the 1-hour will be insufficient for everyone to participate fully</li>
</ul>
<p>We&#8217;ve already had two meetings so far this year with the above format and I thought it went very well. And am looking forward to a more rewarding book club this year.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/running-a-technical-book-club-take-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacing and Patching Java Application and Core classes</title>
		<link>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/</link>
		<comments>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 16:00:40 +0000</pubDate>
		<dc:creator>Alexandru Luchian</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=809</guid>
		<description><![CDATA[Why would you ever need that?
Say you get a jar file. After using the jar for a while you realise that there is a bug in a class in the jar file. Unfortunately you also find out that the jar is no longer supported and there is no way you will get a fix from [...]]]></description>
			<content:encoded><![CDATA[<p>Why would you ever need that?</p>
<p>Say you get a jar file. After using the jar for a while you realise that there is a bug in a class in the jar file. Unfortunately you also find out that the jar is no longer supported and there is no way you will get a fix from the author (who is long gone fishing).</p>
<p>In order to solve this issue, you first need to get the source of the class. If you are lucky enough and the author did not obfuscate the class file you can decompile it with a decompiler (my favourite one is <a href="http://java.decompiler.free.fr/">JD-GUI</a>).</p>
<p><span id="more-809"></span></p>
<p>After you get the source, you modify it, recompile and tell the java interpreter to load your new class first before the old one. Easy right?</p>
<p>Let me give you an example</p>
<p>Say we have this &#8220;outdated&#8221; Spider class</p>
<blockquote><p>alex@tractorash:~/work/java$ pwd<br />
/home/alex/work/java<br />
alex@tractorash:~/work/java$ cat sai/Spider.java</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Spider<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> <span style="color: #003399;">URL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.google.com/search?q=best+software+2009&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">URL</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, we have the Main class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Main<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        Spider spider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Spider<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>spider.<span style="color: #006633;">getURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, if we compile and run the Main class we will get:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac sai/Main.java<br />
alex@tractorash:~/work/java$ java sai.Main<br />
<strong> http://www.google.com/search?q=best+software+2009</strong></p></blockquote>
<p>As we expected. Suppose we want to search for http://www.google.com/search?q=best+software+2010. The only way to do that is create another <strong>sai.Spider</strong> class and return the needed string.</p>
<p>So, here we go</p>
<blockquote><p>alex@tractorash:~/h$ pwd<br />
/home/alex/h<br />
alex@tractorash:~/h$ cat sai/Spider.java</p></blockquote>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">sai</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Spider<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> <span style="color: #003399;">URL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://www.google.com/search?q=best+software+2010&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getURL<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">URL</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We compile our new class</p>
<blockquote><p>alex@tractorash:~/h$ javac sai/Spider.java</p></blockquote>
<p>Then, switch to previous path:</p>
<blockquote><p>alex@tractorash:~/h$ cd ~/work/java/<br />
alex@tractorash:~/work/java$ java -cp /home/alex/h:/home/alex/work/java sai.Main<br />
http://www.google.com/search?q=best+software+<strong>2010</strong></p></blockquote>
<p>So what did we do? We modified the classpath (http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/classpath.html) and told the JVM to load our new class first, since it&#8217;s first in our list of directories we specified as classpath.</p>
<p>This way we &#8220;tricked&#8221; the JVM in loading our new class first so we could get the desired output.</p>
<p>What we&#8217;ve done so far is called replacing and patching application classes.</p>
<p>You will not be able to modify/replace system classes like <em>java.lang.Integer</em> using this method.</p>
<p>You are probably wondering why would you ever want to replace java.lang.Integer. Let me give you an example. As you might know the Integer class is immutable, in other words once the object is initialised you cannot modify it&#8217;s value. So, let&#8217;s add a new method called setValue(int newVal) which will modify the current value of the Integer object.</p>
<p>As you can imagine, applying the method used in the Spider class example will not work. The reason is: to load system classes and libraries, the JVM does not use the CLASSPATH variable, but the BOOTCLASSPATH. The boot strap class loader uses the boot class path to load the main/system classes.</p>
<p>What&#8217;s interesting is that you can specify/modify the BOOTCLASSPATH as well. In order to to that we have to use the -X argument for the java launcher:</p>
<blockquote><p>alex@tractorash:~$ java -X<br />
-Xbootclasspath:&lt;directories and zip/jar files separated by :&gt;<br />
set search path for bootstrap classes and resources<br />
-Xbootclasspath/a:&lt;directories and zip/jar files separated by :&gt;<br />
append to end of bootstrap class path<br />
-Xbootclasspath/p:&lt;directories and zip/jar files separated by :&gt;<br />
prepend in front of bootstrap class path</p></blockquote>
<p>We are interested in the <em>-Xbootclasspath/p:</em> option since we want to prepend the directory that contains the patched Integer class.</p>
<p>First, let&#8217;s add that new method. Get the source of java.lang.Integer. You can get it either from the src.zip file that comes with the JDK, or download it from online.</p>
<p>Switch to our working directory:</p>
<blockquote><p>alex@tractorash:/$ cd /home/alex/work/java/</p></blockquote>
<p>Add the method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">value</span> <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you are modifying a Java 6 version of Integer class you have to remove the final keyword from the value variable.</p>
<p>Was this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">;</span></pre></div></div>

<p>Has to be this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">;</span></pre></div></div>

<p>Now we need a test class where we will test the new functionality. Let&#8217;s create the IntegerTest class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IntegerTest<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Integer</span> tInt <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Initial value: &quot;</span> <span style="color: #339933;">+</span> tInt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tInt.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;New value: &quot;</span> <span style="color: #339933;">+</span> tInt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Compiling patched versions of system classes can be a little tricky.</p>
<p>First let&#8217;s compile our Integer class:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac java/lang/Integer.java<br />
java/lang/Integer.java:571: warning: sun.misc.VM is Sun proprietary API and may be removed in a future release<br />
if (!sun.misc.VM.isBooted()) {<br />
^<br />
Note: java/lang/Integer.java uses unchecked or unsafe operations.<br />
Note: Recompile with -Xlint:unchecked for details.<br />
1 warning</p></blockquote>
<p>We get the warning because we use a sun.* class in our new class. Since sun.* packages and classes are proprietary to Sun, it is strongly advised not to use them, but in our case we just reuse them (so we can ignore the warning).</p>
<p>Now, we have our Integer.class file. We have to use it when we execute our test class.</p>
<p>Now compile IntegerTest.java:</p>
<blockquote><p>alex@tractorash:~/work/java$ javac -Xbootclasspath/p:/home/alex/work/java/lang/:/usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/rt.jar IntegerTest.java<br />
./java/lang/Integer.java:571: warning: sun.misc.VM is Sun proprietary API and may be removed in a future release<br />
if (!sun.misc.VM.isBooted()) {<br />
^<br />
Note: ./java/lang/Integer.java uses unchecked or unsafe operations.<br />
Note: Recompile with -Xlint:unchecked for details.<br />
1 warning</p></blockquote>
<p>Copy Integer.class to the directory where your IntegerTest class is:</p>
<blockquote><p>alex@tractorash:~/work/java$ cp ./java/lang/Integer.class .</p></blockquote>
<p>So IntegerTest.class and Integer.class are in the same directory.</p>
<p>Finally run IntegerTest:</p>
<blockquote><p>alex@tractorash:~/work/java$ java -Xbootclasspath/p:/home/alex/work/java:/usr/lib/jvm/java-6-sun-1.6.0.15/jre/lib/rt.jar IntegerTest<br />
Initial value: 1<br />
New value: 25</p></blockquote>
<p>Success! Now you have a mutable Integer class.</p>
<p>I recommend you read the <a title="Java Covert" href="http://www.amazon.com/Covert-Java-Techniques-Decompiling-Engineering/dp/0672326388" target="_blank">Java Covert</a> book.</p>
<p>However, I have to warn you that this post is for academic purposes only. Patching/removing/replacing Java core classes can be against the Java license agreement. Please consult the license before going down this path.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IP Addresses in PHP/MySQL</title>
		<link>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/</link>
		<comments>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 13:32:39 +0000</pubDate>
		<dc:creator>Mark Ciecior</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=924</guid>
		<description><![CDATA[I&#8217;ve been working on a web-based tool that stores, among other network-related things, IP addresses.  When I first started I stored each IP address as four TINYINTS (0-255 for each octet):

mysql&#62; DESC ipaddresses;
+----------+---------------------+------+-----+---------+----------------+
&#124; FIELD    &#124; Type                [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a web-based tool that stores, among other network-related things, IP addresses.  When I first started I stored each IP address as four TINYINTS (0-255 for each octet):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">DESC</span> ipaddresses;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">FIELD</span>    <span style="color: #66cc66;">|</span> Type                <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #66cc66;">|</span> Extra          <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #66cc66;">|</span> id       <span style="color: #66cc66;">|</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span>    <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span> PRI <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> A        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> B        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> C        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">|</span> D        <span style="color: #66cc66;">|</span> tinyint<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #66cc66;">|</span> NO   <span style="color: #66cc66;">|</span>     <span style="color: #66cc66;">|</span> <span style="color: #993333; font-weight: bold;">NULL</span>    <span style="color: #66cc66;">|</span>                <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------+---------------------+------+-----+---------+----------------+</span>
<span style="color: #cc66cc;">5</span> rows <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> ipaddresses <span style="color: #993333; font-weight: bold;">WHERE</span> id<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> A  <span style="color: #66cc66;">|</span> B  <span style="color: #66cc66;">|</span> C  <span style="color: #66cc66;">|</span> D   <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">131</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+-----+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.02</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As I started manipulating these addresses I found it awkward to do common binary math (like bitwise ANDs).  I decided instead to store these 32-bit values as unsigned integers (of length 32).  To make my life easier yet, MySQL and PHP both have native functions to convert IP addresses between my old and new formats to make this migration extremely easy.<br />
<span id="more-924"></span><br />
In MySQL:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> INET_ATON<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'10.20.30.131'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #66cc66;">|</span> INET_ATON<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'10.20.30.131'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #66cc66;">|</span>                 <span style="color: #cc66cc;">169090691</span> <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">---------------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> INET_NTOA<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">169090691</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #66cc66;">|</span> INET_NTOA<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">169090691</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #66cc66;">|</span> 10<span style="color: #66cc66;">.</span>20<span style="color: #66cc66;">.</span>30<span style="color: #66cc66;">.</span>131         <span style="color: #66cc66;">|</span> 
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----------------------+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ip_string</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;10.20.30.131&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip_integer</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ipstring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip_integer</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">## 169090691
</span><span style="color: #b1b100;">echo</span> <span style="color: #990000;">long2ip</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip_integer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">## 10.20.30.131</span></pre></div></div>

<p>In another example of inefficiency, I used to gather all sorts of info from the user and use PHP to make multiple SQL queries before modifying my data.  I&#8217;ve since learned about stored procedures &#8211; the database does all the work and I only need to make one SQL call from PHP.  This presented me with an unexpected problem, however:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> CALL getAddress<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #66cc66;">|</span> id <span style="color: #66cc66;">|</span> A  <span style="color: #66cc66;">|</span> B  <span style="color: #66cc66;">|</span> C  <span style="color: #66cc66;">|</span> D  <span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #66cc66;">|</span>  <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">|</span> <span style="color: #cc66cc;">131</span><span style="color: #66cc66;">|</span>
<span style="color: #66cc66;">+</span><span style="color: #808080; font-style: italic;">----+----+----+----+----+</span>
<span style="color: #cc66cc;">1</span> row <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span>
&nbsp;
Query OK<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> rows affected <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.00</span> sec<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The string &#8220;Query OK, 0 rows affected (0.00 sec)&#8221; is a second result set, which the PHP <i>mysql</i> library can&#8217;t handle.  The fix here is to use the <i>mysqli</i> library instead and access only the result sets you need.  In my case I&#8217;m only concerned with the first result set so I added a bunch of wasteful while loops after each of my CALL queries.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysqli_next_result</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lastly, PHP&#8217;s ip2long function treats 32-bit numbers as signed ints, so I had to specifically treat them as unsigned ints:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">echo $ip_integer; <span style="color: #808080; font-style: italic;">## BAD</span>
echo sprintf<span style="color: #66cc66;">&#40;</span>%u<span style="color: #66cc66;">,</span> $ip_integer<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">## GOOD</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/ip-addresses-in-phpmysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sonar &#8211; Code Quality Analysis Tool</title>
		<link>http://blogs.sourceallies.com/2010/02/sonar-code-quality-analysis-tool/</link>
		<comments>http://blogs.sourceallies.com/2010/02/sonar-code-quality-analysis-tool/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 16:00:12 +0000</pubDate>
		<dc:creator>Sanjay Shrestha</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Sonar]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=884</guid>
		<description><![CDATA[What is Sonar?
Sonar is a web based code quality analysis tool for Maven based Java projects. It covers a wide area of code quality check points which include: Architecture &#38; Design, Complexity, Duplications, Coding Rules, Potential Bugs, Unit Test etc. Sonar has a rich set of features like what you would get with different tools [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What is Sonar?</strong></p>
<p>Sonar is a web based code quality analysis tool for Maven based Java projects. It covers a wide area of code quality check points which include: Architecture &amp; Design, Complexity, Duplications, Coding Rules, Potential Bugs, Unit Test etc. Sonar has a rich set of features like what you would get with different tools such as Covertura, PMD, FindBugs, Check Styles combined.</p>
<p><a href="http://sonar.codehaus.org/">http://sonar.codehaus.org/</a></p>
<p><strong>Setting up Sonar</strong></p>
<ul>
<li>Download the latest release of Sonar from <a href="http://sonar.codehaus.org/downloads/">http://sonar.codehaus.org/downloads/</a>.</li>
<li>Unzip it to a folder, e.g. C:\Sonar</li>
<p><span id="more-884"></span></p>
<li>Run <em>C:\Sonar\bin\windows-x86-32\StartSonar.bat</em> for Windows. There are several options available for other operationg system such as Linux, Solaris.</li>
<li>Browse to <a href="http://localhost:9000/">http://localhost:9000</a> . The browser will display a &#8220;<em>No projects have been analysed</em>&#8221; message. This is a sanity check to see if Sonar is running properly.</li>
<li>From within your project, run the following maven command: <em>mvn clean install sonar:sonar</em></li>
<li>Refesh the browser. The code quality related reports such as Complexity, Duplication, Voilation, Rules of Compliance etc will be displayed.</li>
<li>To customize the features using the admin console, login/password is <strong>admin/admin</strong>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/02/sonar-code-quality-analysis-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
