<?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>Bonney &#38; Stewart Software Development Blog</title>
	<atom:link href="http://www.bssd.eu/blog/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.bssd.eu/blog</link>
	<description>Getting IT delivered</description>
	<lastBuildDate>Tue, 27 Mar 2012 21:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Netty the Elephant &#8211; Part 2</title>
		<link>http://www.bssd.eu/blog/?p=261</link>
		<comments>http://www.bssd.eu/blog/?p=261#comments</comments>
		<pubDate>Tue, 27 Mar 2012 21:22:11 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=261</guid>
		<description><![CDATA[In my previous blog post I started building a simple RPC demo using the Netty framework.At the end of part 1 I had a simple integration test that verified the message I sent from a client to the server got echoed back again. The next step I took was to add a new integration test [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a title="Netty the Elephant - Part 1" href="http://www.bssd.eu/blog/?p=254" target="_blank">previous blog post</a> I started building a simple RPC demo using the <a title="Netty" href="http://netty.io/" target="_blank">Netty</a> framework.At the end of part 1 I had a simple integration test that verified the message I sent from a client to the server got echoed back again.</p>
<p>The next step I took was to add a new integration test to verify when the server was shutdown all the clients were disconnected and stopped themselves.</p>
<p>We already had a client that we used for the test to send a message and then wait for the response.</p>
<p>So first of all we add a simple DisconnectLatch class:</p>
<pre class="brush: java; gutter: false">package uk.co.bssd.netty.server;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import uk.co.bssd.netty.DisconnectListener;

public class DisconnectLatch implements DisconnectListener {

	private static final long DEFAULT_TIMEOUT_MS = 1000;

	private final CountDownLatch latch;
	private final long timeoutMs;

	public DisconnectLatch() {
		this(DEFAULT_TIMEOUT_MS);
	}

	public DisconnectLatch(long timeoutMs) {
		this.latch = new CountDownLatch(1);
		this.timeoutMs = timeoutMs;
	}

	@Override
	public void onDisconnect() {
		this.latch.countDown();
	}

	public boolean awaitDisconnect() {
		try {
			this.latch.await(this.timeoutMs, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			throw new IllegalStateException(&quot;Interrupted whilst awaiting on disconnect latch&quot;, e);
		}
		return this.latch.getCount() == 0;
	}
}</pre>
<p>Then we amend the MessageCollectingClient to take an instance of this latch in the constructor. The client will call the onDisconnect method on the latch if it receives a disconnect event from the Netty framework.</p>
<p>We can then write a simple test like this:</p>
<pre class="brush: java; gutter: false">	@Test
	public void testStoppingServerCausesDisconnectAtClient() {
		this.server.stop();
		assertThat(this.clientDisconnectLatch.awaitDisconnect(), is(true));
	}
</pre>
<p>The latch was constructed with the default timeout so will wait for 1 second before returning when we then check if it was successful.</p>
<p>Unfortunately the test didn&#8217;t pass first time, the client did not get a disconnect event. Upon investigation the server did not shutdown and release the resources nor close the connection.</p>
<p>The original implementation of the server stop method looked like:</p>
<pre class="brush: java; gutter: false">	public void stop() {
		this.bootstrap.releaseExternalResources();
	}</pre>
<p>This attempts to shutdown the threadpool executors used by Netty to manage both the connections and workers. The problem is because a client is connected there is a thread managing that connection and this call does not shut it down, it would only do so if the client disconnected or the channel was closed.</p>
<p>After a bit of digging and looking at some more examples I found the <a title="ChannelGroup Javadoc" href="http://netty.io/docs/stable/api/org/jboss/netty/channel/group/ChannelGroup.html" target="_blank">ChannelGroup</a> interface. The Javadoc explains how it can be used to shutdown all the channels in a server in one go. Basically when a channel is created you ensure you add it to the group and then the stop method can call close on the group shutting down all the channels held within it. Another good thing about the group is that you don&#8217;t have to worry about Channels that have already been closed (such as if a client disconnects) as these get tidied up for you.</p>
<p>The stop method for the server now looks like:</p>
<pre class="brush: java; gutter: false">	public void stop() {
		this.channelGroup.close();
		this.bootstrap.releaseExternalResources();
	}
</pre>
<p>It also means that the message handlers require a reference to the channel group and must add the channel to the group. To take care of this I created a handler which only concerns itself with channel life-cycle events:</p>
<pre class="brush: java; gutter: false">package uk.co.bssd.netty;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.group.ChannelGroup;

public class ChannelEventHandler extends SimpleChannelUpstreamHandler {

	private final ChannelGroup channelGroup;

	public ChannelEventHandler(ChannelGroup channelGroup) {
		this.channelGroup = channelGroup;
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		this.channelGroup.add(e.getChannel());
	}
}</pre>
<p>As long as the handler is registered with the channel pipeline (such as in the pipeline factory) then it will make sure all channels created get registered with the group. Now when the stop method is called on the server then it disconnects all the clients and the test passes.</p>
<p>This version of the code can be found on GitHub with this <a title="GitHub Commit" href="https://github.com/andystewart79/netty-the-elephant/commit/b01e3b446b9aa30d1fe5ea105b5e229e9df755d4" target="_blank">commit hash</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=261</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Netty the Elephant &#8211; Part 1</title>
		<link>http://www.bssd.eu/blog/?p=254</link>
		<comments>http://www.bssd.eu/blog/?p=254#comments</comments>
		<pubDate>Thu, 15 Mar 2012 21:43:51 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=254</guid>
		<description><![CDATA[In the last couple of weeks I have been using the Netty framework on a client project to integrate with a 3rd party. I have been impressed how straightforward it is to get something up and running and want to play with it a bit more. To this end I have setup a sandbox project [...]]]></description>
			<content:encoded><![CDATA[<p>In the last couple of weeks I have been using the <a title="Netty" href="http://netty.io/" target="_blank">Netty </a>framework on a client project to integrate with a 3rd party. I have been impressed how straightforward it is to get something up and running and want to play with it a bit more.</p>
<p>To this end I have setup a <a title="Netty GitHub Project" href="https://github.com/andystewart79/netty-the-elephant" target="_blank">sandbox project</a> on GitHub to play around with creating a very RPC style example in a similar vein to twitters <a title="Finagle" href="http://twitter.github.com/finagle/" target="_blank">Finagle</a>. This isn&#8217;t about reinventing the wheel though, more about getting to understand the framework a bit better and I also intend to drive the development using an outside in testing approach in the <a title="Growing Object Oriented Software, Guided by Tests" href="http://www.growing-object-oriented-software.com/" target="_blank">GOOS</a> style (see my <a title="GOOS Review" href="http://www.bssd.eu/blog/?p=242" target="_blank">previous pos</a>t on the subject).</p>
<p>So far what I have committed is pretty much the same as the <a title="Object Echo Example" href="http://netty.io/docs/stable/xref/org/jboss/netty/example/objectecho/package-summary.html" target="_blank">Object Echo</a> example provided with Netty. The client sends a message (wrapped in an Object) to the server, which then unpacks the payload before wrapping it in a response Object and sending it back.</p>
<p>The main difference is that I have a single JUnit test which fires up a client and a server, sends a message and then waits for the response, the test looks like this:</p>
<pre class="brush: java; gutter: false">package uk.co.bssd.netty.server;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.co.bssd.netty.client.MessageCollectingClient;
import uk.co.bssd.netty.dto.SimpleRequest;
import uk.co.bssd.netty.dto.SimpleResponse;

public class EchoServerIntegrationTest {

 private static final String HOST = &quot;127.0.0.1&quot;;
 private static final int PORT = 6781;

 private static final long CLIENT_CONNECTION_TIMEOUT_MS = 1000;
 private static final long RECEIVE_MESSAGE_TIMEOUT_MS = 1000;

 private MessageCollectingClient client;
 private EchoServer server;

 @Before
 public void before() {
   this.server = new EchoServer();
   this.server.start(HOST, PORT);

   this.client = new MessageCollectingClient();
   this.client.start(HOST, PORT, CLIENT_CONNECTION_TIMEOUT_MS);
 }

 @After
 public void after() {
   this.client.stop();
   this.server.stop();
 }

 @Test
 public void testServerEchoesBackMessageSentFromClient() {
   String payload = &quot;Hello&quot;;

   this.client.send(new SimpleRequest(payload));

   Object message = this.client.awaitMessage(RECEIVE_MESSAGE_TIMEOUT_MS);
   assertThat(message, is(SimpleResponse.class));

   SimpleResponse response = (SimpleResponse) message;
   assertThat(response.payload(), is(payload));
 }
}</pre>
<p>Now that I have this very first test I intend to drive the development of the client and server by adding end to end tests one at a time and then working outside in adding smaller unit tests and getting them to pass by implementing the functionality. In the past I have tended to work very much inside out and am keen to see how this goes.</p>
<p>In the next part I plan to drive out what happens when a client disconnects from a server and vice-versa. The final part will then move on to being able to register messages handlers and ensuring the correct handler is invoked when the client sends a message.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=254</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Configuration Continued</title>
		<link>http://www.bssd.eu/blog/?p=249</link>
		<comments>http://www.bssd.eu/blog/?p=249#comments</comments>
		<pubDate>Fri, 09 Mar 2012 20:58:12 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Best practice]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=249</guid>
		<description><![CDATA[Today I was asked on Twitter why in my previous post on splitting Spring configuration why I didn&#8217;t use a PropertyPlaceholderConfigurer and switch out the database dialect, connection string, etc. That is a fair point with the example I gave as it was simplified for the sake of illustration. In production we actually tend to [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was asked on Twitter why in my <a title="Spring Configuration Part I" href="http://www.bssd.eu/blog/?p=201" target="_blank">previous post</a> on splitting Spring configuration why I didn&#8217;t use a PropertyPlaceholderConfigurer and switch out the database dialect, connection string, etc. That is a fair point with the example I gave as it was simplified for the sake of illustration.</p>
<p>In production we actually tend to use a JNDI lookup to get hold of our data source from a web container, which is generally a connection pool rather than a single connection as per the unit tests.</p>
<p>To illustrate the point a bit better I will walk through an example involving Spring Integration.</p>
<p>Spring Integration has the concept of <a title="Spring Integration Message Channels" href="http://static.springsource.org/spring-integration/reference/htmlsingle/#overview-components-channel" target="_blank">message channels</a> which is a mechanism to decouple message components. General a service would do some work produce a message output and then send that to a channel. On the end of the channel would be some form of adapter or gateway that would take the message and then route it on somewhere useful.</p>
<p>We have used channels with JMS quite a lot so we might have a JMS outbound adapter that takes a message from the channel and pops it onto a queue or topic ready to be picked up by the next thing.</p>
<p>So a very naive Spring Context file might look like:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:int=&quot;http://www.springframework.org/schema/integration&quot;
 xmlns:int-jms=&quot;http://www.springframework.org/schema/integration/jms&quot;
 xsi:schemaLocation=&quot;
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
 http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.1.xsd&quot;&gt;

 &lt;bean id=&quot;myService&quot; class=&quot;uk.co.bssd.example.MyService&quot;&gt;
   &lt;property name=&quot;dispatcher&quot; ref=&quot;dispatcher&quot; /&gt;
 &lt;/bean&gt;

 &lt;int:channel id=&quot;outbound&quot; /&gt;

 &lt;int:gateway id=&quot;dispatcher&quot; service-interface=&quot;uk.co.bssd.example.Dispatcher&quot;
   request-channel=&quot;outbound&quot; /&gt;

 &lt;int-jms:outbound-channel-adapter channel=&quot;outbound&quot; destination-name=&quot;outbound-queue&quot; /&gt;

&lt;/beans&gt;</pre>
<p>A brief explanation of what is going on here is:</p>
<ol>
<li>The service has a dispatcher injected into it.</li>
<li>In this case the dispatcher is a proxied Spring Integration object that will take any method call and place the object passed to the method and place it on the outbound channel.</li>
<li>The JMS outbound adapter picks up the objects from the outbound channel and sends them to the to outbound-queue.</li>
</ol>
<p>Now this is great and works fine, but it&#8217;s hard to test. If we want to test it then we need to either spin up a JMS queue and that&#8217;s quite involved considering all the service really cares about is that it dispatches the correct object.</p>
<p>So if we split our contexts into two, with context 1:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;&gt;

 &lt;bean id=&quot;myService&quot;&gt;
 &lt;property name=&quot;dispatcher&quot; ref=&quot;dispatcher&quot; /&gt;
 &lt;/bean&gt;

&lt;/beans&gt;</pre>
<p>Then context 2:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:int=&quot;http://www.springframework.org/schema/integration&quot;
 xmlns:int-jms=&quot;http://www.springframework.org/schema/integration/jms&quot;
 xsi:schemaLocation=&quot;
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.1.xsd
 http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms-2.1.xsd&quot;&gt;

 &lt;int:channel id=&quot;outbound&quot; /&gt;

 &lt;int:gateway id=&quot;dispatcher&quot; service-interface=&quot;uk.co.bssd.example.Dispatcher&quot;
 request-channel=&quot;outbound&quot; /&gt;

 &lt;int-jms:outbound-channel-adapter channel=&quot;outbound&quot; destination-name=&quot;outbound-queue&quot; /&gt;

&lt;/beans&gt;</pre>
<p>This gives us a bit more freedom for the unit test. Now we can write a context to stub out the dispatcher:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
 xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
 xsi:schemaLocation=&quot;
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&gt;

 &lt;bean id=&quot;dispatcher&quot;
 factory-method=&quot;createMock&quot; primary=&quot;true&quot;&gt;
 &lt;constructor-arg value=&quot;uk.co.bssd.example.Dispatcher&quot; /&gt;
 &lt;/bean&gt;

&lt;/beans&gt;</pre>
<p>Now what this does is create an EasyMock version of the dispatcher bean.</p>
<p>So in our test we can simply put load context 1 and the test context together using the Spring JUnit test runner and the @ContextConfiguration and we can then @Autowired our mock dispatcher in and assert the correct object is dispatched to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=249</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GOOS Review</title>
		<link>http://www.bssd.eu/blog/?p=242</link>
		<comments>http://www.bssd.eu/blog/?p=242#comments</comments>
		<pubDate>Fri, 09 Mar 2012 10:32:57 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=242</guid>
		<description><![CDATA[Around about last May I bought and read the book Growing Object-Oriented Software Guided by Tests (more commonly referred to as GOOS) by Steve Freeman and Nat Pryce. I have meant to write up a review of it for some time and this blog post I read by Micheal Feathers the other day prompted me [...]]]></description>
			<content:encoded><![CDATA[<p>Around about last May I bought and read the book <a title="Growing Object-Oriented Software Guided by Tests" href="http://www.growing-object-oriented-software.com/" target="_blank">Growing Object-Oriented Software Guided by Tests</a> (more commonly referred to as GOOS) by <a title="Steve Freeman" href="http://www.higherorderlogic.com/" target="_blank">Steve Freeman</a> and <a title="Nat Pryce" href="http://www.natpryce.com/" target="_blank">Nat Pryce</a>. I have meant to write up a review of it for some time and this <a title="The Flawed Theory of Unit Testing" href="http://michaelfeathers.typepad.com/michael_feathers_blog/2008/06/the-flawed-theo.html" target="_blank">blog post I read by Micheal Feathers</a> the other day prompted me to getting around and actually doing it.</p>
<p>To cut a long story short I think GOOS is excellent and is perhaps the best technical book I have read, better than the Pragmatic Programmer or Code Complete 2 in my eyes. I think the team have probably got fed up of me pushing the book at every opportunity!</p>
<p>Having said it&#8217;s a technical book it is not about a specific technology, yes the examples are in Java but they could apply to any Object Orientated language. The mocking framework of choice in the book is jMock but having used EasyMock before I have stuck with that but the ideas still apply, it&#8217;s more a question of personal preference and style.</p>
<p>The major strength of the book is that it flows like a story. The authors outline a realistic (or semi-realistic) application to build and then start at the beginning using the techniques they describe to push and pull the design of the evolving system as they go. They really show how to listen to the tests and how they get from the initial version to the next step.</p>
<p>A heavy emphasis is placed on mock objects, something I had used before but with mixed results. I was very familiar with the mess you can get yourself into with mocks where you end up with a load of tests that assert a method is called x times and when the implementation changes a huge swathe of tests fail. This is bad mocking and should be avoided at all costs. Having read the book and applying the principles I like to think that I don&#8217;t indulge in that kind of thing and use mocks appropriately.</p>
<p>Some of the things we have taken from the book and applied on a recent project to great success are:</p>
<ol>
<li>Test Data Builders &#8211; we know use builders extensively throughout the system making it easy to build our objects and keep them immutable once built. Test Data Builders are then a great way to easily create test data without too much effort or repetition. Having worked on previous projects where large amounts of JUnit code was simply great big chunks of cut and paste code with minor tweaks writing tests is a much more pleasurable experience.</li>
<li>Value Objects and First Class Collections &#8211; provide a much better place to hang behaviour rather than having the same code scattered and repeated throughout services and other objects.</li>
<li>A much tighter focus on making sure that a class has only a single responsibility.</li>
<li>Shorter, more focused unit tests (generally as a result of the point above).</li>
<li>A greater awareness of when a class is too tightly coupled to other parts of the system. There are some great chapters on splitting the business logic in their application from the GUI code and making the two unaware of each other. One of the tips was take a look at the package imports to see what your class depends on, this is something I make sure I do regularly now.</li>
<li>A more considered and reduced use of Spring and an increased use of constructor arguments rather than &#8220;setter injection&#8221;.</li>
<li>Code that is easier to rewire and stub out other systems and services. This has allowed us to write a number of regression and load tools that replace our 3rd party dependencies with fakes and stubs for testing purposes.</li>
<li>A focus on a short build time, mainly due to very quick unit tests because they use mocks rather than instantiating big complicated dependencies.</li>
</ol>
<p>I am sure there all lots more things we have taken and applied I just can&#8217;t think of them. There are still some things we have yet to try, we don&#8217;t really work from the outside in, which is an approach I would like to try. I intend to go back and read the book as it has been a while and I am sure there will be lots more things I will take away.</p>
<p>As my friend who read it on my recommendation said, it&#8217;s mainly just common sense, but it is common sense that you wished you had thought of and written down first! I can&#8217;t recommend GOOS highly enough, as a result of applying the principles in the book it has made me a better developer than I was before I read it and crucially it has improved the quality of the software I deliver.</p>
<p>The project we applied these lessons on went live about 6 months ago, I can count on a single finger the number of issues we have had which I think is an incredible achievement. The test team also commented in retrospectives as to the low defect rate and the quick turnaround on issues raised during testing.</p>
<p>The final thing to say is the book also changed my stance on the I for an interface debate, some of my colleagues remain to be convinced though!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=242</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Spring Configuration</title>
		<link>http://www.bssd.eu/blog/?p=201</link>
		<comments>http://www.bssd.eu/blog/?p=201#comments</comments>
		<pubDate>Thu, 08 Mar 2012 14:53:35 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=201</guid>
		<description><![CDATA[A blog post by Jason Gorman I read the other day got me thinking. The general gist of his post is that people tend to think mainly in terms of their source code as the stuff written in your programming language of choice, be it Java, Scala or whatever. However people should have a wider [...]]]></description>
			<content:encoded><![CDATA[<p>A <a title="But What About Your OTHER Code?" href="http://codemanship.co.uk/parlezuml/blog/?postid=1095" target="_blank">blog post by Jason Gorman</a> I read the other day got me thinking. The general gist of his post is that people tend to think mainly in terms of their source code as the stuff written in your programming language of choice, be it Java, Scala or whatever. However people should have a wider view and think about the other bits and pieces that go into making a working deployable application such as hibernate mapping files, database scripts, etc.</p>
<p>I couldn&#8217;t agree with Jason more, I have seen on a number of occassions how teams can get themselves in a real pickle with their Spring configuration. Some of the problems I have encountered are:</p>
<ul>
<li>Excessive use of imports &#8211; context files import some other context files which in turn import more context files. I have seen the case where they then have circular dependencies as eventually they import a context file which references themselves.</li>
<li>Excessive use of bean overriding &#8211; this tends to be compounded by having lots of imports over which you have no control. You may have resources that you want to mock or stub out but they get pulled in by all the imports so you end up overriding beans left, right and centre. This isn&#8217;t that easy to get working correctly as the order in which the imports occur and the beans get overridden isn&#8217;t always obvious.</li>
<li>Huge test context files created &#8211; because of the problems of excessive imports and bean overriding I have seen developers copy and paste the small bits they want to test out of the real contexts and into a test context defining the stubs/mocks they then need. Now this is obviously a lot simpler for them as everything is in one context file and they can see what is happening. However it&#8217;s not much use to put it mildly, now you are only testing a test context and not what actually gets deployed. With this state of affairs it is easy to make a change to a context file or code that would break everything yet still have all the tests pass and only at deploy/run time do you discover there is an issue.</li>
</ul>
<p>So how can we avoid getting into such a pickle? I find that using a composite approach to building the Spring contexts works well. To explain what I mean by this let&#8217;s take an example.</p>
<p>Say we have a simple service that uses JPA to achieve it&#8217;s goals. We might have a big unwieldy spring context that looks like:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans
  xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  xsi:schemaLocation=&quot;
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd&quot;&gt;

 &lt;bean id=&quot;myService&quot;&gt;
   &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;myEntityManagerFactory&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean class=&quot;org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor&quot; /&gt;

 &lt;bean id=&quot;myEntityManagerFactory&quot;&gt;
   &lt;property name=&quot;persistenceUnitName&quot; value=&quot;my-persistence-unit&quot; /&gt;
   &lt;property name=&quot;dataSource&quot; ref=&quot;myDatasource&quot; /&gt;
   &lt;property name=&quot;jpaVendorAdapter&quot; ref=&quot;myHibernateJpaProps&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;myDatasource&quot;&gt;
   &lt;property name=&quot;driverClassName&quot; value=&quot;oracle.jdbc.OracleDriver&quot; /&gt;
   &lt;property name=&quot;url&quot; value=&quot;jdbc:oracle:thin:@localhost:1521:MYDATABASE&quot; /&gt;
   &lt;property name=&quot;username&quot; value=&quot;username&quot; /&gt;
   &lt;property name=&quot;password&quot; value=&quot;password&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;myHibernateJpaProps&quot;&gt;
   &lt;property name=&quot;databasePlatform&quot; value=&quot;org.hibernate.dialect.Oracle10gDialect&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;transactionManager&quot;&gt;
   &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;myEntityManagerFactory&quot; /&gt;
 &lt;/bean&gt;

 &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;
&lt;/beans&gt;</pre>
<p>Now this has everything in one big lump, so if in my test I don&#8217;t want to connect to an Oracle database then I would have to override the beans for the datasource and the hibernate properties.  What I would do in this situation is split the contexts into two (I would perhaps actually go for three, but the principle is the same), one for the service with the entity manager factory and then the other for the beans I am likely to want to stub out.  So now we have context 1:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans
  xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  xsi:schemaLocation=&quot;
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd&quot;&gt;

 &lt;bean id=&quot;myService&quot;&gt;
   &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;myEntityManagerFactory&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean class=&quot;org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor&quot; /&gt;

 &lt;bean id=&quot;myEntityManagerFactory&quot;&gt;
   &lt;property name=&quot;persistenceUnitName&quot; value=&quot;my-persistence-unit&quot; /&gt;
   &lt;property name=&quot;dataSource&quot; ref=&quot;myDatasource&quot; /&gt;
   &lt;property name=&quot;jpaVendorAdapter&quot; ref=&quot;myHibernateJpaProps&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;transactionManager&quot;&gt;
   &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;myEntityManagerFactory&quot; /&gt;
 &lt;/bean&gt;

 &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;
&lt;/beans&gt;</pre>
<p>Then in context 2:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans
  xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;&gt;

 &lt;bean id=&quot;myDatasource&quot;&gt;
   &lt;property name=&quot;driverClassName&quot; value=&quot;oracle.jdbc.OracleDriver&quot; /&gt;
   &lt;property name=&quot;url&quot; value=&quot;jdbc:oracle:thin:@localhost:1521:MYDATABASE&quot; /&gt;
   &lt;property name=&quot;username&quot; value=&quot;username&quot; /&gt;
   &lt;property name=&quot;password&quot; value=&quot;password&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;myHibernateJpaProps&quot;&gt;
   &lt;property name=&quot;databasePlatform&quot; value=&quot;org.hibernate.dialect.Oracle10gDialect&quot; /&gt;
 &lt;/bean&gt;

&lt;/beans&gt;</pre>
<p>Now what this means is that when we come to write a unit test we can use context 1 and then combine it with a test version of context 2 which uses an in memory HSQL database, like so:</p>
<pre class="brush: xml; gutter: false">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;beans
  xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;&gt;

 &lt;bean id=&quot;myDatasource&quot;&gt;
   &lt;property name=&quot;driverClassName&quot; value=&quot;org.hsqldb.jdbcDriver&quot; /&gt;
   &lt;property name=&quot;url&quot; value=&quot;jdbc:hsqldb:mem:MYINMEMORYDB&quot; /&gt;
   &lt;property name=&quot;username&quot; value=&quot;sa&quot; /&gt;
   &lt;property name=&quot;password&quot; value=&quot;&quot; /&gt;
 &lt;/bean&gt;

 &lt;bean id=&quot;myHibernateJpaProps&quot;&gt;
   &lt;property name=&quot;databasePlatform&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot; /&gt;
 &lt;/bean&gt;

&lt;/beans&gt;</pre>
<p>Using this approach we will end up with one context file at the top that composes all the spring context files it needs for an application, notice that this will be the only context that actually imports others.</p>
<p>It avoids excessive importing and there is no need for bean overriding as you make sure you include the context file that has the version you want. It allows you to test as much of your spring contexts as possible in unit tests before you have to then move on to integration tests or tests against a deployed system. This helps keep the build time and give you a shorter feedback loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=201</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails</title>
		<link>http://www.bssd.eu/blog/?p=191</link>
		<comments>http://www.bssd.eu/blog/?p=191#comments</comments>
		<pubDate>Mon, 24 Jan 2011 22:44:01 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=191</guid>
		<description><![CDATA[As I said in my previous post we have been using Grails a lot and think it&#8217;s great. I promise to blog a bit more about the things we are doing in Grails and how it is aiding our productivity. Some of the interesting things we have used are: Spring Security Core Plugin &#8211; for [...]]]></description>
			<content:encoded><![CDATA[<p>As I said in my previous post we have been using Grails a lot and think it&#8217;s great.</p>
<p>I promise to blog a bit more about the things we are doing in Grails and how it is aiding our productivity.</p>
<p>Some of the interesting things we have used are:</p>
<ul>
<li>Spring Security Core Plugin &#8211; for coarse grained security restricting URLs based on roles</li>
<li>Spring Security ACL Plugin &#8211; for finer grained security restricting users access to specific instances of objects</li>
<li>The Google Chart Plugin &#8211; for drawing fancy charts, obviously <img src='http://www.bssd.eu/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>The Eastwood Chart Plugin &#8211; to provide the functionality of Google Charts without calling out over the internet</li>
</ul>
<p>The security stuff was particularly interesting especially when implementing paging on objects restricted using ACL. I will blog about this next as there doesn&#8217;t seem to be too much out there about how to achieve it successfully.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=191</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Grails &amp; STS/Eclipse Integration</title>
		<link>http://www.bssd.eu/blog/?p=188</link>
		<comments>http://www.bssd.eu/blog/?p=188#comments</comments>
		<pubDate>Mon, 24 Jan 2011 22:35:20 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=188</guid>
		<description><![CDATA[Blimey it&#8217;s been a long time since I last blogged about anything. We have been very busy here with many projects having gone live and more in the pipeline. Since last blogging about JSF 2.0 and the new scopes we have done a couple of projects in Grails. I must admit that I am completely [...]]]></description>
			<content:encoded><![CDATA[<p>Blimey it&#8217;s been a long time since I last blogged about anything. We have been very busy here with many projects having gone live and more in the pipeline.</p>
<p>Since last blogging about JSF 2.0 and the new scopes we have done a couple of projects in Grails. I must admit that I am completely sold on Grails.</p>
<p>So much so that a project we were doing in JSF has been rewritten from scratch in Grails and progress has been at a far more impressive rate.</p>
<p>Development is being done using Springsource Tool Suite (STS) as it has Grails &amp; Groovy integration.</p>
<p>One problem though is we have commited the .classpath and .project files into the repository to allow other developers to set the project up in STS (or eclipse).</p>
<p>Obviously this isn&#8217;t ideal and if it were a maven project then we would just use the eclipse plugin to generate the appropriate files from the command line.</p>
<p>Today I found that Grails actually has a very similar tool which is invoked from the command line.</p>
<p>Simply type <code>grails integrate-with --eclipse</code> and voila, you now have the appropriate .classpath and .project files.</p>
<p>From that point on you can import the existing project into the workspace as normal.</p>
<p>See <a title="Grails &amp; STS integration" href="http://www.grails.org/STS+Integration" target="_blank">this</a> page for more information on integration with STS</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=188</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trying out JSF 2.0 &#8211; View scope &amp; Spring</title>
		<link>http://www.bssd.eu/blog/?p=185</link>
		<comments>http://www.bssd.eu/blog/?p=185#comments</comments>
		<pubDate>Mon, 19 Jul 2010 21:57:32 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=185</guid>
		<description><![CDATA[One of the limitations with JSF up to and including version 1.2 was the types of scope available for the backing beans. The three you had available were Request, Session and Application. In a lot of cases Request scope wasn&#8217;t adequate. An exampe is when rendering a table with command buttons on each row. When [...]]]></description>
			<content:encoded><![CDATA[<p>One of the limitations with JSF up to and including version 1.2 was the types of scope available for the backing beans. The three you had available were Request, Session and Application.</p>
<p>In a lot of cases Request scope wasn&#8217;t adequate. An exampe is when rendering a table with command buttons on each row. When the user clicked on the button the backing bean had been thrown away after the page had been rendered and so didn&#8217;t work correctly.</p>
<p>The workaround for this was you had to create all you beans as Session scope or you had to write some custom components to save away the state. Neither of which is ideal.</p>
<p>JSF 2.0 addresses this issue and adds two new scopes (plus the ability to add custom ones). They are View and Flash scope.</p>
<p>The View scope addresses exactly the problem of the table with command links. The bean is preserved until the user has finished with that particular view.</p>
<p>For a more detailed look at the new scopes and other features of JSF 2.0 I would recommend this <a title="What's new in JSF 2.0" href="http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#scopes-view" target="_blank">blog</a> post that I came across.</p>
<p>So View scope is the thing that I need. The only problem I now have is that I am using Spring to manage my beans for me rather than JSF.</p>
<p>Since View and Flash scope are specific to JSF and not Spring they are not available straight out of the box.</p>
<p>Fortunately it turns out to be easy to write a custom scope in Spring and register it with the context. I found this <a title="Converting JSF viewscope to Spring" href="http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/" target="_blank">blog</a> which details how to do it. The instructions are for Spring 3.0 but I was using version 2.5 and it&#8217;s practically identical.</p>
<p>It works a treat and I am now able to use View scope quite happily!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=185</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the maven eclipse plugin to configure your workspace</title>
		<link>http://www.bssd.eu/blog/?p=180</link>
		<comments>http://www.bssd.eu/blog/?p=180#comments</comments>
		<pubDate>Mon, 19 Jul 2010 10:16:49 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=180</guid>
		<description><![CDATA[I find that the maven eclipse plugin m2 is far more hassle than it is worth and so I never use it. Instead I build my projects from the command line and then use the maven eclipse plugin to update the projects and classpaths when required. One niggle of this approach is when creating a [...]]]></description>
			<content:encoded><![CDATA[<p>I find that the maven eclipse plugin m2 is far more hassle than it is worth and so I never use it. Instead I build my projects from the command line and then use the maven eclipse plugin to update the projects and classpaths when required.</p>
<p>One niggle of this approach is when creating a new workspace sometimes eclipse is unaware of the location of the maven repository and will report many build errors.</p>
<p>This can be fixed from the command line by running the following:</p>
<p style="text-align: left;"><code>mvn eclipse:configure-workspace -Declipse.workspace=&lt;path to workspace&gt;</code></p>
<p>Note that previously you could use the eclipse:add-maven-repo goal but this has since been deprecated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=180</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Piping in a file to SQL Server from the command line</title>
		<link>http://www.bssd.eu/blog/?p=176</link>
		<comments>http://www.bssd.eu/blog/?p=176#comments</comments>
		<pubDate>Thu, 28 Jan 2010 20:46:50 +0000</pubDate>
		<dc:creator>andystewart</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bssd.eu/blog/?p=176</guid>
		<description><![CDATA[SQL Server Management Studio doesn&#8217;t like big SQL scripts. So much so that it just tends to fall over with an error. Many thanks to Jamie for pointing me at his blog where he has the command to pipe in the file from the command line, which works a treat!]]></description>
			<content:encoded><![CDATA[<p>SQL Server Management Studio doesn&#8217;t like big SQL scripts. So much so that it just tends to fall over with an error.</p>
<p>Many thanks to Jamie for <a href="http://knowledge.jamiehinton.co.uk/piping-in-a-file-to-sql-server-2005-on-the-command-line/">pointing me at his blog</a> where he has the command to pipe in the file from the command line, which works a treat!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bssd.eu/blog/?feed=rss2&amp;p=176</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

