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

<channel>
	<title>source code bean</title>
	<atom:link href="http://sourcecodebean.com/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>thoughts and ideas from a .net developer</description>
	<lastBuildDate>Tue, 19 Feb 2013 05:34:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Sometimes Java is just too verbose</title>
		<link>http://sourcecodebean.com/archives/sometimes-java-is-just-too-verbose/1845</link>
		<comments>http://sourcecodebean.com/archives/sometimes-java-is-just-too-verbose/1845#comments</comments>
		<pubDate>Tue, 19 Feb 2013 05:33:54 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1845</guid>
		<description><![CDATA[<p>Sorting a list in Java:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Collections.<span class="me1">sort</span><span class="br0">&#40;</span>list, new Comparator&lt;SomeClass&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">int</span> compare<span class="br0">&#40;</span>SomeClass a, SomeClass b<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>a.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> == b.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> a.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; b.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ? <span class="nu0">-1</span> : <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p></code></p>
<p>Sorting the same list in C#:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">list = list.<span class="me1">OrderBy</span><span class="br0">&#40;</span>x =&gt; x.<span class="me1">Value</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>&#8230; Java need LINQ.</p>
]]></description>
			<content:encoded><![CDATA[<p>Sorting a list in Java:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Collections.<span class="me1">sort</span><span class="br0">&#40;</span>list, new Comparator&lt;SomeClass&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">int</span> compare<span class="br0">&#40;</span>SomeClass a, SomeClass b<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>a.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> == b.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> a.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; b.<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ? <span class="nu0">-1</span> : <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p></code></p>
<p>Sorting the same list in C#:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">list = list.<span class="me1">OrderBy</span><span class="br0">&#40;</span>x =&gt; x.<span class="me1">Value</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>&#8230; Java need LINQ.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/sometimes-java-is-just-too-verbose/1845/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cleaning up JDBC bolierplate code</title>
		<link>http://sourcecodebean.com/archives/cleaning-up-jdbc-bolierplate-code/1799</link>
		<comments>http://sourcecodebean.com/archives/cleaning-up-jdbc-bolierplate-code/1799#comments</comments>
		<pubDate>Tue, 05 Feb 2013 09:58:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1799</guid>
		<description><![CDATA[<p>Recently I have worked with a code base that has a lot of JDBC boilerplate code. The same code is repeated over and over again and can basically be broken down into the following pattern:</p>
<p><code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AConnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Connection</span></a> conn = &nbsp;<span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; conn = ... <span class="co1">// get the connection</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// build query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; stmt = conn.<span class="me1">prepareStatement</span><span class="br0">&#40;</span>query<span class="br0">&#41;</span> <span class="co1">// create prepared statement</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; rs = stmt.<span class="me1">executeQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// execute the query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; returnObject = toReturnObject<span class="br0">&#40;</span>rs<span class="br0">&#41;</span>; <span class="co1">// convert the resultset to a DTO, often right in the method, bloating it terrible</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; rs = <span class="kw2">null</span>; <span class="co1">// close all the stuff</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; conn = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> returnObject;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// handle potential exceptions</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARuntimeException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RuntimeException</span></a><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> <span class="kw2">finally</span> <span class="br0">&#123;</span> <span class="co1">// finally make sure everything is closed...</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>rs != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>stmt != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>conn != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></code></p>
<p>I really don&#8217;t like repeated code, so I wanted to get rid of this, but without bringing in any new library dependencies. So I wrote this very tiny class to assemble most of the repeated code in the pattern above:</p>
<p><code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">interface</span> QueryInput&lt;T&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> getPreparedStatment<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> T toReturnObject<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> QueryExecutor&lt;TReturn&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> TReturn execute<span class="br0">&#40;</span>QueryInput&lt;TReturn&gt; queryInput<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AConnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Connection</span></a> conn = &nbsp;<span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; TReturn returnObject = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt = queryInput.<span class="me1">getPreparedStatment</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn = stmt.<span class="me1">getConnection</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = stmt.<span class="me1">executeQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnObject = queryInput.<span class="me1">toReturnObject</span><span class="br0">&#40;</span>rs<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; conn = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> returnObject;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARuntimeException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RuntimeException</span></a><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">finally</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>rs != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>stmt != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>conn != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Using the QueryExecutor to execute a query is now as simple as:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> ReturnType someDbMethod<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> QueryExecutor&lt;ReturnType&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">execute</span><span class="br0">&#40;</span><span class="kw2">new</span> QueryInput&lt;ReturnType&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> getPreparedStatment<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> toQuery = <span class="st0">&quot;SELECT * FROM table LIMIT 10&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> getConnection<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">prepareStatement</span><span class="br0">&#40;</span>query<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> ReturnType toReturnObject<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> ReturnTypeMapper.<span class="me1">map</span><span class="br0">&#40;</span>rs<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>A simple solution for cleaning up JDBC boilerplate if you don&#8217;t want to bring in any external library like Spring JdbcTemplate or similar.</p>
]]></description>
			<content:encoded><![CDATA[<p>Recently I have worked with a code base that has a lot of JDBC boilerplate code. The same code is repeated over and over again and can basically be broken down into the following pattern:</p>
<p><code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AConnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Connection</span></a> conn = &nbsp;<span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; conn = ... <span class="co1">// get the connection</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="co1">// build query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; stmt = conn.<span class="me1">prepareStatement</span><span class="br0">&#40;</span>query<span class="br0">&#41;</span> <span class="co1">// create prepared statement</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; rs = stmt.<span class="me1">executeQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span class="co1">// execute the query</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; returnObject = toReturnObject<span class="br0">&#40;</span>rs<span class="br0">&#41;</span>; <span class="co1">// convert the resultset to a DTO, often right in the method, bloating it terrible</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; rs = <span class="kw2">null</span>; <span class="co1">// close all the stuff</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; conn = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> returnObject;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co1">// handle potential exceptions</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARuntimeException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RuntimeException</span></a><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> <span class="kw2">finally</span> <span class="br0">&#123;</span> <span class="co1">// finally make sure everything is closed...</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>rs != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>stmt != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>conn != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></code></p>
<p>I really don&#8217;t like repeated code, so I wanted to get rid of this, but without bringing in any new library dependencies. So I wrote this very tiny class to assemble most of the repeated code in the pattern above:</p>
<p><code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">interface</span> QueryInput&lt;T&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> getPreparedStatment<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> T toReturnObject<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> QueryExecutor&lt;TReturn&gt; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> TReturn execute<span class="br0">&#40;</span>QueryInput&lt;TReturn&gt; queryInput<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AConnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Connection</span></a> conn = &nbsp;<span class="kw2">null</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; TReturn returnObject = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt = queryInput.<span class="me1">getPreparedStatment</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn = stmt.<span class="me1">getConnection</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs = stmt.<span class="me1">executeQuery</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returnObject = queryInput.<span class="me1">toReturnObject</span><span class="br0">&#40;</span>rs<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; rs = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; stmt = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; conn = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> returnObject;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">throw</span> <span class="kw2">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARuntimeException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">RuntimeException</span></a><span class="br0">&#40;</span>e<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw2">finally</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>rs != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>rs.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>stmt != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>stmt.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>conn != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="kw2">try</span> <span class="br0">&#123;</span>conn.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span> <span class="kw2">catch</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>Using the QueryExecutor to execute a query is now as simple as:<br />
<code>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> ReturnType someDbMethod<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> QueryExecutor&lt;ReturnType&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">execute</span><span class="br0">&#40;</span><span class="kw2">new</span> QueryInput&lt;ReturnType&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3APreparedStatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">PreparedStatement</span></a> getPreparedStatment<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> toQuery = <span class="st0">&quot;SELECT * FROM table LIMIT 10&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> getConnection<span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">prepareStatement</span><span class="br0">&#40;</span>query<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> ReturnType toReturnObject<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AResultSet+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">ResultSet</span></a> rs<span class="br0">&#41;</span> <span class="kw2">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASQLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">SQLException</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> ReturnTypeMapper.<span class="me1">map</span><span class="br0">&#40;</span>rs<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p></code></p>
<p>A simple solution for cleaning up JDBC boilerplate if you don&#8217;t want to bring in any external library like Spring JdbcTemplate or similar.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/cleaning-up-jdbc-bolierplate-code/1799/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String.replaceAll in Java might not do what you expect it to do</title>
		<link>http://sourcecodebean.com/archives/string-replaceall-in-java-might-not-do-what-you-expect-it-to-do/1804</link>
		<comments>http://sourcecodebean.com/archives/string-replaceall-in-java-might-not-do-what-you-expect-it-to-do/1804#comments</comments>
		<pubDate>Fri, 01 Feb 2013 09:56:22 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1804</guid>
		<description><![CDATA[<p>At the first glance of String.replaceAll(String regexp, String replacement) it seems very obvious what the method does, and most of the times it does exactly what you want, but under some conditions it does not. Let me show you this by showing you a few unit tests I wrote recently when fixing a bug in our production system:</p>
<pre lang='java'>

  @Test
    public void regularReplace1() {
        final String input = "this is a user description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name");

        Assert.assertEquals(input, result);
    }

</pre>
<p>This is what we expected. Lets try something else:</p>
<pre lang='java'>

    @Test
    public void regularReplace2() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name");

        Assert.assertEquals("this is a name description", result);
    }

</pre>
<p>Still fine, but what happens when our replacement string contains a $ ? </p>
<pre lang='java'>

    @Test(expected = StringIndexOutOfBoundsException.class)
    public void regularReplace3() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name $");

        Assert.assertEquals("this is a name $ description", result);
    }

</pre>
<p>As you can see from my test, it expects StringIndexOutOfBoundsException to be thrown. Why? We&#8217;ll get to that later. Lets try moving the $ to the beginning of the string:</p>
<pre lang='java'>

    @Test(expected = IllegalArgumentException.class)
    public void regularReplace4() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "$name");

        Assert.assertEquals("this is a $name description", result);
    }

</pre>
<p>Now replaceAll is going to throw IllegalArgumentException. If you know how regular expressions works you are probably starting to figure out what is going on. Lets try with another magical character, the backslash:</p>
<pre lang='java'>

    @Test
    public void regularReplace5() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "\\ name");

        // We expect them to be the same, but no
        Assert.assertNotSame("this is a \\ name description", result);
    }

</pre>
<p>No exception, but not what we expected. Lets move the backslash to the end of the line:</p>
<pre lang='java'>

    @Test(expected = StringIndexOutOfBoundsException.class)
    public void regularReplace6() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name \\");

        Assert.assertEquals("this is a name \\ description", result);
    }

</pre>
<p>Ok, now we are getting a StringIndexOutOfBoundsException. All this seems rather strange, but figuring out what is causing this is not hard. By reading the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)">Java documentation for String.replaceAll</a> will tell you that replaceAll is implemented like this:</p>
<blockquote><p>Pattern.compile(regex).matcher(str).replaceAll(repl)</p></blockquote>
<p>so moving on to the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String)">documentation for Matcher.replaceAll</a> we can read the following:</p>
<blockquote><p>Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.</p></blockquote>
<p>Really? Not quite what I expected, I thought the replacement string was just a string that would replace what we matched with the regular expression, turns out that replaceAll is a bit more powerful than that, and also somewhat dangerous. If we are dealing with a replacement string that comes from user input we must escape the special characters $ and \ before we can call string.replaceAll. To fix the bug in our system I first implemented a method, safeReplaceAll:</p>
<pre lang='java'>

    public static String safeReplaceAll(String input, String regex, String replacement)
    {
        if (input == null) { return null; }

        // Escape special characters in replacement, then do replace
        return input.replaceAll(regex, Matcher.quoteReplacement(replacement));
    }

</pre>
<p>So here is the tests using our safeReplaceAll:</p>
<pre lang='java'>

    @Test
    public void safeReplace1() {
        final String input = "this is a user description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name");

        Assert.assertEquals(input, result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace2() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name");

        Assert.assertEquals("this is a name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace3() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name $");

        Assert.assertEquals("this is a name $ description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace4() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "$name");

        Assert.assertEquals("this is a $name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace5() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "\\ name");

        Assert.assertEquals("this is a \\ name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace6() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name \\");

        Assert.assertEquals("this is a name \\ description", result);
    }

</pre>
<p>After this I realized that in Java 1.5 an overloaded method to <code>String.replace(char oldChar, char newChar) was introduced:</code></p>
<blockquote><p><code>String.replace(CharSequence target, CharSequence replacement)</code></p></blockquote>
<p>&#8230; this method works as a drop in replacement for String.replaceAll! Sometimes reading the API before coding would save you time &#8230;</p>
]]></description>
			<content:encoded><![CDATA[<p>At the first glance of String.replaceAll(String regexp, String replacement) it seems very obvious what the method does, and most of the times it does exactly what you want, but under some conditions it does not. Let me show you this by showing you a few unit tests I wrote recently when fixing a bug in our production system:</p>
<pre lang='java'>

  @Test
    public void regularReplace1() {
        final String input = "this is a user description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name");

        Assert.assertEquals(input, result);
    }

</pre>
<p>This is what we expected. Lets try something else:</p>
<pre lang='java'>

    @Test
    public void regularReplace2() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name");

        Assert.assertEquals("this is a name description", result);
    }

</pre>
<p>Still fine, but what happens when our replacement string contains a $ ? </p>
<pre lang='java'>

    @Test(expected = StringIndexOutOfBoundsException.class)
    public void regularReplace3() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name $");

        Assert.assertEquals("this is a name $ description", result);
    }

</pre>
<p>As you can see from my test, it expects StringIndexOutOfBoundsException to be thrown. Why? We&#8217;ll get to that later. Lets try moving the $ to the beginning of the string:</p>
<pre lang='java'>

    @Test(expected = IllegalArgumentException.class)
    public void regularReplace4() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "$name");

        Assert.assertEquals("this is a $name description", result);
    }

</pre>
<p>Now replaceAll is going to throw IllegalArgumentException. If you know how regular expressions works you are probably starting to figure out what is going on. Lets try with another magical character, the backslash:</p>
<pre lang='java'>

    @Test
    public void regularReplace5() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "\\ name");

        // We expect them to be the same, but no
        Assert.assertNotSame("this is a \\ name description", result);
    }

</pre>
<p>No exception, but not what we expected. Lets move the backslash to the end of the line:</p>
<pre lang='java'>

    @Test(expected = StringIndexOutOfBoundsException.class)
    public void regularReplace6() {
        final String input = "this is a {[user]} description";
        String result = input.replaceAll("\\{\\[user\\]\\}", "name \\");

        Assert.assertEquals("this is a name \\ description", result);
    }

</pre>
<p>Ok, now we are getting a StringIndexOutOfBoundsException. All this seems rather strange, but figuring out what is causing this is not hard. By reading the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)">Java documentation for String.replaceAll</a> will tell you that replaceAll is implemented like this:</p>
<blockquote><p>Pattern.compile(regex).matcher(str).replaceAll(repl)</p></blockquote>
<p>so moving on to the <a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Matcher.html#replaceAll(java.lang.String)">documentation for Matcher.replaceAll</a> we can read the following:</p>
<blockquote><p>Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.</p></blockquote>
<p>Really? Not quite what I expected, I thought the replacement string was just a string that would replace what we matched with the regular expression, turns out that replaceAll is a bit more powerful than that, and also somewhat dangerous. If we are dealing with a replacement string that comes from user input we must escape the special characters $ and \ before we can call string.replaceAll. To fix the bug in our system I first implemented a method, safeReplaceAll:</p>
<pre lang='java'>

    public static String safeReplaceAll(String input, String regex, String replacement)
    {
        if (input == null) { return null; }

        // Escape special characters in replacement, then do replace
        return input.replaceAll(regex, Matcher.quoteReplacement(replacement));
    }

</pre>
<p>So here is the tests using our safeReplaceAll:</p>
<pre lang='java'>

    @Test
    public void safeReplace1() {
        final String input = "this is a user description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name");

        Assert.assertEquals(input, result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace2() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name");

        Assert.assertEquals("this is a name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace3() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name $");

        Assert.assertEquals("this is a name $ description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace4() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "$name");

        Assert.assertEquals("this is a $name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace5() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "\\ name");

        Assert.assertEquals("this is a \\ name description", result);
    }

</pre>
<pre lang='java'>

    @Test
    public void safeReplace6() {
        final String input = "this is a {[user]} description";
        String result = StringUtil.safeReplaceAll(input, "\\{\\[user\\]\\}", "name \\");

        Assert.assertEquals("this is a name \\ description", result);
    }

</pre>
<p>After this I realized that in Java 1.5 an overloaded method to <code>String.replace(char oldChar, char newChar) was introduced:</code></p>
<blockquote><p><code>String.replace(CharSequence target, CharSequence replacement)</code></p></blockquote>
<p>&#8230; this method works as a drop in replacement for String.replaceAll! Sometimes reading the API before coding would save you time &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/string-replaceall-in-java-might-not-do-what-you-expect-it-to-do/1804/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Experiments with Play! Framework</title>
		<link>http://sourcecodebean.com/archives/experiments-with-play-framework/1787</link>
		<comments>http://sourcecodebean.com/archives/experiments-with-play-framework/1787#comments</comments>
		<pubDate>Wed, 19 Dec 2012 03:28:11 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1787</guid>
		<description><![CDATA[<p>As I wrote in my last post, I recently rewrote one of my hobby projects from ASP.NET MVC to Play! Framework. Play! was very easy to get started with, the only thing required was to have a JDK installed and to download the zip distribution of Play, unzip it to a directory and fire it up using the Play console. Play comes bundled with the Netty http server which can be used for both development and production use. Play is a MVC framework, much in style with ASP.NET MVC. I am not going to go into detail to describe how it works or its features, there is already plenty of articles about it out there, instead I want to touch a few things I think is required from a framework to be productive.</p>
<p><strong>IDE Support</strong><br />
It was quite easy to generate a project for Intellij IDEA was very easy using the Play console. Just type &#8220;idea&#8221; in the console (or &#8220;eclipseify&#8221; if you prefer Eclipse). One thing to remember is that everytime you add a new dependency to the project (in Build.scala) you need to rerun the &#8220;idea&#8221; command, otherwise IDEA will not find the packages and you will not be able to compile from within the IDE. </p>
<p><strong>Support for testing</strong><br />
Play comes with built in support for integration testing (writing tests that test the entire application stack, controller to database). The tests can easily be run from the Play console, but running them from within IDEA turned out to be tricky. Running regular unittests in the project that does not use the in memory database run just fine from the IDE, but i have not been able to configure IDEA to setup the fake application context needed to run the integration tests. It seems like this issue have been brought up by the Play community several times, but no one seems to have an answer. </p>
<p><strong>Dependency injection</strong><br />
Play! doesn&#8217;t have any prefered way of doing DI, so it is up to you to use the DI container of your choice. It turnes out that there is a <a href="https://github.com/typesafehub/play-plugins/blob/master/guice/">Play plugin for Google Guice</a> so getting started with Guice was easy.</p>
<p>This is what my Guice bootstrap looks like:</p>
<pre lang='java'>
public class Dependencies implements Module {

    public void configure(Binder binder) {
        binder.bind(new TypeLiteral<IQueue<QueuedMessage>>(){}).to(RabbitMqQueue.class);
        binder.bind(ISubscriberHandler.class).to(SubscriberHandler.class);
        binder.bind(IEmailHandler.class).to(EmailHandler.class);
        binder.bind(ISubscriberHandler.class).to(SubscriberHandler.class);
        binder.bind(ISendAccountHandler.class).to(SendAccountHandler.class);
        binder.bind(ISettingsReader.class).to(SettingsReader.class);
        binder.bind(IGeoLocationHandler.class).to(GeoLocationHandler.class);
        binder.bind(ISmtpHandler.class).to(SmtpHandler.class);
    }
}
</pre>
<p>In Play controllers are static. The reasoning is that controllers should have no state and therefor should be static. In some way this makes sense, controllers should not keep a state, but it also limits us to property based injection instead of constructor based injection. To use dependency injection in the controllers we annotate the properties like this:</p>
<pre lang ='java'>
public class Emails extends Controller {

    @Inject
    public static EmailQueueHandler emailQueueHandler;

    @Inject
    public static ISmtpHandler smtpHandler;

    @Inject
    public static ISettingsReader settingsReader;

...

</pre>
<p><strong>HTML Templating</strong><br />
Why do I mention the templating? It turnes out that Play is using a new <a href="http://www.playframework.org/documentation/2.0/ScalaTemplates">Scala based templating engine</a> that is heavily inspired by the ASP.NET Razor view engine, witch is the best view engine that I have used. In general the Scala view engine is great, the only complaint that I have is that the error messages can be very cryptic when something doesn&#8217;t compile.</p>
<p><strong>Deployment</strong><br />
The preferred way of deploying Play is by using the built in web server and a proxy, such as Nginx in front of it to serve static files. This is how I have deployed my application. However, there is support for packaging a war file and deploying it to an application server such as Tomcat, the downside is that you lose some functionality. I think this is something that Play need to improve to become more enterprise friendly.</p>
<p><strong>Conclusion</strong><br />
Without any previous knowledge of Play framework I was able to rewrite my application from ASP.NET to Play! in less than a week. I also took the opportunity to rewrite a lot of parts of the application that I have been wanting to do for a long time and switched html/css framework from <a href="http://www.blueprintcss.org/">Blueprint</a> to <a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>. So overall I&#8217;m quite happy with Play.</p>
]]></description>
			<content:encoded><![CDATA[<p>As I wrote in my last post, I recently rewrote one of my hobby projects from ASP.NET MVC to Play! Framework. Play! was very easy to get started with, the only thing required was to have a JDK installed and to download the zip distribution of Play, unzip it to a directory and fire it up using the Play console. Play comes bundled with the Netty http server which can be used for both development and production use. Play is a MVC framework, much in style with ASP.NET MVC. I am not going to go into detail to describe how it works or its features, there is already plenty of articles about it out there, instead I want to touch a few things I think is required from a framework to be productive.</p>
<p><strong>IDE Support</strong><br />
It was quite easy to generate a project for Intellij IDEA was very easy using the Play console. Just type &#8220;idea&#8221; in the console (or &#8220;eclipseify&#8221; if you prefer Eclipse). One thing to remember is that everytime you add a new dependency to the project (in Build.scala) you need to rerun the &#8220;idea&#8221; command, otherwise IDEA will not find the packages and you will not be able to compile from within the IDE. </p>
<p><strong>Support for testing</strong><br />
Play comes with built in support for integration testing (writing tests that test the entire application stack, controller to database). The tests can easily be run from the Play console, but running them from within IDEA turned out to be tricky. Running regular unittests in the project that does not use the in memory database run just fine from the IDE, but i have not been able to configure IDEA to setup the fake application context needed to run the integration tests. It seems like this issue have been brought up by the Play community several times, but no one seems to have an answer. </p>
<p><strong>Dependency injection</strong><br />
Play! doesn&#8217;t have any prefered way of doing DI, so it is up to you to use the DI container of your choice. It turnes out that there is a <a href="https://github.com/typesafehub/play-plugins/blob/master/guice/">Play plugin for Google Guice</a> so getting started with Guice was easy.</p>
<p>This is what my Guice bootstrap looks like:</p>
<pre lang='java'>
public class Dependencies implements Module {

    public void configure(Binder binder) {
        binder.bind(new TypeLiteral<IQueue<QueuedMessage>>(){}).to(RabbitMqQueue.class);
        binder.bind(ISubscriberHandler.class).to(SubscriberHandler.class);
        binder.bind(IEmailHandler.class).to(EmailHandler.class);
        binder.bind(ISubscriberHandler.class).to(SubscriberHandler.class);
        binder.bind(ISendAccountHandler.class).to(SendAccountHandler.class);
        binder.bind(ISettingsReader.class).to(SettingsReader.class);
        binder.bind(IGeoLocationHandler.class).to(GeoLocationHandler.class);
        binder.bind(ISmtpHandler.class).to(SmtpHandler.class);
    }
}
</pre>
<p>In Play controllers are static. The reasoning is that controllers should have no state and therefor should be static. In some way this makes sense, controllers should not keep a state, but it also limits us to property based injection instead of constructor based injection. To use dependency injection in the controllers we annotate the properties like this:</p>
<pre lang ='java'>
public class Emails extends Controller {

    @Inject
    public static EmailQueueHandler emailQueueHandler;

    @Inject
    public static ISmtpHandler smtpHandler;

    @Inject
    public static ISettingsReader settingsReader;

...

</pre>
<p><strong>HTML Templating</strong><br />
Why do I mention the templating? It turnes out that Play is using a new <a href="http://www.playframework.org/documentation/2.0/ScalaTemplates">Scala based templating engine</a> that is heavily inspired by the ASP.NET Razor view engine, witch is the best view engine that I have used. In general the Scala view engine is great, the only complaint that I have is that the error messages can be very cryptic when something doesn&#8217;t compile.</p>
<p><strong>Deployment</strong><br />
The preferred way of deploying Play is by using the built in web server and a proxy, such as Nginx in front of it to serve static files. This is how I have deployed my application. However, there is support for packaging a war file and deploying it to an application server such as Tomcat, the downside is that you lose some functionality. I think this is something that Play need to improve to become more enterprise friendly.</p>
<p><strong>Conclusion</strong><br />
Without any previous knowledge of Play framework I was able to rewrite my application from ASP.NET to Play! in less than a week. I also took the opportunity to rewrite a lot of parts of the application that I have been wanting to do for a long time and switched html/css framework from <a href="http://www.blueprintcss.org/">Blueprint</a> to <a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>. So overall I&#8217;m quite happy with Play.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/experiments-with-play-framework/1787/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New country, new city, new job and back to programming Java</title>
		<link>http://sourcecodebean.com/archives/new-country-new-city-new-job-and-back-to-programming-java/1758</link>
		<comments>http://sourcecodebean.com/archives/new-country-new-city-new-job-and-back-to-programming-java/1758#comments</comments>
		<pubDate>Fri, 07 Dec 2012 05:18:23 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1758</guid>
		<description><![CDATA[<p>So a lot is new since last time I posted. In the end of the summer I moved from Stockholm to San Francisco to start working for a company called Skout, building mobile social networks. With this new job some big changes came:) I am now back to coding Java after several years of mostly working with C#/.NET. Back in a MacOS/Linux environment after having used Windows for most professional development in the last few years. And back to a small company (in total about 100 people) after having worked in a company with over 1000 people in just IT. So a lot has changed, and I am very excited about it! </p>
<p>Lets roll back time a bit. In school I was taught C, C++, Java and Haskell, plus a bunch of languages no one knows of. When I graduated I was expecting getting a job coding Java, however I landed up with a job doing mostly Python and C. After some time at that company I abruptly switched field to building web services, and to .NET. I had done some C# at this time, actually mostly using the Mono compiler. C# at this time was still in version 1.x, and lacks almost everything that makes it a great language today, but I found it to be a better version of Java. Also, at that time a lot about Java felt like it was mainly about enterprise beans, abstractions and more abstractions. And to add on to this, a shit ton of XML to configure every single aspect of everything in XML instead of code. I never liked this. Switching to C# at this time felt like a fresh breeze. Sure .NET has its own baggage as well, for example the core ASP.NET framework could see plenty of improvements and IIS was a mess at that time, but getting things done was much faster and easier compared to Java. </p>
<p>Over time things have changed. .NET is still a great environment to work in (maybe better than ever I would say), but Java has also moved on. Less abstractions, the application stacks are not necessary as tall and more light weight containers are preferred over big application servers. Overall it seems like getting stuff done has become easier. However, Java is still behind C# on many language features such as closures, lambda expressions, expression trees and something like LINQ. But it seems like there is hope! In Java 8 we will get extension methods and lambdas which will allow for a very different style of coding in Java. Today the closest you get is anonymous classes combined with final variables. It does the trick but is very verbose. </p>
<p>Before I got the job I decided to freshen up my Java skills, so I decided to rewrite one of my personal hobby projects from C# to Java. I did some research one what was the most up to date, cool and hip Java framework and it turned out to be Play! So I rewrote it using Play! Naturally my next article is going to be about my experiences and frustations with Play!</p>
<p>Stay tuned.</p>
]]></description>
			<content:encoded><![CDATA[<p>So a lot is new since last time I posted. In the end of the summer I moved from Stockholm to San Francisco to start working for a company called Skout, building mobile social networks. With this new job some big changes came:) I am now back to coding Java after several years of mostly working with C#/.NET. Back in a MacOS/Linux environment after having used Windows for most professional development in the last few years. And back to a small company (in total about 100 people) after having worked in a company with over 1000 people in just IT. So a lot has changed, and I am very excited about it! </p>
<p>Lets roll back time a bit. In school I was taught C, C++, Java and Haskell, plus a bunch of languages no one knows of. When I graduated I was expecting getting a job coding Java, however I landed up with a job doing mostly Python and C. After some time at that company I abruptly switched field to building web services, and to .NET. I had done some C# at this time, actually mostly using the Mono compiler. C# at this time was still in version 1.x, and lacks almost everything that makes it a great language today, but I found it to be a better version of Java. Also, at that time a lot about Java felt like it was mainly about enterprise beans, abstractions and more abstractions. And to add on to this, a shit ton of XML to configure every single aspect of everything in XML instead of code. I never liked this. Switching to C# at this time felt like a fresh breeze. Sure .NET has its own baggage as well, for example the core ASP.NET framework could see plenty of improvements and IIS was a mess at that time, but getting things done was much faster and easier compared to Java. </p>
<p>Over time things have changed. .NET is still a great environment to work in (maybe better than ever I would say), but Java has also moved on. Less abstractions, the application stacks are not necessary as tall and more light weight containers are preferred over big application servers. Overall it seems like getting stuff done has become easier. However, Java is still behind C# on many language features such as closures, lambda expressions, expression trees and something like LINQ. But it seems like there is hope! In Java 8 we will get extension methods and lambdas which will allow for a very different style of coding in Java. Today the closest you get is anonymous classes combined with final variables. It does the trick but is very verbose. </p>
<p>Before I got the job I decided to freshen up my Java skills, so I decided to rewrite one of my personal hobby projects from C# to Java. I did some research one what was the most up to date, cool and hip Java framework and it turned out to be Play! So I rewrote it using Play! Naturally my next article is going to be about my experiences and frustations with Play!</p>
<p>Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/new-country-new-city-new-job-and-back-to-programming-java/1758/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Will the Metro user interface work on the Dektop?</title>
		<link>http://sourcecodebean.com/archives/will-the-metro-user-interface-work-on-the-dektop/1717</link>
		<comments>http://sourcecodebean.com/archives/will-the-metro-user-interface-work-on-the-dektop/1717#comments</comments>
		<pubDate>Sun, 03 Jun 2012 10:13:49 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Operating systems]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1717</guid>
		<description><![CDATA[<p>Two days ago Windows 8 Release Preview was released to the public, while not being the final release of Windows 8, it is probably quite close. You can download the ISO files from here:</p>
<p><a href=" http://windows.microsoft.com/en-US/windows-8/iso"></p>
<p>http://windows.microsoft.com/en-US/windows-8/iso</a></p>
<p>If you like me, had problems finding the key for it (it was hidden on the <a href="http://windows.microsoft.com/en-US/windows-8/faq">FAQ </a>page), here is the key to install it: TK8TP-9JN6P-7X7WW-RFFTV-B7QPF.</p>
<p>I have tried the earlier builds of Windows 8 that has come out and after playing with the release preview for a short while I must say that my feelings toward Windows 8 is still somewhat mixed. I think the new application model, WinRT, is a step in the right direction to get away from the old legacy Win32. The Market Place is a must to compete with Apple. And the Metro interface seems like a good fit for tables, phones, etc. But Metro on the desktop? I am not sure about this.</p>
<p>A big difference between the mobile devices and the desktop is screen estate. On a tablets the screen area is limited and even more so on a phone. In this case it makes sense to run all applications in full screen. But on the desktop you generally have more screen estate available, and I prefer to use this to have more information visible. For example, I always want to be able to:</p>
<ul>
<li>see a list of open applications</li>
<li>see the URL i am browsing</li>
<li>see all the tabs that are open</li>
<li>see my contact list of friends that is online</li>
<li>and something as simple as always have a visible clock on the screen</li>
</ul>
<p>It does not matter if this wastes some of my pixels, I have plenty of them!</p>
<p>The Metro applications i have seen for the desktop so far have all been good looking. But take for example the calender application, full screen on a tablet, that is fine. Full screen on a 27&#8243; monitor? No thanks. Also, most of the Metro applications I have seen so far seems to simple, not so useful applications inline with, app for twitter, app for stocks, app for something else that I already can do just fine in a web app. What I really want to see is a real complex application converted to Metro, like Office, or even better, Visual Studio. I might work, but even if it does I think using a Metro application on the desktop will feel like it is limiting your ability to multitask because of the full screen nature of it.</p>
<p>I guess time will tell, but for now it feels like the &#8220;legacy&#8221; desktop will be the frequently used one in Windows 8.</p>
]]></description>
			<content:encoded><![CDATA[<p>Two days ago Windows 8 Release Preview was released to the public, while not being the final release of Windows 8, it is probably quite close. You can download the ISO files from here:</p>
<p><a href=" http://windows.microsoft.com/en-US/windows-8/iso"></p>
<p>http://windows.microsoft.com/en-US/windows-8/iso</a></p>
<p>If you like me, had problems finding the key for it (it was hidden on the <a href="http://windows.microsoft.com/en-US/windows-8/faq">FAQ </a>page), here is the key to install it: TK8TP-9JN6P-7X7WW-RFFTV-B7QPF.</p>
<p>I have tried the earlier builds of Windows 8 that has come out and after playing with the release preview for a short while I must say that my feelings toward Windows 8 is still somewhat mixed. I think the new application model, WinRT, is a step in the right direction to get away from the old legacy Win32. The Market Place is a must to compete with Apple. And the Metro interface seems like a good fit for tables, phones, etc. But Metro on the desktop? I am not sure about this.</p>
<p>A big difference between the mobile devices and the desktop is screen estate. On a tablets the screen area is limited and even more so on a phone. In this case it makes sense to run all applications in full screen. But on the desktop you generally have more screen estate available, and I prefer to use this to have more information visible. For example, I always want to be able to:</p>
<ul>
<li>see a list of open applications</li>
<li>see the URL i am browsing</li>
<li>see all the tabs that are open</li>
<li>see my contact list of friends that is online</li>
<li>and something as simple as always have a visible clock on the screen</li>
</ul>
<p>It does not matter if this wastes some of my pixels, I have plenty of them!</p>
<p>The Metro applications i have seen for the desktop so far have all been good looking. But take for example the calender application, full screen on a tablet, that is fine. Full screen on a 27&#8243; monitor? No thanks. Also, most of the Metro applications I have seen so far seems to simple, not so useful applications inline with, app for twitter, app for stocks, app for something else that I already can do just fine in a web app. What I really want to see is a real complex application converted to Metro, like Office, or even better, Visual Studio. I might work, but even if it does I think using a Metro application on the desktop will feel like it is limiting your ability to multitask because of the full screen nature of it.</p>
<p>I guess time will tell, but for now it feels like the &#8220;legacy&#8221; desktop will be the frequently used one in Windows 8.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/will-the-metro-user-interface-work-on-the-dektop/1717/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Limiting MongoDB memory usage on Windows 2008 Server</title>
		<link>http://sourcecodebean.com/archives/limiting-mongodb-memory-usage-on-windows-2008-server/1695</link>
		<comments>http://sourcecodebean.com/archives/limiting-mongodb-memory-usage-on-windows-2008-server/1695#comments</comments>
		<pubDate>Sat, 26 May 2012 09:44:34 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Windows Server]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1695</guid>
		<description><![CDATA[<p>By default MongoDB tries to memory map as much as possible of the database file. Given a fairly big database, this will consume all your memory. If MongoDB is running on a dedicated server this is totally fine, but when running on a shared server this will cause unnecessary swapping for your other applications. At this point you want to limit the max amount of memory MongoDB can use. On Linux I have not found a way on how to do this (if you know a way please let me know!), but on Windows Server it is possible to limit it using WSRM. </p>
<p>WSRM allows greater control over the CPU and Memory available to a process and is an additional feature shipped with Windows Server that can be installed from the server manager under &#8220;Features Summary&#8221;:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/05/AddFeature.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/AddFeature.png" alt="" title="AddFeature" width="702" height="201" class="aligncenter size-full wp-image-1700" /></a></p>
<p>Once installed we need to create a new resource allocation policy from the manager:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/2.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/2.png" alt="" title="2" width="935" height="202" class="aligncenter size-full wp-image-1702" /></a></p>
<p>and give it some name:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/3.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/3.png" alt="" title="3" width="355" height="541" class="aligncenter size-full wp-image-1703" /></a></p>
<p>Now we need to select processes to match with the policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/4.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/4.png" alt="" title="4" width="379" height="414" class="aligncenter size-full wp-image-1704" /></a></p>
<p>We will select process from the list of registered services:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/5.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/5.png" alt="" title="5" width="389" height="438" class="aligncenter size-full wp-image-1705" /></a></p>
<p>Here we find MongoDB:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/6.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/6.png" alt="" title="6" width="502" height="264" class="aligncenter size-full wp-image-1706" /></a></p>
<p>Click OK a few times to exit the process selection. On the memory tab we can limit the memory for this resource allocation policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/7.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/7.png" alt="" title="7" width="356" height="420" class="aligncenter size-full wp-image-1707" /></a></p>
<p>The final step is to make the policy active. To do this, go to the top level page in the manager and click the &#8220;Selected Policy&#8221; link. In here you can set the active policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/8.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/8.png" alt="" title="8" width="371" height="420" class="aligncenter size-full wp-image-1708" /></a></p>
<p>That&#8217;s it! Now MongoDB will not consume more than 500MB of memory.</p>
]]></description>
			<content:encoded><![CDATA[<p>By default MongoDB tries to memory map as much as possible of the database file. Given a fairly big database, this will consume all your memory. If MongoDB is running on a dedicated server this is totally fine, but when running on a shared server this will cause unnecessary swapping for your other applications. At this point you want to limit the max amount of memory MongoDB can use. On Linux I have not found a way on how to do this (if you know a way please let me know!), but on Windows Server it is possible to limit it using WSRM. </p>
<p>WSRM allows greater control over the CPU and Memory available to a process and is an additional feature shipped with Windows Server that can be installed from the server manager under &#8220;Features Summary&#8221;:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/05/AddFeature.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/AddFeature.png" alt="" title="AddFeature" width="702" height="201" class="aligncenter size-full wp-image-1700" /></a></p>
<p>Once installed we need to create a new resource allocation policy from the manager:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/2.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/2.png" alt="" title="2" width="935" height="202" class="aligncenter size-full wp-image-1702" /></a></p>
<p>and give it some name:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/3.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/3.png" alt="" title="3" width="355" height="541" class="aligncenter size-full wp-image-1703" /></a></p>
<p>Now we need to select processes to match with the policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/4.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/4.png" alt="" title="4" width="379" height="414" class="aligncenter size-full wp-image-1704" /></a></p>
<p>We will select process from the list of registered services:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/5.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/5.png" alt="" title="5" width="389" height="438" class="aligncenter size-full wp-image-1705" /></a></p>
<p>Here we find MongoDB:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/6.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/6.png" alt="" title="6" width="502" height="264" class="aligncenter size-full wp-image-1706" /></a></p>
<p>Click OK a few times to exit the process selection. On the memory tab we can limit the memory for this resource allocation policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/7.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/7.png" alt="" title="7" width="356" height="420" class="aligncenter size-full wp-image-1707" /></a></p>
<p>The final step is to make the policy active. To do this, go to the top level page in the manager and click the &#8220;Selected Policy&#8221; link. In here you can set the active policy:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/05/8.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/05/8.png" alt="" title="8" width="371" height="420" class="aligncenter size-full wp-image-1708" /></a></p>
<p>That&#8217;s it! Now MongoDB will not consume more than 500MB of memory.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/limiting-mongodb-memory-usage-on-windows-2008-server/1695/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ColourSearch &#8211; a simple image search engine</title>
		<link>http://sourcecodebean.com/archives/coloursearch-a-simple-image-search-engine/1671</link>
		<comments>http://sourcecodebean.com/archives/coloursearch-a-simple-image-search-engine/1671#comments</comments>
		<pubDate>Thu, 26 Apr 2012 20:48:15 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1671</guid>
		<description><![CDATA[<p>A few weeks I randomly got interested in how you match images with other images. Having a very limited background in computer graphics I started reading some research papers on how to match images. One strategy that seemed to work pretty well for most people was a histogram based comparison. Given two histograms it is possible to calculate the distance, or correlation, between the histograms. The image with the lowest distance or highest correlation would be the best match. To try out my newly learnt knowledge I created a small application, ColourSearch, which given a directory calculates a histogram for each image and stores it in memory. In the GUI you can then pick a color to search and the application will find the best matching images using 4 different algorithms and present the result side by side. </p>
<p>A problem with the current implementation is that I have not found any way to index histograms, so when searching the input image needs to be compared to each image in the database (O(n) yay!). For larger datasets this would of course not be feasible, but at least the problem is easy to scale over several CPUs, so I can take advantage of all my cores. </p>
<p>You can find ColourSearch on GitHub (code in C#, works perfect under Mono!):<br />
<a href="https://github.com/moberg/coloursearch">https://github.com/moberg/coloursearch</a></p>
<p>If you have any ideas on how indexing can be done or on more efficient matching algorithms I would be very interested in hearing them!</p>
]]></description>
			<content:encoded><![CDATA[<p>A few weeks I randomly got interested in how you match images with other images. Having a very limited background in computer graphics I started reading some research papers on how to match images. One strategy that seemed to work pretty well for most people was a histogram based comparison. Given two histograms it is possible to calculate the distance, or correlation, between the histograms. The image with the lowest distance or highest correlation would be the best match. To try out my newly learnt knowledge I created a small application, ColourSearch, which given a directory calculates a histogram for each image and stores it in memory. In the GUI you can then pick a color to search and the application will find the best matching images using 4 different algorithms and present the result side by side. </p>
<p>A problem with the current implementation is that I have not found any way to index histograms, so when searching the input image needs to be compared to each image in the database (O(n) yay!). For larger datasets this would of course not be feasible, but at least the problem is easy to scale over several CPUs, so I can take advantage of all my cores. </p>
<p>You can find ColourSearch on GitHub (code in C#, works perfect under Mono!):<br />
<a href="https://github.com/moberg/coloursearch">https://github.com/moberg/coloursearch</a></p>
<p>If you have any ideas on how indexing can be done or on more efficient matching algorithms I would be very interested in hearing them!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/coloursearch-a-simple-image-search-engine/1671/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unlocking your Windows Phone for development</title>
		<link>http://sourcecodebean.com/archives/unlocking-your-windows-phone-for-development-2/1662</link>
		<comments>http://sourcecodebean.com/archives/unlocking-your-windows-phone-for-development-2/1662#comments</comments>
		<pubDate>Sun, 01 Apr 2012 08:00:45 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1662</guid>
		<description><![CDATA[<p>It is not totally obious how to unlock your WP7 device for development. The first step is of course to sign up for the development program at <a href="http://create.msdn.com">http://create.msdn.com</a>. But then? I had the exact same experience as this guy: </p>
<p><a href="http://www.pitorque.de/MisterGoodcat/post/Unlocking-a-Windows-Phone-7-device-for-development.aspx">http://www.pitorque.de/MisterGoodcat/post/Unlocking-a-Windows-Phone-7-device-for-development.aspx</a></p>
<p>So together with the SDK a small activation program was installed, &#8220;Windows Phone Developer Registration&#8221;. Start it and login to active your device! </p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/03/WPDevRegistration.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/03/WPDevRegistration.png" alt="" title="WPDevRegistration" width="510" height="460" class="aligncenter size-full wp-image-1663" /></a></p>
]]></description>
			<content:encoded><![CDATA[<p>It is not totally obious how to unlock your WP7 device for development. The first step is of course to sign up for the development program at <a href="http://create.msdn.com">http://create.msdn.com</a>. But then? I had the exact same experience as this guy: </p>
<p><a href="http://www.pitorque.de/MisterGoodcat/post/Unlocking-a-Windows-Phone-7-device-for-development.aspx">http://www.pitorque.de/MisterGoodcat/post/Unlocking-a-Windows-Phone-7-device-for-development.aspx</a></p>
<p>So together with the SDK a small activation program was installed, &#8220;Windows Phone Developer Registration&#8221;. Start it and login to active your device! </p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/03/WPDevRegistration.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/03/WPDevRegistration.png" alt="" title="WPDevRegistration" width="510" height="460" class="aligncenter size-full wp-image-1663" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/unlocking-your-windows-phone-for-development-2/1662/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Force update of your Lumia 800 to WP7 1600.2487.8107.12070</title>
		<link>http://sourcecodebean.com/archives/force-update-of-your-lumia-800-to-wp7-1600-2487-8107-12070/1650</link>
		<comments>http://sourcecodebean.com/archives/force-update-of-your-lumia-800-to-wp7-1600-2487-8107-12070/1650#comments</comments>
		<pubDate>Sat, 31 Mar 2012 09:28:18 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1650</guid>
		<description><![CDATA[<p>So for some reason it takes Nokia a long time to push out the WP7 updates. 1600.2487.8107.12070 is a new official build of WP7 that was released a while back, but Zune still tells me that there is no update for my phone. The most important features of the update to me seems to be improved battery performance and improved bass in audio performance. I have been quite happy with the current battery performance of my phone (about 1,5 days on one charge), but at the same time I know other Lumia 800 owners that are very unhappy with theirs, not even lasting one work day. It seems like there are some good and some not so great devices out there. Even better battery life would of course be great! On the audio side, the audio has been quite flat and improved bass performance would be very welcome. </p>
<p>This blog post has great step by step instructions on how to force the update:<br />
<a href="http://nokiagadgets.com/2012/03/08/force-the-newest-lumia-800-update-1600-2487-8107-12070-to-your-phone/">http://nokiagadgets.com/2012/03/08/force-the-newest-lumia-800-update-1600-2487-8107-12070-to-your-phone/</a></p>
<p>Basically what you have to do is to download the update and install some tools for phone developers. Once you have downloaded everything you run the &#8220;WP7 Update Cab Sender.bat&#8221;:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/03/WP7_cab_updater.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/03/WP7_cab_updater.png" alt="" title="WP7_cab_updater" width="669" height="751" class="aligncenter size-full wp-image-1651" /></a></p>
<p>The update process takes about 10 minutes. The first thing I tested out after updating was of course to play some music, the audio is indeed much improved. Time will tell if the battery performance got better!</p>
]]></description>
			<content:encoded><![CDATA[<p>So for some reason it takes Nokia a long time to push out the WP7 updates. 1600.2487.8107.12070 is a new official build of WP7 that was released a while back, but Zune still tells me that there is no update for my phone. The most important features of the update to me seems to be improved battery performance and improved bass in audio performance. I have been quite happy with the current battery performance of my phone (about 1,5 days on one charge), but at the same time I know other Lumia 800 owners that are very unhappy with theirs, not even lasting one work day. It seems like there are some good and some not so great devices out there. Even better battery life would of course be great! On the audio side, the audio has been quite flat and improved bass performance would be very welcome. </p>
<p>This blog post has great step by step instructions on how to force the update:<br />
<a href="http://nokiagadgets.com/2012/03/08/force-the-newest-lumia-800-update-1600-2487-8107-12070-to-your-phone/">http://nokiagadgets.com/2012/03/08/force-the-newest-lumia-800-update-1600-2487-8107-12070-to-your-phone/</a></p>
<p>Basically what you have to do is to download the update and install some tools for phone developers. Once you have downloaded everything you run the &#8220;WP7 Update Cab Sender.bat&#8221;:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/03/WP7_cab_updater.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/03/WP7_cab_updater.png" alt="" title="WP7_cab_updater" width="669" height="751" class="aligncenter size-full wp-image-1651" /></a></p>
<p>The update process takes about 10 minutes. The first thing I tested out after updating was of course to play some music, the audio is indeed much improved. Time will tell if the battery performance got better!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/force-update-of-your-lumia-800-to-wp7-1600-2487-8107-12070/1650/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: sourcecodebean.com @ 2013-06-20 03:59:24 -->