<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Inside Coding</title>
	<atom:link href="http://insidecoding.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://insidecoding.com</link>
	<description>&#34;Intellectuals solve problems; geniuses prevent them.&#34; - Albert Einstein</description>
	<lastBuildDate>Tue, 07 May 2013 11:47:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='insidecoding.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/2eb667ae94520367d6f27cbce7368bf4?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Inside Coding</title>
		<link>http://insidecoding.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://insidecoding.com/osd.xml" title="Inside Coding" />
	<atom:link rel='hub' href='http://insidecoding.com/?pushpress=hub'/>
		<item>
		<title>My first open source project: Selenium on Steroids</title>
		<link>http://insidecoding.com/2013/03/12/my-first-open-source-project-selenium-on-steroids/</link>
		<comments>http://insidecoding.com/2013/03/12/my-first-open-source-project-selenium-on-steroids/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 06:00:48 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[selenium]]></category>
		<category><![CDATA[sos]]></category>

		<guid isPermaLink="false">http://insidecoding.com/?p=247</guid>
		<description><![CDATA[The projects targets QA automation engineers that want a clean and simple way to create Selenium tests allowing them to ... <br /><a class="more-link" href="http://insidecoding.com/2013/03/12/my-first-open-source-project-selenium-on-steroids/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=247&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The projects targets QA automation engineers that want a clean and simple way to create Selenium tests allowing them to focus more on designing the tests rather than dealing with technical difficulties. Using a simple property file and Maven profiles the instantiation of a WebDriver objects with different kind of settings reduces to just setting some predefined properties. The project enhances the functionality of the WebDriver API by offering additional methods for common tasks, having as a long run target to implement most of the Selenium IDE functions. SoS also offers different utility methods to interact with simple files, properties files, Excel documents, XML documents, Databases, post XML calls over HTTP, etc. Basically everything you need for your data driven testing scenarios.</p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#features" name="features"></a>Features</h1>
<ul>
<li>Easy Maven integration: the project is published on Maven Central so you can easily add it as a Maven dependency</li>
<li>Easily create new Selenium tests by extending a base class which incorporates all the required logic to configure a WebDriver instance</li>
<li>Utility methods to interact with DB</li>
<li>Enhances the WebDriver API with new methods</li>
<li>Utility methods to interact with Excel files</li>
<li>Utility methods to interact with text files and XML files</li>
<li>Utility methods to use XMLs over HTTP (make WebService calls for example)</li>
<li>Easily control different WebDriver instances and configuration through properties</li>
<li>Get screenshots when Selenium tests are failing</li>
</ul>
<p>In order to get the most out of Selenium on Steriods you must write your Selenium tests using the JUnit framework.</p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#getting-started" name="getting-started"></a>Getting started</h1>
<p><a href="https://github.com/ludovicianul/selenium-on-steroids/wiki/getting-started">https://github.com/ludovicianul/selenium-on-steroids/wiki/getting-started</a></p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#changelog" name="changelog"></a>Changelog</h1>
<p><a href="https://github.com/ludovicianul/selenium-on-steroids/wiki/changelog">https://github.com/ludovicianul/selenium-on-steroids/wiki/changelog</a></p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#homepage" name="homepage"></a>Homepage</h1>
<p><a href="http://insidecoding.com/selenium-on-steroids/">http://insidecoding.com/selenium-on-steroids/</a></p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#wiki" name="wiki"></a>Wiki</h1>
<p><a href="https://github.com/ludovicianul/selenium-on-steroids/wiki">https://github.com/ludovicianul/selenium-on-steroids/wiki</a></p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#issue-tracker" name="issue-tracker"></a>Issue Tracker</h1>
<p><a href="https://github.com/ludovicianul/selenium-on-steroids/issues">https://github.com/ludovicianul/selenium-on-steroids/issues</a></p>
<h1><a href="https://github.com/ludovicianul/selenium-on-steroids/blob/master/README.md#license" name="license"></a>License</h1>
<p><a href="http://www.apache.org/licenses/LICENSE-2.0.txt">The Apache Software License, Version 2.0</a></p>
<br />Filed under: <a href='http://insidecoding.com/category/codeproject/'>CodeProject</a>, <a href='http://insidecoding.com/category/java/'>Java</a>, <a href='http://insidecoding.com/category/selenium/'>Selenium</a>, <a href='http://insidecoding.com/category/testing/'>Testing</a>, <a href='http://insidecoding.com/category/web/'>Web</a> Tagged: <a href='http://insidecoding.com/tag/junit/'>junit</a>, <a href='http://insidecoding.com/tag/maven/'>maven</a>, <a href='http://insidecoding.com/tag/open-source/'>open source</a>, <a href='http://insidecoding.com/tag/selenium-2/'>selenium</a>, <a href='http://insidecoding.com/tag/sos/'>sos</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/247/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=247&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2013/03/12/my-first-open-source-project-selenium-on-steroids/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Google Guice in Web Applications</title>
		<link>http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/</link>
		<comments>http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/#comments</comments>
		<pubDate>Tue, 05 Feb 2013 05:30:51 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[jsr330]]></category>

		<guid isPermaLink="false">http://insidecoding.com/?p=221</guid>
		<description><![CDATA[Introduction As promised in the previous article I’ll continue presenting Google Guice also for web applications. In order to do ... <br /><a class="more-link" href="http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=221&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1>Introduction</h1>
<p>As promised in the <a title="http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/" href="http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/">previous </a>article I’ll continue presenting Google Guice also for web applications. In order to do this you’ll need to get the servlet extension – part of the standard distribution, along with other extensions like JMX, JNDI, Persist, Struts or Spring. Using Guice, the web.xml will be reduced at minimum &#8211; just make the Guice container start. The rest of the configurations will be easily done in Java in the same type-safe manner presented in the previous article.</p>
<p>The servlets will benefit from:</p>
<ul>
<li>Constructor injection</li>
<li>Type-safe configuration</li>
<li>Modularization</li>
<li>AOP</li>
</ul>
<p>In this article I’ll present the following scenarios:</p>
<ul>
<li>Developing a web application from scratch</li>
<li>Adding Guice to an existing web application</li>
</ul>
<h1>Starting a new web application using Guice</h1>
<p>Check the bellow image for an Eclipse screenshot with the project structure.</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/02/g4.png"><img class="aligncenter size-full wp-image-227" alt="g4" src="http://insidecoding.files.wordpress.com/2013/02/g4.png?w=590"   /></a></p>
<p>Besides the core Guice libraries presented in the previous article, we must also add the <b>guice-servlet.jar</b> in the application’s classpath (please check the end of the article for the Maven dependency).</p>
<p>After the classpath is properly configured, we must first define the Guice Filter in web.xml. This will actually be the only configuration present in this file.</p>
<p>This is the web.xml:</p>
<pre>&lt;?xml version=<i>"1.0"</i> encoding=<i>"UTF-8"</i>?&gt;
&lt;web-app xmlns=<i>"http://java.sun.com/xml/ns/javaee"</i> 
xmlns:xsi=<i>"http://www.w3.org/2001/XMLSchema-instance"</i>
       xsi:schemaLocation=<i>"http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</i>
       version=<i>"3.0"</i>&gt;
       &lt;display-name&gt;<span style="text-decoration:underline;">Guice</span> Web&lt;/display-name&gt;
       &lt;filter&gt;
              &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
              &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter
&lt;/filter-class&gt;
       &lt;/filter&gt;
       &lt;filter-mapping&gt;
              &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
              &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
       &lt;/filter-mapping&gt;
&lt;/web-app&gt;</pre>
<p>We are sure now that all requests will be processed by Guice.</p>
<p>Next step is creating the Injector and defining the Modules. Besides the “normal” modules used by an application – presented in the previous article – in order to actually use the Guice Servlet module we must declare an instance of <i>com.google.inject.servlet.ServletModule. </i>This Module is responsible for setting the <i>Request</i> and <i>Sessions</i> scopes and this is the place where we’ll configure the servlets and filters within the application.</p>
<p>Considering that we write a web application, the most logical and intuitive place to create the <i>Injector</i> is within a <i>ServletContextListener</i>. A <i>ServletContextListener </i>is a component that fires just after the application is deployed and before any request received by the server. Guice comes with its own class that must be extended in order to create a valid injector. As we’ll use Servlet 3.0 API we’ll annotate this class with <i>@WebListener</i> – we won’t need to declare it in web.xml.</p>
<p>I was saying that the ServletModule is the place where we configure our servlets and filters. This is the content of the class that it will extend this module. It will configure a servlet mapped to all the <i>.html</i> requests:</p>
<pre><b>package</b> com.insidecoding.samples.guice.modules;

<b>import</b> com.insidecoding.samples.guice.servlet.MyServlet;

<b>import</b> com.google.inject.servlet.ServletModule;

<b>public</b> <b>class</b> MyServletModule <b>extends</b> ServletModule {

       @Override
       <b>protected</b> <b>void</b> configureServlets() {
              serve("*.html").with(MyServlet.<b>class</b>);
       }
}</pre>
<p><b style="font-size:14px;font-family:Geneva, Verdana, sans-serif;line-height:1.625;"> </b></p>
<p>The ServletContextListener:</p>
<pre><b>package</b> com.insidecoding.samples.guice.listener;

<b>import</b> javax.servlet.annotation.WebListener;

<b>import</b> com.insidecoding.samples.guice.modules.MyServletModule;

<b>import</b> com.google.inject.Guice;
<b>import</b> com.google.inject.Injector;
<b>import</b> com.google.inject.servlet.GuiceServletContextListener;

@WebListener
<b>public</b> <b>class</b> MyGuiceConfig <b>extends</b> GuiceServletContextListener {

       @Override
       <b>protected</b> Injector getInjector() {
              <b>return</b> Guice.<i>createInjector</i>(<b>new</b> MyServletModule());
       }

}</pre>
<p><b> </b></p>
<p>Please note inside the <i>getInjector()</i> method the creation of the Injector based on the servlet module defined before. If the application has many other modules all of them must be declare here.</p>
<p>Also, you can see how intuitive the declaration of the servlet mapping is.</p>
<p>This is the <i>MyServlet </i>class:</p>
<pre><b>package</b> com.insidecoding.samples.guice.servlet;

<b>import</b> java.io.IOException;

<b>import</b> javax.servlet.ServletException;
<b>import</b> javax.servlet.http.HttpServlet;
<b>import</b> javax.servlet.http.HttpServletRequest;
<b>import</b> javax.servlet.http.HttpServletResponse;

<b>import</b> com.insidecoding.samples.guice.service.MyService;

<b>import</b> com.google.inject.Inject;
<b>import</b> com.google.inject.Singleton;

@Singleton
<b>public</b> <b>class</b> MyServlet <b>extends</b> HttpServlet {

       <b>private</b> <b>static</b> <b>final</b> <b>long</b> <i>serialVersionUID</i> = 1861227452784320290L;

       @Inject
       <b>private</b> MyService myService;

       <b>protected</b> <b>void</b> service(HttpServletRequest request,
                     HttpServletResponse response) 

<b>throws</b> ServletException, IOException {
              response.getWriter().println(
                           "Service: " + myService.doStuff());
       }
}</pre>
<p><b> </b></p>
<p>Let’s analyze this code:</p>
<ol>
<li>A servlet must be a singleton – mark it using the @Singleton annotation – otherwise the application will throw an Excepton</li>
<li>We use field injection in order to get a <i>MyService</i> instance</li>
<li>The class extends <i>HttpServlet</i> just like any other servlet</li>
</ol>
<p>The <i>MyService</i> interface:</p>
<pre><b>package</b> com.insidecoding.samples.guice.service;

<b>import</b> com.google.inject.ImplementedBy;

@ImplementedBy(MyServiceImpl.<b>class</b>)
<b>public</b> <b>interface</b> MyService {

       String doStuff();
}</pre>
<p><b> </b></p>
<p>And its implementation:</p>
<pre><b>package</b> com.insidecoding.samples.guice.service;

<b>public</b> <b>class</b> MyServiceImpl <b>implements</b> MyService {

       @Override
       <b>public</b> String doStuff() {
              <b>return</b> "doing stuff!";
       }

}</pre>
<p><b> </b></p>
<p>The application is ready to be deployed. This is the result of calling <i>index.html</i>:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/02/g1.png"><img class="aligncenter size-full wp-image-224" alt="g1" src="http://insidecoding.files.wordpress.com/2013/02/g1.png?w=590&#038;h=158" width="590" height="158" /></a></p>
<h1>Integrating Guice into an existing web application</h1>
<p>In order to integrate Google Guice into an existing web application we must make sure that everything is in place:</p>
<ul>
<li>The required jar are in classpath</li>
<li><span style="font-size:14px;line-height:1.625;">The Guice filter is defined in web.xml</span></li>
<li><span style="font-size:14px;line-height:1.625;">We have a </span><i style="font-size:14px;line-height:1.625;">ServletContextListener </i><span style="font-size:14px;line-height:1.625;">that extends GuiceServletContextListener</span></li>
</ul>
<p>At this stage, all these configurations will not have any impact on the application – everything will work as before.</p>
<p>We can have 2 directions:</p>
<ul>
<li><span style="line-height:1.625;">Use Guice only for new things – of course, this is not a best practice, but this is a normal scenario for big applications with legacy code</span></li>
<li><span style="font-size:14px;line-height:1.625;">Guicefy the entire application – ideal case</span></li>
</ul>
<p>For the second case you must follow the same path presented in the first part of the article.</p>
<p>For the first case we’ll end up using DI in servlet classes that are not instrumented by Guice. We can access the <i>Injector</i> instance using the <i>ServletContext:</i></p>
<p><i>Injector injector = (Injector) request.getServletContext().getAttribute(Injector.class.getName());</i></p>
<p>In order to get all the dependencies injected you can:</p>
<ol>
<li>Call <i>injector.injectMembers(this) –</i>this will inject all the dependencies</li>
<li><i>Call injector.getInstance(clazz) </i>for each instance that needs to be injected</li>
</ol>
<h1>Request and Session scope</h1>
<p>The servlet extension adds 2 new scopes: <i>Request</i> and <i>Session.</i> We’ll see next an example of using the <i>Session</i> scope.</p>
<p>We’ll slightly modify some of the classes presented before. Considering that we’ll need to mix scopes and we want to access an object with a narrower scope from an object with a wider scope (access a Session scoped object from a Singleton) we’ll use Providers (see the Note section for details).</p>
<p>The servlet module will look like this:</p>
<pre><b>package</b> com.insidecoding.samples.guice.modules;

<b>import</b> com.insidecoding.samples.guice.provider.PojoProvider;
<b>import</b> com.insidecoding.samples.guice.servlet.MyServlet;
<b>import</b> com.insidecoding.samples.guice.servlet.PojoClass;

<b>import</b> com.google.inject.servlet.ServletModule;
<b>import</b> com.google.inject.servlet.ServletScopes;

<b>public</b> <b>class</b> MyServletModule <b>extends</b> ServletModule {

       @Override
       <b>protected</b> <b>void</b> configureServlets() {
              serve("*.html").with(MyServlet.<b>class</b>);

              bind(PojoClass.<b>class</b>).toProvider(PojoProvider.<b>class</b>).in(
                           ServletScopes.<i>SESSION</i>);
       }
}</pre>
<p>Please note the ServletScopes.SESSION binding.</p>
<p>The PojoProvider class:</p>
<pre><b>package</b> com.insidecoding.samples.guice.provider;

<b>import</b> com.insidecoding.samples.guice.servlet.PojoClass;

<b>import</b> com.google.inject.Provider;

<b>public</b> <b>class</b> PojoProvider <b>implements</b> Provider&lt;PojoClass&gt; {

       <b>public</b> PojoClass get() {
              <b>return</b> <b>new</b> PojoClass();
       }

}</pre>
<p><span style="line-height:1.625;">The PojoClass class:</span></p>
<pre><b>package</b> com.insidecoding.samples.guice.servlet;

<b>public</b> <b>class</b> PojoClass {

       <b>private</b> String name;

       <b>public</b> <b>void</b> setName(String s) {
              <b>this</b>.name = s;
       }

       <b>public</b> String getName() {
              <b>return</b> <b>this</b>.name;
       }
}</pre>
<p><b> </b></p>
<p>In order to prove that the application is actually working, we’ll modify the <i>MyServlet</i> class to display additional information:</p>
<pre><b>package</b> com.insidecoding.samples.guice.servlet;

<b>import</b> java.io.IOException;

<b>import</b> javax.servlet.ServletException;
<b>import</b> javax.servlet.http.HttpServlet;
<b>import</b> javax.servlet.http.HttpServletRequest;
<b>import</b> javax.servlet.http.HttpServletResponse;

<b>import</b> com.insidecoding.samples.guice.service.MyService;

<b>import</b> com.google.inject.Inject;
<b>import</b> com.google.inject.Provider;
<b>import</b> com.google.inject.Singleton;

@Singleton
<b>public</b> <b>class</b> MyServlet <b>extends</b> HttpServlet {

       <b>private</b> <b>static</b> <b>final</b> <b>long</b> <i>serialVersionUID</i> = 1861227452784320290L;

       @Inject
       <b>private</b> Provider pojoClass;

       @Inject
       <b>private</b> MyService myService;

       <b>protected</b> <b>void</b> service(HttpServletRequest request,
                     HttpServletResponse response) 

<b>throws</b> ServletException, IOException {
              response.getWriter().println(
                           "Service: " + myService.doStuff() + " with ");
              <b>if</b> (pojoClass.get().getName() == <b>null</b>) {
                     pojoClass.get().setName("name");
              } <b>else</b> {
                     pojoClass.get().setName("existing name");
              }
              response.getWriter().println(pojoClass.get().getName());
       }
}</pre>
<p>In order to demo the functionality we’ll access the application twice in the same session. The first time it will display the below result:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/02/g2.png"><img class="aligncenter size-full wp-image-225" alt="g2" src="http://insidecoding.files.wordpress.com/2013/02/g2.png?w=590"   /></a></p>
<p>The second time it will display the following result:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/02/g3.png"><img class="aligncenter size-full wp-image-226" alt="g3" src="http://insidecoding.files.wordpress.com/2013/02/g3.png?w=590"   /></a></p>
<p>This example can be easily changed in order to use Request scope instead of Session scope.</p>
<p>This is how a simple Guice web application looks like. I tried to touch the most important points from the Guice Servlet extension. As mentioned in the previous article, this is just a small intro. You can continue experimenting different situations and you’ll learn the most by actually using Guice into a real project.</p>
<h1>Notes</h1>
<h2>Guice as a Maven dependency</h2>
<pre>&lt;dependency&gt;
 &lt;groupId&gt;com.google.inject&lt;/groupId&gt;
 &lt;artifactId&gt;guice&lt;/artifactId&gt;
 &lt;version&gt;3.0&lt;/version&gt;
 &lt;/dependency&gt;</pre>
<h2>Providers</h2>
<p>Providers address the following situations:</p>
<p>-          A client needs more instances of the same dependency per injection</p>
<p>-          A client wants to get the dependency when it will actually use it (lazy loading)</p>
<p>-          You want to inject a narrower scoped object into a wider scoped object</p>
<p>-          Additional logic is needed in order to create the object being injected</p>
<p>-          You want to control the process of creating instance per binding</p>
<p>As you can see in the previous examples, it is very easy to write a Provider. You just need to implement the <i>Provider&lt;T&gt;</i> interface, where T is the concrete type of the object being injected.</p>
<h4>Recommendations:</h4>
<p><a href="http://www.amazon.com/gp/product/1590599977/ref=as_li_qf_sp_asin_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590599977&amp;linkCode=as2&amp;tag=insicodi0f-20"><img alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=1590599977&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=1590599977" width="1" height="1" border="0" /></p>
<br />Filed under: <a href='http://insidecoding.com/category/codeproject/'>CodeProject</a>, <a href='http://insidecoding.com/category/design-patterns/'>Design Patterns</a>, <a href='http://insidecoding.com/category/java/'>Java</a>, <a href='http://insidecoding.com/category/web/'>Web</a> Tagged: <a href='http://insidecoding.com/tag/dependency-injection/'>dependency injection</a>, <a href='http://insidecoding.com/tag/guice/'>guice</a>, <a href='http://insidecoding.com/tag/jsr330/'>jsr330</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=221&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/02/g4.png" medium="image">
			<media:title type="html">g4</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/02/g1.png" medium="image">
			<media:title type="html">g1</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/02/g2.png" medium="image">
			<media:title type="html">g2</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/02/g3.png" medium="image">
			<media:title type="html">g3</media:title>
		</media:content>

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=1590599977&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=1590599977" medium="image" />
	</item>
		<item>
		<title>Code Review Guidelines &#8211; Part 2</title>
		<link>http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/</link>
		<comments>http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/#comments</comments>
		<pubDate>Sun, 27 Jan 2013 07:00:04 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[checkstyle]]></category>
		<category><![CDATA[code review]]></category>
		<category><![CDATA[findbugs]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[pmd]]></category>
		<category><![CDATA[sonar]]></category>
		<category><![CDATA[squid]]></category>

		<guid isPermaLink="false">http://insidecoding.com/?p=192</guid>
		<description><![CDATA[Please read Part 1 first. Automating the Code Review process You can split the Code Review process into 2 parts: ... <br /><a class="more-link" href="http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=192&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Please read <a title="http://insidecoding.com/2013/01/07/code-review-guidelines/" href="http://insidecoding.com/2013/01/07/code-review-guidelines/">Part 1</a> first.</p>
<h1>Automating the Code Review process</h1>
<p>You can split the Code Review process into 2 parts: the <strong>Low Level</strong> part (styling, code duplicate, naming conventions, etc) and the <strong>High Level</strong> part (architecture, design, business).</p>
<p>Starting from a Coding Standards document, you can easily automate the Low level part of the Code Review process. The automation tools for Code Review will eliminate most of the styling issues, naming conventions, cyclomatic complexity, duplicate code, code coverage, etc. They can&#8217;t though detect major design issues, architectural flaws or project specific functionality and this is where the reviewer skills are the most important. I&#8217;ll present below the most used tools for Java projects.</p>
<h2>Sonar</h2>
<p>Each time you talk about Code Quality in the Java world there are very high chances that Sonar is mentioned one way or another in the discussion. Sonar aggregates and&nbsp;displays code quality metrics on a web site allowing drilling down to the source code to view the details of violations. Sonar&#8217;s <strong>main purpose is to display <span id="more-192"></span>the metrics</strong> in a nice visual manner&nbsp;and uses various plugins to produce these code quality metrics.&nbsp;All the below tools integrate with Maven, Ant and are also available as Eclipse plugins. In order to get the same set of rules for both Sonar and these Eclipse plugins, Sonar publishes the rules for PMD, Checkstyle and Findbugs associated to a specific profile. You can easily link to them from your Eclipse plugins.</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/plugins.png"><img class="aligncenter size-full wp-image-197" alt="plugins" src="http://insidecoding.files.wordpress.com/2013/01/plugins.png?w=590"   /></a></p>
<p>Make sure you save all these files as separate XML files: findbugs.xml, checkstyle.xml and pmd.xml.</p>
<h2>PMD</h2>
<p>PMD is a source code analyzer. It finds unused variables, empty catch blocks, unnecessary object creation, and so forth.</p>
<p>This is the official page: <a href="http://pmd.sourceforge.net/">http://pmd.sourceforge.net/</a>.</p>
<p>You can get early warnings about what is wrong with your code by running it directly from eclipse: <a href="http://pmd.sourceforge.net/pmd-5.0.0/integrations.html#eclipse">http://pmd.sourceforge.net/pmd-5.0.0/integrations.html#eclipse</a>. Unfortunatelly the Eclipse plugin is not actively supported and some rules are not recognize by it. In order to fix this you must do the following:</p>
<ul>
<li>Save the PMD rules from Sonar as an XML file</li>
<li>Delete the following rules from the file: GuardDebugLogging, GenericsNaming, DontCallThreadRun, AvoidLiteralsInIfCondition, AvoidCatchingGenericException, AvoidLosingExceptionInformation</li>
</ul>
<p>After doing this you can proceed with the next steps.</p>
<ul>
<li><span style="line-height:13px;">Access Window-&gt;Preferences</span></li>
<li>Expand PMD</li>
<li>Click on Rules Configuration</li>
</ul>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/pmd.png"><img class="aligncenter size-full wp-image-200" alt="pmd" src="http://insidecoding.files.wordpress.com/2013/01/pmd.png?w=590"   /></a></p>
<p>In order to import the Sonar rule set you must do the following:</p>
<ul>
<li>First click on the <b>Clear All</b> button in order to remove all the existing rules</li>
<li>Click on the Import Rules button and select the pmd.xml file you just modified.</li>
</ul>
<p>An alternative to this is to disable these 6 rules in Sonar and import the ruleset from the published URL. In this way you have all the other rules in sync.</p>
<h2>Cobertura</h2>
<p>Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage.</p>
<p>This is the official page: <a href="http://cobertura.sourceforge.net/">http://cobertura.sourceforge.net/</a>.</p>
<p>You can get early warnings about what is wrong with your code by running it directly from eclipse: <a href="http://ecobertura.johoop.de/">http://ecobertura.johoop.de/</a>.</p>
<p>Since Sonar 3.2 Cobertura is not the default code coverage tool anymore. JaCoCo is the new default.</p>
<h2>Checkstyle</h2>
<p>Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.</p>
<p>This is the official page: <a href="http://checkstyle.sourceforge.net/">http://checkstyle.sourceforge.net/</a>.</p>
<p>You can get early warnings about what is wrong with your code by running it directly from eclipse: <a href="http://marketplace.eclipse.org/content/checkstyle-plug#.UFggAI3iZSo">http://marketplace.eclipse.org/content/checkstyle-plug#.UFggAI3iZSo</a>.</p>
<p>You can configure the Checkstyle Eclipse plugin to use the Sonar published rules as follows:</p>
<ul>
<li>Open Window-&gt;Preferences-&gt;Checkstyle</li>
<li>Click on New</li>
<li>Choose Remote Configuration from the Type drop-down</li>
<li>Enter the permalink URL from Sonar</li>
<li>Click OK after</li>
<li>Set the profile as default</li>
</ul>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/c1.png"><img class="aligncenter size-full wp-image-193" alt="c1" src="http://insidecoding.files.wordpress.com/2013/01/c1.png?w=590"   /></a></p>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/c2.png"><img class="aligncenter size-full wp-image-194" alt="c2" src="http://insidecoding.files.wordpress.com/2013/01/c2.png?w=590"   /></a></p>
<h2>Findbugs</h2>
<p>Findbugs is a program which uses static analysis to look for bugs in Java code.</p>
<p>This is the official page: <a href="http://findbugs.sourceforge.net/">http://findbugs.sourceforge.net/</a>.</p>
<p>You can get early warnings about what is wrong with your code by running it directly from eclipse:</p>
<p><a href="http://findbugs.cs.umd.edu/eclipse/">http://findbugs.cs.umd.edu/eclipse/</a></p>
<p>In order to import the Sonar findbugs tules into Eclipse you must do the following:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/f1.png"><img class="aligncenter size-full wp-image-195" alt="f1" src="http://insidecoding.files.wordpress.com/2013/01/f1.png?w=590"   /></a></p>
<p>Choose the findbugs.xml you saved previously and make sure you select the checkbox in order to enable the rules.</p>
<h2>JaCoCo</h2>
<p>JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries over the last five years.</p>
<p>This is the official page: <a href="http://www.eclemma.org/jacoco/">http://www.eclemma.org/jacoco/</a>.</p>
<p>This is the default code coverage library since Sonar 3.2.</p>
<p>For example, in order to use JaCoCo as the code coverage tool you must include the following properties in your pom file:</p>
<pre>&lt;sonar.dynamicAnalysis&gt;true&lt;/sonar.dynamicAnalysis&gt;
&lt;sonar.core.codeCoveragePlugin&gt;jacoco&lt;/sonar.core.codeCoveragePlugin&gt;</pre>
<p>More details here: <a href="http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests">http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Unit+Tests</a>.</p>
<h2>Squid</h2>
<p>Squid is part of the Sonar core plugin. More details here:</p>
<p><a href="http://www.sonarsource.org/detect-dead-code-and-calls-to-deprecated-methods-with-sonar-squid/">http://www.sonarsource.org/detect-dead-code-and-calls-to-deprecated-methods-with-sonar-squid/</a></p>
<h2>More about Sonar</h2>
<p>Sonar offers out of the box support for Java. No additional configuration is needed.</p>
<p>Sonar will use all the above tools in order to display the Code Quality metrics. This is a high level overview of the Sonar architecture:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/sonar.png"><img class="aligncenter size-full wp-image-196" alt="sonar" src="http://insidecoding.files.wordpress.com/2013/01/sonar.png?w=590"   /></a></p>
<p>Although this article presents the default just some of the plugins, Sonar can be further enhanced in order to display additional information using plugins. Additional plugins can be found here: <a href="http://docs.codehaus.org/display/SONAR/Plugin+Library">http://docs.codehaus.org/display/SONAR/Plugin+Library</a>.</p>
<p>You can get the Sonar violations directly into Eclipse. You can find here: <a href="http://docs.codehaus.org/display/SONAR/Installing+Sonar+in+Eclipse">http://docs.codehaus.org/display/SONAR/Installing+Sonar+in+Eclipse</a> how to install Sonar for Eclipse and here: <a href="http://docs.codehaus.org/display/SONAR/Configuring+Sonar+in+Eclipse">http://docs.codehaus.org/display/SONAR/Configuring+Sonar+in+Eclipse</a> how to configure it.</p>
<p>You can also find more info about Sonar for Eclipse here: <a href="http://docs.codehaus.org/display/SONAR/Using+Sonar+in+Eclipse">http://docs.codehaus.org/display/SONAR/Using+Sonar+in+Eclipse</a>.</p>
<h1>Monitoring the Code Review process</h1>
<p>Until now I presented&nbsp;advises&nbsp;about what to pay attention on when implementing a Code Review process within a project and how you can automate a part of the process, allowing the reviewer to focusing on the important things like design, architecture and business requirements.</p>
<p>What&#8217;s the best way to track the review progress:&nbsp;through&nbsp;emails, excel files, just talking or using specialized tools? Although the obvious answer is&nbsp;specialized&nbsp;tools, each team has its own particularities. Usually each team will found its own mechanic when dealing with reviews:</p>
<ul>
<li><span style="line-height:13px;">You can have a rule that no one does a commit until another&nbsp;colleague&nbsp;will review the code</span></li>
<li>Each team member will check all the commits when doing an update</li>
<li>You have a person assigned per day/sprint/iteration that does the code review for everyone</li>
<li>You do peer code review</li>
<li>You have a dedicated that does the review team formed only by senior and architects</li>
</ul>
<p>If you are in an environment where you already use Atlassian tools (JIRA, Confluence, FishEye), the wise choice is Crucible as it integrates with the whole stack of Atlassian products. Currently I think this is the best tool for keeping track of code reviews.</p>
<p>There are also other choices like: Gerrit (dedicated to Git projects) or ReviewBoard.</p>
<h3>Recommandations</h3>
<p><a href="http://www.amazon.com/gp/product/0735619670/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735619670&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0735619670&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="131" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0735619670" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/020161622X/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=020161622X&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=020161622X&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="128" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=020161622X" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0132350882&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0132350882&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="120" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0132350882" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0201485672&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0201485672&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="125" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0201485672" width="1" height="1" border="0" /></p>
<br />Filed under: <a href='http://insidecoding.com/category/best-practices/'>Best Practices</a>, <a href='http://insidecoding.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://insidecoding.com/tag/ant/'>ant</a>, <a href='http://insidecoding.com/tag/checkstyle/'>checkstyle</a>, <a href='http://insidecoding.com/tag/code-review/'>code review</a>, <a href='http://insidecoding.com/tag/findbugs/'>findbugs</a>, <a href='http://insidecoding.com/tag/maven/'>maven</a>, <a href='http://insidecoding.com/tag/pmd/'>pmd</a>, <a href='http://insidecoding.com/tag/sonar/'>sonar</a>, <a href='http://insidecoding.com/tag/squid/'>squid</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/192/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/192/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=192&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/plugins.png" medium="image">
			<media:title type="html">plugins</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/pmd.png" medium="image">
			<media:title type="html">pmd</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/c1.png" medium="image">
			<media:title type="html">c1</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/c2.png" medium="image">
			<media:title type="html">c2</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/f1.png" medium="image">
			<media:title type="html">f1</media:title>
		</media:content>

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/sonar.png" medium="image">
			<media:title type="html">sonar</media:title>
		</media:content>

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0735619670&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0735619670" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=020161622X&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=020161622X" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0132350882&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0132350882" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0201485672&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0201485672" medium="image" />
	</item>
		<item>
		<title>Code review guidelines &#8211; Part 1</title>
		<link>http://insidecoding.com/2013/01/07/code-review-guidelines/</link>
		<comments>http://insidecoding.com/2013/01/07/code-review-guidelines/#comments</comments>
		<pubDate>Mon, 07 Jan 2013 07:13:58 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[code review]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=170</guid>
		<description><![CDATA[Last updated on 27th of January. Please also read&#160;Code Review Guidelines Part 2. What is a Code Review? Code review&#160;is ... <br /><a class="more-link" href="http://insidecoding.com/2013/01/07/code-review-guidelines/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=170&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong><em>Last updated on 27th of January</em></strong>.</p>
<p>Please also read&nbsp;<a title="http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/" href="http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/">Code Review Guidelines Part 2</a>.</p>
<h1>What is a Code Review?</h1>
<p><strong>Code review</strong>&nbsp;is systematic examination (often known as peer review) of computer source code. It is intended to find and fix mistakes&nbsp;overlooked in the initial development phase, improving both the overall quality of software&nbsp;and the developers&#8217; skills.</p>
<h1>Why Reviews are important?</h1>
<p>Quoting from&nbsp;<a href="http://www.amazon.com/gp/product/0735619670/ref=as_li_qf_sp_asin_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735619670&amp;linkCode=as2&amp;tag=insicodi0f-20">Code Complete: A Practical Handbook of Software Construction, Second Edition</a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0735619670" width="1" height="1" border="0" /></p>
<blockquote><p>.. software testing alone has limited effectiveness &#8212; the average defect detection rate is only 25 percent for unit testing, 35 percent for function testing, and 45 percent for integration testing. In contrast,&nbsp;<strong>the average effectiveness of design and code inspections are 55 and 60 percent</strong>. Case studies of review results have been impressive:<span id="more-170"></span></p>
<ul>
<li>In a software-maintenance organization, 55 percent of one-line maintenance changes were in error before code reviews were introduced. After reviews were introduced, only 2 percent of the changes were in error. When all changes were considered, 95 percent were correct the first time after reviews were introduced. Before reviews were introduced, under 20 percent were correct the first time.</li>
<li>In a group of 11 programs developed by the same group of people, the first 5 were developed without reviews. The remaining 6 were developed with reviews. After all the programs were released to production, the first 5 had an average of 4.5 errors per 100 lines of code. The 6 that had been inspected had an average of only 0.82 errors per 100. Reviews cut the errors by over 80 percent.</li>
<li>The Aetna Insurance Company found 82 percent of the errors in a program by using inspections and was able to decrease its development resources by 20 percent.</li>
<li>IBM&#8217;s 500,000 line Orbit project used 11 levels of inspections. It was delivered early and had only about 1 percent of the errors that would normally be expected.</li>
<li>A study of an organization at AT&amp;T with more than 200 people reported a 14 percent increase in productivity and a 90 percent decrease in defects after the organization introduced reviews.</li>
<li>Jet Propulsion Laboratories estimates that it saves about $25,000 per inspection by finding and fixing defects at an early stage.</li>
</ul>
</blockquote>
<p>The main goals of code review are:</p>
<ol>
<li>To spot and fix defects early in the process.</li>
<li>Better-shared understanding of the code base as team members learn from each other</li>
<li>Helps to maintain a level of consistency in design and implementation.</li>
<li>Helps to identify common defects across the team thus reducing rework.</li>
<li>Builds confidence of stakeholders about technical quality of the execution.</li>
<li>Uniformity in understanding will help interchangeability of team members in case of non-availability of any one of them.</li>
<li>A different perspective.&nbsp;“Another set of eyes” adds objectivity. Similar to the reason for separating your coding and testing teams, peer reviews provide the distance needed to recognize problems.</li>
<li>Pride/reward.&nbsp;Recognition of coding prowess is a significant reward for many programmers.</li>
<li>Team cohesiveness.&nbsp;Working together helps draw team members closer. It also provides a brief respite from the isolation that coding often brings.</li>
</ol>
<p>Let&#8217;s see how important is to fix potential defects from the development phase:</p>
<p><a href="http://insidecoding.files.wordpress.com/2013/01/cost.png"><img class="aligncenter size-full wp-image-206" alt="cost" src="http://insidecoding.files.wordpress.com/2013/01/cost.png?w=590"   /></a></p>
<p>The main areas a reviewer is focusing on are as follows:</p>
<ul>
<li>General Unit Testing</li>
<li>Comment and Coding Conventions</li>
<li>Error Handling</li>
<li>Resource Leaks</li>
<li>Thread Safety</li>
<li>Control Structures</li>
<li>Performance</li>
<li>Functionality</li>
<li>Security</li>
<li>Compliance with existing Design &amp;&nbsp;Architecture</li>
<li>Proper use of 3rd party libraries</li>
</ul>
<h1>Roles and Responsibilities</h1>
<ol>
<li><strong><span style="text-decoration:underline;">Developer:</span></strong> is the person who has written the code to be reviewed and has initiated the review request.</li>
<li><strong><span style="text-decoration:underline;">Reviewer/s:</span></strong> are the people who are going to review the code and report the findings to the developer.</li>
</ol>
<div>Like any skill, good peer reviews come with practice. Some tips that should help you get started on the right track are as follows:</div>
<div></div>
<h2>Tips for the Developer:</h2>
<ol start="1">
<li><strong>The primary reviewer is the author i.e. YOU.</strong></li>
<li><strong>Create a checklist for yourself of the things that the code reviews tend to focus on</strong>.&nbsp;Some of this checklist should be easy to put together. It should follow the outline of the coding standards document. Because it&#8217;s your checklist, you can focus on the thing that you struggle with and skip the things that you rarely, if ever, have a problem with. Run through your code with the checklist and fix whatever you find. Not only will you reduce the number of things that the team finds, you&#8217;ll reduce the time to complete the code review meeting—and everyone will be happy to spend less time in the review.</li>
<li><strong>You are not your code.</strong>&nbsp;Remember that the entire point of a review is to find problems, and problems will be found. Don&#8217;t take it personally when one is uncovered.</li>
<li><strong>Understand and accept that you will make mistakes.</strong>&nbsp;The point is to find them early, before they make it into production. Fortunately, except for the few of us developing rocket guidance software at JPL, mistakes are rarely fatal in our industry, so we can, and should, learn, laugh, and move on.</li>
<li><strong>No matter how much &#8220;karate&#8221; you know, someone else will always know more.</strong>&nbsp;Such an individual can teach you some new moves if you ask. Seek and accept input from others,&nbsp;<em>especially</em>&nbsp;when you think it&#8217;s not needed.</li>
<li><strong>Don&#8217;t rewrite code without consultation.</strong>&nbsp;There&#8217;s a fine line between &#8220;fixing code&#8221; and &#8220;rewriting code.&#8221; Know the difference, and pursue stylistic changes within the framework of a code review, not as a lone enforcer.</li>
<li><strong>The only constant in the world is change.</strong>&nbsp;Be open to it and accept it with a smile. Look at each change to your requirements, platform, or tool as a new challenge, not as some serious inconvenience to be fought.</li>
<li><strong>Fight for what you believe, but gracefully accept defeat.</strong>&nbsp;Understand that sometimes your ideas will be overruled. Even if you do turn out to be right, don&#8217;t take revenge or say, &#8220;I told you so&#8221; more than a few times at most, and don&#8217;t make your dearly departed idea a martyr or rallying cry.</li>
<li><strong>Don&#8217;t be &#8220;the guy in the room.&#8221;</strong>&nbsp;Don&#8217;t be the guy coding in the dark office emerging only to buy cola. The guy in the room is out of touch, out of sight, and out of control and has no place in an open, collaborative environment.</li>
<li><strong>Please note that Review meetings are <span style="text-decoration:underline;">NOT</span> problem solving meetings.</strong></li>
<li><strong>Help to maintain the coding standards</strong>.&nbsp;Offer to add to the coding standards for things discussed that aren&#8217;t in the coding standards. One of the challenges that a developer has in an organization with combative code review practices is that they frequently don&#8217;t know where the next problem will come from. If you document each issue into the coding standards, you can check for it with your checklist the next time you come up for code reviews. It also will help cement the concept into your mind so that you&#8217;re less likely to miss opportunities to use the feedback.</li>
</ol>
<h2>&nbsp;Tips for the Reviewer</h2>
<ol start="1">
<li><strong>Critique code instead of people – be kind to the coder, not to the code.</strong>&nbsp;As much as possible, make all of your comments positive and oriented to improving the code. Relate comments to local standards, program specs, increased performance, etc.</li>
<li><strong>Treat people who know less than you with respect, deference, and patience.</strong>&nbsp;Nontechnical people who deal with developers on a regular basis almost universally hold the opinion that we are prima donnas at best and crybabies at worst. Don&#8217;t reinforce this stereotype with anger and impatience.</li>
<li><strong>The only true authority stems from knowledge, not from position.</strong>&nbsp;Knowledge engenders authority, and authority engenders respect – so if you want respect in an egoless environment, cultivate knowledge.</li>
<li><strong>Please note that Review meetings are <span style="text-decoration:underline;">NOT</span> problem solving meetings.</strong></li>
<li><strong>Ask questions rather than make statements</strong>.&nbsp;A statement is accusatory. &#8220;You didn&#8217;t follow the standard here&#8221; is an attack—whether intentional or not. The question, &#8220;What was the reasoning behind the approached you used?&#8221; is seeking more information. Obviously, that question can&#8217;t be said with a sarcastic or condescending tone; but, done correctly, it can often open the developer up to stating their thinking and then asking if there was a better way.</li>
<li><strong>Avoid the &#8220;Why&#8221; questions</strong>.&nbsp;Although extremely difficult at times, avoiding the&#8221;Why&#8221; questions can substantially improve the mood. Just as a statement is accusatory—so is a why question. Most &#8220;Why&#8221; questions can be reworded to a question that doesn&#8217;t include the word &#8220;Why&#8221; and the results can be dramatic. For example, &#8220;Why didn&#8217;t you follow the standards here&#8230;&#8221; versus &#8220;What was the reasoning behind the deviation from the standards here&#8230;&#8221;</li>
<li><strong>Remember to praise</strong>.&nbsp;The purposes of code reviews are not focused at telling developers how they can improve, and not necessarily that they did a good job. Human nature is such that we want and need to be acknowledged for our successes, not just shown our faults. Because development is necessarily a creative work that developers pour their soul into, it often can be close to their hearts. This makes the need for praise even more critical.</li>
<li><strong>Make sure you have good coding standards to reference</strong>.&nbsp;Code reviews find their foundation in the coding standards of the organization. Coding standards are supposed to be the shared agreement that the developers have with one another to produce quality, maintainable code. If you&#8217;re discussing an item that isn&#8217;t in your coding standards, you have some work to do to get the item in the coding standards. You should regularly ask yourself whether the item being discussed is in your coding standards.</li>
<li><strong>Remember that there is often more than one way to approach a solution</strong>.&nbsp;Although the developer might have coded something differently from how you would have, it isn&#8217;t necessarily wrong. The goal is quality, maintainable code. If it meets those goals and follows the coding standards, that&#8217;s all you can ask for.</li>
<li><strong>You shouldn&#8217;t rush through a code review</strong> <strong>- but also, you need to do it promptly.</strong> Your coworkers are waiting for you.</li>
<li>Review fewer than 200-400 lines of code at a time.</li>
</ol>
<h1>Security Code Review</h1>
<p>If the applications requires a very close attention to security then <a title="www.owasp.org" href="www.owasp.org">www.owasp.org</a> is the right place for resources. This is a very good Security Code Review document to consider:&nbsp;<a title="https://www.owasp.org/images/2/2e/OWASP_Code_Review_Guide-V1_1.pdf" href="https://www.owasp.org/images/2/2e/OWASP_Code_Review_Guide-V1_1.pdf">https://www.owasp.org/images/2/2e/OWASP_Code_Review_Guide-V1_1.pdf</a></p>
<h1>Assign Severity to Review Findings</h1>
<p>The severity to find issues with code should go as below. Reviewer must focus on issues with High severity first and then to Medium severity and then Low severity issues.</p>
<ol start="10">
<ol start="1">
<li>Naming Conventions and Coding style = Low</li>
<li>Control Structures and Logical issues = Medium or High</li>
<li>Redundant Code = High</li>
<li>Performance Issues =High</li>
<li>Security Issues = High</li>
<li>Scalability Issues= High</li>
<li>Functional Issues =High</li>
<li>Error Handling = High</li>
<li>Reusability = Medium</li>
<li>Design &amp; Architecture = High</li>
<li>Proper use of 3rd party libraries = Medium</li>
</ol>
</ol>
<h1>Checklist for developers and reviewers</h1>
<p>Checklists are a&nbsp;highly recommended&nbsp;way to&nbsp;find the things&nbsp;you forget to&nbsp;do, and are useful for both authors and reviewers. Omissions&nbsp;are the hardest defects to find – after all, it’s hard&nbsp;to review something that’s not there. A checklist is the&nbsp;single best way to combat the problem, as it reminds the&nbsp;reviewer or author to take the time to look for something&nbsp;that might be missing. A checklist will remind authors and&nbsp;reviewers to confirm that all errors are handled, that function&nbsp;arguments are tested for invalid values, and that unit&nbsp;tests have been created.<br />
Another useful concept is the personal checklist. Each&nbsp;person typically makes the same 15-20 mistakes. If you&nbsp;notice what your typical errors are, you can develop your&nbsp;own personal checklist (PSP, SEI, and CMMI recommend&nbsp;this practice too). Reviewers will do the work of determining&nbsp;your common mistakes. All you have to do is keep a&nbsp;short checklist of the common flaws in your work, particularly&nbsp;the things you forget to do.&nbsp;As soon as you start recording your defects in a checklist,&nbsp;you will start making fewer of them. The rules will be&nbsp;fresh in your mind and your error rate will drop.&nbsp;We&#8217;ve&nbsp;seen this happen over and over.</p>
<h3>Checklist for Developers</h3>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><strong>Description</strong></td>
<td>
<p align="center"><strong>Confirmed?</strong></p>
</td>
</tr>
<tr>
<td>My code compiles</td>
<td></td>
</tr>
<tr>
<td>My code has been developer-tested and includes unit tests</td>
<td></td>
</tr>
<tr>
<td>My code includes javadoc where appropriate</td>
<td></td>
</tr>
<tr>
<td>My code is tidy (indentation, line length, no commented-out code, no spelling mistakes, etc)</td>
<td></td>
</tr>
<tr>
<td>I have considered proper use of exceptions</td>
<td></td>
</tr>
<tr>
<td>I have made appropriate use of logging</td>
<td></td>
</tr>
<tr>
<td>I have eliminated unused imports</td>
<td></td>
</tr>
<tr>
<td>I have eliminated Eclipse warnings</td>
<td></td>
</tr>
<tr>
<td>I have considered possible NPEs</td>
<td></td>
</tr>
<tr>
<td>The code follows the Coding Standards</td>
<td></td>
</tr>
<tr>
<td>Are there any leftover stubs or test routines in the code?</td>
<td></td>
</tr>
<tr>
<td>Are there any hardcoded, development only things still in the code?</td>
<td></td>
</tr>
<tr>
<td>Was performance considered?</td>
<td></td>
</tr>
<tr>
<td>Was security considered?</td>
<td></td>
</tr>
<tr>
<td>Does the code release resources? (HTTP connections, DB connection, files, etc)</td>
<td></td>
</tr>
<tr>
<td>Corner cases well documented or any workaround for a known limitation of the frameworks</td>
</tr>
<tr>
<td>Can any code be replaced by calls to external reusable components or library functions?</td>
<td></td>
</tr>
<tr>
<td>Thread safety and possible deadlocks</td>
<td></td>
</tr>
<tr>
<td>Does the code do what&#8217;s supposed to</td>
<td></td>
</tr>
<tr>
<td>Does my change have any side effects on existing functionality</td>
<td></td>
</tr>
<tr>
<td>Is the code consistent with the current design/architecture</td>
<td></td>
</tr>
<tr>
<td>Checked for existing methods/utilities before implementing new ones</td>
<td></td>
</tr>
</tbody>
</table>
<h3>&nbsp;Checklist for Reviewers</h3>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><strong>Description</strong></td>
<td>
<p align="center"><strong>Confirmed?</strong></p>
</td>
</tr>
<tr>
<td>The code meets the business requirements</td>
<td></td>
</tr>
<tr>
<td>Comments are comprehensible and add something to the maintainability of the code</td>
<td></td>
</tr>
<tr>
<td>Comments are neither too numerous nor verbose</td>
<td></td>
</tr>
<tr>
<td>Types have been generalized where possible</td>
<td></td>
</tr>
<tr>
<td>Parameterized types have been used appropriately</td>
<td></td>
</tr>
<tr>
<td>Exceptions have been used appropriately</td>
<td></td>
</tr>
<tr>
<td>Repetitive code has been factored out</td>
<td></td>
</tr>
<tr>
<td>Frameworks have been used appropriately &#8211; methods have all been defined appropriately</td>
<td></td>
</tr>
<tr>
<td>Command classes have been designed to undertake one task only</td>
<td></td>
</tr>
<tr>
<td>JSPs do not contain business logic</td>
<td></td>
</tr>
<tr>
<td>Unit tests are present and correct</td>
<td></td>
</tr>
<tr>
<td>Common errors have been checked for</td>
<td></td>
</tr>
<tr>
<td>Potential threading issues have been eliminated where possible</td>
<td></td>
</tr>
<tr>
<td>Any security concerns have been addressed</td>
<td></td>
</tr>
<tr>
<td>Performance was considered</td>
<td></td>
</tr>
<tr>
<td>The functionality fits the current design/architecture</td>
<td></td>
</tr>
<tr>
<td>The code is unit testable</td>
<td></td>
</tr>
<tr>
<td>The code does not use unjustifiable static methods/blocks</td>
<td></td>
</tr>
<tr>
<td>The code complies to coding standards</td>
<td></td>
</tr>
<tr>
<td>Logging used appropriately (proper logging level and details)</td>
<td></td>
</tr>
<tr>
<td>NPEs and AIOBs</td>
<td></td>
</tr>
<tr>
<td>The code does not reinvent the wheel</td>
<td></td>
</tr>
<tr>
<td>The code does not have any side effect on existing functionality</td>
<td></td>
</tr>
</tbody>
</table>
<p>I also made them in checklist format to be printed:&nbsp;<a href="http://insidecoding.files.wordpress.com/2013/01/codereview_checklistfordevelopers1.docx">CodeReview_ChecklistforDevelopers</a>,&nbsp;<a href="http://insidecoding.files.wordpress.com/2013/01/codereview_checklistforreviewers1.docx">CodeReview_ChecklistforReviewers</a>.</p>
<p>Continue reading <a title="http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/" href="http://insidecoding.com/2013/01/27/code-review-guidelines-part-2/">Code Review Guidelines Part 2</a>.</p>
<h3> Recommendations </h3>
<p><a href="http://www.amazon.com/gp/product/0735619670/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0735619670&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0735619670&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="131" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0735619670" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/020161622X/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=020161622X&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=020161622X&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="128" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=020161622X" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/0132350882/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0132350882&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0132350882&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="120" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0132350882" width="1" height="1" border="0" /><br />
<a href="http://www.amazon.com/gp/product/0201485672/ref=as_li_tf_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0201485672&amp;linkCode=as2&amp;tag=insicodi0f-20"><img class="alignleft" style="border:0;" alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=0201485672&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" width="125" height="160" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0201485672" width="1" height="1" border="0" /><br />
References:</p>
<ol>
<li><a href="http://www.codinghorror.com/blog/2006/05/the-ten-commandments-of-egoless-programming.html">http://www.codinghorror.com/blog/2006/05/the-ten-commandments-of-egoless-programming.html</a></li>
<li><a href="http://www.developer.com/java/other/article.php/3579756">http://www.developer.com/java/other/article.php/3579756</a></li>
<li><a href="http://www.smartbear.com/docs/BestPracticesForPeerCodeReview.pdf">http://www.smartbear.com/docs/BestPracticesForPeerCodeReview.pdf</a></li>
<li><a href="http://www.amazon.com/gp/product/0201734850/ref=as_li_qf_sp_asin_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0201734850&amp;linkCode=as2&amp;tag=insicodi0f-20">Peer Reviews in Software: A Practical Guide</a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=0201734850" width="1" height="1" border="0" /></li>
<li><a title="http://www.techrepublic.com/article/developers-guide-to-peer-reviews/1050834" href="http://www.techrepublic.com/article/developers-guide-to-peer-reviews/1050834">http://www.techrepublic.com/article/developers-guide-to-peer-reviews/1050834</a></li>
<li><a title="http://smartbear.com/SmartBear/media/pdfs/best-kept-secrets-of-peer-code-review.pdf" href="http://smartbear.com/SmartBear/media/pdfs/best-kept-secrets-of-peer-code-review.pdf">http://smartbear.com/SmartBear/media/pdfs/best-kept-secrets-of-peer-code-review.pdf</a></li>
</ol>
<br />Filed under: <a href='http://insidecoding.com/category/best-practices/'>Best Practices</a>, <a href='http://insidecoding.com/category/codeproject/'>CodeProject</a> Tagged: <a href='http://insidecoding.com/tag/code-review/'>code review</a>, <a href='http://insidecoding.com/tag/security/'>security</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/170/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=170&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2013/01/07/code-review-guidelines/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0735619670" medium="image" />

		<media:content url="http://insidecoding.files.wordpress.com/2013/01/cost.png" medium="image">
			<media:title type="html">cost</media:title>
		</media:content>

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0735619670&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0735619670" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=020161622X&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=020161622X" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0132350882&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0132350882" medium="image" />

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=0201485672&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0201485672" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=0201734850" medium="image" />
	</item>
		<item>
		<title>Having JAXB1 and JAXB2 in the same classpath</title>
		<link>http://insidecoding.com/2012/08/30/having-jaxb1-and-jaxb2-in-the-same-classpath/</link>
		<comments>http://insidecoding.com/2012/08/30/having-jaxb1-and-jaxb2-in-the-same-classpath/#comments</comments>
		<pubDate>Thu, 30 Aug 2012 09:10:07 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WebServices]]></category>
		<category><![CDATA[classpath]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jaxb]]></category>
		<category><![CDATA[jaxb1]]></category>
		<category><![CDATA[jaxb2]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=167</guid>
		<description><![CDATA[If you just add JAXB1 and JAXB2 jars into the same classpath you may end up with the following exception: ... <br /><a class="more-link" href="http://insidecoding.com/2012/08/30/having-jaxb1-and-jaxb2-in-the-same-classpath/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=167&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>If you just add JAXB1 and JAXB2 jars into the same classpath you may end up with the following exception:</p>
<pre>You are trying to run JAXB 2.0 runtime but you have old JAXB 1.0 
runtime earlier in the classpath.
Please remove the JAXB 1.0 runtime for 2.0 runtime to work correctly.</pre>
<p>The ideal solution would be to migrate everything to JAXB2 and remove the JAXB1 dependencies, but if you have certain constrains around this (and there usually are a lot) like: time, legacy systems, too much effort to retest everythig, etc there is an easy solution to have the old code run smooth and in the same time write the new code using the latest version of JAXB.</p>
<p>Here are the steps you need to follow:</p>
<ul>
<li>Download jax1-impl-XXX.jar, where XXX is something like 2.x.x (this can be found in the JAXB2 distribution)</li>
<li>Remove your old jaxb-impl.jar (version 1) from the classpath</li>
<li>Add the new jaxb1-impl-XXX.jar into the classpath</li>
</ul>
<p>All done!</p>
<br />Filed under: <a href='http://insidecoding.com/category/java/'>Java</a>, <a href='http://insidecoding.com/category/java-ee/'>Java EE</a>, <a href='http://insidecoding.com/category/web/'>Web</a>, <a href='http://insidecoding.com/category/webservices/'>WebServices</a> Tagged: <a href='http://insidecoding.com/tag/classpath/'>classpath</a>, <a href='http://insidecoding.com/tag/exception/'>exception</a>, <a href='http://insidecoding.com/tag/java-2/'>java</a>, <a href='http://insidecoding.com/tag/jaxb/'>jaxb</a>, <a href='http://insidecoding.com/tag/jaxb1/'>jaxb1</a>, <a href='http://insidecoding.com/tag/jaxb2/'>jaxb2</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=167&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2012/08/30/having-jaxb1-and-jaxb2-in-the-same-classpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>
	</item>
		<item>
		<title>Some SQL injection in Android &#8211; how to use GROUP BY and CASE when you are not allowed to do so</title>
		<link>http://insidecoding.com/2012/02/08/some-sql-injection-in-android-how-to-use-group-by-and-case-when-you-are-not-allowed-to-do-so/</link>
		<comments>http://insidecoding.com/2012/02/08/some-sql-injection-in-android-how-to-use-group-by-and-case-when-you-are-not-allowed-to-do-so/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 00:02:53 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[content resolver]]></category>
		<category><![CDATA[group by]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=158</guid>
		<description><![CDATA[I suppose you&#8217;re all familiar with the &#8220;academic&#8221; examples of SQL injection when you put a AND 1=1 at the ... <br /><a class="more-link" href="http://insidecoding.com/2012/02/08/some-sql-injection-in-android-how-to-use-group-by-and-case-when-you-are-not-allowed-to-do-so/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=158&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I suppose you&#8217;re all familiar with the &#8220;academic&#8221; examples of SQL injection when you put a AND 1=1 at the end of the SQL query and magically you get access to all kind of nasty things. I&#8217;ll show you how to use the same trick to do a SQL injection in Android, but for a good thing <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I had the following situation: I&#8217;ve wanted to select group all the calls from the call log either by the caller name if the number was in the agenda or by number if the number was not in the agenda. To do this you need a content resolver, a URI and a projection to tell Android which columns you want to select. A typical call will look as follows:</p>
<pre>getContentResolver().query(CallLog.Calls.CONTENT_URI,&nbsp;
new String[] { projection}, selection, new String[] 
{selectionArgs}, sortOrder)</pre>
<p>As you notice there is no way of telling android how to group the rows. But we<span id="more-158"></span> can trick it:</p>
<pre>getContentResolver().query(CallLog.Calls.CONTENT_URI, new String[] 
{"_id", "name", "number",
 "duration","type", "numbertype", MAX(date) AS date",
"(CASE WHEN name=null THEN name ELSE number END) as logname"},&nbsp;
" 1=1 ) GROUP BY (logname",null,null);</pre>
<p>You can notice the <em>1=1 ) GROUP by (logname</em>&nbsp;String placed as the <em>selection</em> argument. This is why the magic happens. <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Also notice the syntax for the CASE statement that&nbsp;conditionally&nbsp;selects the number or the name. You can further use it in any place you need conditional select of the columns.</p>
<p>Happy Android programming!</p>
<br />Filed under: <a href='http://insidecoding.com/category/android/'>Android</a>, <a href='http://insidecoding.com/category/tips/'>Tips</a> Tagged: <a href='http://insidecoding.com/tag/android-2/'>android</a>, <a href='http://insidecoding.com/tag/case/'>case</a>, <a href='http://insidecoding.com/tag/content-resolver/'>content resolver</a>, <a href='http://insidecoding.com/tag/group-by/'>group by</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=158&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2012/02/08/some-sql-injection-in-android-how-to-use-group-by-and-case-when-you-are-not-allowed-to-do-so/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>
	</item>
		<item>
		<title>Guice &#8211; a lightweight DI framework alternative to Spring</title>
		<link>http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/</link>
		<comments>http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 18:22:55 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[DI]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[jsr-330]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=144</guid>
		<description><![CDATA[What is Guice? To quote from the Guice official page &#8220;Guice alleviates the need for factories and the use of new in ... <br /><a class="more-link" href="http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=144&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h2>What is Guice?</h2>
<p>To quote from the Guice <a title="Guice" href="http://code.google.com/p/google-guice/" target="_blank">official page </a></p>
<blockquote><p>&#8220;Guice alleviates the need for factories and the use of <tt>new</tt> in your Java code. Think of Guice&#8217;s <tt>@Inject</tt> as the new <tt>new</tt>. Guice embraces Java&#8217;s type safe nature, especially when it comes to features introduced in Java 5 such as generics and annotations. You might think of Guice as filling in missing features for core Java. Ideally, the language itself would provide most of the same features, but until such a language comes along, we have Guice&#8221;</p></blockquote>
<p>If you&#8217;ve used Spring or other DI frameworks you&#8217;re probably familiar to the above ideas. I&#8217;ll present in this article how to get started with Guice in a non-web application. I&#8217;ll detail the web side in a future article.</p>
<h2>Is it better than Spring?</h2>
<p>Well, this is one of those talks that could last forever. Like EJB vs Spring, Struts vs JSF and so on. It&#8217;s a matter of choice. I can say one thing though: if you need just  DI in your application go for Guice. It&#8217;s very lightweight, it&#8217;s type safe and it&#8217;s faster than Spring.</p>
<h2>Using Guice for a non-web application</h2>
<p>Suppose you have an application and the class that contains the main method its called <em>Application</em>. When using Guice the approach is a little changed. Besides starting the application you&#8217;ll also want a way to trigger the dependency injection. In order to to this you must have an &#8220;entry-point&#8221; that &#8220;manually&#8221; triggers the creation of the graph of objects that will get injected and only after that starts the actual application logic. We&#8217;ll call the class responsible for these 2 things the Bootstrap class. We also need to create a <em>Module</em> by extending the <em>AbstractModule</em> class. Quoting from the Java Doc of the <em>Module</em> interface:</p>
<blockquote><p>A module contributes configuration information, typically interface bindings, which will be used to create an <code><a href="http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Injector.html">Injector</a></code>. A Guice-based application is ultimately composed of little more than a set of <code>Module</code>s and some bootstrapping code.</p></blockquote>
<p>Let&#8217;s see the code:</p>
<h3>The Bootstrap class:</h3>
<pre>package com.insidecoding.guice;
import com.google.inject.Guice;</pre>
<p><span id="more-144"></span></p>
<pre>import com.google.inject.Injector;
/**
 * Bootstrap class that creates the root object that triggers the graph of
 * objects creation and injection
 */
public class Bootstrap {
public static void main(String[] args) {
 Injector injector = Guice.createInjector(new MyModule());
 IApplication app = injector.getInstance(IApplication.class);
 app.start();
 }
}</pre>
<h3>The IApplication interface:</h3>
<pre>package com.insidecoding.guice;
public interface IApplication {
 /**
 * Starts the current application
 */
 void start();
}</pre>
<h3>The Application class:</h3>
<pre>package com.insidecoding.guice;

import com.google.inject.Inject;

public class Application implements IApplication {
@Inject
 private IPrintingService ip;
public void start() {
 ip.print("Hello from guice");
 }
}</pre>
<p>Suppose the logic of your application is as follows: use the <em>IPrintingService</em> to print a <em>String</em> and the printing service will use a <em>ICachingService</em> to cache that <em>String</em>. The implementations of these services will be injected using Guice in the <em>Appplication, </em>respective the <em>ICachingService</em> implementation class.</p>
<h3>The <em>IPrintingService</em> interface:</h3>
<pre>package com.insidecoding.guice;
import com.google.inject.ImplementedBy;

@ImplementedBy(PrintingService.class)
public interface IPrintingService {
/**
 * Prints a string
 * @param s
 */
 void print(String s);
}</pre>
<h3>The <em>PrintingService</em> class:</h3>
<pre>package com.insidecoding.guice;

import javax.inject.Inject;

public class PrintingService implements IPrintingService {

	@Inject
	private ICachingService cs;

	@Override
	public void print(String s) {
		System.out.println(s);
		cs.cacheIt(s);
	}

}</pre>
<h3>The <em>ICachingService</em>:</h3>
<pre>package com.insidecoding.guice;

import com.google.inject.ImplementedBy;

@ImplementedBy(CachingService.class)
public interface ICachingService {

	/**
	 * Caches a String
	 * @param s
	 */
	void cacheIt(String s);
}</pre>
<h3>The <em>CachingService</em> class:</h3>
<pre>package com.insidecoding.guice;

public class CachingService implements ICachingService {

	@Override
	public void cacheIt(String s) {
		System.out.println("Caching: " + s);
	}

}</pre>
<h3>The <em>MyModule</em> class:</h3>
<pre>package com.insidecoding.guice;

import com.google.inject.AbstractModule;

public class MyModule extends AbstractModule {

       @Override
       protected void configure() {
          bind(IApplication.class).to(Application.class);
       }
}</pre>
<p>The code is pretty simple and straightforward. In order to make this code work, you need to add just the following JARs from the Guice distribution:</p>
<ul>
<li>aopalliance.jar</li>
<li>guice-3.0.jar</li>
<li>javax.inject.jar</li>
</ul>
<p>If you take a look closer on the above code you&#8217;ll see a small different as far as injection is concerned: the <em>Application</em> class uses the <em>com.google.inject.Inject</em> annotation and the <em>PritingService</em> class uses the<em> javax.inject.Inject</em> annotation. This is because Guice 3.0 is compatible with JSR-330. Mixing JSR-330 and Guice annotations though is discouraged so stick with Guice <em>@Inject</em> for now.</p>
<h2>Types of injection</h2>
<p>Guice has 3 types of injection:</p>
<ul>
<li>field injection</li>
<li>constructor injection</li>
<li>method injection</li>
</ul>
<div>We&#8217;ve seen the <strong>field injection</strong> type in action in the above example. In order to use <strong>constructor injection</strong> we can modify the <em>PrintingService</em> class to look like this:</div>
<div></div>
<div>
<pre>package com.insidecoding.guice;

import javax.inject.Inject;

public class PrintingService implements IPrintingService {

	private ICachingService cs;

        @Inject
        public PrintingService(ICachingService cachingService) {
             this.cs = cachingService;
        }

	@Override
	public void print(String s) {
		System.out.println(s);
		cs.cacheIt(s);
	}

}</pre>
</div>
<div>Note that when using field injection Guice will call the no-arg constructor in order to instantiate objects.</div>
<div>Method injection can be used to inject the parameters. The Guice injector will resolve all dependencies before invoking the method. Injected method can have any number of parameters and the method name has no impact on the injection. Method injection looks as follows:</div>
<pre>       @Inject
       public void printMe(IPrintingService ps) {
            ps.print("Printing with Guice method injection");
       }</pre>
<div></div>
<h2>@ImplementedBy vs bind()</h2>
<p>You can notice in the above code that there are 2 ways of declaring bindings between interfaces and implementations:</p>
<ul>
<li>annotate the interface with <em>@ImplementedBy</em></li>
<li>use the bind()<em> method</em> in a Module to specify the actual implementation: see the <em>MyModule.configure()</em> method.</li>
</ul>
<div>For simple cases and interfaces with just one implementation you can go with the <em>@ImplemenedBy</em> approach, but if you need more control over your bindings or if you deal with multiple implementations definitely use the <em>bind()</em> method.</div>
<div>One important thing to notice is that Guice returns a new instance by default each time it supplies a value. You can switch to singletons using the following code:</div>
<pre>bind(ICachingService).to(CachingService).in(Scopes.SINGLETON)</pre>
<div>This is just a high level presentation of Guice. You can do a lot more with it and I hope I&#8217;ve opened you appetite to discover its capabilities. If you need a lightweight framework for DI, Guice beats Spring all the way.</div>
<p>Please continue reading about <a title="http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/" href="http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/">using Guice for Web applications</a>.</p>
<h4>Recommendations:</h4>
<p><a href="http://www.amazon.com/gp/product/1590599977/ref=as_li_qf_sp_asin_il?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1590599977&amp;linkCode=as2&amp;tag=insicodi0f-20"><img alt="" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;ASIN=1590599977&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=insicodi0f-20" border="0" /></a><img style="border:none!important;margin:0!important;" alt="" src="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&amp;l=as2&amp;o=1&amp;a=1590599977" width="1" height="1" border="0" /></p>
<br />Filed under: <a href='http://insidecoding.com/category/design-patterns/'>Design Patterns</a>, <a href='http://insidecoding.com/category/java/'>Java</a>, <a href='http://insidecoding.com/category/spring/'>Spring</a>, <a href='http://insidecoding.com/category/web/'>Web</a> Tagged: <a href='http://insidecoding.com/tag/annotation/'>annotation</a>, <a href='http://insidecoding.com/tag/aop/'>aop</a>, <a href='http://insidecoding.com/tag/di/'>DI</a>, <a href='http://insidecoding.com/tag/guice/'>guice</a>, <a href='http://insidecoding.com/tag/jsr-330/'>jsr-330</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=144&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2012/02/03/guice-a-lightweight-di-framework-alternative-to-spring-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>

		<media:content url="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&#38;ASIN=1590599977&#38;Format=_SL160_&#38;ID=AsinImage&#38;MarketPlace=US&#38;ServiceVersion=20070822&#38;WS=1&#38;tag=insicodi0f-20" medium="image" />

		<media:content url="http://www.assoc-amazon.com/e/ir?t=insicodi0f-20&#38;l=as2&#38;o=1&#38;a=1590599977" medium="image" />
	</item>
		<item>
		<title>Chrono Touch &#8211; my new Android application</title>
		<link>http://insidecoding.com/2012/01/25/chrono-touch-my-new-android-application/</link>
		<comments>http://insidecoding.com/2012/01/25/chrono-touch-my-new-android-application/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 17:57:43 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[chronometer]]></category>
		<category><![CDATA[timer]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=140</guid>
		<description><![CDATA[I&#8217;ve just released a new Android application. You can download it for free from Android Market. https://market.android.com/details?id=ro.tipspedia.chronotimer QR Code: &#160; ... <br /><a class="more-link" href="http://insidecoding.com/2012/01/25/chrono-touch-my-new-android-application/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=140&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve just released a new Android application. You can download it for free from Android Market.</p>
<p><a href="https://market.android.com/details?id=ro.tipspedia.chronotimer" rel="nofollow">https://market.android.com/details?id=ro.tipspedia.chronotimer</a></p>
<p>QR Code:</p>
<p><img src="http://chart.apis.google.com/chart?cht=qr&amp;chs=100x100&amp;choe=UTF-8&amp;chld=H%7C0&amp;chl=http://goo.gl/l8fLD" alt="QR code" /></p>
<p>&nbsp;</p>
<br />Filed under: <a href='http://insidecoding.com/category/android/'>Android</a> Tagged: <a href='http://insidecoding.com/tag/android-2/'>android</a>, <a href='http://insidecoding.com/tag/app/'>app</a>, <a href='http://insidecoding.com/tag/chronometer/'>chronometer</a>, <a href='http://insidecoding.com/tag/timer/'>timer</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=140&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2012/01/25/chrono-touch-my-new-android-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>

		<media:content url="http://chart.apis.google.com/chart?cht=qr&#38;chs=100x100&#38;choe=UTF-8&#38;chld=H%7C0&#38;chl=http://goo.gl/l8fLD" medium="image">
			<media:title type="html">QR code</media:title>
		</media:content>
	</item>
		<item>
		<title>My JavaWorld article</title>
		<link>http://insidecoding.com/2011/10/06/my-javaworld-article/</link>
		<comments>http://insidecoding.com/2011/10/06/my-javaworld-article/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 17:47:06 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaWorld]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=137</guid>
		<description><![CDATA[The link to my first JavaWorld article: http://www.javaworld.com/javaworld/jw-10-2011/111004-jtip-recursion-in-java-7.html Filed under: Java Tagged: JavaWorld<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=137&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The link to my first JavaWorld article: <a href="http://www.javaworld.com/javaworld/jw-10-2011/111004-jtip-recursion-in-java-7.html">http://www.javaworld.com/javaworld/jw-10-2011/111004-jtip-recursion-in-java-7.html</a></p>
<br />Filed under: <a href='http://insidecoding.com/category/java/'>Java</a> Tagged: <a href='http://insidecoding.com/tag/javaworld/'>JavaWorld</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/137/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=137&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2011/10/06/my-javaworld-article/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>
	</item>
		<item>
		<title>How to display images from the database in your jsp pages &#8211; the easy way</title>
		<link>http://insidecoding.com/2011/09/25/how-to-display-images-from-the-database-in-your-jsp-pages-the-easy-way/</link>
		<comments>http://insidecoding.com/2011/09/25/how-to-display-images-from-the-database-in-your-jsp-pages-the-easy-way/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 21:24:11 +0000</pubDate>
		<dc:creator>ludovicianul</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[Servlets]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://insidecoding.wordpress.com/?p=112</guid>
		<description><![CDATA[The easiest and elegant way to read images from the database and display them in the UI is through a ... <br /><a class="more-link" href="http://insidecoding.com/2011/09/25/how-to-display-images-from-the-database-in-your-jsp-pages-the-easy-way/">Continue reading</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=112&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The easiest and elegant way to read images from the database and display them in the UI is through a servlet that handles the image processing for you. I&#8217;ll present below a sample that displays a user avatar in the jsp page. This sample uses Spring 3 and the tip from <a href="http://insidecoding.wordpress.com/2011/09/08/how-to-inject-spring-beans-into-servlets/">this</a> article to inject a spring bean into this servlet. This example can easily be adapted to many kinds of situations. Here is the code:</p>
<h3>The servlet</h3>
<pre>package insidecoding.servlet;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import insidecoding.service.UserService;

@Component("imageServlet")
public class ImageServlet implements HttpRequestHandler {

    @Autowired
    private UserService UserService;

    @Override
    public void handleRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // get the thumb from the user entity
        byte[] thumb = userService.getCurrentUserAvatar();

        String name = "userAvatar";
        response.setContentType("image/jpeg");
        response.setContentLength(thumb.length);

        response.setHeader("Content-Disposition", "inline; filename=\"" + name
                + "\"");

        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new ByteArrayInputStream(thumb));
            output = new BufferedOutputStream(response.getOutputStream());
            byte[] buffer = new byte[8192];
            int length;
            while ((length = input.read(buffer)) &gt; 0) {
                output.write(buffer, 0, length);
            }
        } catch (IOException e) {
            System.out.println("There are errors in reading/writing image stream "
                    + e.getMessage());
        } finally {
            if (output != null)
                try {
                    output.close();
                } catch (IOException ignore) {
                }
            if (input != null)
                try {
                    input.close();
                } catch (IOException ignore) {
                }
        }

    }
}</pre>
<h3>Map this servlet to the /image path in web.xml</h3>
<pre>     &lt;servlet&gt;
        &lt;servlet-name&gt;imageServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.springframework.web.context.support.HttpRequestHandlerServlet&lt;/servlet-class&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;imageServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/image/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;</pre>
<h3>Usage in JSP<span id="more-112"></span></h3>
<pre>....
&lt;img src="image/avatar" height="75px" width="75px" align="left" /&gt;
....</pre>
<p>The code is pretty self-explanatory:</p>
<ul>
<li>get the byte[] array from the database</li>
<li>wrap it with an InputStream</li>
<li>write the data from the InputStream to the servlet output stream in chunks</li>
</ul>
<p>This code version supposes that you want to display the avatar of the current logged user. But this servlet can be used with any type of situation. For example, if you want to display images by the imageId, you can do this:</p>
<pre>....
&lt;img src="image/1212" height="75px" width="75px" align="left" /&gt;
....</pre>
<p>Where 1212 is the imageId. You can then grab this imageId in the servlet, get the corresponding byte[] from the DB and continue as in the above code.</p>
<p>Nice, elegant and easy.</p>
<p>If you have questions please leave a comment below.</p>
<br />Filed under: <a href='http://insidecoding.com/category/java/'>Java</a>, <a href='http://insidecoding.com/category/java-ee/'>Java EE</a>, <a href='http://insidecoding.com/category/servlets-2/'>Servlets</a>, <a href='http://insidecoding.com/category/web/'>Web</a> Tagged: <a href='http://insidecoding.com/tag/database/'>database</a>, <a href='http://insidecoding.com/tag/image/'>image</a>, <a href='http://insidecoding.com/tag/jsp/'>jsp</a>, <a href='http://insidecoding.com/tag/servlet/'>servlet</a>, <a href='http://insidecoding.com/tag/thumbnail/'>thumbnail</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/insidecoding.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/insidecoding.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=insidecoding.com&#038;blog=26795754&#038;post=112&#038;subd=insidecoding&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://insidecoding.com/2011/09/25/how-to-display-images-from-the-database-in-your-jsp-pages-the-easy-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/4c7380516634da842eea629ea3a3509a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ludovicianul</media:title>
		</media:content>
	</item>
	</channel>
</rss>
