<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>raulraja.com</title>
	
	<link>http://raulraja.com</link>
	<description>About software dev and more</description>
	<lastBuildDate>Wed, 06 Jul 2011 04:20:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/raulraja" /><feedburner:info uri="raulraja" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>47 Degrees comes to life</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/US55iou9VEg/</link>
		<comments>http://raulraja.com/2010/10/05/47/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 22:09:17 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=515</guid>
		<description><![CDATA[47 Degrees is now a reality. Based out of the Pacific Northwest, 47 Degrees is a unique development team with a penchant for creating awesome websites and rich applications. I&#8217;m a Co-founder a Sr. Software Engineer at 47 Degrees. We&#8217;re eager to power your next startup, project or idea. Contact us if you need any [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://47deg.com">47 Degrees</a> is now a reality. Based out of the Pacific Northwest, 47 Degrees is a unique development team with a penchant for creating awesome websites and rich applications. I&#8217;m a Co-founder a Sr. Software Engineer at 47 Degrees. We&#8217;re eager to power your next startup, project or idea. Contact us if you need any Web, iPhone, Android or other software development needs. check us out at <a href="http://47deg.com">http://47deg.com</a></p>
<p><img class="alignnone size-large wp-image-516" title="47 Degrees" src="http://raulraja.com/wp-content/uploads/2010/10/Screen-shot-2010-10-05-at-3.01.21-PM-593x225.png" alt="" width="534" height="203" /></p>

<p><a href="http://feedads.g.doubleclick.net/~a/SOE-1jry4Fneqg26VIyCbW2jz98/0/da"><img src="http://feedads.g.doubleclick.net/~a/SOE-1jry4Fneqg26VIyCbW2jz98/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/SOE-1jry4Fneqg26VIyCbW2jz98/1/da"><img src="http://feedads.g.doubleclick.net/~a/SOE-1jry4Fneqg26VIyCbW2jz98/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/US55iou9VEg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2010/10/05/47/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://raulraja.com/2010/10/05/47/</feedburner:origLink></item>
		<item>
		<title>Streamd.in, a web based Twitter client paired with Google Maps</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/IvpwdTvzDZk/</link>
		<comments>http://raulraja.com/2010/03/23/streamd-in/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 06:11:04 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Tapestry]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[DWR]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[http://streamd.in]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Mashup]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[streamd.in]]></category>
		<category><![CDATA[Tokbox]]></category>
		<category><![CDATA[Twitter Inc]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=494</guid>
		<description><![CDATA[ Streamd.in is live!. A simple web based Twitter client paired with Google Maps. Streamd.in allows you to discover what&#8217;s happening around you, let others know what you&#8217;re doing and broadcast your thoughts &#38; location with live video and twitter. As far as technology, Streamd.in features Javascript, Html, Ajax, Google Maps Api, Tokbox, Apache Tapestry for the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-505" title="Streamd.in - A simple web based Twitter client paired with Google Maps. See what's happening around you" src="http://raulraja.com/wp-content/uploads/2010/03/Screen-shot-2010-03-23-at-8.43.13-PM1-502x200.png" alt="Streamd.in - A simple web based Twitter client paired with Google Maps. See what's happening around you" width="502" height="200" /></p>
<p> <a href="http://streamd.in">Streamd.in</a> is live!. A simple web based Twitter client paired with Google Maps. <a href="http://streamd.in">Streamd.in</a> allows you to discover what&#8217;s happening around you, let others know what you&#8217;re doing and broadcast your thoughts &amp; location with live video and twitter.</p>
<p>As far as technology, <a href="http://streamd.in">Streamd.in</a> features Javascript, Html, Ajax, <a href="http://code.google.com/apis/maps/">Google Maps Api</a>, <a href="http://www.tokbox.com/">Tokbox</a>, <a href="http://tapestry.apache.org/tapestry5/">Apache Tapestry</a> for the client frontend and <a href="http://java.sun.com/">Java</a>, <a href="http://tapestry.apache.org/tapestry5/">Apache Tapestry</a>, <a href="http://www.springsource.org/">Spring</a>, <a href="http://directwebremoting.org">DWR</a>, <a href="http://httpd.apache.org/">Apache Httpd</a>, <a href="http://www.hibernate.org/">Hibernate</a>, <a href="http://www.mysql.com/">MySQL</a>, the <a href="http://apiwiki.twitter.com/">Twitter api</a> and <a href="http://bit.ly/">Bit.ly</a> in its backend.</p>
<p>Give it a try an see for yourself what people are saying around you at <a href="http://streamd.in">http://streamd.in</a></p>
<p>Follow us on Twitter <a title="Twitter - Streamdin" href="http://twitter.com/streamdin">@streamdin</a> to stay up to date with new features.</p>
<p><img class="alignnone size-medium wp-image-497" title="Streamd.in - A simple web based Twitter client paired with Google Maps" src="http://raulraja.com/wp-content/uploads/2010/03/screenshot-502x200.png" alt="Streamd.in - A simple web based Twitter client paired with Google Maps" width="502" height="200" /></p>

<p><a href="http://feedads.g.doubleclick.net/~a/_vfSdzKyQVxdZXRcBvta0oYWIak/0/da"><img src="http://feedads.g.doubleclick.net/~a/_vfSdzKyQVxdZXRcBvta0oYWIak/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_vfSdzKyQVxdZXRcBvta0oYWIak/1/da"><img src="http://feedads.g.doubleclick.net/~a/_vfSdzKyQVxdZXRcBvta0oYWIak/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/IvpwdTvzDZk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2010/03/23/streamd-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://raulraja.com/2010/03/23/streamd-in/</feedburner:origLink></item>
		<item>
		<title>JAVA – java.beans.Introspector odd behavior discovering javabeans properties – Is this is a bug?</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/bY0QiK-J-4U/</link>
		<comments>http://raulraja.com/2009/09/12/java-beans-introspector-odd-behavio/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 22:14:06 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[Java platform]]></category>
		<category><![CDATA[JavaBean]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=406</guid>
		<description><![CDATA[We use DWR extensively in our AJAX enabled apps. Well the other day I found this weird case where some of our beans properties were not being properly set when being converted from Javascript to Java. Looking at the source I saw how DWR uses the Introspector on its BeanConverter. This also true for other [...]]]></description>
			<content:encoded><![CDATA[<p>We use <a href="http://directwebremoting.org/dwr/index.html">DWR</a> extensively in our AJAX enabled apps. Well the other day I found this weird case where some of our beans properties were not being properly set when being converted from Javascript to Java.<br />
Looking at the source I saw how <a href="http://directwebremoting.org/dwr/index.html">DWR</a> uses the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a> on its BeanConverter. This also true for other utils such as <a href="http://commons.apache.org/beanutils/">Apache commons BeanUtils and PropertyUtils</a>. </p>
<p>I&#8217;m Probably missing something obvious here, but regardles of this beans conforming to the Javabeans spec or not the way the introspector finds setter and getters seems backwards to me.</p>
<p>If a bean implements an interface with different return types on its getter than the interface but compatible with the interface the Introspector is unable to see that there are valid accessors in the bean.<br />
I wrote a very simple test case that shows this behavior and possible nasty workaround using reflection.<br />
I wrote all classes and interfaces in the same file but you can use the main method to test with your own classes and you&#8217;ll see that the behavior is the same.</p>
<p>I&#8217;m not an expert in the javabeans spec and if I&#8217;m missing something obvious please let me know.</p>
<p>I also found this post at sun&#8217;s forums where other people think this is backwards .</p>
<p><a href="http://forums.sun.com/thread.jspa?threadID=5404791">http://forums.sun.com/thread.jspa?threadID=5404791</a></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.BeanInfo</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.IntrospectionException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.Introspector</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.beans.PropertyDescriptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Shows odd behavior when using an introspector from the java.beans api
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IntrospectorPossibleBugTest <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Marker interface for all locations
	 */</span>
	<span style="color: #000000; font-weight: bold;">interface</span> Location <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * A City is a location
	 */</span>
	<span style="color: #000000; font-weight: bold;">class</span> City <span style="color: #000000; font-weight: bold;">implements</span> Location <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Marker interface for people, a person must have a location assigned
	 */</span>
	<span style="color: #000000; font-weight: bold;">interface</span> Person <span style="color: #009900;">&#123;</span>
		Location getLocation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * A worker is a Person and should have a location that is actually a City
	 */</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> Worker <span style="color: #000000; font-weight: bold;">implements</span> Person, <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">private</span> City location<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> City getLocation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> location<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLocation<span style="color: #009900;">&#40;</span>City location<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">location</span> <span style="color: #339933;">=</span> location<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * This small test cases shows the odd behavior.
	 * Even though Worker conforms with the getters and setters for the javabean spec the
	 * Introspector seems to not be able to find any setter for the property location.
	 * Seems like instead of following a bottom up loockup for property accessors it starts and stops once it
	 * finds a compatible property in one of the implemented interfaces.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IntrospectionException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">BeanInfo</span> info <span style="color: #339933;">=</span> <span style="color: #003399;">Introspector</span>.<span style="color: #006633;">getBeanInfo</span><span style="color: #009900;">&#40;</span>Worker.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">PropertyDescriptor</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> descriptors <span style="color: #339933;">=</span> info.<span style="color: #006633;">getPropertyDescriptors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">PropertyDescriptor</span> descriptor <span style="color: #339933;">:</span> descriptors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>descriptor.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property: <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> descriptor.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getter: <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> descriptor.<span style="color: #006633;">getReadMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;getter returns: <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> descriptor.<span style="color: #006633;">getReadMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getReturnType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;setter: <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> descriptor.<span style="color: #006633;">getWriteMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">String</span> propertyName <span style="color: #339933;">=</span> descriptor.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">String</span> setterName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;set&quot;</span> <span style="color: #339933;">+</span> propertyName.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> propertyName.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">Method</span> setter <span style="color: #339933;">=</span> findMethodImplFirst<span style="color: #009900;">&#40;</span>Worker.<span style="color: #000000; font-weight: bold;">class</span>, setterName, City.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;setter with reflection: <span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">+</span> setter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * workaround for introspector odd behavior with javabeans that implement interfaces with comaptible return types
	 * but instrospection is unable to find the right accessors
	 *
	 * @param currentTargetClass the class being evaluated
	 * @param methodName		 the method name we are looking for
	 * @param argTypes		   the arg types for the method name
	 * @return a method if found
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Method</span> findMethodImplFirst<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> currentTargetClass, <span style="color: #003399;">String</span> methodName, Class<span style="color: #339933;">&lt;?&gt;</span>... <span style="color: #006633;">argTypes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>currentTargetClass <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> methodName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				method <span style="color: #339933;">=</span> currentTargetClass.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span>methodName, argTypes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// nothing we can do but continue</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #666666; font-style: italic;">//Is the method in one of our parent classes</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>method <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				Class<span style="color: #339933;">&lt;?&gt;</span> superclass <span style="color: #339933;">=</span> currentTargetClass.<span style="color: #006633;">getSuperclass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>superclass.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					method <span style="color: #339933;">=</span> findMethodImplFirst<span style="color: #009900;">&#40;</span>superclass, methodName, argTypes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> method<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>output:<br />
<code><br />
property: 						location<br />
getter: 						public InstrospectorPossibleBugTest$Location InstrospectorPossibleBugTest$Worker.getLocation()<br />
getter returns: 				interface InstrospectorPossibleBugTest$Location<br />
<i><br />
<strong>setter: 						null</strong></i></p>
<p><i><br />
<strong>setter with reflection: 		public void InstrospectorPossibleBugTest$Worker.setLocation(InstrospectorPossibleBugTest$City)</strong></i></code></p>

<p><a href="http://feedads.g.doubleclick.net/~a/rdSZifyYwKNFAZ-_CpPgbmJ1Lm0/0/da"><img src="http://feedads.g.doubleclick.net/~a/rdSZifyYwKNFAZ-_CpPgbmJ1Lm0/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/rdSZifyYwKNFAZ-_CpPgbmJ1Lm0/1/da"><img src="http://feedads.g.doubleclick.net/~a/rdSZifyYwKNFAZ-_CpPgbmJ1Lm0/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/bY0QiK-J-4U" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/09/12/java-beans-introspector-odd-behavio/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/09/12/java-beans-introspector-odd-behavio/</feedburner:origLink></item>
		<item>
		<title>Spring – Hibernate – JAX-RS Putting Hibernate to REST</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/8mPXjtbm--I/</link>
		<comments>http://raulraja.com/2009/08/27/spring-hibernate-jax-rs-putting-hibernate-to-rest/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 00:25:56 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[Entity]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=383</guid>
		<description><![CDATA[This pattern exposes your domain model as a http REST interface. It violates MVC and many other good practices but it&#8217;s way cool and easy to not blog about it. There are certain scenarios too where you might want to expose your model this way, for example if you&#8217;re not building an app but a [...]]]></description>
			<content:encoded><![CDATA[<p>This pattern exposes your domain model as a http REST interface. It violates MVC and many other good practices but it&#8217;s way cool and easy to not blog about it. There are certain scenarios too where you might want to expose your model this way, for example if you&#8217;re not building an app but a framework to build apps and you want to give raw access to some of the core model beans or data.</p>
<p>In most examples I found on the web, all REST services go through services before they hit the model and I wanted an automatic way to access the model by simply adding a few annotations to the beans.</p>
<p>This pattern allows you to</p>
<ol>
<li>List objects by class in a paginated fashion</li>
<li>!!! List objects properties in a paginated fashion</li>
<li>Update objects</li>
<li>Get individual object properties</li>
<li>!!! Update individual object properties</li>
<li>automatically serialize / desiarilize in xml, json, etc&#8230;</li>
</ol>
<p>I have not added search but the implementation is trivial based on Hibernate Search by simply annotating the entities and properties.<br />
The code also does not go into the details in handling aggregated collections @OneToMany, @ManyToMany, @Any or agregated objects @ManyToOne</p>
<p>The code uses some non standard way of doing things such as injection of a Service in one of the entities base class properties.</p>
<p>This code in this article and some other examples are available for download with svn:<br />
svn checkout http://raulrajatutorials.googlecode.com/svn/trunk/ raulrajatutorials-read-only<br />
Comments in the code itself should be self-explanatory if you already have some java + spring experience.</p>
<p>See embedded video for a demo of the code at work</p>
<p>Demo video<br />
<object width="502" height="406"><param name="movie" value="http://www.youtube.com/v/tut-4SNoFbw&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tut-4SNoFbw&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="502" height="406"></embed></object></p>
<p>web.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee</span>
<span style="color: #009900;">		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hibernate-rest<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hibernate-rest<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Spring config  --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               /WEB-INF/conf/applicationContext.xml
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4jConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/conf/log4j.properties<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Spring Listeners --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.util.Log4jConfigListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.ContextLoaderListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.request.RequestContextListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>openSessionInViewFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>openSessionInViewFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Remote web services --&gt;</span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CXFServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               org.apache.cxf.transport.servlet.CXFServlet
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>remoteApiBasicHttpHandlerImpl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.support.HttpRequestHandlerServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
       <span style="color: #808080; font-style: italic;">&lt;!-- Remote web services mapping --&gt;</span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CXFServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/api/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>hibernate.cfg.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD//EN&quot; &quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;hibernate-configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-factory</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;com.raulraja.tutorials&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- Persistent entities --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.model.domain.Person&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-factory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/hibernate-configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>applicationContext.xml</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:aop</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/aop&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:tx</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/tx&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:cxf</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/core&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:jaxws</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxws&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:jaxrs</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxrs&quot;</span> <span style="color: #000066;">xmlns:util</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/util&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</span>
<span style="color: #009900;">       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd</span>
<span style="color: #009900;">       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/core</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/core.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/jaxws</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/jaxws.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/jaxrs</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/jaxrs.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">default-autowire</span>=<span style="color: #ff0000;">&quot;byName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Load CXF modules from cxf.jar --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:bus<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:logging</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cxf:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cxf:bus<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- A template instance to expose as the service --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;person&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.model.domain.Person&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The rest server configuration --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:server</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;restServer&quot;</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;/rest/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:serviceBeans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;person&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:serviceBeans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:extensionMappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;feed&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/atom+xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;json&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/json&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;html&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:extensionMappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The datasource --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot;</span> <span style="color: #000066;">destroy-method</span>=<span style="color: #ff0000;">&quot;close&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;driverClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.postgresql.Driver&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jdbcUrl&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;jdbc:postgresql://localhost:5432/tutorials?autoReconnect=true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;user&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;tutorials&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;tutorials&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The hibernate session factory --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;configLocations&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>WEB-INF/conf/hibernate.cfg.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;configurationClass&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.hibernate.cfg.AnnotationConfiguration<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateProperties&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>org.hibernate.dialect.PostgreSQLDialect<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.hbm2ddl.auto&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>update<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- A persistence adapter implementation that acts as a data access service --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;persistenceAdapter&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.model.persistence.impl.HibernatePersistenceAdapterImpl&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultResultSize&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The transaction manager --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- enable the configuration of transactional behavior based on annotations --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tx:annotation-driven</span> <span style="color: #000066;">transaction-manager</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The hibernate template --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;hibernateTemplate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTemplate&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- statically injects the persistence service in the entities base class --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.beans.factory.config.MethodInvokingFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;staticMethod&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.raulraja.model.domain.PersistentEntity.setPersistenceAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;arguments&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;persistenceAdapter&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>PaginatedList.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.model.domain</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * An object that encapsualtes pagination over list of records
 */</span>
@XmlRootElement<span style="color: #009900;">&#40;</span>namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>
@XmlType<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;paginatedList&quot;</span>, namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * the total amount of items available in the data store
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> count<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * the starting record displayed on the list
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> start<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * the size of this list
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> size<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * the records contained on the list
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * default constructor
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Constructor that takes the data
	 * @param count the total amount of items available in the data store
	 * @param start the starting record displayed on the list
	 * @param items the records contained on the list
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> count, <span style="color: #000066; font-weight: bold;">long</span> start, List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setCount<span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setStart<span style="color: #009900;">&#40;</span>start<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setItems<span style="color: #009900;">&#40;</span>items<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setSize<span style="color: #009900;">&#40;</span>items.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the total amount of items available in the data store
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setCount<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">count</span> <span style="color: #339933;">=</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the starting record displayed on the list
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> start<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setStart<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> start<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">start</span> <span style="color: #339933;">=</span> start<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the size of this list
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getSize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> size<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSize<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">size</span> <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the records contained on the list
	 */</span>
	@XmlElementWrapper<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;items&quot;</span><span style="color: #009900;">&#41;</span>
	@XmlElements<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            @XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;person&quot;</span>, type<span style="color: #339933;">=</span>Person.<span style="color: #000000; font-weight: bold;">class</span>, namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>,
			@XmlElement<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;property&quot;</span>, type<span style="color: #339933;">=</span><span style="color: #003399;">Object</span>.<span style="color: #000000; font-weight: bold;">class</span>, namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> getItems<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> items<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setItems<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">items</span> <span style="color: #339933;">=</span> items<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>PersistentEntity.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.model.domain</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.model.persistence.PersistenceAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.beanutils.BeanUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.beanutils.PropertyUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.InvocationTargetException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Base class for entities with static access to the persistent context
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> PersistentEntity<span style="color: #339933;">&lt;</span>Entity <span style="color: #000000; font-weight: bold;">extends</span> PersistentEntity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> NAMESPACE <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;https://apps.raulraja.org/api&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> PersistenceAdapter<span style="color: #339933;">&lt;</span>PersistentEntity<span style="color: #339933;">&gt;</span> persistenceAdapter<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setPersistenceAdapter<span style="color: #009900;">&#40;</span>PersistenceAdapter<span style="color: #339933;">&lt;</span>PersistentEntity<span style="color: #339933;">&gt;</span> persistenceAdapter<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PersistentEntity.<span style="color: #006633;">persistenceAdapter</span> <span style="color: #339933;">=</span> persistenceAdapter<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> o.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		PersistentEntity<span style="color: #339933;">&lt;?&gt;</span> entity <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersistentEntity<span style="color: #339933;">&lt;?&gt;</span><span style="color: #009900;">&#41;</span> o<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #339933;">!</span>getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of all entities for this class
	 * @param start the start record
	 * @param size the limit
	 * @return the list of matches
	 */</span>
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;list&quot;</span><span style="color: #009900;">&#41;</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span>@QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;start&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">int</span> start, @QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;size&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">int</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> mappedClass <span style="color: #339933;">=</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> persistenceAdapter.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span>mappedClass, start, size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> list<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of all entities for this class
	 * @param start the start record
	 * @param size the limit
	 * @return the list of matches
	 */</span>
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{property}/list&quot;</span><span style="color: #009900;">&#41;</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">String</span> property, @QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;start&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">int</span> start, @QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;size&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">int</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> mappedClass <span style="color: #339933;">=</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> persistenceAdapter.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span>mappedClass, property, start, size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> list<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{id}&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> PersistentEntity get<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> mappedClass <span style="color: #339933;">=</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersistentEntity entity <span style="color: #339933;">=</span> persistenceAdapter.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>mappedClass, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> entity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@GET @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{id}/{property}&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getProperty<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Long</span> id, @PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">String</span> property<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>, <span style="color: #003399;">IllegalAccessException</span>, <span style="color: #003399;">InvocationTargetException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> mappedClass <span style="color: #339933;">=</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersistentEntity entity <span style="color: #339933;">=</span> persistenceAdapter.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>mappedClass, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> PropertyUtils.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span>entity, property<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{id}/{property}&quot;</span><span style="color: #009900;">&#41;</span>
	@Transactional
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> updateProperty<span style="color: #009900;">&#40;</span>@PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Long</span> id, @PathParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;property&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">String</span> property, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NoSuchMethodException</span>, <span style="color: #003399;">IllegalAccessException</span>, <span style="color: #003399;">InvocationTargetException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">Class</span> mappedClass <span style="color: #339933;">=</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		PersistentEntity entity <span style="color: #339933;">=</span> persistenceAdapter.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>mappedClass, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		BeanUtils.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span>entity, property, value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> persistenceAdapter.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;create&quot;</span><span style="color: #009900;">&#41;</span>
	@Transactional
    <span style="color: #000000; font-weight: bold;">public</span> PersistentEntity create<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PersistentEntity persistentEntity <span style="color: #339933;">=</span> get<span style="color: #009900;">&#40;</span>entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>persistentEntity <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There is already a &quot;</span> <span style="color: #339933;">+</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; with id = &quot;</span> <span style="color: #339933;">+</span> entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		persistenceAdapter.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> entity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;update&quot;</span><span style="color: #009900;">&#41;</span>
	@Transactional
    <span style="color: #000000; font-weight: bold;">public</span> PersistentEntity update<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		PersistentEntity<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> persistentEntity <span style="color: #339933;">=</span> get<span style="color: #009900;">&#40;</span>entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>persistentEntity <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There is no&quot;</span> <span style="color: #339933;">+</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; with id = &quot;</span> <span style="color: #339933;">+</span> entity.<span style="color: #006633;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		persistentEntity.<span style="color: #006633;">updateFromRequest</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		persistenceAdapter.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>persistentEntity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> persistentEntity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">Entity</span> updateFromRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@POST @Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;delete&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		persistenceAdapter.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> entity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Person.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.model.domain</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.ManyToOne</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.OneToMany</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.Path</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/person/&quot;</span><span style="color: #009900;">&#41;</span>
@XmlRootElement
@XmlType<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;person&quot;</span>, namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">extends</span> PersistentEntity<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> email<span style="color: #339933;">;</span>
&nbsp;
	@ManyToOne
	<span style="color: #000000; font-weight: bold;">private</span> Person parent<span style="color: #339933;">;</span>
&nbsp;
	@OneToMany<span style="color: #009900;">&#40;</span>mappedBy <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;parent&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #003399;">Long</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Long</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #003399;">Long</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> email<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> email<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">email</span> <span style="color: #339933;">=</span> email<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Person getParent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> parent<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setParent<span style="color: #009900;">&#40;</span>Person parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">parent</span> <span style="color: #339933;">=</span> parent<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@XmlElement<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> Person.<span style="color: #000000; font-weight: bold;">class</span>, name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;person&quot;</span>, namespace <span style="color: #339933;">=</span> PersistentEntity.<span style="color: #006633;">NAMESPACE</span><span style="color: #009900;">&#41;</span>
	@XmlElementWrapper<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;children&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> getChildren<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> children<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setChildren<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> children<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">children</span> <span style="color: #339933;">=</span> children<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Class<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> getMappedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Person updateFromRequest<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		setName<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setEmail<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>PersistenceAdapter.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.model.persistence</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.model.domain.PaginatedList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> PersistenceAdapter<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of all entities for this class
	 * @param entityClass the entity class
	 * @param start the start record
	 * @param limit the limit
	 * @return the list of matches
	 */</span>
	PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> limit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of the selected property for this class
	 * @param entityClass the entity class
	 * @param start the start record
	 * @param limit the limit
	 * @return the list of matches
	 */</span>
	PaginatedList<span style="color: #339933;">&lt;?&gt;</span> list<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #003399;">String</span> property, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> limit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Find a persistent object based on its unique id
     * @param entityClass the entity's class
     * @param id unique identifier
     * @return returns a persistent object
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> get<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #003399;">Serializable</span> id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Persists an object
     * @param entity to be persisted
	 * @return the saved entity
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> save<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Removes an entity from the data store
     * @param entity to be removed
	 * @return the deleted entity
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>HibernatePersistenceAdapterImpl.java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.model.persistence.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.model.domain.PaginatedList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.model.persistence.PersistenceAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.criterion.DetachedCriteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.criterion.Projections</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.metadata.ClassMetadata</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.orm.hibernate3.HibernateAccessor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.orm.hibernate3.HibernateCallback</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.orm.hibernate3.HibernateTemplate</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.orm.hibernate3.support.HibernateDaoSupport</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Hibernate spring based impl of the persistence adapter
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernatePersistenceAdapterImpl<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> HibernateDaoSupport <span style="color: #000000; font-weight: bold;">implements</span> PersistenceAdapter<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>HibernatePersistenceAdapterImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> defaultResultSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDefaultResultSize<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> defaultResultSize<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">defaultResultSize</span> <span style="color: #339933;">=</span> defaultResultSize<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of all entities for this class
	 *
	 * @param entityClass the entity class
	 * @param start	   the start record
	 * @param limit	   the limit
	 * @return the list of matches
	 */</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> list<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> limit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		limit <span style="color: #339933;">=</span> limit <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">?</span> limit <span style="color: #339933;">:</span> defaultResultSize<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>entityClass <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entities<span style="color: #339933;">;</span>
		PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> page <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> total<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			DetachedCriteria criteria <span style="color: #339933;">=</span> DetachedCriteria.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			entities <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findByCriteria</span><span style="color: #009900;">&#40;</span>criteria, start, limit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			total <span style="color: #339933;">=</span> count<span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			page <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PaginatedList<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>total, start, entities<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span>e, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> page<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Returns a paginated list of the selected property for this class
	 *
	 * @param entityClass the entity class
	 * @param start	   the start record
	 * @param limit	   the limit
	 * @return the list of matches
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> PaginatedList<span style="color: #339933;">&lt;?&gt;</span> list<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #003399;">String</span> property, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> limit<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		limit <span style="color: #339933;">=</span> limit <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">?</span> limit <span style="color: #339933;">:</span> defaultResultSize<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>entityClass <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> properties<span style="color: #339933;">;</span>
		PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> page <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;">int</span> total<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			DetachedCriteria criteria <span style="color: #339933;">=</span> DetachedCriteria.<span style="color: #006633;">forClass</span><span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			criteria.<span style="color: #006633;">setProjection</span><span style="color: #009900;">&#40;</span>Projections.<span style="color: #006633;">property</span><span style="color: #009900;">&#40;</span>property<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			properties <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findByCriteria</span><span style="color: #009900;">&#40;</span>criteria, start, limit<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			total <span style="color: #339933;">=</span> count<span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			page <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PaginatedList<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>total, start, properties<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span>e, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> page<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> count<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HibernateCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> doInHibernate<span style="color: #009900;">&#40;</span>Session session<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
				Criteria criteria <span style="color: #339933;">=</span> session.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>entityClass<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				criteria.<span style="color: #006633;">setProjection</span><span style="color: #009900;">&#40;</span>Projections.<span style="color: #006633;">rowCount</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">return</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Find a persistent object based on its unique id
	 *
	 * @param entityClass the entity's class
	 * @param id		  unique identifier
	 * @return returns a persistent object
	 */</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> get<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> entityClass, <span style="color: #003399;">Serializable</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Entity</span> entity <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span><span style="color: #009900;">&#41;</span> getHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>entityClass, id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> initializeProxy<span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Persists an object
	 *
	 * @param entity to be persisted
	 */</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> save<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		getHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> initializeProxy<span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Removes an entity from the data store
	 *
	 * @param entity to be removed
	 */</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Entity</span> delete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		getHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> initializeProxy<span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> HibernateTemplate getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        HibernateTemplate template <span style="color: #339933;">=</span> getHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        template.<span style="color: #006633;">setCacheQueries</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        template.<span style="color: #006633;">setFlushMode</span><span style="color: #009900;">&#40;</span>HibernateAccessor.<span style="color: #006633;">FLUSH_AUTO</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> template<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Entity</span> initializeProxy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Entity</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">initialize</span><span style="color: #009900;">&#40;</span>entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> entity<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Finds a class associated with this path name
	 *
	 * @param name the name
	 * @return the found class if any
	 */</span>
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span> getClassForName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> normalizedName <span style="color: #339933;">=</span> name.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">?</span> name.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">lastIndexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span>,<span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toUpperCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> name.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> name<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>Entity<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> getPreparedHibernateTemplate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HibernateCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> doInHibernate<span style="color: #009900;">&#40;</span>Session session<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
				SessionFactory factory <span style="color: #339933;">=</span> session.<span style="color: #006633;">getSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				ClassMetadata classMetadata <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getClassMetadata</span><span style="color: #009900;">&#40;</span>normalizedName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">return</span> classMetadata <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> classMetadata.<span style="color: #006633;">getMappedClass</span><span style="color: #009900;">&#40;</span>EntityMode.<span style="color: #006633;">POJO</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/o_z7_YVLlGecbbIbTlbEQiW505w/0/da"><img src="http://feedads.g.doubleclick.net/~a/o_z7_YVLlGecbbIbTlbEQiW505w/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/o_z7_YVLlGecbbIbTlbEQiW505w/1/da"><img src="http://feedads.g.doubleclick.net/~a/o_z7_YVLlGecbbIbTlbEQiW505w/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/8mPXjtbm--I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/08/27/spring-hibernate-jax-rs-putting-hibernate-to-rest/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/08/27/spring-hibernate-jax-rs-putting-hibernate-to-rest/</feedburner:origLink></item>
		<item>
		<title>Spring – Java thumbnail generation based on content type</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/QP4FDvyx0Pk/</link>
		<comments>http://raulraja.com/2009/07/12/spring-java-thumbnail-generation-based-on-content-type/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 08:06:43 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[JPEG]]></category>
		<category><![CDATA[Thumbnail]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=311</guid>
		<description><![CDATA[The purpose of this tutorial is to demonstrate how to generate thumbnails in Java using the Spring framework. The thumbnail generation is based on the algorithm by Phil Reeve . It has also been further improved by Jim McCabe to base the thumbnail generation in previously generated thumbnail so that the image is not continuously [...]]]></description>
			<content:encoded><![CDATA[<p>The purpose of this tutorial is to demonstrate how to generate thumbnails in Java using the <a href="http://www.springsource.org/">Spring framework</a>.</p>
<p>The thumbnail generation is based on the algorithm by <a href="http://www.philreeve.com/java_high_quality_thumbnails.php">Phil Reeve </a>.</p>
<p>It has also been further improved by <a href="http://jmccabe.com/jim/">Jim McCabe</a> to base the thumbnail generation in previously generated thumbnail so that the image is not continuously evaluated.</p>
<p>The code included contains also a file upload form that demonstrates uploading files with Spring MVC although you can use the ThumbnailGeneratorEngine in any of your service beans.</p>
<p>This tutorial also uses the Engine / Processor pattern as explained <a href="http://raulraja.com/2009/06/30/spring-engine-processors-pattern/">here</a>.</p>
<p>The ThumbnailGeneratorEngine can be configured with processors that know how to process certain content types and input streams. The code only demonstrates generating thumbnails from images but other libraries can be used to implement processors that generate thumbnails from pdf, word, excel, urls, etc&#8230;</p>
<p>The pattern allows to add as many processors as necessary by plugin them into the spring configuration and associating them to content types in the engine configuration.</p>
<p>This tutorial is available for download with svn:<br />
svn checkout <a href="http://raulrajatutorials.googlecode.com/svn/trunk/">http://raulrajatutorials.googlecode.com/svn/trunk/</a> raulrajatutorials-read-only<br />
Comments in the code itself should be self-explanatory if you already have some java + spring experience.</p>
<p>And now to the point.</p>
<p>First a video of the actual code at work and then the code</p>
<p><object width="502" height="406"><param name="movie" value="http://www.youtube.com/v/zq3pbKaVL9o&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/zq3pbKaVL9o&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="502" height="406"></embed></object></p>
<p><strong>applicationContext.xml</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">default-autowire</span>=<span style="color: #ff0000;">&quot;byName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- thumbnail generator engine --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;thumbnailGeneratorEngine&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.thumbnails.impl.ThumbnailGeneratorEngineImpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- the file extension for the thumbnail files --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;generatedExtension&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>.jpg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- the different sizes we want to generate, adjusting the aspect ratio based on the biggest dimension --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;supportedSizes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>900<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>768<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>375<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>128<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>64<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>48<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>22<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- mappings from the different content types to the right generator that handles each type --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;thumbnailGenerators&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/jpeg&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/jpg&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/pjpeg&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/gif&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/png&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/tiff&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;image/bmp&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;application/pdf&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;pdfThumbnailGenerator&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- A default thumbnail generator to be used for unregistered mime types --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultThumbnailGenerator&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #808080; font-style: italic;">&lt;!-- location for the generated thumbnails --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;thumbnailsLocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/Users/raul/Pictures/test/&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- a thumbnail generator that generates thumbnails from images --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;imageThumbnailGenerator&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.thumbnails.impl.ImageThumbnailGeneratorImpl&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- a thumbnail generator that generates thumbnails from pdf files not implemented for simplicity --&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;pdfThumbnailGenerator&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.thumbnails.impl.PDFThumbnailGeneratorImpl&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- upload code below for demo purposes --&gt;</span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;multipartResolver&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.multipart.commons.CommonsMultipartResolver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- one of the properties available; the maximum file size in bytes --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;maxUploadSize&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;50000000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;urlMapping&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;mappings&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                /upload.form=fileUploader
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;alwaysUseFullPath&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;fileUploader&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.upload.FileUploadService&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;session&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;commandClass&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.upload.FileUpload&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;formView&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;uploadFile.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;successView&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;success.jsp&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>web.xml</strong>  (for demo purposes)  </p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee</span>
<span style="color: #009900;">		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>method-profiling-with-spring<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>method-profiling-with-spring<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Spring config  --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               /WEB-INF/conf/applicationContext.xml
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4jConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/conf/log4j.properties<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Spring Listeners --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.util.Log4jConfigListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.ContextLoaderListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.context.request.RequestContextListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>springDispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.servlet.DispatcherServlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- maps the dispatcher to *.form --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>springDispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*.form<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>uploadFile.jsp</strong>  (for demo purposes)  </p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;form&quot;</span> <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/tags/form&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;upload.form&quot;</span> <span style="color: #000066;">enctype</span>=<span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;file&quot;</span>  <span style="color: #000000; font-weight: bold;">/&gt;</span></span>   
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Upload&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>success.jsp</strong>  (for demo purposes)  </p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">        &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    sucess!!!
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>ThumbnailGeneratorEngine.java</strong><br />
 (This is the interface and entry point for services to ask for thumbnails to be generated)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * An engine in charge of generating thumbnails for files
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ThumbnailGeneratorEngine <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param fileNamePrefix the prefix for the generated thumbnails
	 * @param inputStream	the stream to generate thumbnails for
	 * @param contentType	the content type of this input stream for example image/jpeg
	 */</span>
	<span style="color: #000066; font-weight: bold;">void</span> generateThumbnails<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fileNamePrefix, <span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>ThumbnailGeneratorImpl.java</strong><br />
 (The implementation for the thumbnail generator engine that is configured in the spring configuration)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.util.thumbnails.ThumbnailGenerator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.util.thumbnails.ThumbnailGeneratorEngine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Default impl for the Thumbnail generator engine
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ThumbnailGeneratorEngineImpl <span style="color: #000000; font-weight: bold;">implements</span> ThumbnailGeneratorEngine <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>ThumbnailGeneratorEngineImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> generatedExtension<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param generatedExtension The extension for the generated thumbnails
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setGeneratedExtension<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> generatedExtension<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">generatedExtension</span> <span style="color: #339933;">=</span> generatedExtension<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getGeneratedExtension<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> generatedExtension<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>String, ThumbnailGenerator<span style="color: #339933;">&gt;</span> thumbnailGenerators<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param thumbnailGenerators The thumbnail generators known by this engine mapped to a content type
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setThumbnailGenerators<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>String, ThumbnailGenerator<span style="color: #339933;">&gt;</span> thumbnailGenerators<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thumbnailGenerators</span> <span style="color: #339933;">=</span> thumbnailGenerators<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> supportedSizes<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param supportedSizes The suported sizes for the batch of generated thumbs
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setSupportedSizes<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> supportedSizes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">supportedSizes</span> <span style="color: #339933;">=</span> supportedSizes<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ThumbnailGenerator defaultThumbnailGenerator<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param defaultThumbnailGenerator the default thumbnail generator to be used for unregistered mime types
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDefaultThumbnailGenerator<span style="color: #009900;">&#40;</span>ThumbnailGenerator defaultThumbnailGenerator<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">defaultThumbnailGenerator</span> <span style="color: #339933;">=</span> defaultThumbnailGenerator<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> thumbnailsLocation<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param thumbnailsLocation location for the generated thumbnails
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setThumbnailsLocation<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> thumbnailsLocation<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thumbnailsLocation</span> <span style="color: #339933;">=</span> thumbnailsLocation<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param fileNamePrefix the prefix for the generated thumbnails
	 * @param inputStream	the stream to generate thumbnails for
	 * @param contentType	the content type of this input stream for example image/jpeg
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> generateThumbnails<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fileNamePrefix, <span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">String</span> contentType<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ThumbnailGenerator thumbnailGenerator <span style="color: #339933;">=</span> thumbnailGenerators.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>contentType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		thumbnailGenerator <span style="color: #339933;">=</span> thumbnailGenerator <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> thumbnailGenerator <span style="color: #339933;">:</span> defaultThumbnailGenerator<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>thumbnailGenerator <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Object</span> hint <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> dimension <span style="color: #339933;">:</span> supportedSizes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #003399;">File</span> fileOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>thumbnailsLocation, fileNamePrefix <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;_&quot;</span> <span style="color: #339933;">+</span> dimension <span style="color: #339933;">+</span> generatedExtension<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					hint <span style="color: #339933;">=</span> thumbnailGenerator.<span style="color: #006633;">createThumbnail</span><span style="color: #009900;">&#40;</span>inputStream, fileOut, dimension, hint<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Generated thumbnail for: &quot;</span> <span style="color: #339933;">+</span> inputStream <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; in &quot;</span> <span style="color: #339933;">+</span> fileOut <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; for type &quot;</span> <span style="color: #339933;">+</span> contentType<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error generating thumbnail for: &quot;</span> <span style="color: #339933;">+</span> inputStream <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; in &quot;</span> <span style="color: #339933;">+</span> fileOut <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; for type &quot;</span> <span style="color: #339933;">+</span> contentType, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Thumbnail generator not found for content type: &quot;</span> <span style="color: #339933;">+</span> contentType <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and no default generator was provided&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>ThumbnailGenerator.java</strong><br />
 (This is the interface the different generators implement)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Interface implemented by all thumbnail generators.
 *
 * &lt;p&gt;The thumbnail generation process is always performed in order with the largest thumbnails first.
 * Therefore it may be very handy for the generator to create smaller thumbnails using the results from
 * the prior iteration instead of always using the full-sized image as the source.  To accomplish this,
 * the generator can return a &quot;hint&quot; object that it can use in subsequent iterations, containing whatever
 * helpers it might want (such as the prior image already loaded in memory, etc).
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ThumbnailGenerator <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Create the thumbnail.  The thumbnail should always save as a JPEG file.
     *
     * @param inputStream       The source data.
     * @param fileOut           The output file.
     * @param largestDimension  The max width and height.  The generator should size the thumbnail so
     *                          that the width and height both stay within this limit.
     * @param hint              Optional hint that was returned from the prior thumbnail generation
     *                          on this same file, null if none was returned or if this is the first
     *                          thumbnail in this context.
     *
     * @return an optional hint object that will be passed to subsequent thumbnail generation calls
     *         for this same source data.  Return null if you don't use hints, otherwise return some
     *         object which allows you to communicate extra information to the next round, such as
     *         the scaled image already loaded.
	 * @throws java.io.IOException if something goes wrong handling the io
     */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> createThumbnail<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">File</span> fileOut, <span style="color: #000066; font-weight: bold;">int</span> largestDimension, <span style="color: #003399;">Object</span> hint<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>AbstractThumbnailGenerator.java</strong><br />
 (An abstract class with a utility method to save images as jpeg for all thumbnail generators to extend)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.image.codec.jpeg.JPEGCodec</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.image.codec.jpeg.JPEGImageEncoder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.image.BufferedImage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.OutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Class providing convenience method for abstract thumbnail generators
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractThumbnailGenerator <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>AbstractThumbnailGenerator.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Save an image as a JPEG file on disk.
     *
     * @param image     The raw image to save.
     * @param fileOut   The location where you want to save the file.
     *
     * @return true if successful, false if unsuccessful.
	 * @throws java.io.IOException if something goes wrong closing the stream
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> saveImageAsJPEG<span style="color: #009900;">&#40;</span><span style="color: #003399;">BufferedImage</span> image, <span style="color: #003399;">File</span> fileOut<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">OutputStream</span> streamOut <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span>      bSuccess <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            streamOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileOut<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            JPEGImageEncoder encoder <span style="color: #339933;">=</span> JPEGCodec.<span style="color: #006633;">createJPEGEncoder</span><span style="color: #009900;">&#40;</span>streamOut<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            encoder.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span>image<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            bSuccess <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Files.saveImageAsJPEG(&quot;</span> <span style="color: #339933;">+</span> fileOut <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;): &quot;</span> <span style="color: #339933;">+</span> t, t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>streamOut <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				streamOut.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> bSuccess<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>ImageThumbnailGeneratorImpl.java<br />
 (A concrete implementation a thumbnail generator that is handling multiple image content types)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails.impl</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.util.thumbnails.ThumbnailGenerator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.imageio.ImageIO</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.geom.AffineTransform</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.image.BufferedImage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Generate thumbnails for images.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageThumbnailGeneratorImpl <span style="color: #000000; font-weight: bold;">extends</span> AbstractThumbnailGenerator <span style="color: #000000; font-weight: bold;">implements</span> ThumbnailGenerator <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>ImageThumbnailGeneratorImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> createThumbnail<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">File</span> fileOut, <span style="color: #000066; font-weight: bold;">int</span> largestDimension, <span style="color: #003399;">Object</span> hint<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// What's the base image that we are starting with?  If there's a hint, that's the scaled image</span>
        <span style="color: #666666; font-style: italic;">// from the last time around, use that... (since we know we always iterate downwards in scale)</span>
        <span style="color: #003399;">Image</span> imageIn<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>hint <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Image</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            imageIn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Image</span><span style="color: #009900;">&#41;</span> hint<span style="color: #339933;">;</span>
            log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;createThumbnail(&quot;</span> <span style="color: #339933;">+</span> fileOut <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) reusing prior result image...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;createThumbnail(&quot;</span> <span style="color: #339933;">+</span> fileOut <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;) reading image from stream &quot;</span> <span style="color: #339933;">+</span> inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            imageIn <span style="color: #339933;">=</span> ImageIO.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>imageIn <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not read image file: &quot;</span> <span style="color: #339933;">+</span> inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> hint<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">BufferedImage</span> imageOut <span style="color: #339933;">=</span> createThumbnailImage<span style="color: #009900;">&#40;</span>imageIn, fileOut, largestDimension<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Return this image now as the hint for the next scaling iteration</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>imageOut <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            hint <span style="color: #339933;">=</span> imageOut<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> hint<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Create a thumbnail image and save it to disk.
     *
     * This algorithm is based on:
     *      http://www.philreeve.com/java_high_quality_thumbnails.php
     *
     * @param imageIn           The image you want to scale.
     * @param fileOut           The output file.
     * @param largestDimension  The largest dimension, so that neither the width nor height
     *                          will exceed this value.
     *
     * @return the image that was created, null if imageIn or fileOut is null.
	 * @throws java.io.IOException if something goes wrong when saving as jpeg
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">BufferedImage</span> createThumbnailImage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Image</span> imageIn, <span style="color: #003399;">File</span> fileOut, <span style="color: #000066; font-weight: bold;">int</span> largestDimension<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>imageIn <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span>fileOut <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//it seems to not return the right size until the methods get called for the first time</span>
        imageIn.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        imageIn.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Find biggest dimension</span>
        <span style="color: #000066; font-weight: bold;">int</span>     nImageWidth <span style="color: #339933;">=</span> imageIn.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span>     nImageHeight <span style="color: #339933;">=</span> imageIn.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span>     nImageLargestDim <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span>nImageWidth, nImageHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">double</span>  scale <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#41;</span> largestDimension <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#41;</span> nImageLargestDim<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span>     sizeDifference <span style="color: #339933;">=</span> nImageLargestDim <span style="color: #339933;">-</span> largestDimension<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//create an image buffer to draw to</span>
        <span style="color: #003399;">BufferedImage</span> imageOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedImage</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #003399;">BufferedImage</span>.<span style="color: #006633;">TYPE_INT_RGB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 8-bit RGB</span>
        <span style="color: #003399;">Graphics2D</span> g2d<span style="color: #339933;">;</span>
        <span style="color: #003399;">AffineTransform</span> tx<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Use a few steps if the sizes are drastically different, and only scale</span>
        <span style="color: #666666; font-style: italic;">// if the desired size is smaller than the original.</span>
        <span style="color: #000066; font-weight: bold;">int</span> numSteps <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>scale <span style="color: #339933;">&lt;</span> 1.0d<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Make sure we have at least 1 step</span>
            numSteps <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #009900;">&#40;</span>sizeDifference <span style="color: #339933;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>numSteps <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">int</span> stepSize <span style="color: #339933;">=</span> sizeDifference <span style="color: #339933;">/</span> numSteps<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> stepWeight <span style="color: #339933;">=</span> stepSize <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> heavierStepSize <span style="color: #339933;">=</span> stepSize <span style="color: #339933;">+</span> stepWeight<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> lighterStepSize <span style="color: #339933;">=</span> stepSize <span style="color: #339933;">-</span> stepWeight<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> currentStepSize, centerStep<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">double</span> scaledW <span style="color: #339933;">=</span> imageIn.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">double</span> scaledH <span style="color: #339933;">=</span> imageIn.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>numSteps <span style="color: #339933;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if there's an odd number of steps</span>
                centerStep <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#41;</span> numSteps <span style="color: #339933;">/</span> 2d<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//find the center step</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                centerStep <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//set it to -1 so it's ignored later</span>
&nbsp;
            <span style="color: #003399;">Integer</span> intermediateSize<span style="color: #339933;">;</span>
            <span style="color: #003399;">Integer</span> previousIntermediateSize <span style="color: #339933;">=</span> nImageLargestDim<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> numSteps<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">!=</span> centerStep<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//if this isn't the center step</span>
&nbsp;
                    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">==</span> numSteps <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//if this is the last step</span>
                        <span style="color: #666666; font-style: italic;">//fix the stepsize to account for decimal place errors previously</span>
                        currentStepSize <span style="color: #339933;">=</span> previousIntermediateSize <span style="color: #339933;">-</span> largestDimension<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>numSteps <span style="color: #339933;">-</span> i <span style="color: #339933;">&gt;</span> numSteps <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//if we're in the first half of the reductions</span>
                            currentStepSize <span style="color: #339933;">=</span> heavierStepSize<span style="color: #339933;">;</span>
                        <span style="color: #000000; font-weight: bold;">else</span>
                            currentStepSize <span style="color: #339933;">=</span> lighterStepSize<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//center step, use natural step size</span>
                    currentStepSize <span style="color: #339933;">=</span> stepSize<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                intermediateSize <span style="color: #339933;">=</span> previousIntermediateSize <span style="color: #339933;">-</span> currentStepSize<span style="color: #339933;">;</span>
                scale <span style="color: #339933;">=</span> intermediateSize <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#41;</span> previousIntermediateSize<span style="color: #339933;">;</span>
                scaledW <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>scaledW <span style="color: #339933;">*</span> scale<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                scaledH <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">max</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>scaledH <span style="color: #339933;">*</span> scale<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;step &quot;</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: scaling to &quot;</span> <span style="color: #339933;">+</span> scaledW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; x &quot;</span> <span style="color: #339933;">+</span> scaledH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                imageOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedImage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> scaledW, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> scaledH, <span style="color: #003399;">BufferedImage</span>.<span style="color: #006633;">TYPE_INT_RGB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 8 bit RGB</span>
                g2d <span style="color: #339933;">=</span> imageOut.<span style="color: #006633;">createGraphics</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                g2d.<span style="color: #006633;">setBackground</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">WHITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                g2d.<span style="color: #006633;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, imageOut.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, imageOut .<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                g2d.<span style="color: #006633;">setRenderingHint</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">RenderingHints</span>.<span style="color: #006633;">KEY_RENDERING</span>, <span style="color: #003399;">RenderingHints</span>.<span style="color: #006633;">VALUE_RENDER_QUALITY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                tx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">AffineTransform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                tx.<span style="color: #006633;">scale</span><span style="color: #009900;">&#40;</span>scale, scale<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                g2d.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>imageIn, tx, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                g2d.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                imageIn <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ImageIcon</span><span style="color: #009900;">&#40;</span>imageOut<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getImage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                previousIntermediateSize <span style="color: #339933;">=</span> intermediateSize<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// This enforces a rule that we always have an 8-bit image with white background for the thumbnail.  Plus, for large</span>
            <span style="color: #666666; font-style: italic;">// images, this makes subsequent downscaling really fast because we are working on a large 8-bit image</span>
            <span style="color: #666666; font-style: italic;">// instead of a large 12 or 24 bit image, so the downstream effect is very noticable.</span>
            imageOut <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedImage</span><span style="color: #009900;">&#40;</span>imageIn.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>, imageIn.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>, <span style="color: #003399;">BufferedImage</span>.<span style="color: #006633;">TYPE_INT_RGB</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            g2d <span style="color: #339933;">=</span> imageOut.<span style="color: #006633;">createGraphics</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            g2d.<span style="color: #006633;">setBackground</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Color</span>.<span style="color: #006633;">WHITE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            g2d.<span style="color: #006633;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, imageOut.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, imageOut.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tx <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">AffineTransform</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tx.<span style="color: #006633;">setToIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//use identity matrix so image is copied exactly</span>
            g2d.<span style="color: #006633;">drawImage</span><span style="color: #009900;">&#40;</span>imageIn, tx, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            g2d.<span style="color: #006633;">dispose</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// JPEG-encode the image and write to file.</span>
        saveImageAsJPEG<span style="color: #009900;">&#40;</span>imageOut, fileOut<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> imageOut<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>PDFThumbnailGeneratorImpl.java<br />
 (An empty stub impl for a PDF based thumbnail generator)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.thumbnails.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.util.thumbnails.ThumbnailGenerator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Empty impl for generating thumbnails for pdfs
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PDFThumbnailGeneratorImpl <span style="color: #000000; font-weight: bold;">implements</span> ThumbnailGenerator <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Create the thumbnail.  The thumbnail should always save as a JPEG file.
	 *
	 * @param inputStream	  The source data.
	 * @param fileOut		  The output file.
	 * @param largestDimension The max width and height.  The generator should size the thumbnail so
	 *                         that the width and height both stay within this limit.
	 * @param hint			 Optional hint that was returned from the prior thumbnail generation
	 *                         on this same file, null if none was returned or if this is the first
	 *                         thumbnail in this context.
	 * @return an optional hint object that will be passed to subsequent thumbnail generation calls
	 *         for this same source data.  Return null if you don't use hints, otherwise return some
	 *         object which allows you to communicate extra information to the next round, such as
	 *         the scaled image already loaded.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> createThumbnail<span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> inputStream, <span style="color: #003399;">File</span> fileOut, <span style="color: #000066; font-weight: bold;">int</span> largestDimension, <span style="color: #003399;">Object</span> hint<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;thumbnails for pdf's not implemented in this tutorial...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>FileUploadService.java </p>
<p> (The service that demonstrates how the ThumbnailGeneratorEngine can be injected as interface and used to handle streams and content types for thumbnails to be generated without actually knowing the content type or stream type. This is here for demo purposes but you can inject the engine into any other service not based on file uploads)  </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.upload</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.util.thumbnails.ThumbnailGeneratorEngine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.validation.BindException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.bind.ServletRequestDataBinder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.multipart.MultipartFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.multipart.support.ByteArrayMultipartFileEditor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.ModelAndView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.servlet.mvc.SimpleFormController</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * File upload service to demonstrate files uploads and handing over the files to the thumbnail generation system
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FileUploadService <span style="color: #000000; font-weight: bold;">extends</span> SimpleFormController <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>FileUploadService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> ThumbnailGeneratorEngine thumbnailGeneratorEngine<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setThumbnailGeneratorEngine<span style="color: #009900;">&#40;</span>ThumbnailGeneratorEngine thumbnailGeneratorEngine<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">thumbnailGeneratorEngine</span> <span style="color: #339933;">=</span> thumbnailGeneratorEngine<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> ModelAndView onSubmit<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response, <span style="color: #003399;">Object</span> command, <span style="color: #003399;">BindException</span> errors<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		FileUpload fileUpload <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FileUpload<span style="color: #009900;">&#41;</span> command<span style="color: #339933;">;</span>
		MultipartFile file <span style="color: #339933;">=</span> fileUpload.<span style="color: #006633;">getFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">InputStream</span> stream <span style="color: #339933;">=</span> file.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//generate thumbnails</span>
			thumbnailGeneratorEngine.<span style="color: #006633;">generateThumbnails</span><span style="color: #009900;">&#40;</span>file.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, stream, file.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
			stream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSubmit</span><span style="color: #009900;">&#40;</span>request, response, command, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> initBinder<span style="color: #009900;">&#40;</span>HttpServletRequest request, ServletRequestDataBinder binder<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> ServletException <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// to actually be able to convert Multipart instance to byte[]</span>
		<span style="color: #666666; font-style: italic;">// we have to register a custom editor</span>
		binder.<span style="color: #006633;">registerCustomEditor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> ByteArrayMultipartFileEditor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// now Spring knows how to handle multipart object and convert them</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>FileUpload.java</strong> </p>
<p> (A simple bean that encapsulates the File reference at the time of uploading a file)</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.upload</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.multipart.MultipartFile</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A file upload object
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FileUpload <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> MultipartFile file<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> MultipartFile getFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> file<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFile<span style="color: #009900;">&#40;</span>MultipartFile file<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">file</span> <span style="color: #339933;">=</span> file<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/_paxdY-b0GRWGdggBUZx4dLONhs/0/da"><img src="http://feedads.g.doubleclick.net/~a/_paxdY-b0GRWGdggBUZx4dLONhs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/_paxdY-b0GRWGdggBUZx4dLONhs/1/da"><img src="http://feedads.g.doubleclick.net/~a/_paxdY-b0GRWGdggBUZx4dLONhs/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/QP4FDvyx0Pk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/07/12/spring-java-thumbnail-generation-based-on-content-type/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/07/12/spring-java-thumbnail-generation-based-on-content-type/</feedburner:origLink></item>
		<item>
		<title>Script – from google code svn to google code downloads</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/9i9TD6clFU4/</link>
		<comments>http://raulraja.com/2009/07/11/script-from-google-code-svn-to-google-code-downloads/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 02:28:05 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[google code]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=295</guid>
		<description><![CDATA[You may have a google code project and the project has the same layout in the google code svn repository as the target files you wish to publish in the google code download section. Manually downloading a copy of your repo and exporting to a zip file then manually uploading to google can be a pain. [...]]]></description>
			<content:encoded><![CDATA[<p>You may have a google code project and the project has the same layout in the google code svn repository as the target files you wish to publish in the google code download section.</p>
<p>Manually downloading a copy of your repo and exporting to a zip file then manually uploading to google can be a pain. The following script takes care of doing all that for you</p>
<p>And now to the point you can get the <a href="http://raulrajatutorials.googlecode.com/svn/trunk/raulraja.com.tutorials/google-code-svn-downloads/src/main/sh/"> two files here </a></p>
<p>Once you download both files and you place them in a folder make sure both are executable and to get a list of options you can run:</p>
<p><strong>./google-code-svn-to-download.sh </strong></p>
<p> Usage: ./google-code-svn-to-download.sh </p>
<p> Required parameters </p>
<p> -p Google Code project name </p>
<p> Optional parameters </p>
<p> -u Your Google Code username (will ask if not provided)<br />
 -w Your Google Code password (will ask if not provided)<br />
 -s Short description of the file (Defaults to project name)<br />
 -l A list of comma-separated labels to attach to the file<br />
 -f the name of the file to upload (Defaults to project name) </p>
<p>And here is an example of the script running passing all parameters:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
raul-raja-martinezs-macbook:<span style="color: #7a0874; font-weight: bold;">test</span> raul$ .<span style="color: #000000; font-weight: bold;">/</span>google-code-svn-to-download.sh <span style="color: #660033;">-p</span> raulrajatutorials <span style="color: #660033;">-u</span> raulraja <span style="color: #660033;">-w</span> your_google_code_pass <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;my file summary&quot;</span> <span style="color: #660033;">-l</span> tag1,tag2,tag3,tag4 <span style="color: #660033;">-f</span> myfilename
fetching raulrajatutorials from google <span style="color: #c20cb9; font-weight: bold;">svn</span> repository
cleaning up <span style="color: #7a0874; font-weight: bold;">local</span> <span style="color: #c20cb9; font-weight: bold;">svn</span> files
zipping raulrajatutorials to myfilename.zip
removing <span style="color: #c20cb9; font-weight: bold;">svn</span> downloaded files
uploading raulrajatutorials to google code downloads area
The <span style="color: #c20cb9; font-weight: bold;">file</span> was uploaded successfully.
URL: http:<span style="color: #000000; font-weight: bold;">//</span>raulrajatutorials.googlecode.com<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span>myfilename.ZIP
Removing myfilename.zip after uploaded to google</pre></div></div>

<p>This is what the script does:</p>
<ol>
<li>Download a copy of your trunk from your google code project svn repository.</li>
<li>Zip the download</li>
<li>Uploads the zipped file to your google code downloads section using this <a href="http://support.googlecode.com/svn/trunk/scripts/googlecode_upload.py">handy python script provided by google</a></li>
</ol>
<p>And in case you&#8217;re wondering about the script itself here it is. If you copy it from here don&#8217;t forget to grab also the <a href="http://support.googlecode.com/svn/trunk/scripts/googlecode_upload.py">google script here</a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #666666; font-style: italic;"># checkouts your google code project and makes a zip and uploads it to the downloads section</span>
<span style="color: #666666; font-style: italic;">#  -s SUMMARY, Short description of the file</span>
<span style="color: #666666; font-style: italic;">#  -p PROJECT, Google Code project name</span>
<span style="color: #666666; font-style: italic;">#  -u USER, Your Google Code username</span>
<span style="color: #666666; font-style: italic;">#  -w PASSWORD, Your Google Code password</span>
<span style="color: #666666; font-style: italic;">#  -l LABELS, An optional list of comma-separated labels to attach to the file</span>
<span style="color: #666666; font-style: italic;">#  -f FILENAME, the file name to be used on the upload</span>
&nbsp;
<span style="color: #007800;">SCRIPT_USAGE</span>=<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\
</span><span style="color: #000099; font-weight: bold;">\n</span>    Usage: $0
<span style="color: #000099; font-weight: bold;">\n</span>
<span style="color: #000099; font-weight: bold;">\n</span>    Required parameters
<span style="color: #000099; font-weight: bold;">\n</span>
<span style="color: #000099; font-weight: bold;">\n</span>      -p Google Code project name
<span style="color: #000099; font-weight: bold;">\n</span>
<span style="color: #000099; font-weight: bold;">\n</span>    Optional parameters
<span style="color: #000099; font-weight: bold;">\n</span>
<span style="color: #000099; font-weight: bold;">\n</span>      -u Your Google Code username (will ask if not provided)
<span style="color: #000099; font-weight: bold;">\n</span>      -w Your Google Code password (will ask if not provided)
<span style="color: #000099; font-weight: bold;">\n</span>      -s Short description of the file (Defaults to project name)
<span style="color: #000099; font-weight: bold;">\n</span>      -l A list of comma-separated labels to attach to the file
<span style="color: #000099; font-weight: bold;">\n</span>      -f the name of the file to upload (Defaults to project name)
<span style="color: #000099; font-weight: bold;">\n</span>
<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Iterate over the options passed to the script</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">getopts</span> <span style="color: #ff0000;">&quot;s:p:u:w:l:f:&quot;</span> MYOPTION;
<span style="color: #000000; font-weight: bold;">do</span>
	<span style="color: #000000; font-weight: bold;">case</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MYOPTION</span>&quot;</span> <span style="color: #000000; font-weight: bold;">in</span>
		s<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_SUMMARY</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
		p<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_PROJECT</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
		u<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_USER</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
		<span style="color: #c20cb9; font-weight: bold;">w</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_PASSWORD</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
		l<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_LABELS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
		f<span style="color: #7a0874; font-weight: bold;">&#41;</span>   <span style="color: #007800;">GOOGLE_CODE_FILENAME</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$OPTARG</span>&quot;</span> <span style="color: #000000; font-weight: bold;">;;</span>
	<span style="color: #000000; font-weight: bold;">esac</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># if all the required options are there</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-z</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${GOOGLE_CODE_PROJECT}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># One of the required arguments is missing</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$SCRIPT_USAGE</span>
<span style="color: #000000; font-weight: bold;">else</span>
     <span style="color: #666666; font-style: italic;"># setup default values if necessary</span>
    <span style="color: #007800;">GOOGLE_CODE_FILENAME</span>=<span style="color: #800000;">${GOOGLE_CODE_FILENAME:=&quot;$GOOGLE_CODE_PROJECT&quot;}</span>
    <span style="color: #007800;">GOOGLE_CODE_SUMMARY</span>=<span style="color: #800000;">${GOOGLE_CODE_SUMMARY:=&quot;latest $GOOGLE_CODE_PROJECT&quot;}</span>
    <span style="color: #666666; font-style: italic;"># fetch project from svn</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;fetching <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span> from google svn repository&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #660033;">-q</span> http:<span style="color: #000000; font-weight: bold;">//</span><span style="color: #007800;">$GOOGLE_CODE_PROJECT</span>.googlecode.com<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># zip the project</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;zipping <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span> to <span style="color: #007800;">$GOOGLE_CODE_FILENAME</span>.zip&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">zip</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-q</span> <span style="color: #007800;">$GOOGLE_CODE_FILENAME</span>.zip <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># remove the svn download folders</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;removing svn downloaded files&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># upload the project to the google code downloads</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;uploading <span style="color: #007800;">$GOOGLE_CODE_PROJECT</span> to google code downloads area&quot;</span>
    .<span style="color: #000000; font-weight: bold;">/</span>googlecode_upload.py <span style="color: #660033;">-s</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$GOOGLE_CODE_SUMMARY</span>&quot;</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$GOOGLE_CODE_PROJECT</span>&quot;</span> <span style="color: #660033;">-u</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$GOOGLE_CODE_USER</span>&quot;</span> <span style="color: #660033;">-w</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$GOOGLE_CODE_PASSWORD</span>&quot;</span> <span style="color: #660033;">-l</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$GOOGLE_CODE_LABELS</span>&quot;</span> <span style="color: #007800;">$GOOGLE_CODE_FILENAME</span>.ZIP
&nbsp;
    <span style="color: #666666; font-style: italic;"># remove the zip file</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Removing <span style="color: #007800;">$GOOGLE_CODE_FILENAME</span>.zip after uploaded to google&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #007800;">$GOOGLE_CODE_FILENAME</span>.ZIP   
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>


<p><a href="http://feedads.g.doubleclick.net/~a/q2bEGNRcsAbZMTNkd0bZld8bOLo/0/da"><img src="http://feedads.g.doubleclick.net/~a/q2bEGNRcsAbZMTNkd0bZld8bOLo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/q2bEGNRcsAbZMTNkd0bZld8bOLo/1/da"><img src="http://feedads.g.doubleclick.net/~a/q2bEGNRcsAbZMTNkd0bZld8bOLo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/9i9TD6clFU4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/07/11/script-from-google-code-svn-to-google-code-downloads/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/07/11/script-from-google-code-svn-to-google-code-downloads/</feedburner:origLink></item>
		<item>
		<title>Bayonet at The Sunset</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/K8C3elYiIn0/</link>
		<comments>http://raulraja.com/2009/07/07/bayonet-at-the-sunset/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 05:32:27 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Ballard]]></category>
		<category><![CDATA[Bayonet]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=288</guid>
		<description><![CDATA[Tawny and I went tonight to see Bayonet at The Sunset Tavern in Ballard. It was a great show. I specially liked the first song Long Shadows, although all the other songs were great too. It&#8217;s good to see how people go to a show on a Tuesday night in Ballard, I didn&#8217;t know that [...]]]></description>
			<content:encoded><![CDATA[<p>Tawny and I went tonight to see <a href="http://www.myspace.com/closequarterbattle">Bayonet</a> at <a href="http://www.sunsettavern.com/">The Sunset Tavern</a> in Ballard.<br />
It was a great show. I specially liked the first song Long Shadows, although all the other songs were great too.<br />
It&#8217;s good to see how people go to a show on a Tuesday night in Ballard, I didn&#8217;t know that there are concerts at The Sunset pretty much every night.</p>
<p>I&#8217;m Looking forward to Bayonet&#8217;s first EP.</p>
<p><a href="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.26.17.jpg"><img class="alignnone size-medium wp-image-291" title="Bayonet at The Sunset" src="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.26.17-502x200.jpg" alt="Bayonet at The Sunset" width="502" height="200" /></a></p>
<p><a href="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.18.44.jpg"><img class="alignnone size-medium wp-image-290" title="Bayonet at The Sunset" src="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.18.44-502x200.jpg" alt="Bayonet at The Sunset" width="502" height="200" /></a></p>
<p><a href="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.17.52.jpg"><img class="alignnone size-medium wp-image-289" title="Bayonet at The Sunset" src="http://raulraja.com/wp-content/uploads/2009/07/2009-07-07-21.17.52-502x200.jpg" alt="Bayonet at The Sunset" width="502" height="200" /></a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/oFyFyHCZhcYBFDvw0G8-r-ObSBA/0/da"><img src="http://feedads.g.doubleclick.net/~a/oFyFyHCZhcYBFDvw0G8-r-ObSBA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/oFyFyHCZhcYBFDvw0G8-r-ObSBA/1/da"><img src="http://feedads.g.doubleclick.net/~a/oFyFyHCZhcYBFDvw0G8-r-ObSBA/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/K8C3elYiIn0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/07/07/bayonet-at-the-sunset/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/07/07/bayonet-at-the-sunset/</feedburner:origLink></item>
		<item>
		<title>Spring – Engine / Processors pattern</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/eZkwwl8ghU8/</link>
		<comments>http://raulraja.com/2009/06/30/spring-engine-processors-pattern/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 17:27:28 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=274</guid>
		<description><![CDATA[This is a pattern that is very easy to configure and use in Spring. It allows you to map object or any arbitrary key in a map to a service that will process objects of any types. The Engine delegates calls to each one of the configured processors. Although the example is very simple and [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pattern that is very easy to configure and use in Spring. It allows you to map object or any arbitrary key in a map to a service that will process objects of any types.</p>
<p>The Engine delegates calls to each one of the configured processors. Although the example is very simple and shows no real functionality I have used this same pattern to create services that generate thumbnails, <a href="http://raulraja.com/2009/07/12/spring-java-thumbnail-generation-based-on-content-type/">process images</a>, extract text from files based on content types, etc&#8230;</p>
<p>Once the engine is configured you can smply call engine.process(object) and based on the configuration the engine will delegate to the right processor.</p>
<p>This same pattern with slight modifications can be reused to implement filters and chain of commands/responsibility.<br />
You can base the keys on the map on any other attribute besides the class.</p>
<p>This tutorial is available for download with svn:<br style="padding: 0px; margin: 0px;" />svn checkout <a style="color: #3c6c92; outline-style: none; outline-width: initial; outline-color: initial; text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #dddddd; font-weight: bold; padding: 0px; margin: 0px;" href="http://raulrajatutorials.googlecode.com/svn/trunk">http://raulrajatutorials.googlecode.com/svn/trunk</a>/ raulrajatutorials-read-only<br style="padding: 0px; margin: 0px;" />Comments in the code itself should be self-explanatory if you already have some java + spring experience.</p>
<p>And now to the point.</p>
<p><strong>applicationContext.xml</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">default-autowire</span>=<span style="color: #ff0000;">&quot;byName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- A processor that know about Person objects --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;personProcessor&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.processor.impl.PersonProcessorImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- A processor that knows about Place objects --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;placeProcessor&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.processor.impl.PlaceProcessorImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- A configured processing engine that maps object types to their processors and delegates calls to each one of them --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;processingEngine&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.processor.impl.DefaultProcessingEngineImpl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;processors&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;com.raulraja.domain.Person&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;personProcessor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;com.raulraja.domain.Place&quot;</span> <span style="color: #000066;">value-ref</span>=<span style="color: #ff0000;">&quot;placeProcessor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/map<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">        This handler is actually not required but used for demonstrating the tutorial by invoking</span>
<span style="color: #808080; font-style: italic;">        http://localhost:port/tutorialName</span>
<span style="color: #808080; font-style: italic;">    --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;serviceInvokerHandler&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.util.handler.impl.ServiceInvokerHandlerImpl&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>ProcessingEngine.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.processor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A processing engine that you can feed objects to and processes them
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ProcessingEngine<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Processes something
	 * @param target the target object to be processed
	 */</span>
	<span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Target target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>DefaultProcessingEngineImpl.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.processor.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.processor.ProcessingEngine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.processor.Processor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Default impl of the processing engine based on a spring configured service.
 * Delegates processing calls to the right processor
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DefaultProcessingEngineImpl<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">implements</span> ProcessingEngine<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>DefaultProcessingEngineImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * A map of processor that maps object types to a processor that know how to process it
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span>, Processor<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;&gt;</span> processors<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProcessors<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span>Class<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span>, Processor<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;&gt;</span> processors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">processors</span> <span style="color: #339933;">=</span> processors<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Processes something
	 *
	 * @param target the target object to be processed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Target target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;started processing &quot;</span> <span style="color: #339933;">+</span> target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Processor<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span> processor <span style="color: #339933;">=</span> processors.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>target.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>processor <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			processor.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;processor not found for &quot;</span> <span style="color: #339933;">+</span> target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #009900;">&#125;</span>
		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;finished processing &quot;</span> <span style="color: #339933;">+</span> target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Processor.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.processor</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * A service that know how to process a type of object
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Processor<span style="color: #339933;">&lt;</span>Target<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Processes something
	 * @param target the target object to be processed
	 */</span>
	<span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Target target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>PersonProcessorImpl.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.processor.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.domain.Person</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.processor.Processor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Demonstrates how a Person can be processed
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PersonProcessorImpl <span style="color: #000000; font-weight: bold;">implements</span> Processor<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>PersonProcessorImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Processes something
	 *
	 * @param person the target object to be processed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span>person <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; something is being done to a person object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>PlaceProcessorImpl.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.processor.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.domain.Place</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.processor.Processor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Demonstrates how a Person can be processed
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PlaceProcessorImpl <span style="color: #000000; font-weight: bold;">implements</span> Processor<span style="color: #339933;">&lt;</span>Place<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>PlaceProcessorImpl.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Processes something
	 *
	 * @param place the target object to be processed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Place place<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   		log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span>place <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; something is being done to a place object&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Person.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.domain</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Represents a person
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Place.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.domain</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Represents a place
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Place <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>ServiceInvokerHandlerImpl.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.util.handler.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.processor.ProcessingEngine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.domain.Person</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.domain.Place</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.HttpRequestHandler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Frontend entry point for the demo
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServiceInvokerHandlerImpl <span style="color: #000000; font-weight: bold;">implements</span> HttpRequestHandler <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * The engine that processes object types
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> ProcessingEngine<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> processingEngine<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * sets the engine that processes object types
	 * @param processingEngine the engine
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProcessingEngine<span style="color: #009900;">&#40;</span>ProcessingEngine<span style="color: #339933;">&lt;</span>Object<span style="color: #339933;">&gt;</span> processingEngine<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">processingEngine</span> <span style="color: #339933;">=</span> processingEngine<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Process the given request, generating a response.
	 *
	 * @param request  current HTTP request
	 * @param response current HTTP response
	 * @throws javax.servlet.ServletException in case of general errors
	 * @throws java.io.IOException			in case of I/O errors
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> handleRequest<span style="color: #009900;">&#40;</span>HttpServletRequest request, HttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Place place <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Place<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		processingEngine.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		processingEngine.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>place<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output</strong></p>
<p>07/01 08:53:59 DEBUG com.raulraja.processor.impl.DefaultProcessingEngineImpl &#8211; started processing com.raulraja.domain.Person@53fb687a<br />
07/01 08:53:59 DEBUG com.raulraja.processor.impl.PersonProcessorImpl &#8211; com.raulraja.domain.Person@53fb687a <strong>something is being done to a person object</strong><br />
07/01 08:53:59 DEBUG com.raulraja.processor.impl.DefaultProcessingEngineImpl &#8211; finished processing com.raulraja.domain.Person@53fb687a<br />
07/01 08:53:59 DEBUG com.raulraja.processor.impl.DefaultProcessingEngineImpl &#8211; started processing com.raulraja.domain.Place@a427a0b<br />
07/01 08:53:59 DEBUG com.raulraja.processor.impl.PlaceProcessorImpl &#8211; com.raulraja.domain.Place@a427a0b<strong> something is being done to a place object</strong><br />
07/01 08:53:59 DEBUG com.raulraja.processor.impl.DefaultProcessingEngineImpl &#8211; finished processing com.raulraja.domain.Place@a427a0b</p>

<p><a href="http://feedads.g.doubleclick.net/~a/4PZELnJDoLex2V9jz7Vs2PV5vhY/0/da"><img src="http://feedads.g.doubleclick.net/~a/4PZELnJDoLex2V9jz7Vs2PV5vhY/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/4PZELnJDoLex2V9jz7Vs2PV5vhY/1/da"><img src="http://feedads.g.doubleclick.net/~a/4PZELnJDoLex2V9jz7Vs2PV5vhY/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/eZkwwl8ghU8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/06/30/spring-engine-processors-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/06/30/spring-engine-processors-pattern/</feedburner:origLink></item>
		<item>
		<title>Spring – Exposing a bean as both REST (xml, json, …) and SOAP WebServices</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/BVuEumzwiAU/</link>
		<comments>http://raulraja.com/2009/06/25/spring-exposing-a-bean-as-rest-xml-json-and-soap-webservice/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 00:10:12 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WebServices]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=248</guid>
		<description><![CDATA[Often times you have Spring services that you want to expose as web-services. In this small tutorial I&#8217;ll just show a few configuration files and code that demonstrate how a service and its implementation can accommodate both REST and SOAP access to the same backbone. Thanks Justin for the REST part. This tutorial is available [...]]]></description>
			<content:encoded><![CDATA[<p>Often times you have Spring services that you want to expose as web-services. In this small tutorial I&#8217;ll just show a few configuration files and code that demonstrate how a service and its implementation can accommodate both REST and SOAP access to the same backbone.</p>
<p>Thanks Justin for the REST part.</p>
<p>This tutorial is available for download with svn:<br />
svn checkout <a href="http://raulrajatutorials.googlecode.com/svn/trunk">http://raulrajatutorials.googlecode.com/svn/trunk</a>/ raulrajatutorials-read-only<br />
Comments in the code itself should be self-explanatory if you already have some java + spring experience.<br />
I have not included the dependencies on CXF for simplicity but you can take a look at them and some for the other tutorials <a href="http://raulrajatutorials.googlecode.com/svn/trunk/raulraja.com.tutorials/pom.xml">here</a>. </p>
<p>And now to the point.</p>
<p><strong>applicationContext.xml</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:cxf</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/core&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:jaxws</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxws&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:jaxrs</span>=<span style="color: #ff0000;">&quot;http://cxf.apache.org/jaxrs&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;</span>
<span style="color: #009900;">       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/core</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/core.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/jaxws</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/jaxws.xsd</span>
<span style="color: #009900;">       http://cxf.apache.org/jaxrs</span>
<span style="color: #009900;">       http://cxf.apache.org/schemas/jaxrs.xsd&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">default-autowire</span>=<span style="color: #ff0000;">&quot;byName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Load CXF modules from cxf.jar --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-soap.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-servlet.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;import</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:bus<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cxf:logging</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cxf:features<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cxf:bus<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- The hello world service --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helloWorldService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.raulraja.ws.impl.HelloWorldServiceImpl&quot;</span> <span style="color: #000066;">autowire</span>=<span style="color: #ff0000;">&quot;autodetect&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Exposing the Helloworld service as a rest service --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:server</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;restServer&quot;</span> <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;/rest/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:serviceBeans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;helloWorldService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:serviceBeans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxrs:extensionMappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;feed&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/atom+xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;json&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/json&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;xml&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;html&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:extensionMappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxrs:server<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Exposing the HelloWorld service as a SOAP service --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jaxbBean&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.jaxb.JAXBDataBinding&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;prototype&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;jaxws-and-aegis-service-factory&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean&quot;</span></span>
<span style="color: #009900;">          <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;prototype&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataBinding&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;jaxbBean&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;serviceConfigurations&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.jaxws.support.JaxWsServiceConfiguration&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.aegis.databinding.AegisServiceConfiguration&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.cxf.service.factory.DefaultServiceConfiguration&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:endpoint</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;helloWorldServiceEndpoint&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">serviceName</span>=<span style="color: #ff0000;">&quot;HelloWorld&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">implementorClass</span>=<span style="color: #ff0000;">&quot;com.raulraja.ws.HelloWorldService&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">implementor</span>=<span style="color: #ff0000;">&quot;#helloWorldService&quot;</span></span>
<span style="color: #009900;">                    <span style="color: #000066;">address</span>=<span style="color: #ff0000;">&quot;/helloWorldService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jaxws:serviceFactory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;jaxws-and-aegis-service-factory&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:serviceFactory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jaxws:endpoint<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>HelloWorldService.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.ws</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.Path</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.GET</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jws.WebService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jws.WebMethod</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * The hello world service interface exposed as soap and rest
 *
 * @ WebService is for soap
 * @ Path is for the rest top path
 */</span>
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/helloWorldService/&quot;</span><span style="color: #009900;">&#41;</span>
@WebService<span style="color: #009900;">&#40;</span>serviceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HelloWorld&quot;</span>, name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HelloWorldService&quot;</span>, targetNamespace <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://ws.raulraja.com&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> HelloWorldService <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Simple methods that says hello
	 *
	 * @return hello world rest and soap!!!
	 * @ WebMethod is for soap
	 * @ GET is for REST
	 * @ Path is for the REST service path
	 */</span>
	@WebMethod
	@GET
	@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span>
	HelloWorld sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>HelloWorldServiceImpl.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.ws.impl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.ws.HelloWorld</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.raulraja.ws.HelloWorldService</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Implementation of the hello world web service.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorldServiceImpl <span style="color: #000000; font-weight: bold;">implements</span> HelloWorldService <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Simple methods that says hello
	 *
	 * @return hello world rest and soap!!!
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> HelloWorld sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		HelloWorld helloWorld <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HelloWorld<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		helloWorld.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello world rest and soap!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> helloWorld<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>HelloWorld.java</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.raulraja.ws</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Hello world object that demonstrates the service
 */</span>
@XmlRootElement
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * the message
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @return the message
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * @param message sets the message
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>And here is some screen captures for the results&#8230;</strong></p>
<p><strong>Soap Services list</strong></p>
<div id="attachment_251" class="wp-caption alignnone" style="width: 512px"><a href="http://raulraja.com/wp-content/uploads/2009/06/Picture-3.png"><img src="http://raulraja.com/wp-content/uploads/2009/06/Picture-3-502x200.png" alt="CXF exposed soap services" title="CXF exposed soap services" width="502" height="200" class="size-medium wp-image-251" /></a><p class="wp-caption-text">CXF exposed soap services</p></div>
<p><strong>Hello World Service WSDL (autogenerated)</strong></p>
<div id="attachment_252" class="wp-caption alignnone" style="width: 512px"><a href="http://raulraja.com/wp-content/uploads/2009/06/Picture-4.png"><img src="http://raulraja.com/wp-content/uploads/2009/06/Picture-4-502x200.png" alt="Hello World Service WSDL" title="Hello World Service WSDL" width="502" height="200" class="size-medium wp-image-252" /></a><p class="wp-caption-text">Hello World Service WSDL</p></div>
<p><strong>Hello World Service REST XML Result (add .json or .xml, &#8230; at the end of the service path to get results in the different format exposed)</strong></p>
<div id="attachment_253" class="wp-caption alignnone" style="width: 512px"><a href="http://raulraja.com/wp-content/uploads/2009/06/Picture-5.png"><img src="http://raulraja.com/wp-content/uploads/2009/06/Picture-5-502x200.png" alt="Hello World REST xml result" title="Hello World REST xml result" width="502" height="200" class="size-medium wp-image-253" /></a><p class="wp-caption-text">Hello World REST xml result</p></div>

<p><a href="http://feedads.g.doubleclick.net/~a/d_Kn4TXDD70hlw-bWS4iTOqodnE/0/da"><img src="http://feedads.g.doubleclick.net/~a/d_Kn4TXDD70hlw-bWS4iTOqodnE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/d_Kn4TXDD70hlw-bWS4iTOqodnE/1/da"><img src="http://feedads.g.doubleclick.net/~a/d_Kn4TXDD70hlw-bWS4iTOqodnE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/BVuEumzwiAU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/06/25/spring-exposing-a-bean-as-rest-xml-json-and-soap-webservice/feed/</wfw:commentRss>
		<slash:comments>66</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/06/25/spring-exposing-a-bean-as-rest-xml-json-and-soap-webservice/</feedburner:origLink></item>
		<item>
		<title>Mero Gigante en la Azohia, Cartagena</title>
		<link>http://feedproxy.google.com/~r/raulraja/~3/ZeGqjSIIeW0/</link>
		<comments>http://raulraja.com/2009/06/21/mero-gigante-en-la-azohia-cartagena/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 06:11:37 +0000</pubDate>
		<dc:creator>Raul Raja</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cartagena]]></category>

		<guid isPermaLink="false">http://raulraja.com/?p=224</guid>
		<description><![CDATA[This is real, a while ago very close to were I was born. People, friends of my grandpa.]]></description>
			<content:encoded><![CDATA[<p>This is real, a while ago <a href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=azohia+cartagena&amp;sll=37.0625,-95.677068&amp;sspn=34.396866,60.205078&amp;ie=UTF8&amp;ll=37.563086,-1.170044&amp;spn=0.26888,0.470352&amp;z=11&amp;iwloc=A">very close to were I was born</a>. People, friends of my grandpa.</p>
<div id="attachment_225" class="wp-caption alignnone" style="width: 512px"><a href="http://raulraja.com/wp-content/uploads/2009/06/Mero_pescado_en_la_Azoh_a_Cartagena_nov_1967.jpg"><img class="size-medium wp-image-225" title="Mero Azohia 1967" src="http://raulraja.com/wp-content/uploads/2009/06/Mero_pescado_en_la_Azoh_a_Cartagena_nov_1967-502x200.jpg" alt="Mero Azohia 1967" width="502" height="200" /></a><p class="wp-caption-text">Mero Azohia 1967</p></div>

<p><a href="http://feedads.g.doubleclick.net/~a/T7ixhGz0vQDeLfHtnCCLL-7rNDs/0/da"><img src="http://feedads.g.doubleclick.net/~a/T7ixhGz0vQDeLfHtnCCLL-7rNDs/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/T7ixhGz0vQDeLfHtnCCLL-7rNDs/1/da"><img src="http://feedads.g.doubleclick.net/~a/T7ixhGz0vQDeLfHtnCCLL-7rNDs/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/raulraja/~4/ZeGqjSIIeW0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://raulraja.com/2009/06/21/mero-gigante-en-la-azohia-cartagena/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://raulraja.com/2009/06/21/mero-gigante-en-la-azohia-cartagena/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 11.070 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-11 07:20:32 -->

