<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Marten Deinum</title>
	<atom:link href="http://mdeinum.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mdeinum.wordpress.com</link>
	<description>Java, Spring, Web Flow rants</description>
	<lastBuildDate>Fri, 02 Oct 2009 17:10:45 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='mdeinum.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/c4b5f704e320f82c29a87ad690f5a474?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Marten Deinum</title>
		<link>http://mdeinum.wordpress.com</link>
	</image>
			<item>
		<title>Migrate classic J2EE to Spring (Step 1) (revised)</title>
		<link>http://mdeinum.wordpress.com/2009/10/02/migrate-classic-j2ee-to-spring-step-1-revised/</link>
		<comments>http://mdeinum.wordpress.com/2009/10/02/migrate-classic-j2ee-to-spring-step-1-revised/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 17:10:45 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Conversion]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Migration]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/?p=51</guid>
		<description><![CDATA[A couple of months ago I wrote a step 1 on migrating a classis J(2)EE application to a spring based application. Recently I had some time again on my hands and after studying the application and also after some discussions I had I decided to structure the project a little different. 
&#8220;External&#8221; applications
The Adventure Builder [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=51&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A couple of months ago I wrote a <a href="http://mdeinum.wordpress.com/2009/05/06/migrate-classic-j2ee-to-spring/">step 1 on migrating a classis J(2)EE application to a spring based application</a>. Recently I had some time again on my hands and after studying the application and also after some discussions I had I decided to structure the project a little different. </p>
<h4>&#8220;External&#8221; applications</h4>
<p>The Adventure Builder applications uses 4 other applications to deliver its services. However in a normal real situation those 4 applications are outside of our control. We cannot change them nor redeploy them. So I decided to just include the ear files for those 4 projects instead of rebuilding them each time.</p>
<h4>Generated stubs/skeletons</h4>
<p>The application makes use of generated java and xml files. Also in a real situation you should generate those once and after that reuse. You should only (re)generate those files if the external interface changes (best would be to not generate at all). I decided to use the generated java files and xml files and include them in the project. They can be found in the src/generated directory.</p>
<h4>Deployment</h4>
<p>Deployment is now maven based, maven can start and create a glassfish domain for you. It will create all jms/jdbc/mail resources and deploy the 4 external applications. After that setup you can choose to start glassfish with the normal startServ command or reuse maven to start it. Simply run the setup.bat/setup.sh file from the setup directory. To deploy our own 2 applications simply go to the apps directory and type mvn glassfish:deploy.</p>
<h4>Testing</h4>
<p>The initial project as is has some test classes available so that we can make sure that the application still behaves as it should behave after we are going to change it to use Spring and Hibernate.</p>
<p>Hopefully Step 2 will follow shortly and that we can have a look at the different steps and easier code.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/51/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/51/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/51/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=51&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2009/10/02/migrate-classic-j2ee-to-spring-step-1-revised/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring Web Flow 2 Web Development</title>
		<link>http://mdeinum.wordpress.com/2009/05/25/spring-web-flow-2-web-development/</link>
		<comments>http://mdeinum.wordpress.com/2009/05/25/spring-web-flow-2-web-development/#comments</comments>
		<pubDate>Mon, 25 May 2009 06:59:27 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Reviews]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/?p=44</guid>
		<description><![CDATA[A few weeks ago I was contacted by packt publishing to review one of their books. They asked me if I wanted to review Spring Web Flow 2 Web Development (sample chapter).
In short the book consists of 254 pages in all those pages they try to explain Spring Web Flow, Maven, Ant+Ivy, Spring Security and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=44&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A few weeks ago I was contacted by <a href="http://www.packtpub.com">packt publishing</a> to review one of their books. They asked me if I wanted to review <a href="http://www.packtpub.com/develop-powerful-web-applications-with-spring-web-flow-2/book">Spring Web Flow 2 Web Development</a> (sample <a href="http://www.packtpub.com/files/spring-web-flow-2-web-development-sample-chapter-4-spring-faces.pdf">chapter</a>).</p>
<p>In short the book consists of 254 pages in all those pages they try to explain Spring Web Flow, Maven, Ant+Ivy, Spring Security and some basics (and also not all basics) about the Spring Framework it self. Ofcourse not to mention all the normal stuff like title pages and indexes. All the explaining of the added frameworks takes away from the actual goal and that is to cover and explain Spring Web Flow. I also found that there is no real layering/build up in the book, they directly start with FlowExecutionListeners even before they covered the basics. Another draw back is the samples used, they don&#8217;t really use one sample (application) but different snippets which make it harded for a new Spring Web Flow user to get an overall feel. So the short verdict I wouldn&#8217;t say the book is bad but it could be a lot better, most of the information to be found in the book but it can be quite a search at times. If you are a Spring Web Flow beginner I would try to find other books covering the basics of Spring Web Flow.<br />
<span id="more-44"></span><br />
After the short version here is the somewhat longer version a chapter by chapter brief coverage of the book. The opinions here are my own and are quite influenced by the fact that I have written some initial code for Spring Web Flow and Spring Security integration and that I was part of the team that wrote <a href="http://www.springsource.com/training/rwa001">the training on Spring Web Flow</a>.</p>
<p>The book starts with a short and quick introduction to the 3 pillars/corners stones and briefly discusses the changes between Spring Web Flow 1 and 2. The chapter is a bit confusing due to the fact that they use flow and conversation interchangeably. There are some small parts that could be improved here but all in all not a bad chapter.</p>
<p>The second chapter discusses the setup to run the examples which come with the book. They explain it using maven, ant+ivy and how to do it with various IDEs. They also give a &#8216;basic&#8217; sample of a flow, however they right away start with FlowExecutionListeners, all the different states that there are and try to explain it. However the explanation is to brief (allthough everything is covered in the book). I would have sticked with 1 build solution and 1 IDE. That would also make it easier to read and understand, now with each sample the book covers both maven and ant+ivy. Also showing a sample with everything included and calling it &#8216;a basic sample&#8217; isn&#8217;t the best thing to do. The sample flow also uses depracted states (action-state is basically deprecated) and doesn&#8217;t use/follow best practices. Overall everything is in the chapter but it is either to much or to little.</p>
<p>The third chapter explains the basics of Spring Web Flow. The chapter start with the explanation of how xml is composed and after that starts with explaining the persistence context and FlowExecutionListeners. Both not really &#8216;basics of Spring WebFlow&#8217;. Halfway the chapter they start explaining the basics, the different states, the different scopes, how the variable resolution mechanism works etc. Basically they try to explain everything about Spring Web Flow with jsp in 1 chapter and they don&#8217;t really explain it in the right order. Some parts could have been in more detail others are in to much detail. Also parts that would be interesting, like how to influence binding and how to do type conversion, aren&#8217;t covered. In my opinion this chapter lacks what it needs.</p>
<p>The fourth chapter is about integration JSF with Spring Web Flow. They again briefly explain how to do it and they try to explain how to integrate 2 different JSF frameworks with Spring Web Flow. However the latter removes us from the real objective which is the Spring Faces module and how to use it. Some of the code and things they explain aren&#8217;t really true (like the custom ResourceServlet you would need!), next to that they are also trying to explain how the Spring Namespace support works which doesn&#8217;t really apply nor does it add something here.</p>
<p>The fifth chapter covers AJAX. They brush on element decoration and progressive enhancement, however they don&#8217;t really explain why one should use it. The book covers the configuration and usage of Spring Javascript and next delves into partial page rendering with the use of Apache Tiles. The beginning of the chapter is pretty good however they end with explaining the whole Spring Web Flow configuration xsd. This is better left to the <a href="http://static.springframework.org/spring-webflow/docs/2.0.x/reference/html/index.html">reference guide</a> or in an appendix in the book. The chapter starts off good however seems/feels likes to be rushed to an end with explaining the xsd which also feels oddly out of place here.</p>
<p>Chapter six covers testing and the testing of subflows. One of the best chapters in the book, it covers testing pretty good and also touches on the subject of mocking your objects and perform tests. Confusing in this chapter arises due to the use of 2 different versions of the testing framework, stick with 1 version I would say. </p>
<p>Chapter seven dives into Spring Security and using that to secure your flows. Again they try to explain Spring Security and the mix with Spring Web Flow in 1 chapter, this doesn&#8217;t really work. They explain to little and in places don&#8217;t do justice to what Spring Security can do. Not all to bad but it again coud have been better then what it is.</p>
<p>A lot of stuff gets covered in this book but it doesn&#8217;t read pretty easy. The beginning of the book directly covers FlowExecutionListeners even before explaining the real basics of Spring Web Flow. If you need something it probably is in there but not in the most likely location. They also use different samples through out the book, if they would have used just one sample application it would have been easier to understand and to give everything a place. The code samples are at times also confusing and use different styles (jsp 2 and jsp 1 syntax, use of deprecated code, incomplete/incorrect code samples etc) and sometimes use different versions of the same framework. Things that would have been good to explain aren&#8217;t explained (binding, object conversion).  </p>
<p>All of this leaves me no choice then to not recommend this book especially if you are a Spring Web Flow beginner. Try to find another book there are probably better ones out there.  </p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/44/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/44/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/44/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=44&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2009/05/25/spring-web-flow-2-web-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Migrate classic J2EE to Spring (Step 1)</title>
		<link>http://mdeinum.wordpress.com/2009/05/06/migrate-classic-j2ee-to-spring/</link>
		<comments>http://mdeinum.wordpress.com/2009/05/06/migrate-classic-j2ee-to-spring/#comments</comments>
		<pubDate>Wed, 06 May 2009 11:10:45 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Conversion]]></category>
		<category><![CDATA[Migration]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/?p=21</guid>
		<description><![CDATA[A few months ago SpringSource released a white paper describing the migration from J2EE to a Spring framework based application. Even before they wrote that white paper I already had the idea of writing something about how to migrate from J2EE to a Spring based application. However Colin Sampaleanu (at al.) beat me to writing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=21&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A few months ago <a href="http://www.springsource.com">SpringSource </a>released <a href="http://www.springsource.com/files/MigratingAppsToSpring.pdf">a white paper</a> describing the migration from J2EE to a Spring framework based application. Even before they wrote that white paper I already had the idea of writing something about how to migrate from J2EE to a Spring based application. However Colin Sampaleanu (at al.) beat me to writing the white paper <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>However I also had the idea of providing some practical information like a sample. The white paper gives some thought on what to do and where but it doesn&#8217;t give a clear sample. So I started writing a possible migration path (multi step sample) from a J2EE application to a Spring based application.<br />
<span id="more-21"></span><br />
The code is available in a <a href="http://springstore.googlecode.com">repository hosted on googlecode</a>. Please feel free to checkout the code (for each step there will be a branch/tag). If you have suggestions/issues with the code also please feel free to register an issue or supply a patch. </p>
<p>For this sampe I took the SUN blueprint application (I focused on J2EE 1.4 because that is widely used and adopted and there is still a lot of &#8216;legacy&#8217; out there build with the 1.4 spec) called the &#8216;<a href="http://java.sun.com/developer/releases/adventure">Adventure Builder</a>&#8216;. To make it a easier to build and migrate I started migrating the application to a maven build (instead of the asant build). </p>
<p>In the next few post I&#8217;ll explain the migration strategy, the different steps and here and there some best practices which later on will ease the migration path. I&#8217;m not going to migrate all the applications (the Adventure Builder sample consists of several applications), I&#8217;m focussing on the &#8216;Order Processing Module&#8217; and later on the &#8216;Consumer Website&#8217; application all of this without changing the other applications (or with minor changes).</p>
<p>Development for this project is done in the <a href="http://www.springsource.com/products/suite/sts">SpringSource Toolsuite</a>. Deployment is done in the &#8216;<a href="http://www.glassfish.org">Glassfish</a>&#8216; application server. To make it easier to build I first migrated the blueprint application to a <a href="http://maven.apache.org">maven</a> based build.</p>
<p>To build, install and deploy the application follow the following steps (after checking out the code from svn).</p>
<ol>
<li>goto the parent project inside the springstore application</li>
<li><code>mvn clean -Dskip.resources=true</code> (skip.resources disables deletion of the jms/jdbc resources in glassfish)</li>
<li><code>mvn glassfish:start-domain</code> (start glassfish)</li>
<li><code>mvn install</code> (will build the ear files, and generate the jms/jdbc resources and fills the test database)</li>
<li><code>mvn glassfish:deploy</code></li>
</ol>
<p>Afterwards the applications is available on your <a href="http://localhost:8080/ab">localhost</a> just play around with the application and have a look at the code. (If you want you can compare it to the original Adventure Builder application it should be more or less the same. </p>
<p>If you want to undeploy the applications simply type <code>glassfish:undeploy</code></p>
<p>I still have <a href="http://code.google.com/p/springstore/issues/list?can=2&amp;q=label%3AComponent-Scripts">a few issues with the maven based build</a>, so if someone can help/advice on that please let <a href="mailto:mdeinum@gmail.com">me</a> know.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=21&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2009/05/06/migrate-classic-j2ee-to-spring/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuring JNDI Resources in Tomcat</title>
		<link>http://mdeinum.wordpress.com/2008/01/14/configuring-jndi-resources-in-tomcat/</link>
		<comments>http://mdeinum.wordpress.com/2008/01/14/configuring-jndi-resources-in-tomcat/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 13:40:54 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[jndi]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2008/01/14/configuring-jndi-resources-in-tomcat/</guid>
		<description><![CDATA[It seems quite hard to configure a JNDI Resource in Tomcat. Especially when it comes to configuring XA capable resources. The key lies in understanding how Resources work/need to be configured.

A Resource has a few properties which are used by Tomcat, those are the auth, name, description, scope and type, from those name and type [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=19&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>It seems quite hard to configure a <a href="http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html">JNDI Resource</a> in <a href="http://tomcat.apache.org">Tomcat</a>. Especially when it comes to configuring XA capable resources. The key lies in understanding how Resources work/need to be configured.<br />
<span id="more-19"></span><br />
A <a href="http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Resource%20Definitions">Resource</a> has a few properties which are used by Tomcat, those are the <u>auth</u>, <u>name</u>, <u>description</u>, <u>scope</u> and <u>type</u>, from those <b>name</b> and <b>type</b> are required. Type is the actual type (fully qualified classname) it needs to create and the name is the jndi name relative to java:comp/env.</p>
<p>All the other properties specified on the Resource element are either used by the factory or the type created by the factory. So if you specify a property named &#8216;username&#8217; in your Resource then there must be a get/set pair for that property on the type (i.e. setUsername/getUsername).</p>
<p>If we look at the mysql sample from the Tomcat documentation.</p>
<pre class="brush: xml;">
&lt;Resource name=&quot;jdbc/TestDB&quot; auth=&quot;Container&quot; type=&quot;javax.sql.DataSource&quot;
          maxActive=&quot;100&quot; maxIdle=&quot;30&quot; maxWait=&quot;10000&quot;
          username=&quot;javauser&quot; password=&quot;javadude&quot;
          driverClassName=&quot;com.mysql.jdbc.Driver&quot;
          url=&quot;jdbc:mysql://localhost:3306/javatest?autoReconnect=true&quot;/&gt;
</pre>
<p>We register a javax.sql.DataSource with the name jdbc/TestDB and the authentication is done by the Container. Now if we look further we see a few properties.</p>
<ul>
<li>maxActive</li>
<li>maxIdle</li>
<li>maxWait</li>
<li>username</li>
<li>password</li>
<li>driverClassName</li>
<li>url</li>
</ul>
<p>If we don&#8217;t specify an explicit type and/or a factory Tomcat uses its default implementation which is <a href="http://commons.apache.org/dbcp/">Apache Commons DBCP</a>. For the default javax.sql.DataSource it uses also the default/basic implementation of that library, it uses <a href="http://commons.apache.org/dbcp/api-1.2.2/org/apache/commons/dbcp/BasicDataSource.html">BasicDataSource</a>. If you study the javadoc of that class you will see a getter/setter pair for each of the properties named above. </p>
<p>Now lets put into practice all what we have learned here and try to configure a Mysql XA Capable datasource. We first need to specify the specific factory to use else the default factory will be used. The factory to use is the <a href="http://www.docjar.com/docs/api/com/mysql/jdbc/jdbc2/optional/MysqlDataSourceFactory.html">MysqlDataSourceFactory</a></p>
<p>Next we need to specify the type the factory needs to instantiate, we want a MySQL XA DataSource and thus we need to instantiate<em> com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</em>. A list of the configurable properties can be found in the MySQL <a href="http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html">reference documentation</a>.</p>
<pre class="brush: xml;">
&lt;Resource name=&quot;jdbc/TestDB&quot; auth=&quot;Container&quot;
          type=&quot;com.mysql.jdbc.jdbc2.optional.MysqlXADataSource&quot;
          factory=&quot;com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory&quot;
          user=&quot;javauser&quot; password=&quot;javadude&quot; explicitUrl=&quot;true&quot;
          url=&quot;jdbc:mysql://localhost:3306/javatest?autoReconnect=true&quot;/&gt;
</pre>
<p>And there we have it a configured XA Capable Mysql datasource. I think the MysqlDataSourceFactory can be improved a lot the same goes for the javadocs for the connector. However with a look at the sourcecode and a good understanding on how JNDI Resources can be configured in Tomcat it is quite easy to figure out how Resources can be configured.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/19/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/19/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=19&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2008/01/14/configuring-jndi-resources-in-tomcat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Securing Spring Web Flow</title>
		<link>http://mdeinum.wordpress.com/2007/06/13/securing-spring-web-flow/</link>
		<comments>http://mdeinum.wordpress.com/2007/06/13/securing-spring-web-flow/#comments</comments>
		<pubDate>Wed, 13 Jun 2007 09:58:31 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Security]]></category>
		<category><![CDATA[Spring Web Flow]]></category>
		<category><![CDATA[acegi]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[web flow]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2007/06/13/securing-spring-web-flow/</guid>
		<description><![CDATA[
Well that is the title of the presentation I just gave at the NL-JUG 2007 conference. The presentation was about the Spring Web Flow solution we created to secure flows. The presentation can be found and the conference website. The code is available in the JIRA under issue SWF-93 but for your convenience also available [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=18&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><!-- Traffic Statistics --><br />
Well that is the title of the presentation I just gave at the <a href="http://www.nl-jug.org">NL-JUG 2007</a> conference. The presentation was about the <a href="www.springframework.org/webflow">Spring Web Flow</a> solution we created to secure flows. The presentation can be found and the conference website. The code is available in the JIRA under issue <a href="http://opensource.atlassian.com/projects/spring/browse/SWF-93">SWF-93</a> but for your convenience also available for download (press the link on the bottom of this post).</p>
<p>The presentation went well, there were some nice questions and a little bit of discussion. So my conclusion was that it was a nice presentation. I will blog about using this security solutions shortly. However for now I have a conference to attend.</p>
<p><a href="http://blog.deinum.biz/wp-content/uploads/2007/06/spring-webflow-security.zip" title="WebFlow Security">WebFlow Security</a><br />
<!-- End Traffic Statistics --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/18/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/18/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=18&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2007/06/13/securing-spring-web-flow/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Converting String to Date and Date-formatting in Spring-MVC</title>
		<link>http://mdeinum.wordpress.com/2007/02/09/converting-string-to-date-and-date-formatting-in-spring-mvc/</link>
		<comments>http://mdeinum.wordpress.com/2007/02/09/converting-string-to-date-and-date-formatting-in-spring-mvc/#comments</comments>
		<pubDate>Fri, 09 Feb 2007 09:24:04 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2007/02/09/converting-string-to-date-and-date-formatting-in-spring-mvc/</guid>
		<description><![CDATA[This week alone I answered the question about date formatting and how to bind Strings to Objects multiple times. If I would get an euro/dollar for everytime I gave the same answer. So I figured maybe it is time to create simple example to show how it is done in Spring. (The sources used are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=16&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>This week alone I answered the question about date formatting and how to bind Strings to Objects multiple times. If I would get an euro/dollar for everytime I gave the same answer. So I figured maybe it is time to create simple example to show how it is done in Spring. (The sources used are included in the <a href="http://www.deinum.biz/wp-content/uploads/2007/02/propertyeditors.zip" id="p12">propertyeditors.zip</a> attached to this post, rename to .war to deploy it in tomcat or jetty (tested in Jetty 6.0.2))<br />
<span id="more-16"></span><br />
For this project we will only use a WebApplicationContext so the only thing we need to do is configure the Spring DispatcherServlet.</p>
<pre class="brush: xml;">
Step-By-Step
sbs
org.springframework.web.servlet.DispatcherServlet
1

sbs
*.do

index.jsp
</pre>
<p>As you see we mapped all the *.do to the dispatcher servlet. Problem with the servlet spec is that the welcome page has to be a physical file. So the index.jsp only does a redirect to index.do. The Servlet is named <strong>sbs</strong>. As with the Spring convention we then need to create <strong>sbs-servlet.xml</strong> to get the context loaded. For this example I use the default BeanNameUrlHandlerMapping, which resolve an URI to a Controller.</p>
<p>We also need something to resolve our views (a ViewResolver) so I configured an InternalResourceViewResolver, which looks up jsp in the WEB-INF/jsp directory.</p>
<p>[sourcode language='xml']<br />
xmlns:xsi=&#8221;http://www.w3.org/2001/XMLSchema-instance&#8221;<br />
xsi:schemaLocation=&#8221;<br />
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&#8221;&gt;</p>
<p>[/code]Well that is it, configuration wise. We have a controller, with a form and a validator, we have 2 views (entry and success).</p>
<p>The controller isn't that big of a deal, it extends simpleformcontroller and only implements the initBinder method. For binding to other objects then Strings or primitives we need a PropertyEditor. Luckily for us Spring comes shipped with some PropertyEditors. For this example we will use the CustomDatePropertyEditor. The PropertyEditor will convert a String to a Date and vice-versa.</p>
<pre class="brush: java;">
public class DateEntryController extends SimpleFormController {
  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;dd-MM-yyyy&quot;);
    CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
    binder.registerCustomEditor(Date.class, editor);
  }
}
</pre>
<p>The <a href="http://www.springframework.org/docs/api/org/springframework/web/servlet/mvc/BaseCommandController.html#initBinder(javax.servlet.http.HttpServletRequest,%20org.springframework.web.bind.ServletRequestDataBinder)">initBinder</a> method as mentioned here, registers a <a href="http://www.springframework.org/docs/api/org/springframework/beans/propertyeditors/CustomDateEditor.html">CustomDateEditor</a> which takes a text in the format (dd-MM-yyyy) and transforms that into a valid date.</p>
<p>the line '<em>binder.registerCustomEditor(Date.class, editor);</em>' tells the binder to register this propertyeditor for all the Date properties on the command object. If there is a need for multiple property editors for the same type you can also specify the name of the property on the command object.</p>
<p>The Validator is a simple one (just to generate some errors and show that valid dates are translated back into strings).</p>
<pre class="brush: java;">
public class DateEntryValidator implements Validator {

  public boolean supports(Class clazz) {
    return DateEntryFormObject.class.isAssignableFrom(clazz);
  }

  public void validate(Object target, Errors errors) {
    DateEntryFormObject commandObject = (DateEntryFormObject) target;
    Date startDate = commandObject.getStartDate();
    Date endDate = commandObject.getEndDate();

    if (startDate == null) {
      errors.rejectValue(&quot;startDate&quot;, &quot;required&quot;, &quot;Starting Date is required.&quot;);
    }

    if (startDate != null &amp;amp;&amp;amp; endDate != null &amp;amp;&amp;amp; endDate.before(startDate)) {
      errors.rejectValue(&quot;endDate&quot;, &quot;notbefore.startdate&quot;, &quot;End date cannot be before start date.&quot;);
    }
  }
}
</pre>
<p>Well that's all there is. The only remaining things are the 2 jsps and the command object.</p>
<pre class="brush: java;">
public class DateEntryFormObject implements Serializable {

  private Date startDate;
  private Date endDate;

  public Date getEndDate() {
    return endDate;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }

  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
}
</pre>
<p>entry.jsp</p>
<pre class="brush: xml;">
&lt;%@taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot;&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;3&quot;&gt;Enter dates in format (dd-MM-yyyy)&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Start Date:&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;End Date:&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td colspan=&quot;3&quot;&gt;&lt;input value=&quot;Submit dates&quot; name=&quot;enter&quot; type=&quot;submit&quot; /&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</pre>
<pre class="brush: xml;">
&lt;%@taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot;&gt;
&lt;table&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;2&quot;&gt;&lt;h2&gt;Dates entered&lt;/h2&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Start Date:&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;End Date:&lt;/td&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</pre>
<p>There is just one important thing to remember PropertyEditors work only for the command object, not for reference data, or objects stored somewhere else. For those to format correctly you will need the good ol' fmt tags.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=16&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2007/02/09/converting-string-to-date-and-date-formatting-in-spring-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>One application, per client database</title>
		<link>http://mdeinum.wordpress.com/2007/01/05/one-application-per-client-database/</link>
		<comments>http://mdeinum.wordpress.com/2007/01/05/one-application-per-client-database/#comments</comments>
		<pubDate>Fri, 05 Jan 2007 14:45:13 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[spring aop]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2007/01/05/one-application-per-client-database/</guid>
		<description><![CDATA[In one of my recent projects we came a cross an application model which had 1 codebase but for every client they had (around 40) they deployed one application. Sometimes they had to redeploy several times because they had memory and performance issues. We soon realized that we needed to do something about this way [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=15&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In one of my recent projects we came a cross an application model which had 1 codebase but for every client they had (around 40) they deployed one application. Sometimes they had to redeploy several times because they had memory and performance issues. We soon realized that we needed to do something about this way of deploying. The only two things which where different per client where the database connection and the front-end.<br />
<span id="more-15"></span></p>
<p><strong>Front-end</strong><br />
Well the front-end is simple enough there are enough templating engines around which you can use. We decided on using <a href="http://www.opensymphony.com/sitemesh/">sitemesh</a> this in combination with JSTL gave us all the power we needed.</p>
<p><strong>Database connection</strong><br />
But what about the database connections. We had 40 of them configured in our tomcat context file. We figured we needed something like the <a href="http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/aop/target/HotSwappableTargetSource.html">HotSwappableTargetSource</a>. The challenge was how do we decide for which client we need to process something. The application has urls like http://www.ourcomp.com/client1 and http://www.ourcomp.com/client2 etc. We created a filter which extracted the client1 part from the URL and put that in a ContextHolder (which is a ThreadLocal).</p>
<pre class="brush: java;">
public abstract class ContextHolder {

  private static final ThreadLocal holder = new ThreadLocal();

  public static void setContext(String context) {
    LoggerFactory.getLogger(ContextHolder.class).debug(&quot;context set '{}'&quot;, context);
    holder.set(context);
  }

  public static String getContext() {
    return (String) holder.get();
  }
}
</pre>
<p>Now we had the context to use in a property we could retrieve anywhere. So next we took the idea of the HotSwappableTargetSource and adapted it for our situation. We created a ContextSwappableTargetSource, we need to create it with the targetClass is provides (in our case a javax.sql.DataSource) and with a map of DataSources to use. The keys in the map correspond to the context (or clientnames) used in the url.</p>
<pre class="brush: java;">
/**
* TargetSource which returns the correct target based on the current context set in the {@link biz.deinum.springframework.core.ContextHolder}.
* If no context is found a {@link TargetLookupFailureException} is thrown or the &lt;code&gt;defaultTarget&lt;/code&gt; is returned
* , depending on the setting of the alwaysReturnTarget property (default is false);
*
* @author M. Deinum
* @version 1.0
* @see ContextHolder
* @see TargetSource
*/
public class ContextSwappableTargetSource implements TargetSource, InitializingBean {
  private final Logger logger = LoggerFactory.getLogger(ContextSwappableTargetSource.class);
  private Map targets = Collections.synchronizedMap(new HashMap());
  private Class targetClass;
  private boolean alwaysReturnTarget = false;
  private Object defaultTarget;

  /**
  * Constructor for the {@link ContextSwappableTargetSource} class. It takes a
  * Class as a parameter.
  *
  * @param targetClass The Class which this TargetSource represents.
  */
  public ContextSwappableTargetSource(Class targetClass) {
    super();
    this.targetClass=targetClass;
  }

  /**
  * Locate and return the sessionfactory for the current context.
  *
  * First we lookup the context name from the {@link ContextHolder}
  * this context name is used to lookup the desired target. When none
  * is found we return the default target.
  *
  * If the targetClass is of a invalid type we throw a {@link BeanNotOfRequiredTypeException}
  *
  * @see ContextHolder
  */
  public Object getTarget() throws Exception {
    // Determine the current context name from theclass that holds the
    // context name for the current thread.
    String contextName = ContextHolder.getContext();
    logger.debug(&quot;Current context: '{}'&quot;, contextName);

    Object target = targets.get(contextName);
    if (target == null &amp;&amp; alwaysReturnTarget) {
      logger.debug(&quot;Return default target for context '{}'&quot;, contextName);
      target = defaultTarget;
    } else if (target == null &amp;&amp; !alwaysReturnTarget){
      logger.error(&quot;Cannot locate a target of type '{}' for context '{}'&quot;, targetClass.getName(), contextName);
      throw new TargetLookupFailureException(&quot;Cannot locate a target for context '&quot;+contextName+&quot;'&quot;);
    }

    if (!targetClass.isAssignableFrom(target.getClass())) {
      throw new TargetLookupFailureException(&quot;The target for '&quot;+contextName+&quot;' is not of the required type.&quot; + &quot;Expected '&quot;+targetClass.getName()+&quot;' and got '&quot;+target.getClass().getName()+&quot;'&quot;);
    }
    return target;

  }

  public final Class getTargetClass() {
    return targetClass;
  }

  public final boolean isStatic() {
    return false;
  }

  public void releaseTarget(Object arg0) throws Exception {}

  public final void afterPropertiesSet() throws Exception {
    Assert.notNull(targetClass, &quot;TargetClass property must be set!&quot;);

    if (alwaysReturnTarget &amp;&amp; defaultTarget == null) {
      throw new IllegalStateException(&quot;The defaultTarget property is null, while alwaysReturnTarget is set to true. &quot; + &quot;When alwaysReturnTarget is set to true a defaultTarget must be set!&quot;);
    }
  }

  public final void setAlwaysReturnTarget(final boolean alwaysReturnTarget) {
    this.alwaysReturnTarget=alwaysReturnTarget;
  }

  public final void setDefaultTarget(final Object defaultTarget) {
    this.defaultTarget=defaultTarget;
  }

  public final void setTargets(final Map targets) {
    this.targets.clear();
    this.targets.putAll(targets);
  }
}
</pre>
<p>All the classes are in place, now we only needed to wire things up in our application context and we should be good to go. First we configure the datasources.</p>
<p>Next we need to setup the ContextSwappableTargetSource, the key in the map is the value which is going to be set in the ContextHolder. In a WebApplication this value could be set by a ServletFilter on each request. The TargetSource is wrapped in a ProxyFactoryBean so a Proxy will be created for the ContextSwappableTargetSource.</p>
<p>And that is it. Now inject the datasourceTargetSource into a JdbcTemplates datasource property and you are good to go.</p>
<p>At every request the context is set in the ContextHolder. Then at every action on the JdbcTemplate the getTarget method on the ContextSwappableTargetSource is called returning the real and correct datasource instance for that request.</p>
<p>In this example we used it to dynamically replace DataSource instances but this can work with in theory every object. We have also succesfully used it with multiple hibernate SessionFactories.</p>
<p>The source code (as available on posttime) can be found <a href="http://www.deinum.biz/2007/01/05/one-application-per-client-database/contextswappabletargetsourcezip/" id="p10" rel="attachment" title="contextswappabletargetsource.zip">here</a>. Or point yuor favorite SVN client to <a href="http://bespring.googlecode.com/svn/trunk/">http://bespring.googlecode.com/svn/trunk/</a>. I also submitted this to the Spring framework in <a href="http://opensource.atlassian.com/projects/spring/browse/SPR-3014">JIRA issue SPR-3014</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=15&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2007/01/05/one-application-per-client-database/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Spring Web Flow Tag Library</title>
		<link>http://mdeinum.wordpress.com/2006/12/21/spring-web-flow-tag-library/</link>
		<comments>http://mdeinum.wordpress.com/2006/12/21/spring-web-flow-tag-library/#comments</comments>
		<pubDate>Thu, 21 Dec 2006 09:58:27 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Web Flow]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[tag library]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[web flow]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2007/12/20/spring-web-flow-tag-library/</guid>
		<description><![CDATA[A while ago we started to use Spring Web Flow. We needed to convert our old WizardForms and Multi SimpleFormController screens to the Spring Web Flow ones. After converting about three jsp&#8217;s I got fed up with the hidden fields, the submit buttons with the specified name. Generating urls with a flowExecutionKey and eventId was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=17&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>A while ago we started to use <a href="http://opensource.atlassian.com/confluence/spring/display/WEBFLOW/Home">Spring Web Flow</a>. We needed to convert our old WizardForms and Multi SimpleFormController screens to the Spring Web Flow ones. After converting about three jsp&#8217;s I got fed up with the hidden fields, the submit buttons with the specified name. Generating urls with a flowExecutionKey and eventId was even worse. Also after making al those typos in flowExecutionKey I decided to create a taglibrary which can write different HTML tags needed in Spring Web Flow.<br />
<span id="more-17"></span><br />
So after experimenting a bit I wrote three tags:</p>
<ul>
<li>FlowExecutionKeyTag: Writes hidden field with the flowExecutionKey</li>
<li>UrlTag: Writes a url containing the flowExecutionKey and eventId</li>
<li>SubmitTag: Writes a submit button and optional a hidden flowExecutionKey field</li>
</ul>
<p><strong>FlowExecutionKeyTag</strong></p>
<pre class="brush: xml;">
&lt;swf:flowexecutionkey/&gt;
</pre>
<p>will generate</p>
<pre>
&lt;input type="hidden" name="_flowExecutionKey" value="&lt;current_flowexecutionkey&gt;"&gt;</pre>
<p><strong>UrlTag</strong></p>
<pre class="brush: xml;">
&lt;swf:url url=&quot;somelink.flow&quot; eventId=&quot;read&quot; paramName=&quot;someId&quot; value=&quot;${someObject.id}&quot;&gt;Link&lt;/swf:url&gt;&lt;/pre&gt;
</pre>
<p>will generate</p>
<pre>
&lt;a href="somelink.flow?_flowExectionKey=&lt;current_flowexecutionkey&gt;&amp;_eventId=read&amp;someId=123"&gt;Link&lt;/a&gt;</pre>
<p><strong>SubmitTag</strong></p>
<pre class="brush: xml;">
&lt;swf:submit eventId=&quot;save&quot; value=&quot;Save&quot;/&gt;
</pre>
<p>will generate</p>
<pre>
&lt;input type="submit" value="Save" name="_eventId_save"&gt;</pre>
<p>There are 2 JIRA issues concerning Web Flow Tags, I attached this code to one of them.</p>
<ul>
<li><a href="http://opensource.atlassian.com/projects/spring/browse/SWF-87">SWF-87</a></li>
<li><a href="http://opensource.atlassian.com/projects/spring/browse/SWF-116">SWF-116</a></li>
</ul>
<p>The sources can also be downloaded from <a href="http://www.deinum.biz/wp-content/uploads/2006/12/webflowtags.zip" id="p8">here</a> or point your favorite subversion client to the repository at <a href="http://bespring.googlecode.com/svn/">http://bespring.googlecode.com/svn/.</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=17&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2006/12/21/spring-web-flow-tag-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
		<item>
		<title>Using multiple validators in Controllers and FormAction</title>
		<link>http://mdeinum.wordpress.com/2006/12/13/using-multiple-validators-in-controllers-and-formaction/</link>
		<comments>http://mdeinum.wordpress.com/2006/12/13/using-multiple-validators-in-controllers-and-formaction/#comments</comments>
		<pubDate>Wed, 13 Dec 2006 13:38:56 +0000</pubDate>
		<dc:creator>Marten Deinum</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[spring mvc]]></category>
		<category><![CDATA[web flow]]></category>

		<guid isPermaLink="false">http://mdeinum.wordpress.com/2006/12/13/using-multiple-validators-in-controllers-and-formaction/</guid>
		<description><![CDATA[On a job I did recently we did a lot of refactoring the old (web) application, they used an abundance of (Web) Frameworks, we reduced it to 1 (well actually 2 if you count Spring Web Flow   ).
They already had a lot of Controllers and Validators build and also somewhere some custom validation [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=8&subd=mdeinum&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>On a job I did recently we did a lot of refactoring the old (web) application, they used an abundance of (Web) Frameworks, we reduced it to 1 (well actually 2 if you count Spring Web Flow <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<p>They already had a lot of Controllers and Validators build and also somewhere some custom validation logic in the desired classes. One thing I noticed is that they had a few command objects which had an emailaddress or telephone number. For each of those objects they also wrote a suitable Validator. Copying and pasting all the logic concering emailaddress validation each time, or even worse reinvented the logic.<br />
<span id="more-8"></span><br />
What I decided to do is to put the logic concerning the emailaddress validation into 1 Validator. Problem is you can only have 1 Validator on a Controller of FormAction. A bit problematic, however I couldn&#8217;t imagine that we where the first ones to run into this. So off to the <a href="http://forum.springframework.org" target="_blank">Spring Forum</a> after some search we found the <a href="http://forum.springframework.org/showpost.php?p=26308&amp;postcount=3" target="_blank">CompositeValidator</a> which we copied <sub>Thanks to Colin Yates for this</sub>.</p>
<pre class="brush: java;">
public final class CompoundValidator implements Validator {

  private final Validator[] validators;

  public CompoundValidator(final Validator[] validators) {
    super();
    this.validators=validators;
  }

  /**
  * Will return true if this class is in the specified map.
  */
  public boolean supports(final Class clazz) {
    for (Validator v : validators) {
      if (v.supports(clazz)) {
        return true;
      }
    }
    return false;
  }

  /**
  * Validate the specified object using the validator registered for the object's class.
  */
  public void validate(final Object obj, final Errors errors) {
    for (Validator v: validators) {
      if (v.supports(obj.getClass())) {
        v.validate(obj, errors);
      }
    }
  }
}
</pre>
<p>Now we have the solution for registering multiple validators for a Controller of FormAction you can create a CompositeValidator which wraps other validators.</p>
<p>Still we had all the emailaddress validation cluttered around, another problem to solve was in each command object the field containing the emailaddress was named differently i.e. email, emailadres, emailaddress etc. So we need to design something which could map classes to the field name needed. I couldn&#8217;t find something on the excellent Spring Forum or with Google so it was back to the coding board <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . I came up with the AbstractClassMappingValidator, this allows you to specify a Class and the desired field to check for that Class.</p>
<pre class="brush: java;">
public abstract class AbstractClassMappingValidator implements Validator {    /** Prefix Strings which are added to the field names */

  protected static final String REQUIRED = &quot;required&quot;;
  protected static final String INVALID = &quot;invalid&quot;;

  private Map mappings = new HashMap();

  protected boolean allowEmpty = false;

  public final boolean supports(Class clazz) {
    for (Class targetClazz: mappings.keySet()) {
      if (ClassUtils.isAssignable(targetClazz, clazz)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Gets the fieldname from the configured map.
   * @param target
   * @return
   */
  protected final String getFieldName(Object target) {
    for (Class targetClazz: mappings.keySet()) {
      if (ClassUtils.isAssignable(targetClazz, target.getClass())) {
        return mappings.get(targetClazz);
      }
    }
    throw new IllegalStateException(&quot;Cannot find fieldname for class &quot; + target.getClass().getName() + &quot;. Class is not compatible with declared types. [&quot;+mappings+&quot;]&quot;);
  }

  public final void setMappings(final Map mappings) {
    this.mappings = mappings;
  }

  public final void setAllowEmpty(final boolean allowEmpty) {
    this.allowEmpty = allowEmpty;
  }
}
</pre>
<p>You can configure this validator by setting its <strong>mappings</strong> property. If we have two objects(BusinessObjectOne &amp; BusinessObjectTwo) with the fields <em>email</em> and <em>emailaddress</em> we can use the following snippet to configure the validator.</p>
<pre class="brush: xml;">
&lt;bean id=&quot;id&quot; class=&quot;SomeClassExtendingAbstractClassMappingValidator&quot;&gt;
  &lt;property name=&quot;mappings&quot;&gt;
    &lt;map key-type=&quot;java.lang.Class&quot; value-type=&quot;java.lang.String&quot;&gt;
      &lt;entry key=&quot;com.mycompany.objects.BusinessObjectOne&quot; value=&quot;email&quot;/&gt;
      &lt;entry key=&quot;com.mycompany.objects.BusinessObjectTwo&quot; value=&quot;emailAddressNew&quot;/&gt;
      &lt;entry key=&quot;com.mycompany.objects.BusinessObjectThree&quot; value=&quot;emailAddress&quot;/&gt;
    &lt;/map&gt;
  &lt;/property&gt;
&lt;/bean&gt;
</pre>
<p>Now we have a CompositeValidator, we can map classes to fields to validate but we still need to check the emailaddresses against a defined regular expression. So we created a RegExpValidator it takes a regular expression and matches the value of the field against this regexp. If a field is required can also be configured.</p>
<pre class="brush: java;">
/**
 * Validator which matches a value against a defined regular expression.
 *
 * First it checks if a given value is not empty, if empty values are allowed
 * this can be switched off. Next the value (if not empty) is checked against
 * the configured regular expression. If it doesn't match the value is rejected.
 *
 * When empty values aren't allowed and the string is empty the errorcode is
 * generated as &lt;strong&gt;required.fieldname&lt;/strong&gt;. If the format is invalid the errorcode
 * is generated as &lt;strong&gt;invalid.fieldname&lt;/strong&gt;.
 *
 * @author Marten Deinum
 *
 */

public final class RegExpValidator extends AbstractClassMappingValidator {    private String              regexp     = null;

  public void validate(Object target, Errors errors) {
    String field = getFieldName(target);
    String value = (String) errors.getFieldValue(field);
    boolean isEmpty = !StringUtils.hasText(value);

    if (!allowEmpty &amp;amp;&amp;amp; isEmpty) {
      ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, REQUIRED + &quot;.&quot; + field);
    }

    if (!isEmpty) {
      if (!value.matches(regexp)) {
         errors.rejectValue(field, INVALID + &quot;.&quot; + field, new Object[] {field, value}, &quot;{1} is not a valid value for {0}.&quot;);
      }
    }
  }

  public void setRegExp(final String regexp) {
    this.regexp = regexp;
  }
}
</pre>
<p>Now we have all the components in place to make the magic work. First we configure the emailaddress validator. When we have done that we can setup the CompositeValidator with multiple validators and inject the configured one into a Controller or FormAction.</p>
<p>Feel free to comment and to use the code, if you make any improvements please let me know everything is welcome <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .The code can be found on <a href="http://code.google.com/p/bespring">code.google.com</a> connect to the subversion repository to get the sources of the validators(projects core and validation).</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mdeinum.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mdeinum.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mdeinum.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mdeinum.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mdeinum.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mdeinum.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mdeinum.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mdeinum.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mdeinum.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mdeinum.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mdeinum.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mdeinum.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mdeinum.wordpress.com&blog=595090&post=8&subd=mdeinum&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://mdeinum.wordpress.com/2006/12/13/using-multiple-validators-in-controllers-and-formaction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cfa2cbe233d83832d799785813057bd2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mdeinum</media:title>
		</media:content>
	</item>
	</channel>
</rss>