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

<channel>
	<title>Pragmatic Programmer Issues - pietrowski.info &#187; java</title>
	<atom:link href="http://pietrowski.info/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://pietrowski.info</link>
	<description></description>
	<lastBuildDate>Tue, 27 Jul 2010 07:31:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Pedro Newsletter 16.07.2010</title>
		<link>http://pietrowski.info/2010/07/pedro-newsletter-16-07-2010/</link>
		<comments>http://pietrowski.info/2010/07/pedro-newsletter-16-07-2010/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 20:51:40 +0000</pubDate>
		<dc:creator>pedro</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[news]]></category>

		<guid isPermaLink="false">http://pietrowski.info/?p=345</guid>
		<description><![CDATA[Google and SpringSource are closer and closer. New SpringSource Tool Suite have additional support for Google stuff. Really cook &#8211; Practical Designer 1.3 preview. A lot of Scala libraries updates (as 2.8 is finally released). And of course there is a lot of buzz over that. Spring Roo now integrated with SpringDS (Flex), SpringSource post [...]]]></description>
			<content:encoded><![CDATA[<ul style="clear: both">
<li>Google and SpringSource are <a href="http://www.springsource.org/node/2713">closer and closer</a>. New SpringSource Tool Suite have additional support for Google stuff.</li>
<li>Really cook &#8211; <a href="http://vimeo.com/13393974">Practical Designer 1.3 preview</a>.</li>
<li>A lot of Scala libraries updates (as 2.8 is finally released). And of course there is a lot of <a href="http://www.artima.com/forums/flat.jsp?forum=270&#038;thread=298244">buzz over that</a>.</li>
<li>Spring Roo now integrated with SpringDS (Flex), SpringSource <a href="http://blog.springsource.com/2010/07/14/introducing-the-flex-addon-for-spring-roo">post some example of that</a>.</li>
<li>Django Admin extension to provide <a href="http://opensource.washingtontimes.com/blog/post/coordt/2010/07/creating-alphabetical-filter-djangos-admin/">Alphabet Filer</a>.</li>
<li>New <a href="http://blogs.jetbrains.com/idea/2010/07/password-safe/">IntelliJ Idea version</a> (9.0.3) have safe password storing support. Great work!</li>
<li>Last part of <a href="http://tapity.com/iphone-app-marketing/lessons-learned-from-apple-part-3-of-3">Lesson Learned from Apple</a>.</li>
<li>Project Coin &#8211; Automatic Resource Management spec <a href="http://blogs.sun.com/darcy/entry/project_coin_updated_arm_spec">updated</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://pietrowski.info/2010/07/pedro-newsletter-16-07-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring jdbc namespace</title>
		<link>http://pietrowski.info/2010/07/spring-jdbc-namespace/</link>
		<comments>http://pietrowski.info/2010/07/spring-jdbc-namespace/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 12:51:45 +0000</pubDate>
		<dc:creator>pedro</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://pietrowski.info/2010/07/spring-jdbc-namespace/</guid>
		<description><![CDATA[One of the new feature of Spring Framework 3.0 is more namespaces. Today I want to write about jdbc namespace, which I&#8217;m using in last project. The namespace is really simple, it has two main elements initialize-database &#8211; which allow us to initialize database with scripts. embedded-database &#8211; which allow us to start embedded database. [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both;">One of the new feature of Spring Framework 3.0 is more namespaces. Today I want to write about jdbc namespace, which I&#8217;m using in last project.</p>
<p>The namespace is really simple, it has two main elements</p>
<ul style="clear: both;">
<li>initialize-database &#8211; which allow us to initialize database with scripts.</li>
<li>embedded-database &#8211; which allow us to start embedded database.</li>
</ul>
<p style="clear: both;">
<h3>initialize-database</h3>
<p style="clear: both;">
<p style="clear: both;">So we can use that xml tag to initialize our datasource with SQL scripts. There are some properties we should set:</p>
<ul style="clear: both;">
<li>data-source &#8211; which is javax.sql.DataSource bean reference. It will be used to connect and execute all the scripts.</li>
<li>enabled &#8211; if we should do anything so if we are enabled or not. (remember it can be property ${} or even spring EL), by default it is enabled.</li>
<li>ignore-failures &#8211; if we should ignore when statements end with errors.
<ul style="clear: both;">
<li>NONE &#8211; do not ignore <strong>default value.</strong></li>
<li>DROPS &#8211; we are ignoring failed DROP statements.</li>
<li>ALL &#8211; we ignore all failed statements.</li>
</ul>
</li>
</ul>
<p style="clear: both;">And what is more important this tag has zero or more scripts tags. All this tags has location property which behave exactly the same as spring resources. In my application I create schema for production (I don&#8217;t use any ORM) and for tests I have the same schema with one more script (test data/fixtures &#8211; no more DbUnit problems <img src='http://pietrowski.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<p style="clear: both;">It is soooo simple.</p>
<pre id="line92" class="brush:xml">&lt;jdbc:initialize-database data-source="dataSource"&gt;
&lt;jdbc:script location="classpath:/schema.sql"&gt;
&lt;/jdbc:script&gt;&lt;/jdbc:initialize-database&gt;</pre>
<h3>embedded-database</h3>
<p style="clear: both;">
<p style="clear: both;">The second tag, is really helpful when you want to use embedded database. Embedded database is helpful for things like storing configuration or other persistent application stuff.</p>
<p>It allow to simple start embedded database and make them visible as javax.sql.DataSource, ready to be injected to our JDBCTemplete objects. It has just two attributes:</p>
<ul style="clear: both;">
<li>id &#8211; which value becomes the bean name.</li>
<li>type &#8211; embedded database type</li>
</ul>
<p style="clear: both;">Actually those types are supported:</p>
<ul style="clear: both;">
<li>H2 &#8211; <a href="http://www.h2database.com/html/main.html">H2 database Engine</a>.</li>
<li>DERBY &#8211; <a href="http://db.apache.org/derby/">Apache Derby database</a>.</li>
<li>HSQL &#8211; <a href="http://hsqldb.org/">HSQLDB</a>. This one is default.</li>
</ul>
<pre class="brush:xml">
<jdbc:embedded-database id="datasource" type="H2" >
        <jdbc:script location="classpath:/schema.sql"/>
        <jdbc:script location="classpath:/test/data.sql"/>
    </jdbc:embedded-database>
</pre>
<p style="clear: both;">
<h3>Under The Hood</h3>
<p style="clear: both;">
<p style="clear: both;">All things starts at <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/config/JdbcNamespaceHandler.html">JdbcNamespaceHandler</a> class, which register two parsers for elements. There is <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.html">EmbeddedDatabaseBeanDefinitionParser</a> for <strong>embedded-database</strong> element and <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.html">InitializeDatabaseBeanDefinitionParser</a> for <strong>initialize-database</strong>.</p>
<p>When everything is ok this elements build and configure two beans.</p>
<ul style="clear: both;">
<li><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/datasource/init/DataSourceInitializer.html"><strong>DataSourceInitializer</strong></a> is build for <strong>initialize-database </strong>element and all configuration is embedded in <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/datasource/init/DatabasePopulator.html">DatabasePopulator</a> property which will do all the work. BTW you can use <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.html">ResourceDatabasePopulator</a> if you need functionality like executing statements from scripts.</li>
<li><strong><em><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactoryBean.html">EmbeddedDatabaseFactoryBean</a> </em></strong>is build for <strong>embedded-database</strong> element. Database is configured by <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseConfigurer.html">EmbeddedDatabaseConfigurer</a> which has subclass per every supported Database.</li>
</ul>
<p style="clear: both;">And that&#8217;s all.</p>
<p><br class="final-break" style="clear: both;" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pietrowski.info/2010/07/spring-jdbc-namespace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring MVC Exception Handler</title>
		<link>http://pietrowski.info/2010/06/spring-mvc-exception-handler/</link>
		<comments>http://pietrowski.info/2010/06/spring-mvc-exception-handler/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 09:38:19 +0000</pubDate>
		<dc:creator>pedro</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[springframework]]></category>

		<guid isPermaLink="false">http://pietrowski.info/2010/06/spring-mvc-exception-handler/</guid>
		<description><![CDATA[In previous version of Spring Framework to handle exception we used HandlerExceptionResolver interface. Spring also provides simple implementations which was suitable in most of cases. Since 3.0 version we have few additional exception resolvers plus we can use annotation to specify our exception handler methods or classes without implementing HandleExceptionResolver interface. AnnotationMethodHandlerExceptionResolver (new in 3.0) [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both">In previous version of Spring Framework to handle exception we used HandlerExceptionResolver interface. Spring also provides simple implementations which was suitable in most of cases. Since 3.0 version we have few additional exception resolvers plus we can use annotation to specify our exception handler methods or classes without implementing HandleExceptionResolver interface.</p>
<ol style="clear: both">
<li><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.html">AnnotationMethodHandlerExceptionResolver</a> (new in 3.0)</li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.html">ResponseStatusExceptionResolver</a> (new in 3.0)</li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html" title="">DefaultHandlerExceptionResolver</a> (new in 3.0)</li>
<li><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/portlet/handler/SimpleMappingExceptionResolver.html">SimpleMappingExceptionResolver</a></li>
</ol>
<p style="clear: both">
<h3>SimpleMappingExceptionResolver</h3>
</p>
<p style="clear: both">Should be configured in XML, there are few properties which drives this resolver behaviors. </p>
<ul style="clear: both">
<li>defaultErrorView -> this view name will be returned if no mapping will be found.</li>
<li>defaultStatusCode -> once the view name was resolved if the view hasn&#8217;t status code the <em><strong>defaultStatusCode</strong></em> will be applied.</li>
<li>statusCodes -> Map with &#8220;view name&#8221; to &#8220;status code&#8221; mapping.</li>
<li>exceptionAttribute -> the name of attribute where exception is hold by default &#8220;exception&#8221; is used.</li>
<li>exceptionMappings -> Properties which maps Exception &#8220;class name&#8221; to &#8220;view name&#8221;. Watch out as you can easily maps more than you want, this is because only String.contains is checked, so <em>a=viewName</em> mapping will catch all exception with &#8220;a&#8221; in the name. <br />Another thing to consider is that if you provide deep Exception hierarchy (<strong>n</strong>) and you provide a lot of exception mapping (<strong>m</strong>), you will end up with <strong>O(n*m)</strong> lookup algorithm. It may seem not problem, but if somebody spots that (Error page which render time is quite long), than it may be used with DDOS to increase load on ours machines. </li>
</ul>
<p style="clear: both">
<h3>DefaultHandlerExceptionResolver</h3>
</p>
<p style="clear: both"> First at all it has pageNotFound logger which is <strong>org.springframework.web.servlet.PageNotFound </strong>logger category, second it decides what to do depending on exception type, it is base class to extend when we want change default behavior:</p>
<ul style="clear: both">
<li>NoSuchRequestHandlingMethodException : no request handler method was found by default it sends 404 error. Override <em><strong>handleNoSuchRequestHandlingMethod</strong></em>.</li>
<li>HttpRequestMethodNotSupportedException : no request handler method was found for the particular HTTP request method by default 405 error.Override <em><strong>handleHttpRequestMethodNotSupported</strong></em>.</li>
<li>HttpMediaTypeNotSupportedException : no HttpMessageConverter were found for the PUT or POSTed content by default 415 error. Override <strong><em>handleHttpMediaTypeNotSupported</em></strong>.</li>
<li>HttpMediaTypeNotAcceptableException : no HttpMessageConverter were found that were acceptable for the client (Accept header) by default 406 error. Override <em><strong>handleHttpMediaTypeNotAcceptable</strong></em>.</li>
<li>MissingServletRequestParameterException : required parameter is missing by default 400 error. Override <em><strong>handleMissingServletRequestParameter</strong></em>.</li>
<li>ConversionNotSupportedException : WebDataBinder conversion cannot occur by default 500 error. Override <strong><em>handleConversionNotSupported</em></strong>.</li>
<li>TypeMismatchException : WebDataBinder conversion error occurs by default 400. Override <strong><em>handleTypeMismatch</em></strong>.</li>
<li>HttpMessageNotReadableException : HttpMessageConverter cannot read from HTTP request by default 400 error. Override <strong><em>handleHttpMessageNotReadable</em></strong>.</li>
<li>HttpMessageNotWritableException : HttpMessageConverter cannot write to HTTP response by default 500 error. Override <strong><em>handleHttpMessageNotWritable</em></strong>.</li>
</ul>
<p style="clear: both">
<h3>ResponseStatusExceptionResolver</h3>
</p>
<p style="clear: both">This resolver allows us to use @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html">ResponseStatus</a> annotation. If we annotate our Exception with @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html">ResponseStatus</a> annotation than the response will get status code from annotation. </p>
<ul style="clear: both">
<li>ResponseStatus.value as status code (it is HttpStatus enum)</li>
<li>ResponseStatus.reason as reason or default HttpResponse reason if not set. </li>
</ul>
<p style="clear: both">
<h3>AnnotationMethodHandlerExceptionResolver</h3>
</p>
<p style="clear: both">Last but not least, and from my point of view this resolver is the most important. This exception resolver allows us to use @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a> annotation, every method annotated by @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a> will become exception handler. As parameters @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a> need an array of Throwable. We also should use @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html">ResponseStatus</a> to indicate status code.</p>
<p style="clear: both">The method signatures possibilities are vary so see documentation to get all the proper combinations of parameters and return types. </p>
<p>The idea is pretty simple, for all the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a>.value exception the exception -> Method handler is created, when Exception happens Method will be invoked.</p>
<p style="clear: both">
<h3>Conclusions</h3>
</p>
<p style="clear: both">Since 3.0 we rather has no need to setup handlers by ourself, the most useful way is to use @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a> annotation. By default DispatcherServlet will setup <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/portlet/mvc/annotation/AnnotationMethodHandlerExceptionResolver.html">AnnotationMethodHandlerExceptionResolver</a>, <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.html">ResponseStatusExceptionResolver</a> and <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.html" title="Untitled">DefaultHandlerExceptionResolver</a> as handlers resolvers. So we are ready to start with annotation based approach.</p>
<p>My approach is that I try to create moduleExceptionHandler class which has all methods annotated by @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html">ExceptionHandler</a> (except utility methods of course) and I always annotate them by @<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/ResponseStatus.html">ResponseStatus</a> as well. The methods parameters and return type vary depending on my needs. It a little bit central approach but it avoids problem with more than one handler per Exception.</p>
<p>If you need a code, please send me a comment. I&#8217;m currently in the process of choosing git repository.</p>
<p><br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pietrowski.info/2010/06/spring-mvc-exception-handler/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Pedro Newsletter 11.06.2010</title>
		<link>http://pietrowski.info/2010/06/pedro-newsletter-11-06-2010/</link>
		<comments>http://pietrowski.info/2010/06/pedro-newsletter-11-06-2010/#comments</comments>
		<pubDate>Sat, 12 Jun 2010 02:00:28 +0000</pubDate>
		<dc:creator>pedro</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://pietrowski.info/2010/06/pedro-newsletter-11-06-2010/</guid>
		<description><![CDATA[Today is shopping day @ SF, I&#8217;ve got a long list of stuff to buy, we&#8217;ll see if I manage that . AWS management console has S3 support, so you can download, delete etc your stuff from console For World Cup 2010 fans Google provide iGoole Gadget, iGoogle new Team Themas and Chrome extension. Setup [...]]]></description>
			<content:encoded><![CDATA[<p style="clear: both">Today is shopping day @ SF, I&#8217;ve got a long list of stuff to buy, we&#8217;ll see if I manage that <img src='http://pietrowski.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<ul style="clear: both">
<li><a href="http://aws.typepad.com/aws/2010/06/aws-management-console-now-supports-amazon-s3.html">AWS management console has S3 support</a>, so you can download, delete etc your stuff from console</li>
<li>For World Cup 2010 fans Google provide iGoole Gadget, iGoogle new Team Themas and Chrome extension. <a href="http://googleblog.blogspot.com/2010/06/fifacom-and-google-team-up-to-help-fans.html">Setup your Team Theme</a>.</li>
<li>Interesting in BP oil spill data, <a href="http://slashdot.org/story/10/06/11/1739253/DoE-Posts-Raw-Data-From-Oil-Spill-Coast-Guard-Asks-For-Tech-Help">read this</a> and go o US Department of Energy page.</li>
<li>Really <a href="http://www.nytimes.com/2010/06/10/technology/personaltech/10pogue.html?pagewanted=print">interesting article about new app phone</a> by David Pogue. Must read it. </li>
<li>Great free <a href="http://www.boingboing.net/2010/06/10/cc-licensed-book-on.html">book for kids to teach Python</a>. You may buy also printed version from amazon as well.</li>
</ul>
<p> <br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pietrowski.info/2010/06/pedro-newsletter-11-06-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Master</title>
		<link>http://pietrowski.info/2010/06/java-master/</link>
		<comments>http://pietrowski.info/2010/06/java-master/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 18:26:39 +0000</pubDate>
		<dc:creator>pedro</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://pietrowski.info/2010/06/java-master/</guid>
		<description><![CDATA[That was great training and from today I will use Java Master title with pleasure. Every java developer should get this training. And here is my online certificate.]]></description>
			<content:encoded><![CDATA[<p style="clear: both"><a href="http://pietrowski.info/wp-content/uploads/2010/06/Certificate_MASTER_Pietrowski1.png" class="image-link"><img class="linked-to-original" src="http://pietrowski.info/wp-content/uploads/2010/06/Certificate_MASTER_Pietrowski1-thumb.png" height="304" align="left" width="380" style=" display: inline; float: left; margin: 0 10px 10px 0;" /></a>That was great training and from today I will use Java Master title with pleasure. Every java developer should get this training. And <a href="http://tinyurl.com/pedroJavaMaster">here is my online certificate</a>.   </p>
<p><br class="final-break" style="clear: both" /></p>
]]></content:encoded>
			<wfw:commentRss>http://pietrowski.info/2010/06/java-master/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
