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

<channel>
	<title>labs@bsb.com</title>
	<atom:link href="http://labs.bsb.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://labs.bsb.com</link>
	<description>Technical stuff related to Enterprise Java, Spring, RIA, …</description>
	<lastBuildDate>Wed, 06 Mar 2013 13:40:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Creating CheckStyle rules for Sonar</title>
		<link>http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=creating-checkstyle-rules-for-sonar</link>
		<comments>http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/#comments</comments>
		<pubDate>Thu, 06 Dec 2012 14:47:14 +0000</pubDate>
		<dc:creator>Sébastien</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=1172</guid>
		<description><![CDATA[This article aims to explain how we created and tested a CheckStyle rule and integrated it in Sonar. A project available at github provides a concrete example of this for a simple CheckStyle rule. [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.sonarsource.org/">Sonar</a> is a code analyser performing quality checks on various languages. It is composed of different code analysers:  CheckStyle, PMD, FindBugs and Squid. Each of them provides its own set of rules and Sonar integrates those various reports in a unified web interface.</p>
<p>Internally, we have what we call the <em>tech watch day</em> where we investigate a library and/or a technology during a day with a concrete objective.  This article aims to explain what we have tried to integrate custom rules in Sonar. We choose CheckStyle because we have quickly found documentation on how to write rules and there is also a nice <a title="IntelliJ plugin" href="https://github.com/jshiell/checkstyle-idea" target="_blank">IntelliJ plugin</a> to run rules inside the IDE. We do not pretend it was the best option, but a first trial.</p>
<p>CheckStyle uses its own API on top of AST. The analysis is based on the source code and is limited to the class itself. It means that we cannot create a constraint based on the source code of multiple classes. For instance, if a class <em>A</em> must implement an interface <em>I</em>, <em>A</em> must directly implement this interface; <em>A</em> extending <em>B</em> where <em>B</em> implements <em>I</em> is not supported. This is a major drawback, but for the rules we intended to implement this was not blocking.</p>
<p><a href="https://github.com/bsblabs/sonar-rule-showcase" target="_blank">The project hosted on github</a> has the following modules:</p>
<ul>
<li>A reusable tester for CheckStyle rules,</li>
<li>Some CheckStyle utilities,</li>
<li>One sample implementation of a CheckStyle rule that does not depend on Sonar at all,</li>
<li>A module that generates a  sonar extension plugin based on that rule,</li>
<li>A integration test that runs a sample project against the created rule.</li>
</ul>
<h4>Checkstyle Tester</h4>
<p>This module provides a tester performing a CheckStyle <strong>analysis inside a (unit) test</strong>. After that it <strong>helps to easily write assertions</strong> on the result. The example below shows how the rule <tt>UnusedImportsCheck</tt> provided out-of-the-box by CheckStyle could be tested. Assume that we run this rule against a class that imports java.util.Date and does not use it. The unused import is located at line 3, column 8.</p><pre class="crayon-plain-tag">@Test
public void classWithUnusedImport() {
    getAnalysis().get(ClassWithUnusedImport.class).assertOnlyThose(
        violation(UnusedImportsCheck.class)
            .on(3, 8)
            .withMessage("Unused import - java.util.Date.")
            .withLevel(SeverityLevel.ERROR)
    );
}</pre><p>The tester also allows to test localized messages. In the snippet below we test the French message:</p><pre class="crayon-plain-tag">@Test
public void classWithUnusedImportFrench() {
   final CheckStyleTester analysis =          
      CheckStyleTesterBuilder.forConfigFile("/checkstyle-config.xml")
         .withSourceLocationAsProperty().withLocale(Locale.FRENCH).build().assertNoException();

   analysis.get(ClassWithUnusedImport.class).assertOnlyThose(
      violation(UnusedImportsCheck.class)
         .on(3, 8)
         .withMessage("Import inutilisé - java.util.Date.")
         .withLevel(SeverityLevel.ERROR)
   );
}</pre><p></p>
<h4>Custom Rule</h4>
<p>Now we are able to test a CheckStyle rule, it is about time to create a new rule using some of the utilities exposed in the <a id="ae4f5376609e8a6e89a36ea09945aa78-aac7416c052cbbcc905668edb362332286285441" title="com.bsb.common.integration.checkstyle-utils" href="https://github.com/bsblabs/sonar-rule-showcase/tree/master/com.bsb.common.integration.checkstyle-utils">com.bsb.common.integration.checkstyle-utils</a> module. Let&#8217;s implement a rule (<em>ImplementClausesCounterCheck</em>)<em> </em>that checks that a given class does not implement more than two interfaces. Since we have a single potential violation, let&#8217;s extend from A<em>bstractSingleCheck</em>. An instance of the check class is created once per analysis regardless of the number of classes to be analysed. The <strong>check</strong> class is <strong>called</strong> back when a <strong>Java file starts</strong> to be processed and <strong>when</strong> the analysis on this file is <strong>finished</strong>. There is also a callback <strong>when a tree node of interest is encountered</strong>. A node can be the declaration of a class, method, or a statement like an <em>implements</em>, <em>extends</em>, etc.</p>
<p>In this case, it means that when a class definition is found, the rule is called back. The rule simply asks for the implements statement and the number of identifiers in it, that is the number of implemented interfaces. If the maximum is reached, a message is logged.</p><pre class="crayon-plain-tag">@Override
public void finishTree(DetailAST def) {
    super.finishTree(def);

    if (!isConstraintSatisfied()) {
        log(getViolationLine(), getViolationColumn(), getViolationMessage());
    }
}</pre><p>The rule is tested against a set of sample classes representing various use cases: one with no implement, one with a number of <em>implements</em> just before the maximum, a class with a violation and a class with an inner class. There are also translation tests that validates the proper message is set in English and French.</p>
<p>This project is also configured to generate a JAR file containing the project and its dependencies. Its purpose is to be usable by the IntelliJ plugin <a title="checkstyle-idea" href="https://github.com/jshiell/checkstyle-idea" target="_blank"><em>checkstyle-idea</em></a>. The screenshots bellow describe how to configure the plugin.</p>

<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/checkstyle-01/' title='CheckStyle in IntelliJ (1)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/checkstyle-01-150x150.jpg" class="attachment-thumbnail" alt="CheckStyle in IntelliJ (1)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/checkstyle-02/' title='CheckStyle in IntelliJ (2)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/checkstyle-02-150x150.jpg" class="attachment-thumbnail" alt="CheckStyle in IntelliJ (2)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/checkstyle-03/' title='CheckStyle in IntelliJ (3)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/checkstyle-03-150x150.jpg" class="attachment-thumbnail" alt="CheckStyle in IntelliJ (3)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/checkstyle-04/' title='CheckStyle in IntelliJ (3)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/checkstyle-04-150x150.jpg" class="attachment-thumbnail" alt="CheckStyle in IntelliJ (3)" /></a>

<h4>Sonar Plugin</h4>
<p>The CheckStyle rule can be now exposed as a Sonar plugin. The following description is based on <a title="Sonar documentation page" href="http://docs.codehaus.org/display/SONAR/Extending+Coding+Rules" target="_blank">the Sonar documentation page</a>. An XML file contains the declaration of the new Sonar rule (description, id, severity, etc). The class <em>CheckStyleExtensionRepository</em> is responsible of providing Sonar rules. The repository loads the XML file and loads the CheckStyle rule as a Sonar rule. <em>CheckStyleExtensionPlugin</em> declares the extension and specifies the provider to use.</p>
<p>The POM file of this module can install the extension in your Sonar instance. For that, it expects that the directory containing your Sonar installation can be accessed by the file system. The command simply copies the JAR to the path specified in the main POM file (see the <em>sonar.dir</em> property).</p>
<p>You can either change the value of <em>sonar.dir</em> in the POM itself or override it on the command line. For instance, to install the plugin in <em>c:\tools\sonar</em>:</p><pre class="crayon-plain-tag">bsblabs@bsblabs ~/sonar-rule-showcase/samples/com.bsb.samples.sonar.implements-checker-plugin
$ mvn clean install -PinstallPlugin -Dsonar.dir=c:\tools\sonar</pre><p></p>
<h4>Sonar Integration Tests Module</h4>
<p>The integration tests module (not part of the Maven reactor) is configured to analyse the samples written to test the rule in Sonar itself. Of course it expects that the Sonar extension has been previously deployed. The properties that Sonar need are located in the POM file. The analysis is simply performed during the package phase:</p><pre class="crayon-plain-tag">bsblabs@bsblabs ~/sonar-rule-showcase/samples/com.bsb.samples.sonar.implements-checker-plugin-its
$ mvn clean package</pre><p>During this phase, <a title="Maven Checkstyle Plugin" href="http://maven.apache.org/plugins/maven-checkstyle-plugin/" target="_blank">the Maven Checkstyle Plugin</a> also performs an analysis. This analysis is completely independent from the Sonar analysis. The only purpose is to show how a new rule can be tested by this plugin. The generated reports are available in the target directory of that module. Of course, the report contains the same violations as the ones displayed by Sonar.</p>

<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/sonar-01/' title='Sample Sonar Rule (1)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/sonar-01-150x150.jpg" class="attachment-thumbnail" alt="Sample Sonar Rule (1)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/sonar-02/' title='Sample Sonar Rule (2)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/sonar-02-150x150.jpg" class="attachment-thumbnail" alt="Sample Sonar Rule (2)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/sonar-03/' title='Sample Sonar Rule (3)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/sonar-03-150x150.jpg" class="attachment-thumbnail" alt="Sample Sonar Rule (3)" /></a>
<a href='http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/sonar-04/' title='Sample Sonar Rule (4)'><img width="150" height="150" src="http://labs.bsb.com/wp-content/uploads/2012/11/sonar-04-150x150.jpg" class="attachment-thumbnail" alt="Sample Sonar Rule (4)" /></a>

<p>Note that this automated process is optional. When you want to quickly test a rule that you are changing this process might be handy. But you can of course copy the plugin and analyse one of your projects through the Sonar interface as well.</p>
<h4>Conclusion</h4>
<p>This article briefly describe how one could implement and test a CheckStyle rule and define it as a Sonar plugin. The project available on github describes the different steps to achieve that goal and contains reusable classes to write and test CheckStyle rules.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2012/12/creating-checkstyle-rules-for-sonar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Aether to resolve dependencies in a Maven plugin</title>
		<link>http://labs.bsb.com/2012/10/using-aether-to-resolve-dependencies-in-a-maven-plugins/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-aether-to-resolve-dependencies-in-a-maven-plugins</link>
		<comments>http://labs.bsb.com/2012/10/using-aether-to-resolve-dependencies-in-a-maven-plugins/#comments</comments>
		<pubDate>Mon, 15 Oct 2012 14:40:00 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[maven aether]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=1122</guid>
		<description><![CDATA[When we upgraded to the latest version of Maven (3.0.4), some of our in-house plugins had issues with dependency resolution. We already has similar problems when migrating from Maven 2 to Maven 3, due to the new library used by Maven to access the repository system: Aether. These problems only came when using plugins that had to resolve dependencies by themselves. [...]]]></description>
				<content:encoded><![CDATA[<p>When we upgraded to the latest version of <a title="Maven" href="http://maven.apache.org/" target="_blank">Maven</a> (3.0.4), some of our in-house plugins had issues with dependency resolution.</p>
<p>We already has similar problems when migrating from Maven 2 to Maven 3, due to the new library used by Maven to access the repository system: <a title="Aether" href="http://www.sonatype.org/aether" target="_blank">Aether</a>. These problems only came when using plugins that had to resolve dependencies by themselves.</p>
<p>Running these plugins with Maven 2 require the use of the <a href="http://maven.apache.org/ref/2.2.1/maven-artifact/apidocs/org/apache/maven/artifact/resolver/package-summary.html" target="_blank">org.apache.maven.artifact.resolver</a> package (ArtifactResolver and ArtifactCollector classes, a. o.). Although still valid when running the plugin with Maven 3, this can lead to some side effects we experienced, such as:</p>
<ul>
<li>Dependencies explicitly marked as excluded still being resolved</li>
<li>Dependencies taken from a Maven 1 repository being unresolvable because coordinates stated as <em>null</em>:<em>null</em>:<em>jar </em>(seems specific to Maven 3.0.4+)</li>
<li>Inconsistencies with the dependency mechanism used internally by Maven (<a href="https://cwiki.apache.org/MAVEN/maven-3x-compatibility-notes.html#Maven3.xCompatibilityNotes-DependencyResolution" target="_blank">see here</a>)</li>
</ul>
<p>So we had to update our plugins to use the new Aether library to perform dependency resolution, and found a good starting point on the <a href="http://www.sonatype.com/people/2011/01/how-to-use-aether-in-maven-plugins/" target="_blank">Sonatype blog</a>.</p>
<p>Let&#8217;s take one sample method that would need to be updated. For instance, the following method is responsible of resolving all dependencies of a project:</p><pre class="crayon-plain-tag">public static Set&lt;Artifact&gt; getDependencyArtifacts(MavenProject project, ArtifactResolver resolver, ArtifactRepository localRepository, 
            List&lt;?&gt; remoteRepositories, ArtifactMetadataSource artifactMetadataSource) throws MojoExecutionException {
    ArtifactResolutionResult resolutionResult;
    try {
        resolutionResult = resolver.resolveTransitively(project.getArtifacts(), project.getArtifact(), remoteRepositories, 
                localRepository, artifactMetadataSource);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    return resolutionResult.getArtifacts();
}</pre><p>This is quite a simple process, as all needed components (project, resolver, localRepository, remoteRepositories and artifactMetadataSource) can be injected by Plexus, either by an expression or a component &#8220;annotation&#8221;:</p><pre class="crayon-plain-tag">/**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * Local repository to be used by the plugin to resolve dependencies.
     * @parameter expression="${localRepository}"
     */
    protected ArtifactRepository localRepository;

    /**
     * List of remote repositories to be used by the plugin to resolve dependencies.
     * @parameter expression="${project.remoteArtifactRepositories}"
     */
    protected List remoteRepositories;

    /**
     * @component
     */
    protected ArtifactResolver resolver;

    /**
     * @component
     */
    protected ArtifactMetadataSource artifactMetadataSource;</pre><p>As already said, this code can still be executed with Maven 3, thanks to the great job the Maven guys did in making it globally backward compatible. But this sometimes fails to do the job, and this is not the way we should write new plugins!</p>
<p>The <a href="http://www.sonatype.com/people/2011/01/how-to-use-aether-in-maven-plugins/">Sonatype blog post</a> is quite nice, but using it as is would mix a lot of the same concepts. Yes, part of the classes you manipulate to handle dependency resolution are both in Aether and Maven API, notably <a href="http://maven.apache.org/ref/3.0.4/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html">Maven Artifact</a> and <a href="http://sonatype.github.com/sonatype-aether/apidocs/org/sonatype/aether/artifact/Artifact.html">Aether Artifact</a>. A plugin will usually work on the Maven Artifact side, so we rather want to use the Maven packages as much as possible, with ideally no explicit reference about the underlying library that Maven uses. That would also minimize the modifications needed inside the plugin when upgrading to Maven 3.</p>
<p>Unfortunately, there&#8217;s no way to achieve dependency resolution without at least one import of an Aether class, but still this is valuable as it is limited to the dependency resolution process, not the dependencies or artifact structures, which is still nice to reduce the impact on the <em>business</em> part of the plugin work.</p>
<p>Here is the method modified to be compliant with Maven 3.0.4 and Aether 1.13.1</p><pre class="crayon-plain-tag">public static Set getDependencyArtifacts(MavenProject project, RepositorySystemSession repoSession, 
        ProjectDependenciesResolver projectDependenciesResolver) throws MojoExecutionException {

    DefaultDependencyResolutionRequest dependencyResolutionRequest = new DefaultDependencyResolutionRequest(project, repoSession);
    DependencyResolutionResult dependencyResolutionResult;

    try {
        dependencyResolutionResult = projectDependenciesResolver.resolve(dependencyResolutionRequest);
    } catch (DependencyResolutionException ex) {
        throw new MojoExecutionException(ex.getMessage(), ex);
    }

    Set artifacts = new LinkedHashSet();
    if (dependencyResolutionResult.getDependencyGraph() != null
            &amp;&amp; !dependencyResolutionResult.getDependencyGraph().getChildren().isEmpty()) {
        RepositoryUtils.toArtifacts(artifacts, dependencyResolutionResult.getDependencyGraph().getChildren(), 
                Collections.singletonList(project.getArtifact().getId()), null);
    }
    return artifacts;
}</pre><p>The only Aether class that we still have to use is <a href="http://sonatype.github.com/sonatype-aether/apidocs/org/sonatype/aether/RepositorySystemSession.html">RepositorySystemSession</a>, all other are core Maven ones, especially the <em>Artifact</em> one. Also note that you do not have to depend on Aether explicitly, as it will be taken transitively from the <em>org.apache.maven:maven-core:jar:3.0.4</em> dependency.</p>
<p>As for the previous implementation, the required parameters can be obtained with Plexus injections:</p><pre class="crayon-plain-tag">/**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * The current repository/network configuration of Maven.
     *
     * @parameter default-value="{repositorySystemSession}"
     * @readonly
     */
    protected RepositorySystemSession repoSession;

    /**
     * @component
     */
    protected ProjectDependenciesResolver projectDependenciesResolver;</pre><p>With an isolation of dependency resolution inside specific methods that will not have their return type changed, a plugin will easily be updated to support Maven 3 dependency resolution mechanism.<br />
One drawback is the use of <em>RepositoryUtils.toArtifacts</em>, which is marked by Maven as intended to be used internally.</p>
<p>Similarly, resolving one artifact can be done like this:</p><pre class="crayon-plain-tag">public static void resolveArtifact(Artifact artifact, RepositorySystem repositorySystem, List remoteRepositories, 
        ArtifactRepository localRepository) throws MojoExecutionException {
    ArtifactResolutionRequest request = new ArtifactResolutionRequest()
        .setArtifact(artifact)
        .setRemoteRepositories(remoteRepositories)
        .setLocalRepository(localRepository);
    ArtifactResolutionResult resolutionResult = repositorySystem.resolve(request);
    if(resolutionResult.hasExceptions()){
        throw new MojoExecutionException("Could not resolve artifact: " + artifact, resolutionResult.getExceptions().get(0));
    }
}</pre><p>This code does not depend directly on any Aether class.</p>
<p>Using the above samples, plugins can easily be migrated to fully comply with the new Maven 3 dependency resolution library.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2012/10/using-aether-to-resolve-dependencies-in-a-maven-plugins/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Improving code quality with Sonar and Jenkins</title>
		<link>http://labs.bsb.com/2012/09/improving-code-quality-with-sonar-and-jenkins/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=improving-code-quality-with-sonar-and-jenkins</link>
		<comments>http://labs.bsb.com/2012/09/improving-code-quality-with-sonar-and-jenkins/#comments</comments>
		<pubDate>Fri, 07 Sep 2012 06:22:53 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=1022</guid>
		<description><![CDATA[Last year, we started an ambitious internal project and since we started more or less greenfield we thought it was a good opportunity to test and integrate new development methodologies. At the time we were already using Jenkins and Sonar but we felt we could get more from those tools and that they were definitely not the only things to consider to achieve our goal. In this article, we describe the methodologies we put in place to improve the quality of our code. [...]]]></description>
				<content:encoded><![CDATA[<p>Last year, we started an ambitious internal project and since we started more or less greenfield we thought it was a good opportunity to test and integrate new development methodologies. At the time we were already using <a href="http://jenkins-ci.org/">Jenkins</a> and <a href="http://www.sonarsource.org/">Sonar</a> but we felt we could get more from those tools and that they were definitely not the only things to consider to achieve our goal.</p>
<h4>What is code quality?</h4>
<p>The first step is to define precisely what code quality means for us and how we can measure it. We decided to focus on these areas:</p>
<ul>
<li>Duplications (code copy-paste)</li>
<li>Rules violations (static analysis)</li>
<li>Code coverage</li>
<li>Documentation (API)</li>
<li>Package tangle</li>
<li>Compiler warnings</li>
<li><em>TODOs</em>, <em>FIXMEs</em> and others reminders in code</li>
</ul>
<p>From a tool standpoint, Jenkins can track <a href="https://wiki.jenkins-ci.org/display/JENKINS/Warnings+Plugin ">compiler warning</a>s and <a href="https://wiki.jenkins-ci.org/display/JENKINS/Task+Scanner+Plugin ">open tasks</a> (TODO, etc) easily. Sonar can actually manage all the rest and much more.</p>
<p>So where do we go from there? Our team is using some of the agile methodologies, in particular the daily stand-up, iteration-based development, iteration review and retrospective. The end goal is not about measuring, it is about controlling how that measure evolves. Everything starts with an initial measure that gives an overview. Alongside the initial measure we need some guidelines to define what the objectives are and how to achieve them. These guidelines should improve over time and should adapt to new requirements.</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2012/08/quality-lifecycle.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1024" title="quality-lifecycle" src="http://labs.bsb.com/wp-content/uploads/2012/08/quality-lifecycle-300x234.png" alt="" width="300" height="234" /></a></p>
<p>At the end of an iteration (a development lifecycle), we measure the quality and we take a set of actions for the next one if not all quality criteria are met. Of course, this only works if those actions are known by the whole team and every single team member embraces them.</p>
<h4>How do we measure code quality?</h4>
<p>Measuring quality is about applying the concept of Continuous Inspection. Just like continuous integration makes sure your project builds and all tests pass, continuous inspection is about looking at your code and notify you when the quality decreases. Both Jenkins and Sonar have the ability to give a trend of what is going on over time.</p>
<p>The plugins we use in Jenkins allow to give an history of compiler warnings and open tasks:</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2012/08/compiler-warning.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1031" title="Compiler warnings" src="http://labs.bsb.com/wp-content/uploads/2012/08/compiler-warning-300x127.png" alt="" width="300" height="127" /><img class="alignnone size-medium wp-image-1032" title="Open tasks" src="http://labs.bsb.com/wp-content/uploads/2012/08/open-tasks-300x130.png" alt="" width="300" height="130" /></a></p>
<p>The problem with those graphs is that you need to keep enough builds to get a proper history. By default (especially for the Maven project style), Jenkins stores a lot of data at each build but you can easily fix that by checking the <em>Disable automatic artifact archiving</em> advanced option of the <em>build</em> section in the project configuration. Once you fixed that, you still have the issue that Jenkins does not play nice if a project has a big job history since it seems to parse it when it starts. To fix that, you have to either discard old builds or keep only the latest X builds. In both cases, you&#8217;ll be back to the original problem since the history of your project will be lost when you reach that threshold. Luckily there is a solution. In our case, we want to eventually keep <strong>one</strong> build per iteration so we use the <em>Keep this build forever</em> action on the latest build of every iteration. That way the graph has a deeper history.</p>
<p>Sonar has a wonderful feature called <a href="http://www.sonarsource.org/differential-services-in-sonar-for-a-complete-support-of-continuous-inspection/">the differential dashboard</a> that basically allows you to diff two quality snapshots. Similarly to Jenkins, Sonar purges those quality snapshots according to a configurable policy. Sonar also allows you to <em>name </em>one particular quality snapshot either by assigning a version to it or an event. What we do is that at the end of each iteration, we assign the name of the iteration to the latest quality snapshot of that iteration.</p>
<p>To do so, go to the project in sonar and in the left menu, choose the <strong>History</strong> element under the <strong>Configuration</strong> section. Then choose the quality snapshot and click on the create button in the version column. Once you&#8217;ve done that, you can register that version so that it is available in the <strong>Time changes</strong> drop down list. To do so, go to <strong>Settings</strong> &gt; <strong>Differential views</strong> and fill in the name of your version in one of the period fields.</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2012/08/sonar-history.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1034" title="Sonar history settings" src="http://labs.bsb.com/wp-content/uploads/2012/08/sonar-history-300x166.png" alt="" width="300" height="166" /></a><a href="http://labs.bsb.com/wp-content/uploads/2012/08/sonar-diff-view.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1035" title="Sonar differential dashboard settings" src="http://labs.bsb.com/wp-content/uploads/2012/08/sonar-diff-view-300x166.png" alt="" width="300" height="166" /></a></p>
<p>Once we have configured Sonar that way, we are able to <em>diff</em> the current state of the project with the last or the before last iteration:</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2012/08/20120328-anonymous.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1041" title="Diff view march" src="http://labs.bsb.com/wp-content/uploads/2012/08/20120328-anonymous-300x145.png" alt="" width="300" height="145" /></a><a href="http://labs.bsb.com/wp-content/uploads/2012/08/20120830-san-francisco.png" rel="lightbox[1022]"><img class="alignnone size-medium wp-image-1042" title="Diff view August" src="http://labs.bsb.com/wp-content/uploads/2012/08/20120830-san-francisco-300x145.png" alt="" width="300" height="145" /></a></p>
<h4>How do we improve code quality?</h4>
<p>Properly configured tools are not enough to improve the overall quality of a project but a feature like the differential view in Sonar is definitely a very good leverage to achieve that goal. On top of these tools, we need Key Progress Indicator (KPIs), guidelines, rules and conventions. But most important of all, we need to do all this as a team. This is why we have built incrementally a team agreement page where we write down the things that we have openly discussed and agreed upon.</p>
<p>Concretely, we have the following code quality checks during the iteration:</p>
<ul>
<li>Jenkins mail notification (using <a href="https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin">the email-ext plugin</a>) that provides a summary of what is going on: commits since the last build, tests that are failing and the proper excerpt of the console log</li>
<li><a href="http://www.sonarsource.org/sonar-2.14-in-screenshots/">Sonar notifications</a> that are sent when new violations are introduced in the project</li>
<li>A manual email and/or reminder at the stand-up that any team member can trigger if he feels something needs our attention</li>
<li>The iteration retrospective where we look at Jenkins and Sonar to figure out together how the iteration went and determine if concrete actions are needed for the next iteration</li>
</ul>
<p>If the quality objectives are not met, each team member has a concrete objectives for the next iteration: fix 10 <em>items</em>. An item is an generic concept: it defines one rule violation, one compiler warning, one open task, one package tangle, one copy-pasted block, 0.1% of code coverage or 0.1% of API documentation. Obviously, these may sound unfair and not well balanced (fixing a package tangle is probably much harder than fixing a simple rule violation) but on the other hand simple items never last until the end of the iteration. This gives a concrete objective to the team and nobody (including me as team lead) do care about who does what as long as we meet our team objective (6 team members = 60 items to fix).</p>
<p>There is also things that a tool will have a hard time to track: conventions. And code quality is amongst others about <strong>consistency</strong>. To make sure we are doing the same thing the same way, we have built a set of <em>rules &amp; conventions </em>pages. These pages are also built incrementally and are based on concrete issues and/or inconsistency in the code. These are invaluable when we have to remember what we decided years ago or when a new team mate joins our team.</p>
<p>This process is also continuously improved: any  team member can write a proposal or request a change in the Sonar configuration. Based on a proposal that <a href="https://confluence.atlassian.com/display/DOC/Working+with+Anchors">is fully linkable</a> on our Wiki, we discuss it together at the stand-up, the iteration retrospective or in a specific meeting depending on the complexity of the proposal. Discussing about such things improves our understanding of what others see as good code and why: it helps us to improve our code quality  in general, even for things that are not tracked by the tools.</p>
<h4>Wrapping up</h4>
<p>Tools like Sonar and Jenkins really helped us to improve the quality of our code on a daily basis. Sonar is very powerful and easy to configure and the differential dashboard is really a feature that makes a huge difference. But having good tools is not enough: we need to make sure that the way we write code and the design principles we apply are consistent. Making that an open process with a formalized trace on the Wiki helped us as a team to achieve our goal.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2012/09/improving-code-quality-with-sonar-and-jenkins/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Vaadin WidgetSet compilation optimizations</title>
		<link>http://labs.bsb.com/2012/04/vaadin-widgetset-compilation-optimizations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=vaadin-widgetset-compilation-optimizations</link>
		<comments>http://labs.bsb.com/2012/04/vaadin-widgetset-compilation-optimizations/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 16:29:15 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Vaadin]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=998</guid>
		<description><![CDATA[<p>Vaadin is a framework using GWT behind the scene. And for that it needs a WidgetSet, which is basically the definition of the widgets that you want to use on the client-side. If you are using the core widgets of Vaadin, you are good because the vaadin.jar ships with a default WidgetSet for all the [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.vaadin.com">Vaadin</a> is a framework using GWT behind the scene. And for that it needs a <a href="https://vaadin.com/book/-/page/gwt.widgetset.html">WidgetSet</a>, which is basically the definition of the <em>widgets</em> that you want to use on the client-side. If you are using the core widgets of Vaadin, you are good because the <code>vaadin.jar</code> ships with a default <code>WidgetSet</code> for all the core components. However, Vaadin has also a very active community and the <a href="http://www.vaadin.com/directory">Vaadin directory</a> holds hundreds of add-ons. Some of them may require you to compile your own <code>WidgetSet</code> because they have a custom client-side counter part. Or, after all, you might need you own client-side widget which would require you to define a custom <code>WidgetSet</code> as well.</p>
<p>This blog entry is not about explaining how you could define such <code>WidgetSet</code> and build it. The <a href="https://vaadin.com/book/-/page/gwt.widgetset.html">official documentation</a> and this <a href="https://vaadin.com/wiki/-/wiki/Main/Using%20Vaadin%20with%20Maven#section-Using+Vaadin+with+Maven-AddingWidgetsetCompilationToAnExistingVaadinProject">wiki page</a> already explains most of it for <a href="http://maven.apache.org">Apache Maven</a>. One very annoying issue with this mandatory compilation step is that it is quite slow even on modern hardware and it significantly increate the time of your build. There are several workarounds to this: reduce the number of supported browsers or make sure the result of the compilation can be cached by compiling it in the source directory of your web application project (see <a href="http://www.jorambarrez.be/blog/2011/04/28/howto-vaadin-addon-maven/">this blog post</a> for a complete example).</p>
<p>We also tried to tackle this problem and we thought that if Vaadin can hold the result of the <code>WidgetSet</code> in their <code>jar</code> file, why shouldn&#8217;t we? And since a multi-modules build tool is a commodity these days (again, we use Maven but this should work with others too), having an extra module is not really a problem.</p>
<p>Our Vaadin-based projects are composed of several modules describing the different pieces of the application. For very simple project we just have one <em>core</em> module holding all the code that uses the Vaadin API and a webapp project (<em>server</em> module) to package and start the application easily. The <em>core</em> project might use add-ons, in which case we compile the <code>WidgetSet</code> in the webapp project.</p>
<p>Our optimization is about creating another module, depending on <em>core</em> and solely being responsible to compute the <code>WidgetSet</code>. The trick is that module is not active by default so it does not run unless you explicitly ask for it. The result of the compilation is stored in the <code>jar</code> file and can even be shared within the team by deploying a <code>SNAPSHOT</code> to a shared repository. Another nice advantage regarding this technique is that a full project clean no longer requires you to recompile the <code>WidgetSet</code> from your IDE since the project is or can be disabled, depending of the Maven support of your IDE <img src='http://labs.bsb.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The following is an excerpt of the Maven pom we used to compile the <code>WidgetSet</code> in a regular <code>jar</code> file</p><pre class="crayon-plain-tag">&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;configuration&gt;
                &lt;excludes&gt;
                    &lt;exclude&gt;VAADIN/widgetsets/WEB-INF/**&lt;/exclude&gt;
                &lt;/excludes&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
            &lt;artifactId&gt;gwt-maven-plugin&lt;/artifactId&gt;
            &lt;configuration&gt;
                &lt;extraJvmArgs&gt;-Xmx512M -Xss1024k&lt;/extraJvmArgs&gt;
                &lt;webappDirectory&gt;${project.build.outputDirectory}/VAADIN/widgetsets&lt;/webappDirectory&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;resources&lt;/goal&gt;
                        &lt;goal&gt;compile&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;com.vaadin&lt;/groupId&gt;
            &lt;artifactId&gt;vaadin-maven-plugin&lt;/artifactId&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;update-widgetset&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;</pre><p>The main tricks here as follows:</p>
<ul>
<li>We configure the <em>GWT plugin</em> so that the result of the compilation lands in <code>target/classes/VAADIN/widgetsets</code> which is the location Vaadin uses to locate a <code>WidgetSet</code> in the classpath</li>
<li>We configure the <em>jar plugin</em> to remove extra things that the plugin has generated and we don&#8217;t need</li>
</ul>
<p>Once you are done with this, you only need to add a dependency to this new module in your <em>server</em> module. In order to illustrate these concepts, we have created <a href="https://github.com/bsblabs/vaadin-widgetset-showcase">a showcase on github</a> using the <a href="https://vaadin.com/directory#addon/wizards-for-vaadin">Wizards for Vaadin add-on</a>. This showcase also illustrates a full integration with Maven as well as the use of <a href="http://cargo.codehaus.org/">Cargo</a> to start the container without even the need to install one first.</p>
<p>And, while we were at it, we released a new version of our <a href="https://vaadin.com/directory#addon/embed-for-vaadin">Embed for Vaadin add-on</a> (see <a href="http://labs.bsb.com/2012/03/embed-for-vaadin/">our previous blog post</a> and the <a href="https://github.com/bsblabs/embed-for-vaadin">release notes</a>).  This add-on allows you to start your Vaadin application right from your IDE. There is one class in the <code>wizard-sample-server</code> that you can use to see how easy this all integrate together.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2012/04/vaadin-widgetset-compilation-optimizations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Embed for Vaadin: run your Vaadin applications in an embedded server</title>
		<link>http://labs.bsb.com/2012/03/embed-for-vaadin/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=embed-for-vaadin</link>
		<comments>http://labs.bsb.com/2012/03/embed-for-vaadin/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 08:59:16 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Vaadin]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=973</guid>
		<description><![CDATA[Embed for vaadin is a Vaadin add-on that allows you to start your Vaadin application directly from your IDE. In this article, we will explain the rationale of such plugin and how you could use it in your own Vaadin applications. [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.vaadin.com">Vaadin</a> is a framework for building very nice web applications in fluent Java. Vaadin has a <a href="http://demo.vaadin.com/sampler">very rich component model</a> that allows to design an application quite quickly. It only requires a servlet container and a single servlet definition with the <a href="https://vaadin.com/api/com/vaadin/Application.html">Application</a> class to use as a servlet parameter.</p>
<p>As everything we do here internally, we were searching for mechanisms to optimize our daily work tasks. In the case of Vaadin, two come naturally to mind:</p>
<ol>
<li>Quickly show a component while prototyping a screen or part of a screen to validate its layout, style and basic behavior</li>
<li>In case of a test case failing, be able to quickly display the faulty component to get more <em>visual </em>details</li>
</ol>
<p>In Vaadin 6 (the upcoming 7 version will change that slightly), a Vaadin application needs the following servlet definition to work properly:</p><pre class="crayon-plain-tag">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5"&gt;

    &lt;servlet&gt;
        &lt;servlet-name&gt;vaadin&lt;/servlet-name&gt;
        &lt;servlet-class&gt;com.vaadin.terminal.gwt.server.ApplicationServlet&lt;/servlet-class&gt;
        &lt;init-param&gt;
            &lt;param-name&gt;application&lt;/param-name&gt;
            &lt;param-value&gt;com.bsb.incubator.vaadin.ShowcaseApplication&lt;/param-value&gt;
        &lt;/init-param&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;vaadin&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;</pre><p>Since its version 7, <a href="http://tomcat.apache.org/">Apache Tomcat</a> has a very nice <a href="http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/startup/Tomcat.html">embed API</a> that allows you to configure and start a Tomcat container without the need to package a web application (or install Tomcat, for that matter). And it offers other nice feature such as the ability to allocate a random HTTP port that is available on your box instead of forcing you to choose one.</p>
<p><a href="https://github.com/bsblabs/embed-for-vaadin">Embed for Vaadin</a> is a Vaadin add-on we developed that takes care of the boring tasks of configuring and starting a servlet container. For instance, the following line would start a tomcat container with an application showing a simple button</p><pre class="crayon-plain-tag">EmbedVaadin.forComponent(new Button("Hello")).start();</pre><p>This code gives the following, very simple result:</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2012/03/embed-one.png" rel="lightbox[973]"><img class="aligncenter size-full wp-image-974" title="embed-one" src="http://labs.bsb.com/wp-content/uploads/2012/03/embed-one.png" alt="A simple button embedded in a Vaadin application" width="833" height="416" /></a></p>
<p>Since we are embedding Tomcat, we can pass an initialized component to the application. Even though we could serialize the component and deserialize it for another user, this mode does not support multi clients and multi sessions as the purpose is for a developer to debug or prototype  a particular component.</p>
<p><code>forComponent</code> takes any Vaadin <a href="https://vaadin.com/api/com/vaadin/ui/Component.html">Component</a>. If the component is a <a href="https://vaadin.com/api/com/vaadin/ui/Window.html">Window</a>, we use it as the main window of the Application; If it is a <a href="https://vaadin.com/api/com/vaadin/ui/Layout.html">Layout</a>, we wrap the layout in a simple Window; If it is a more basic component, we wrap it in a <a href="https://vaadin.com/api/com/vaadin/ui/VerticalLayout.html">VerticalLayout</a>. It is also possible to pass an Application class, just like you would do while building a regular web application. In that case, we really configure a web.xml that is similar to the one above. For instance, the following would start the ShowcaseApplication:</p><pre class="crayon-plain-tag">EmbedVaadin.forApplication(ShowcaseApplication.class).openBrowser(true).start();</pre><p>Noticed the openBrowser instruction? EmbedVaadin supports more options and these can be combined very easily before starting the embed server. In this case, this instructs to open the default browser at the URL that the server is using for the application. This speeds up the process a little bit more and it works for basic component too.</p>
<p>By default, the thread that started the server is blocking so that you can use the web application without caring about the server lifecycle. But you could use the same mechanism for integration tests purposes. The <code>start()</code> instruction returns you an <code>EmbedVaadinServer</code> that you can stop once your test has run:</p><pre class="crayon-plain-tag">final EmbedVaadinServer server = EmbedVaadin.forComponent(myComponent).wait(false).start();
// Your test
server.stop();</pre><p>To use this add-on in your application, just add the following dependency to your project:</p><pre class="crayon-plain-tag">&lt;dependency&gt;
    &lt;groupId&gt;com.bsb.common.vaadin&lt;/groupId&gt;
    &lt;artifactId&gt;com.bsb.common.vaadin.embed&lt;/artifactId&gt;
    &lt;version&gt;0.1&lt;/version&gt;
&lt;/dependency&gt;</pre><p>The add-on is available on the <a href="http://search.maven.org/">Central repository</a> so you should not have any problem to get it with Maven.</p>
<p>We believe this plugin will improve in the future. Do not hesitate to fork <a href="https://github.com/bsblabs/embed-for-vaadin">the repository</a> or give us feedback!</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2012/03/embed-for-vaadin/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring profiles management for application customization</title>
		<link>http://labs.bsb.com/2011/12/spring-profiles-management-for-application-customization/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=spring-profiles-management-for-application-customization</link>
		<comments>http://labs.bsb.com/2011/12/spring-profiles-management-for-application-customization/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 09:52:24 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=921</guid>
		<description><![CDATA[Spring 3.1 GA was announced early this month. One feature we have been looking for some time is the new environment abstraction with bean definitions profile. While this abstraction allows to configure the profiles to use in many different locations, we found useful to be able to customize this even further to suit our needs. This blog post contains a simple implementation showcase, available on github. [...]]]></description>
				<content:encoded><![CDATA[<p>Spring 3.1 GA <a href="http://blog.springsource.org/2011/12/13/spring-framework-3-1-goes-ga/">was announced</a> early this month. One feature we have been looking for some time is the new environment abstraction with bean definitions profile. This is covered in details in two excellent articles (<a href="http://blog.springsource.org/2011/02/11/spring-framework-3-1-m1-released/">here </a>and <a href="http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/">here</a>).</p>
<p>In short, Spring 3.1 permits the use of nested <code>&lt;beans/&gt;</code> elements with a new <em>profile </em>attribute. If the profile defined in that attribute is enabled, the bean definitions and import statements are parsed and included in the application context. Consider for instance the following <code>bootstrap-environment.xml</code> configuration file:</p><pre class="crayon-plain-tag">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

    &lt;description&gt;
        Bootstrap the environment according to the active profiles.
    &lt;/description&gt;

    &lt;beans profile="env1"&gt;
        &lt;import resource="classpath:/META-INF/app/bootstrap/env1.xml"/&gt;
    &lt;/beans&gt;

    &lt;beans profile="env2"&gt;
        &lt;import resource="classpath:/META-INF/app/bootstrap/env2.xml"/&gt;
    &lt;/beans&gt;

&lt;/beans&gt;</pre><p>This basically states that if <em>env1 </em>is enabled, the <code>env1.xml</code> configuration file will be included in the application context. We could assume here that this would load something specific to that environment.</p>
<p>We have applications that can either deploy on a full blown JavaEE compliant server or on a lightweight container such as Apache Tomcat. A standard JavaEE server will bring a JTA transaction manager for distributed transaction and a message broker for JMS-based communication. A lightweight container, however, does not provide that infrastructure out-of-the-box so we have to bridge the gap somehow, yet keeping that maximum flexibility for customization.</p>
<p>Internally, we use a web application skeleton. This skeleton contains all the resources file, the web.xml and the dependencies required to build the application. We have then one project per environment were we can customize the skeleton at will, including overriding files. For that we use the <a href="http://maven.apache.org/plugins/maven-war-plugin/overlays.html">overlay feature</a> of the <a href="http://maven.apache.org/plugins/maven-war-plugin/">maven war plugin</a>. In the end, the customization required to start the application is restricted to the environment itself.</p>
<p>It would be much better if we didn&#8217;t have to provide these projects in the first place and simply use a properties file containing the profile(s) to activate. This use case is not supported out-of-the-box:  Spring can determine which profile(s) to activate based on an environment property, a system property or a servlet context parameter. We do not want our users to hack their startup scripts to deploy our applications and since our web.xml is shared, we can&#8217;t really use the servlet context parameter either.</p>
<p>Fortunately, Spring offers an access to the lower-level API for more fine grained customization. A custom strategy simply needs to implement the <a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/ApplicationContextInitializer.html">ApplicationContextInitializer </a>interface and register it properly before the context is refreshed. We have <a href="https://github.com/bsblabs/spring-profile-showcase">a showcase on github demoing a simple implementation</a> of that interface looking up for a properties file at a default location (<code>META-INF/application-context.properties</code>). Once you have that in your project, you only need to add an extra &lt;context-param/&gt; to your <code>web.xml</code>:</p><pre class="crayon-plain-tag">&lt;context-param&gt;
    &lt;param-name&gt;contextInitializerClasses&lt;/param-name&gt;
    &lt;param-value&gt;com.bsb.showcase.spring.profile.ProfilesActivationApplicationInitializer&lt;/param-value&gt;
&lt;/context-param&gt;</pre><p>To enable a set of profiles, place a properties file at the default location in the classpath of your application:</p><pre class="crayon-plain-tag">#
# The spring profiles to activate
#
spring.profiles.active=env2</pre><p>Profiles activation relies now on a simple properties file. You can add as many profiles as you want and the value can even be filtered by your build tool if you cannot hardcode the value. The advantage of this solution is that we can still use our shared infrastructure and it does not require any system or environment property.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2011/12/spring-profiles-management-for-application-customization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stabilizing &amp; speeding up compilation and annotation processing in Maven builds</title>
		<link>http://labs.bsb.com/2011/12/stabilizing-speeding-up-compilation-and-annotation-processing-in-maven-builds/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stabilizing-speeding-up-compilation-and-annotation-processing-in-maven-builds</link>
		<comments>http://labs.bsb.com/2011/12/stabilizing-speeding-up-compilation-and-annotation-processing-in-maven-builds/#comments</comments>
		<pubDate>Mon, 19 Dec 2011 15:31:38 +0000</pubDate>
		<dc:creator>Thomas</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[javac]]></category>
		<category><![CDATA[jdt]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=881</guid>
		<description><![CDATA[Stability and performance of a build is a critical point when a project dealing with a large codebase and is composed of many developers. A better stability can be achieved by limiting the amount of resources that are platform or environment dependent. Performance, when working on projects that are several years old, and have dozens developers working on it, can be regularly threatened by a significant growth of its codebase, making performance issues start arising in various areas. In this article, we will cover a configuration change that will help ensuring a better build stability, along with helping in performance (effects can be different depending or current tools used). [...]]]></description>
				<content:encoded><![CDATA[<p>Stability and performance of a build is a critical point when a project dealing with a large codebase and is composed of many developers.</p>
<p>A better stability can be achieved by limiting the amount of resources that are platform or environment dependent.</p>
<p>Performance, when working on projects that are several years old, and have dozens developers working on it,  can be regularly threatened by a significant growth of its codebase, making performance issues start arising in various areas.</p>
<p>In this article, we will cover a configuration change that will help ensuring a better build stability, along with helping in performance (effects can be different depending or current tools used).</p>
<h2>Dealing with compilation performance problems</h2>
<p>On some of our projects, the compilation made during the Maven build started to be significantly slower than a full build performed in Eclipse IDE by our developers .<br />
The difference between these two executions is quite simple: Maven uses by default the <em>javac </em>compiler, whereas Eclipse IDE uses its own implementation, <a href="http://www.eclipse.org/jdt/core/index.php">JDT Core</a>.<br />
With a project that has 4000+ classes to compile the difference can be huge: with a 32bit JVM having its <em>-Xmx</em> option set to 1280M, compilation time and memory usage are:</p>
<ul>
<li><strong>113s</strong> and <strong>1237M</strong> for javac (1.6.0_22)</li>
<li><strong>49s </strong>and <strong>414M</strong> for eclipse (3.8.0.v_C03)</li>
</ul>
<p>I hope not everyone has to deal with such a huge number of classes, but still it is a real-life sample. Smaller projects have the following build times</p>
<ul>
<li>~1750 classes: <strong>72s</strong> for javac, <strong>11s </strong>for eclipse</li>
<li>~380 classes: <strong>33s</strong> for javac, <strong>5s </strong>for eclipse</li>
</ul>
<p>One interesting thing about <em>javac</em> is its memory footprint, which is always way higher than <em>eclipse</em>. In Maven projects that have tons of modules, that can lead to an additional performance gain.</p>
<p>So, Maven has be configured to use JDT compiler. This should be straightforward as stated in the <a href="http://maven.apache.org/plugins/maven-compiler-plugin/non-javac-compilers.html">compiler plugin page</a>:</p><pre class="crayon-plain-tag">&lt;plugin&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;2.3.2&lt;/version&gt;
    &lt;configuration&gt;
        &lt;compilerId&gt;eclipse&lt;/compilerId&gt;
    &lt;/configuration&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.codehaus.plexus&lt;/groupId&gt;
            &lt;artifactId&gt;plexus-compiler-eclipse&lt;/artifactId&gt;
            &lt;version&gt;1.8.1&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/plugin&gt;</pre><p>This works most of the time, but for some particular cases, it is not good enough (case sensitivity issues, debugging problems, log output to be improved), and as we really needed something to help compilation time, we started working on adapting the <em>plexus-compiler-eclipse</em> to be usable for all our projects (more than 2 million LOC!). The patch containing everything needed is attached to the Codehaus <a href="http://jira.codehaus.org/browse/PLXCOMP-173">PLXCOMP-173</a> issue. I guess that most of projects can be compiled without this, but some old ones should need it.</p>
<p>Using the <em>eclipse</em> implementation, the compiler plugin gets all it needs from Maven dependencies , reducing potential build instability.</p>
<p>No issue has been found regarding the potential bytecode difference.</p>
<p>Unfortunately, this issue we raised is still unassigned, and seeing the current project activity, will unlikely be resolved in a near future. As we did, it is nevertheless possible to create your own artefact by patching the <em>plexus-compiler-eclipse 1.8.1</em> <a href="https://svn.codehaus.org/plexus/plexus-components/tags/1.8.1/plexus-compilers/plexus-compiler-eclipse/">source code </a>and installing it to your own repository.</p>
<h2>Annotation processing performance</h2>
<p>Annotation processing is also something that can have a significant performance issue during Maven builds.</p>
<p>The processing can be made in two ways:</p>
<ul>
<li>Using one of the specific plugins (<a href="http://maven-annotation-plugin.googlecode.com/svn/docs/plugin-info.html">maven-processor-plugin</a>, <a href="http://mojo.codehaus.org/apt-maven-plugin/">apt-maven-plugin</a>)</li>
<li>Performing processing during compilation (introduced with <a href="http://jcp.org/en/jsr/detail?id=269">JSR-269</a>, supported since <a href="http://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#annotationProcessors">maven-compiler-plugin 2.2</a>)</li>
</ul>
<p>The first option sometimes led us to processing times longer that 1 minute. This seemed to be dependent of the number of classes for the project, but also the number of its dependencies. For one of our projects, annotation processing was taking more time that all other phases together. The plugins will in the end either call <em>javac</em> of <em>apt</em> executables.</p>
<p>We couldn&#8217;t manage to perform annotation processing with <em>plexus-compiler-eclipse</em>, probably because of the eclipse compiler implementation chosen by the component. Going back to use javac as compiler in Maven builds was not an option, and would not have improved APT time.</p>
<p>Fortunately, there was another compiler component that was using JDT, and was close to our requirement regarding APT: <em>tycho-compiler-jdt</em>, a component of the <a href="http://www.eclipse.org/tycho/">Tycho Eclipse project</a>.</p>
<p>Configuring  <em>tycho-jdt-compiler</em> as the one  used within Maven builds is quite easy:</p><pre class="crayon-plain-tag">&lt;plugin&gt;
    &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    &lt;version&gt;2.3.2&lt;/version&gt;
    &lt;configuration&gt;
        &lt;compilerId&gt;jdt&lt;/compilerId&gt;
    &lt;/configuration&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.eclipse.tycho&lt;groupId&gt;
            &lt;artifactId&gt;tycho-compiler-jdt&lt;/artifactId&gt;
            &lt;version&gt;0.13.0&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/plugin&gt;</pre><p>This configuration is good enough for compiling Java classes with an up-to-date version of the JDT compiler. And is also available directly from Maven Central, so no patching and custom packaging  has to be done.</p>
<p>But that component is just missing a few enhancements to be able to process annotations during compilation. As for the <em>plexus-compiler-eclipse</em>, we made these and proposed the patch to the project community (see <a href="https://issues.sonatype.org/browse/TYCHO-590">TYCHO-590</a> and <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=360427">Eclipse bugzilla bug #360427</a>). That way, we can use annotation processing within the <em>maven-compiler-plugin</em>, whatever <em>compilerId</em> is chosen.</p>
<p>Annotation processing time on the most problematic module fell <strong>from 80 seconds to 5 seconds</strong>, although most of the time the change was <strong>from 20 seconds to 2 seconds</strong> for one module.</p>
<p>As for the compiler, using <em>jdt</em> implementation means that nothing related to the running JVM or <em>JAVA_HOME</em> is potentially affecting the processing output.</p>
<h2>Impact on stability</h2>
<p>The advantage about using Eclipse JDT for Java compilation and annotation processing is that your compilation phase doesn&#8217;t rely on any system configuration (here, mainly <em>JAVA_HOME</em> environment variable).  Your compilation and annotation processing stay identical whatever JVM is used. Although <em>javac</em> regularly improves its compilation performance,  being sure that the compilation output stays identical on any potential environment is a significant <em>plus</em>.</p>
<h2>Other paths for improving compilation and annotation processing</h2>
<ul>
<li>Recent Oracle JDKs (1.7 and 1.6.0_25+) provide significant improvements regarding compilation time, memory footprint stays huge.</li>
<li>Oracle JRockit 1.6 has APT performance close to Eclipse JDT. Compilation time is also a lot better than pre Oracle 1.6.0_25 JDKs, but it also uses a lot of memory.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2011/12/stabilizing-speeding-up-compilation-and-annotation-processing-in-maven-builds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remote partitioning with Spring Batch</title>
		<link>http://labs.bsb.com/2011/11/remote-partitioning-with-spring-batch/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=remote-partitioning-with-spring-batch</link>
		<comments>http://labs.bsb.com/2011/11/remote-partitioning-with-spring-batch/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 22:27:49 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=864</guid>
		<description><![CDATA[<p>In a previous article, we have discussed how staging the data to process with a sequence number could help us to build partitions that can be executed concurrently.  As explained in the documentation, the PartitionHandler is the component that knows about the mean to transport StepExecution requests to the actual worker(s). It basically sends one [...]]]></description>
				<content:encoded><![CDATA[<p>In a previous article, we have discussed how staging the data to process with a sequence number could help us to build partitions that can be executed concurrently.  As explained in <a href="http://static.springsource.org/spring-batch/reference/html/scalability.html">the documentation</a>, the <code>PartitionHandler</code> is the component that knows about the mean to transport <a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/StepExecution.html">StepExecution</a> requests to the actual worker(s). It basically sends one <code>StepExecution</code> request per partition to a set of  workers and wait for the results. Once each worker has contributed a result back to the handler, it returns the Collection of updated <code>StepExecution</code> instances. This simple and open design leads to a very simple <a href="http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/partition/PartitionHandler.html">PartitionHandler contract</a> to fulfill.</p>
<p>Spring Batch provides a default <code>PartitionHandler</code> implementation that delegates the processing to a <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html">TaskExecutor</a> (see <a href="https://github.com/SpringSource/spring-batch/blob/2.1.8.RELEASE/spring-batch-core/src/main/java/org/springframework/batch/core/partition/support/TaskExecutorPartitionHandler.java">TaskExecutorPartitionHandler</a>). What it basically does is wrapping the execution of the partition in a <code>FutureTask</code> and delegating its processing to the underlying <code>TaskExecutor</code> (a thread pool). Once each <code>FutureTask</code> has completed, the implementation simply returns the StepExecution instances that each task has returned. Simple !</p>
<p>Our approach is to take those simple principles and see how they can be applied to a remote execution where JMS would be used to transport the requests and responses. Executing the partition remotely brings new challenges:</p>
<ol>
<li>The remote worker does not have the actual <code>Step</code> instance so it needs to be able to retrieve it based on a reference</li>
<li>Compared to the default <code>PartitionHandler</code>, we need to wait for responses coming from different JVM(s)</li>
</ol>
<h2>Retrieving the Step instance</h2>
<p>In order to provide a complete isolation for each batch, Spring Batch provides the ability to declare each job in a dedicated child application context. When the application starts with this feature, a component scans all the jobs matching a configurable expression and creates a child context for each of those files (we specify one job per file). For instance, the following configuration scans (and therefore register) all jobs declared in a XML files found in the config root package and starting with &#8216;job&#8217;.</p><pre class="crayon-plain-tag">&lt;bean class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar"&gt;
    &lt;property name="applicationContextFactories"&gt;
        &lt;bean class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean"&gt;
            &lt;property name="resources" value="classpath*:/config/job*.xml"/&gt;
        &lt;/bean&gt;
    &lt;/property&gt;
    &lt;property name="jobLoader"&gt;
        &lt;bean class="org.springframework.batch.core.configuration.support.DefaultJobLoader"&gt;
            &lt;property name="jobRegistry" ref="jobRegistry"/&gt;
        &lt;/bean&gt;
    &lt;/property&gt;
&lt;/bean&gt;</pre><p>The  JobLoader updates a component called the <code>JobRegistry</code> which is able to locate a Job by its name. This is the piece we have updated by introducing the concept of <code>StepRegistry</code>:</p><pre class="crayon-plain-tag">/**
 * Registry keeping track of all the {@link Step} defined in a
 * {@link org.springframework.batch.core.Job}.
 */
public interface StepRegistry {

    /**
     * Registers all the step of the given job. If the job is already registered,
     * the method {@link #unregisterStepsFromJob(String)} is called before registering
     * the given steps.
     *
     * @param jobName the give job name
     * @param steps the job steps
     */
    void register(String jobName, Collection&lt;Step&gt; steps);

    /**
     * Unregisters all the steps of the given job. If the job is not registered,
     * nothing happens.
     *
     * @param jobName the given job name
     */
    void unregisterStepsFromJob(String jobName);

    /**
     * Returns the {@link Step} of the specified job based on its name.
     *
     * @param jobName the name of the job
     * @param stepName the name of the step to retrieve
     * @return the step with the given name belonging to the mentioned job
     * @throws NoSuchJobException no such job with that name exists
     * @throws NoSuchStepException no such step with that name for that job exists
     */
    Step getStep(String jobName, String stepName) throws NoSuchJobException, NoSuchStepException;

}</pre><p>And of course, our JobLoader now updates the content of this registry.</p>
<h2>Aggregating the result</h2>
<p>Our partition handler needs now to send one <code>StepExecutionRequest</code> per partition to run, as a <em>JMS message</em>. Let&#8217;s ignore for now what happens on the remote end; we still need to deal with the results aggregation. In Spring Batch, one thread (the master) is managing the state of the entire job. So this thread must wait (i.e. block) until all responses have been received from the workers. In a cluster, we never know where the master thread will be so we had to create a mechanism where each job has a temporary queue associated to it. Having a temporary queue makes sure that only the master thread can listen for messages. And because we set the reference to this temporary queue as the <code>JMSReplyTO</code> header of each request, our remote workers are able to send the response to that queue as well.</p>
<p>Remains a classical problem in asynchronous communication: timeout. To deal with this problem, we have built a <code>JmsAggregationService</code> that is managed by the following interfaces:</p>
<ul>
<li><code>AggregationItemListener</code>: receives an event when a response is received by the aggregation service.</li>
<li><code>AggregationCompletionPolicy</code>: determine when the aggregation is done, regardless of a potential timeout. In our case, this policy is straighforward since we expect one response per request so the only thing we have to do in this case is to use a default implementation that counts the number of responses</li>
<li><code>AggregationTimeoutPolicy</code>: determines if a timeout should occur based on the time at which the aggregation started</li>
</ul>
<p>While we could have built a straightforward policy that would timeout after some time, we decided to build an implementation that benefits from the metadata that Spring Batch stores in its internal model. Every time a <em>chunk</em> completes, the thread contributes some metadata to its <code>StepExecutionContext</code> and flushes it to the database. This is very handy because we can easily find out if a given partition is running or the last time it contributed something.</p>
<p>Based on these facilities, the aggregation service performs the following:</p>
<ol>
<li>Creates a JMS session</li>
<li>Creates a temporary JMS queue that will be used for the aggregation</li>
<li>Sends one message per request. Each message holds a reference to the temporary JMS queue</li>
<li>Listen for incoming response
<ul>
<li>When no response has been received, it ask the timeout policy if it should timeout. Based on the result, it either throws an exception or listen again for a response</li>
<li>When a response has been received, it invokes all registered listeners and ask if it should complete. Based on the result, it either returns the result or listen again for a new response</li>
</ul>
</li>
</ol>
<h2>Executing the partition on the remote worker</h2>
<p>Executing the actual partition is now easy. Upon the reception of a <code>StepExecutionRequest</code>, the worker retrieves the actual Step instance using the <code>StepRegistry</code>. Once the partition completes or if an error occurs a response is sent to the destination that was set in the <code>JMSReplyTO</code> header of the request.</p>
<p>The diagram below summarizes the global solution</p>
<p><a href="http://labs.bsb.com/wp-content/uploads/2011/09/distributed-jms.png" rel="lightbox[864]"><img class="aligncenter size-full wp-image-867" title="Distributed JMS execution with Spring Batch" src="http://labs.bsb.com/wp-content/uploads/2011/09/distributed-jms.png" alt="" width="591" height="449" /></a></p>
<p>Note that even though the master seems to be separate from the actual slaves, it does not have to be that way in practice. This video demonstrates how an additional node can join a running instance. It also showcases what happens when a node is killed in the middle of a job!</p>
<p><a href="http://labs.bsb.com/2011/11/remote-partitioning-with-spring-batch/"><em>Click here to view the embedded video.</em></a></p>
<h2><strong>Conclusion</strong></h2>
<p>These developments allows to support a remote partitioning use case for Spring Batch. And the good news is that BSB open sourced them (see the <a href="https://github.com/snicoll/spring-batch/tree/execution-jms">spring-batch</a> and <a href="https://github.com/snicoll/spring-batch-admin/tree/execution-jms">spring-batch-admin</a> forks on github!). It should be noted that not a lot of code is actually JMS specific: most of the infrastructure can be reused to implement use cases where the transport protocol is different. Only the aggregation service and the remote invoker should be changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2011/11/remote-partitioning-with-spring-batch/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Staging and business chunking with Spring Batch</title>
		<link>http://labs.bsb.com/2011/09/staging-and-business-chunking-with-spring-batch/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=staging-and-business-chunking-with-spring-batch</link>
		<comments>http://labs.bsb.com/2011/09/staging-and-business-chunking-with-spring-batch/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 15:18:52 +0000</pubDate>
		<dc:creator>Stéphane</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=853</guid>
		<description><![CDATA[Spring Batch is a powerful batch framework in Java with obviously a close integration with the Spring framework. One core feature for us is the ability to stage the data to process for a particular batch instance. In this article, we will cover how data can be staged and the features that can be built on top of it. More specifically, we will cover how a partition can contain business chunks, i.e. a set of elements to process that are linked to another business concept. [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://static.springsource.org/spring-batch/">Spring Batch</a> is a powerful batch framework in Java with obviously a close integration with the Spring framework. One core feature for us is the ability to stage the data to process for a particular batch instance. There are several reasons for this:</p>
<ol>
<li>We have multiples ways to handle an error. Some issues might force the batch execution to fail. Others could be ignored and treated specifically by a next attempt</li>
<li>We need to be able to restart where we left-off</li>
<li>Processing should be distributed (<a href="http://static.springsource.org/spring-batch/reference/html-single/index.html#partitioning">partitioning</a>)</li>
</ol>
<p>Therefore such a job starts with a first <em>staging step</em> whose purpose is to execute a query on the domain model and feed the <em>staging table</em> with the items to process. Each row in this staging table represents a <em>staging item</em> and has the following information:</p>
<ul>
<li>a technical identifier (primary key)</li>
<li>a reference to the job instance (<em>jobInstanceId</em>)</li>
<li>a sequence number: the sequence starts at 0 and is incremented for every new item</li>
<li>the name of the last step that handled the item</li>
<li>the current status of the item which could be either <code>PENDING</code>, <code>COMPLETED</code>, <code>ERROR</code> or <code>SKIPPED</code></li>
<li>the actual item</li>
</ul>
<p>The staging step is a regular, chunk-oriented, Spring Batch step. It has an <em>ItemReader</em>, an <em>ItemProcessor</em> and a no-op <em>ItemWriter</em>. The item reader can be any item reader, really. For the explanation, say that our reader is reading items from a database. Our item processor might look like the following:</p><pre class="crayon-plain-tag">/**
 * A default {@link StagingItemProcessor} that returns a {@link StagingItemProvider}
 * instance with no custom staging information by default. Sub-classes can override
 * the {@link #getStagingValue(Object)} or {@link #getCustomStagingItemProvider(Object)}
 * methods to override the default behavior.
 */
public class DefaultStagingItemProcessor&lt;I&gt; implements StagingItemProcessor&lt;I&gt; {

    private StagingValueProvider&lt;I&gt; stagingValueProvider;

    public StagingItemProvider process(I item) {
        return new StagingItemProvider(getStagingValue(item),
                getCustomStagingItemProvider(item));
    }

    /**
     * Returns the {@link StagingValue} of the specified item.
     *
     * @param item the item
     * @return the staging value
     */
    protected StagingValue getStagingValue(I item) {
        return stagingValueProvider.getStagingValue(item);
    }

    protected CustomStagingItemProvider getCustomStagingItemProvider(I item) {
        return null;
    }

    /**
     * Sets the {@link StagingValueProvider} to use.
     *
     * @param stagingValueProvider the staging value provider
     */
    public void setStagingValueProvider(StagingValueProvider&lt;I&gt; stagingValueProvider) {
        this.stagingValueProvider = stagingValueProvider;
    }
}</pre><p>What this code shows is that the staging item processor is responsible to process an incoming item and return a <code>StagingItemProvider</code> instance based on a <code>StagingValueProvider</code>.  The <code>StagingValueProvider</code> is responsible to provide the <em>staging value</em> out of a given item, this value being the <code>item</code> field in the staging table. Say for instance that a set of policies have been read from the database, an instance of <code>StagingValueProvider</code> would return a <code>StagingValue</code> holding the primary key of the policy. We use a typed object (<code>StagingValue</code>) and not the raw <em>Serializable</em> item because it contains additional metadata such as how to actually store it. Indeed, the staging table has three columns to hold the item, a <code>LONG</code>, a <code>VARCHAR</code> and a <code>CLOB</code> depending on the serialization strategy that was chosen. The <code>CustomStagingItemProvider</code>, which is <em>null</em> in this case is used for business chunking that we will cover later.</p>
<p>The framework uses an internal <code>ItemProcessor</code> delegating to this one to actually create the <em>staging items</em> in the database. When the transaction commits for the chunk, one staging item per item to process is added to the staging table (which explains why the writer is a no-op).</p>
<p>Once the staging step has completed, our staging item table is filled for a particular job instance and the actual processing can start. A step that relies on this mechanism is called a <em>staged step</em>: it will read the staging value and process the actual item. Before processing the actual item, the staging value needs to be translated back to the actual item. This is done through an implementation of the <code>StagingValueReader</code>. In the previous example, such instance might retrieve the actual policy out of the database based on the primary key. In practice, our developers have to pick the instance to use based on the ones we provide for most of our use cases.</p>
<p>Since every item in the job has a status and a step name, it is possible to skip an item and move along with the rest of the data to process. In this case, the job fails with a COMPLETED WITH ERRORS status. Restarting the job would restart the processing of failed items at the step mentioned in the staging table, giving the ability to restart exactly where we left-off.</p>
<h2>Partitioning</h2>
<p>Moreover, the sequence number allows to easily identify the boundaries of partitions, allowing to distribute the items to process to several workers. Our <code>Partitioner</code> simply puts the min and max sequence for each partition in the execution context. Our reader gets these information from the execution context and performs an SQL query that looks like the following (simplified):</p><pre class="crayon-plain-tag">SELECT OBJECTID, STATUS, SEQUENCE
FROM STAGING_ITEM
WHERE JOBINSTANCEID = 1234 AND SEQUENCE &gt;= 0 AND SEQUENCE &lt;= 999 AND (STATUS = 'PENDING' AND STEPNAME = 'step1')
ORDER BY SEQUENCE;</pre><p>where 1234 is the id of the job instance, step1 is the name of the staged step that is running and dealing with a partition of 1000 elements (in this case the first partition). Using this procedure, each partition reads the data it has to process independently (and also updates the status of each individual item independently).</p>
<h2>Business chunking</h2>
<p>Simple partitioning might not be enough in more complex use cases. Often the data to process (for instance policies) is linked to another business concept (for instance the customer) and it is important to process all the items related to that concept in the same thread. One obvious reason is data aggregation that should be stored somewhere when all the policies for a particular customer have been handled. To achieve this scenario, each partition must contain a set of business chunks and we need to be able to identify them. The way we have chosen to fulfill this need is to open the possibility for developers to stores arbitrary data in a custom table that is linked to the staging item with a foreign key.</p>
<p>Back on our example, a batch needs to process all the policies of the system and we want to perform some aggregation on the customer.</p>
<ul>
<li>Item type to process: the policy</li>
<li>Business chunk: all the policies of a customer</li>
</ul>
<p>During the staging process, we store additional information in a table called <code>policy_custom_item</code>. The developer can provide an implementation of the <code>CustomStagingItemProvider</code> to store the custom data at the same time as the default one. Again, we provide a default implementation, based on conventions, where the developer has to set the name of the custom table and a map of column names to values and the framework will deal with the primary key of the row and the reference to the item in the <em>staging_item</em> table (column <code>stagingitemid</code>).</p>
<p>We should then give the list of business partitions (one business partition here means all the policies of a given customer), something like:</p><pre class="crayon-plain-tag">SELECT MIN(S.sequence) as minSeq, MAX(S.sequence) as maxSeq
FROM staging_item S, policy_custom_item C
WHERE S.jobinstanceId = ? AND C.stagingitemid = S.objectid
GROUP BY C.clientid ORDER BY maxSeq</pre><p>Our custom <code>Partitioner</code> is now responsible to rebuild the partitions based on these business chunks. In this case, it means that the size of the partitions might differ, especially if the number of policies per customer differ a lot. Notice that the only thing that the developer had to do in this case is provide the SQL query used to build the business chunk boundaries!</p>
<p>The processor of such a step is also able to detect that the business chunk has changed and offer the ability to implement callbacks offered  by the <code>BusinessChunkListener</code> interface (<code>beforeBusinessChunk</code>, <code>afterBusinessChunk</code>).</p>
<h2>Conclusion</h2>
<p>Spring Batch offers an open contract that allowed us to build an infrastructure that suited our needs. There is no demo available at this time as the features that we have exposed in this article are part of other features that we have built on top of Spring Batch. In a next article we will cover how partitions can be invoked on several machines, offering a distributed partitioning support for Spring Batch.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2011/09/staging-and-business-chunking-with-spring-batch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit test production route in Apache Camel using mock endpoints</title>
		<link>http://labs.bsb.com/2011/06/unit-test-production-route-in-apache-camel-using-mock-endpoints/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unit-test-production-route-in-apache-camel-using-mock-endpoints</link>
		<comments>http://labs.bsb.com/2011/06/unit-test-production-route-in-apache-camel-using-mock-endpoints/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 14:38:18 +0000</pubDate>
		<dc:creator>Olivier</dc:creator>
				<category><![CDATA[Integration]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Test]]></category>
		<category><![CDATA[camel]]></category>

		<guid isPermaLink="false">http://labs.bsb.com/?p=824</guid>
		<description><![CDATA[<p>A previous article was focused on Apache Camel aggregation component, its new JDBC persistent repository more precisely. The sample provided includes a unit test which uses multiple good practices to test Camel routes. Let’s analyse them one by one.</p> Have a test spring context <p>The main idea is to keep unit test as simple as [...]]]></description>
				<content:encoded><![CDATA[<p>A <a title="jdbc persistence for camel aggregator" href="http://labs.bsb.com/2011/04/jdbc-persistence-for-camel-aggregator/" target="_self">previous article</a> was focused on Apache Camel aggregation component, its new JDBC persistent repository more precisely. The sample provided includes a unit test which uses multiple good practices to test Camel routes. Let’s analyse them one by one.</p>
<h2>Have a test spring context</h2>
<p>The main idea is to keep unit test as simple as possible. External resources should not be involved. If required mock, in-memory database… should be used. Therefore a separate spring context has been created for tests.</p>
<p>This context imports the initial context and overrides components using external resources. This is the case for properties and endpoints.</p><pre class="crayon-plain-tag">(…)
    &lt;!-- Import Production context --&gt;
    &lt;import resource="camel-context.xml" /&gt;
    (…)</pre><p></p>
<h2>Use properties for the endpoints URL</h2>
<p>Most of the time endpoints URL need to be adapted to the environment. <em>java.util.Properites</em> is very useful for that. It can be initialized with values from a properties file. This file can be externalized or even provided by the environment, e.g. OSGi. In the sample, the <em>Properties</em> object is created in the context for simplicity’s sake. The production spring context looks like this:</p><pre class="crayon-plain-tag">&lt;!-- Define Production endpoint URLs --&gt;
    &lt;util:properties id="spring-properties"&gt;
        &lt;prop key="in"&gt;file:target/in&lt;/prop&gt;
        &lt;prop key="out"&gt;file:target/out&lt;/prop&gt;
    &lt;/util:properties&gt;

    &lt;camelContext id="CamelContext" xmlns="http://camel.apache.org/schema/spring" trace="true"&gt;
        &lt;propertyPlaceholder id="properties" location="ref:spring-properties" /&gt;</pre><p>Notice that two <em>propertyPlacerholder </em>objects are created: one for spring and one for Camel. The Camel one simple refers to Spring’s one. This is required to use properties in endpoint URLs. <a href="http://camel.apache.org/properties.html">Camel properties component</a> is available since Camel 2.3.</p>
<p>The test context overrides the URLs and makes the endpoint independent of the file system.</p><pre class="crayon-plain-tag">&lt;!-- Define test endpoint URLs --&gt;
    &lt;util:properties id="spring-properties"&gt;
        &lt;prop key="in"&gt;direct:in&lt;/prop&gt;
        &lt;prop key="out"&gt;log:out&lt;/prop&gt;
    &lt;/util:properties&gt;</pre><p></p>
<h2>Perform test on production route</h2>
<p>The more interesting element is that the test uses the production route. This was made possible since Camel 2.7 thanks to <a href="https://issues.apache.org/jira/browse/CAMEL-3578">CAMEL-3578</a>. The idea was to be able to create mock endpoints using existing endpoints automatically.<br />
This functionality is disabled by default. The code snippets below show how to enable this new functionality using Java and Spring domain specific language (DSL).</p>
<h3>JAVA</h3>
<p></p><pre class="crayon-plain-tag">public class IsMockEndpointsJUnit4Test extends CamelTestSupport {

    @Override
    public String isMockEndpoints() {
        // override this method and return the pattern for which endpoints to mock.
        // use * to indicate all
        return "*";
    }</pre><p>The returned value is the pattern of all the endpoints to mock as indicated in the comment.</p>
<h3>XML</h3>
<p></p><pre class="crayon-plain-tag">&lt;bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"&gt;
    &lt;constructor-arg index="0" value="log*"/&gt;
&lt;/bean&gt;</pre><p>With the XML DSL all endpoint are mocked by default. The <em>constructor-arg</em> shown in the example allows restricting the number of endpoints created like in Java. See <a href="http://camel.apache.org/mock.html#Mock-MockingexistingendpointswithXMLDSL">the documentation</a> for more details.<br />
The endpoint can then be tested simply by prefixing the URL with <em>mock:</em> in the unit test.</p><pre class="crayon-plain-tag">MockEndpoint mock = getMockEndpoint("mock:" + endpoints.getProperty("out"));</pre><p></p>
<h2>Conclusion</h2>
<p>Camel 2.7 provides nice features to make unit testing easier. The best part is that the production routes can now be directly unit tested.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.bsb.com/2011/06/unit-test-production-route-in-apache-camel-using-mock-endpoints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
