<?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>Martin Ahrer - Together we&#039;ll make IT &#187; Blog</title>
	<atom:link href="http://www.martinahrer.at/topics/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.martinahrer.at</link>
	<description>Java Enterprise Softwareentwicklung und Consulting</description>
	<lastBuildDate>Sun, 11 Dec 2011 16:19:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<meta name="generator" content="deSignum 0.8.1" />
		<item>
		<title>Migrate from Hades to Spring Data JPA</title>
		<link>http://www.martinahrer.at/2011/09/13/migrate-from-hades-to-spring-data-jpa/</link>
		<comments>http://www.martinahrer.at/2011/09/13/migrate-from-hades-to-spring-data-jpa/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 19:27:44 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[hades]]></category>
		<category><![CDATA[spring-data-jpa]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=818</guid>
		<description><![CDATA[For the past 18 months I have been utilizing hades for implementing JPA based repositories/DAOs. Hades has now been moved into the Spring Data JPA project. Starting with their 1.0.1 release I tried migrating one of my projects to this release. Following I document the steps that were necessary to achieve this. Their project page [...]]]></description>
			<content:encoded><![CDATA[<p>For the past 18 months I have been utilizing <a href="http://redmine.synyx.org/projects/hades">hades</a> for implementing JPA based repositories/DAOs. Hades has now been moved into the <a href="http://www.springsource.org/spring-data/jpa">Spring Data JPA</a> project. Starting with their 1.0.1 release I tried migrating one of my projects to this release. Following I document the steps that were necessary to achieve this.</p>
<p>Their project page starts off with showing the Maven repositories and dependency descriptors (in case you are using Maven). Obviously the first thing is to replace the hades artifact by the spring-data-jpa artifact.</p>
<h2>Domain classes, JPA entities</h2>
<p>The project&#8217;s domain classes implement the <code>org.synyx.hades.domain.Persistable</code> interface which has to be replaced by <code>org.springframework.data.domain.Persistable</code> (takes a single generics parameter for the id type).</p>
<h2>CRUD repositories</h2>
<p>DAO interfaces extending <code>org.synyx.hades.dao.GenericDao</code> have to be modified to extend <code>org.springframework.data.repository.CrudRepository</code>. With this modification you will face a couple of compile problems. <code>read*</code>-methods have been renamed to <code>find*</code>-methods. Also you are loosing the paging and sorting capable <code>find*</code> &#8211; methods (which I didn&#8217;t use). If those are required then extend the <code>org.springframework.data.repository.PagingAndSortingRepository</code> interface.</p>
<p><code>find*</code> &#8211; methods now return iterables rather than list types requiring minor adjustments to existing codebase. To get around this, the interface can also extend from <code>JpaRepository</code>. Using this base interface hands you a pretty powerful DAO that goes far beyond a simple CRUD repository.</p>
<h2>CRUD repository instantiation</h2>
<p>When I started off with hades many month ago I used an explicit XML-based configuration of DAO components.</p>
<pre class="brush:xml">
&lt;bean id="addressDao" class="org.synyx.hades.dao.orm.GenericDaoFactoryBean" p:daoInterface="at.martinahrer.training.spring.jpa.dao.hades.AddressDao" /&gt;
</pre>
<p>For migration the factory bean class has to be changed:</p>
<pre class="brush:xml">
&lt;bean id="addressDao" class="org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean" p:daoInterface="at.martinahrer.training.spring.jpa.dao.hades.AddressDao" /&gt;</pre>
<p>With these changes in place all tests were showing the green bar again. However, today I would recommend using repository namespace http://www.springframework.org/schema/data/jpa which significantly reduces the amount of configuration needed by utilizing class path scanning to detect repository components.</p>
<pre class="brush:xml">
&lt;repository:repositories base-package="at.martinahrer.training.spring.jpa.dao" /&gt;
</pre>
<p>Certainly you might encounter more steps during migration depending on the features you have used from the old hades code base. In my case I only had used named JPA queries which just continued to work like before.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2011/09/13/migrate-from-hades-to-spring-data-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Analyzing class metadata with the Springframework</title>
		<link>http://www.martinahrer.at/2011/08/16/analyzing-class-metadata-with-the-springframework/</link>
		<comments>http://www.martinahrer.at/2011/08/16/analyzing-class-metadata-with-the-springframework/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 20:11:45 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Bytecode]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Springframework]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=805</guid>
		<description><![CDATA[What a shame I haven&#8217;t published anything for a long time. To make up for this long pause, I&#8217;m going to discuss how Spring can support with analyzing class metadata. These days everybody is crazy about using annotations in their frameworks. Occasionally you need to know what classes/methods are annotated. A good example might be [...]]]></description>
			<content:encoded><![CDATA[<p>What a shame I haven&#8217;t published anything for a long time. To make up for this long pause, I&#8217;m going to discuss how Spring can support with analyzing class metadata. These days everybody is crazy about using annotations in their frameworks. Occasionally you need to know what classes/methods are annotated. A good example might be to build a list of class names for all JPA entities (those classes annotated with javax.persistence.Entity and the like).</p>
<p><strong>A simple use-case showing how to detect classes annotated as components</strong></p>
<p>This use-case is using a few Spring APIs and some home grown code that controls resource matching (ClassResourceResolver and AnnotationMetadataMatcher).</p>
<pre class="brush:java">
import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.ClassUtils;

public class ClassResourceResolverTest {
	@Test
	public void testSelect() throws IOException {
		// given
		ClassResourceResolver scanner = new ClassResourceResolver(
				ClassUtils.convertClassNameToResourcePath(AComponent.class.getPackage().getName()));
		final MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(
				new PathMatchingResourcePatternResolver());

		// when
		String[] classNames = scanner.select(new AnnotationMetadataMatcher(metadataReaderFactory, false,
				Component.class));
		// then
		Assert.assertArrayEquals(new String[] { AComponent.class.getName() }, classNames);
		Assert.assertTrue(metadataReaderFactory.getMetadataReader(classNames[0]).getAnnotationMetadata()
				.isAnnotated(Component.class.getName()));
	}
}

@Component
class AComponent {
}

@Repository
class ARepository {
}
</pre>
<p><strong>ClassResourceResolver is doing the resource matching all from a set of resource paths</strong></p>
<pre class="brush:java">
	public String[] select(ClassResourceMetadataMatcher matcher) throws IOException {
		List&lt;String&gt; result = new ArrayList&lt;String&gt;();

		for (String path : resourcePath) {
			String resourcePattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + path + "/**/*.class";
			Resource[] resources = this.resourcePatternResolver.getResources(resourcePattern);
			for (Resource resource : resources) {
				if (resource.isReadable()) {
					MetadataReader reader = matcher.match(resource, resourcePatternResolver);
					if (reader != null) {
						result.add(reader.getClassMetadata().getClassName());
					}
				}
			}
		}
		return result.toArray(new String[result.size()]);
	}
</pre>
<p><strong>A ClassResourceMetadataMatcher is looking at the annotations present on a class resource</strong></p>
<pre class="brush:java">
	@Override
	public MetadataReader match(Resource resource, ResourceLoader resolver) throws IOException {
		for (Class&lt;? extends Annotation&gt; annotationType : annotationTypes) {
			MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
			if (new AnnotationTypeFilter(annotationType, considerMetaAnnotations).match(metadataReader,
					metadataReaderFactory)) {
				return metadataReader;
			}
		}
		return null;
	}
</pre>
<p>Under the hood Spring is using the <a href="http://asm.ow2.org/">ASM</a> bytecode library for looking into the class files. Not that no class examined by the code above is actually loading any of the class objects so it is fairly lightweight and not filling up your perm gen space. Still you need to make up useful search paths so you don&#8217;t end up scanning through you full classpath.</p>
<p>The full code just is available as a gist
<pre class="brush:bash">git clone git@gist.github.com:73f21e68d24032ad7672.git</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2011/08/16/analyzing-class-metadata-with-the-springframework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing static methods…</title>
		<link>http://www.martinahrer.at/2011/01/19/testing-static-methods/</link>
		<comments>http://www.martinahrer.at/2011/01/19/testing-static-methods/#comments</comments>
		<pubDate>Wed, 19 Jan 2011 11:46:42 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=685</guid>
		<description><![CDATA[We all have learned that it is not wise to overuse static methods as these are kind of hard to test and deal with. So in general I strive to get out of their way. Sometimes I&#8217;m facing the situation that the use of a framework or that some technical infrastructure requires me to implement [...]]]></description>
			<content:encoded><![CDATA[<p>We all have learned that it is not wise to overuse static methods as these are kind of hard to test and deal with. So in general I strive to get out of their way. Sometimes I&#8217;m facing the situation that the use of a framework or that some technical infrastructure requires me to implement static methods.<br />
So for testing I found <a href="https://code.google.com/p/powermock/">PowerMock</a> which nicely integrates with <a href="https://code.google.com/p/powermock/wiki/EasyMock">EasyMock</a> and Mockito.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2011/01/19/testing-static-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveMQ in embedded mode</title>
		<link>http://www.martinahrer.at/2010/10/19/activemq-in-embedded-mode/</link>
		<comments>http://www.martinahrer.at/2010/10/19/activemq-in-embedded-mode/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 20:41:38 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[ActiveMQ]]></category>
		<category><![CDATA[Springframework]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=665</guid>
		<description><![CDATA[I&#8217;m using Apache ActiveMQ for testing JMS based code. With their Springframework support it&#8217;s pretty simple to embed it into a test suite. Today I encountered a strange problem. After I had broken my test and fixed the bug later I was not able to restart the embedded ActiveMQ container. Obviously java.io.EOFException: Chunk stream does [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using Apache ActiveMQ for testing JMS based code. With their Springframework support it&#8217;s pretty simple to embed it into a test suite.<br />
Today I encountered a strange problem. After I had broken my test and fixed the bug later I was not able to restart the embedded ActiveMQ container.</p>
<p>Obviously <strong>java.io.EOFException: Chunk stream does not exist at page: 0</strong> sounds like some temporary file data was corrupt. <code>JobSchedulerStore</code> creates a directory <code>activemq-data/localhost/scheduler</code> for storing scheduler data. Clean/delete that and it should be startable again. If you don&#8217;t need that then you are better off with completely deactivating that feature.<br />
<span id="more-665"></span></p>
<pre class="brush:xml">
	&lt;amq:broker useJmx="true" persistent="false" start="false" schedulerSupport="true"&gt;
		&lt;amq:transportConnectors&gt;
			&lt;amq:transportConnector uri="${amq.brokerURL}" /&gt;
		&lt;/amq:transportConnectors&gt;
	&lt;/amq:broker&gt;
</pre>
<p>Also its helpful to disable automatic start of the queue container so the test can fully control it.</p>
<pre class="brush:java">
public class ServiceOperationsJmsProducerTest extends AbstractJUnit4SpringContextTests implements BrokerServiceAware {
	private BrokerService brokerService;

	@Test
	public void testProducer() throws Exception {
		try {
			brokerService.start();
			// do the testing
		} finally {
			brokerService.stop();
			brokerService.waitUntilStopped();
		}
	}

	@Override
	@Resource
	public void setBrokerService(BrokerService brokerService) {
		this.brokerService = brokerService;
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/10/19/activemq-in-embedded-mode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AspectJ runtime weaving within a Maven build</title>
		<link>http://www.martinahrer.at/2010/10/16/aspectj-runtime-weaving-within-a-maven-build/</link>
		<comments>http://www.martinahrer.at/2010/10/16/aspectj-runtime-weaving-within-a-maven-build/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 16:11:06 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[AspectJ]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=654</guid>
		<description><![CDATA[In order to perform AspectJ runtime weaving, an agent is required. Here we are using the agent (spring-instrument-3.0.3.RELEASE) provided by the Springframework. So for surefire we have to use a command line argument -javaagent: that specifies the JAR file containing the agent. To keep the build portable we copy that artifact to the build directory [...]]]></description>
			<content:encoded><![CDATA[<p>In order to perform AspectJ runtime weaving, an agent is required. Here we are using the agent (spring-instrument-3.0.3.RELEASE) provided by the Springframework. So for surefire we have to use a command line argument -javaagent: that specifies the JAR file containing the agent. To keep the build portable we copy that artifact to the build directory first using the maven-dependency-plugin.<br />
<span id="more-654"></span></p>
<pre class="brush:xml">
&lt;plugin&gt;
	&lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
	&lt;executions&gt;
		&lt;execution&gt;
			&lt;id&gt;copy&lt;/id&gt;
			&lt;phase&gt;process-sources&lt;/phase&gt;
			&lt;goals&gt;
				&lt;goal&gt;copy&lt;/goal&gt;
			&lt;/goals&gt;
			&lt;configuration&gt;
				&lt;artifactItems&gt;
					&lt;artifactItem&gt;
						&lt;groupId&gt;org.springframework&lt;/groupId&gt;
						&lt;artifactId&gt;spring-instrument&lt;/artifactId&gt;
						&lt;version&gt;3.0.3.RELEASE&lt;/version&gt;
					&lt;/artifactItem&gt;
				&lt;/artifactItems&gt;
			&lt;/configuration&gt;
		&lt;/execution&gt;
	&lt;/executions&gt;
&lt;/plugin&gt;
</pre>
<p>We have to pass in the weaving agent to surefire.</p>
<pre class="brush:xml">
&lt;plugin&gt;
	&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
	&lt;configuration&gt;
		&lt;argLine&gt;-javaagent:${project.build.directory}/dependency/spring-instrument-3.0.3.RELEASE.jar&lt;/argLine&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/10/16/aspectj-runtime-weaving-within-a-maven-build/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using a blackberry device as a modem on your Mac</title>
		<link>http://www.martinahrer.at/2010/10/11/using-a-blackberry-device-as-a-modem-on-your-mac/</link>
		<comments>http://www.martinahrer.at/2010/10/11/using-a-blackberry-device-as-a-modem-on-your-mac/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 15:08:25 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Blackberry]]></category>
		<category><![CDATA[macosx]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=650</guid>
		<description><![CDATA[While on the windows platform it was sufficient to only provide the username and password for the providers APN, with Mac OSX a generic modem phone number is required. This phone number is &#8220;*99***1#&#8220;. For a username and password for the providers APN you have to consult your provider. The user name for A1 (Austria) [...]]]></description>
			<content:encoded><![CDATA[<p>While on the windows platform it was sufficient to only provide the username and password for the providers APN, with Mac OSX a generic modem phone number is required. This phone number is &#8220;<strong>*99***1#</strong>&#8220;.<br />
For a username and password for the providers APN you have to consult your provider.<br />
The user name for A1 (Austria) is <strong>ppp@A1plus.at</strong> with the password <strong>ppp</strong>.</p>
<p>In the &#8220;Advanced&#8221; settings choose &#8220;Research in Motion&#8221; as vendor, &#8220;BlackBerry IP Modem (GSM)&#8221; as model, &#8220;a1.net&#8221; as APN!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/10/11/using-a-blackberry-device-as-a-modem-on-your-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Register ActiveMQ Spring namespace with eclipse 3.6</title>
		<link>http://www.martinahrer.at/2010/10/06/register-activemq-spring-namespace-with-eclipse-3-6/</link>
		<comments>http://www.martinahrer.at/2010/10/06/register-activemq-spring-namespace-with-eclipse-3-6/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 19:45:33 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[ActiveMQ]]></category>
		<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=633</guid>
		<description><![CDATA[In a earlier post I described how to setup the eclipse XML editor to validate a Spring context file containing the AMQ namespace. Looks like with eclipse 3.6 that does not work anymore, I was not able to actually use a JAR file as location like I used to set this up with eclipse 3.4. [...]]]></description>
			<content:encoded><![CDATA[<p>In a earlier <a href="http://www.martinahrer.at/2009/03/13/register-xml-schema-in-eclipse-xml-catalog/">post</a> I described how to setup the eclipse XML editor to validate a Spring context file containing the AMQ namespace.</p>
<p>Looks like with eclipse 3.6 that does not work anymore, I was not able to actually use a JAR file as location like I used to set this up with eclipse 3.4.<br />
So I&#8217;m directly associating the AMQ namespace with the <a href="http://activemq.apache.org/xml-reference.html">XML schema</a> available from the AMQ project.<br />
<span id="more-633"></span></p>
<pre class="brush:xml; wrap-lines=false">
&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"
        xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:amq="http://activemq.apache.org/schema/core"

	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
		http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"&gt;

	&lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
		p:locations="classpath:jms.properties"&gt;
	&lt;/bean&gt;
	&lt;amq:broker useJmx="true" persistent="false"&gt;
		&lt;amq:transportConnectors&gt;
			&lt;amq:transportConnector uri="${amq.brokerURL}" /&gt;
		&lt;/amq:transportConnectors&gt;
	&lt;/amq:broker&gt;
&lt;/beans&gt;
</pre>
<p>Go to the eclipse XML catalog editor and add a custom entry:</p>
<pre>
Location: http://activemq.apache.org/schema/core/activemq-core-5.4.1.xsd
Key type: Schema location
Key: http://activemq.apache.org/schema/core/activemq-core.xsd
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/10/06/register-activemq-spring-namespace-with-eclipse-3-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Nexus and the Nexus REST API for implementing a software update tool</title>
		<link>http://www.martinahrer.at/2010/05/25/using-nexus-and-the-nexus-rest-api-for-implementing-a-software-update-tool/</link>
		<comments>http://www.martinahrer.at/2010/05/25/using-nexus-and-the-nexus-rest-api-for-implementing-a-software-update-tool/#comments</comments>
		<pubDate>Tue, 25 May 2010 20:03:42 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=456</guid>
		<description><![CDATA[Nexus is using a pretty well documented REST API which is usable externally as well. For one of my customers I implemented a kind of automatic software update tool that can be embedded into any product. It is based on the Sonatype NEXUS repository manager. That tool performs the following steps Detect the current software [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://nexus.sonatype.org/">Nexus </a> is using a pretty well documented REST API which is usable externally as well.<br />
For one of my customers I implemented a kind of automatic software update tool that can be embedded into any product. It is based on the Sonatype NEXUS repository manager.</p>
<p><span id="more-456"></span></p>
<p>That tool performs the following steps</p>
<ol>
<li>Detect the current software version from the artifact metadata that is embedded into any Maven-built artifact</li>
<li>Query Nexus for available artifacts for the installed software (identified by Maven groupId and artifactId).</li>
<li>Test if any of the available artfacts has a version number higher than the installed one</li>
<li>If a newer version is available then download it</li>
<li>Move the new version to the target directory (e.g. the deployment folder of the application server)</li>
</ol>
<p>The Nexus REST url for a simple query for some artifact looks like <code>http://localhost/nexus/service/local/data_index/repositories/releases/content?q=commons-lang</code>. This query asks for the <em>commons-lang</em> artifact from the <em>releases repository</em>.</p>
<p>More URL schemes are documented <a href="https://docs.sonatype.com/display/NX/Nexus+Rest+API">here</a> and <a href="https://grid.sonatype.org/ci/view/Nexus/job/Nexus/label=ubuntu/ws/trunk/nexus/nexus-rest-api/target/classes/docs/index.html">there</a>.</p>
<p>The result from this query is like (only the latest versions are included for brevity):</p>
<pre class="brush:xml">
&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;search-results&gt;
	&lt;totalCount&gt;5&lt;/totalCount&gt;
	&lt;from&gt;-1&lt;/from&gt;
	&lt;count&gt;-1&lt;/count&gt;
	&lt;tooManyResults&gt;false&lt;/tooManyResults&gt;
	&lt;data&gt;
		&lt;artifact&gt;

			&lt;resourceURI&gt;http://localhost/nexus/service/local/repositories/central/content/commons-lang/commons-lang/2.4/commons-lang-2.4.jar&lt;/resourceURI&gt;
			&lt;groupId&gt;commons-lang&lt;/groupId&gt;
			&lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
			&lt;version&gt;2.4&lt;/version&gt;
			&lt;packaging&gt;jar&lt;/packaging&gt;
			&lt;extension&gt;jar&lt;/extension&gt;

			&lt;repoId&gt;central&lt;/repoId&gt;
			&lt;contextId&gt;Maven Central (Cache)&lt;/contextId&gt;
			&lt;pomLink&gt;http://localhost/nexus/service/local/artifact/maven/redirect?r=central&amp;g=commons-lang&amp;a=commons-lang&amp;v=2.4&amp;e=pom&lt;/pomLink&gt;
			&lt;artifactLink&gt;http://localhost/nexus/service/local/artifact/maven/redirect?r=central&amp;g=commons-lang&amp;a=commons-lang&amp;v=2.4&amp;e=jar&lt;/artifactLink&gt;

		&lt;/artifact&gt;
		&lt;artifact&gt;
			&lt;resourceURI&gt;http://localhost/nexus/service/local/repositories/central/content/commons-lang/commons-lang/2.4/commons-lang-2.4-sources.jar&lt;/resourceURI&gt;
			&lt;groupId&gt;commons-lang&lt;/groupId&gt;
			&lt;artifactId&gt;commons-lang&lt;/artifactId&gt;
			&lt;version&gt;2.4&lt;/version&gt;
			&lt;classifier&gt;sources&lt;/classifier&gt;

			&lt;packaging&gt;jar&lt;/packaging&gt;
			&lt;extension&gt;jar&lt;/extension&gt;
			&lt;repoId&gt;central&lt;/repoId&gt;
			&lt;contextId&gt;Maven Central (Cache)&lt;/contextId&gt;
			&lt;pomLink&gt;&lt;/pomLink&gt;
			&lt;artifactLink&gt;http://localhost/nexus/service/local/artifact/maven/redirect?r=central&amp;g=commons-lang&amp;a=commons-lang&amp;v=2.4&amp;e=jar&amp;c=sources&lt;/artifactLink&gt;

		&lt;/artifact&gt;

	&lt;/data&gt;
&lt;/search-results&gt;
</pre>
<p>That only needs to be parsed using a SAX parser for extracting the most important elements. This is the Maven GAV, packaging, classifier, and  the download url which are stored in a POJO. This POJO implements the algorithm for selecting the latest version by its compareTo method.</p>
<pre class="brush:java">
public class MavenArtifactMetadata implements Comparable&lt;MavenArtifactMetadata&gt; {
	private String groupId;
	private String artifactId;
	private String version;
	private String resourceURI;
	private String packaging;
	private String classifier;

	@Override
	public int compareTo(MavenArtifactMetadata o) {
		if (!this.groupId.equals(o.groupId) || !this.artifactId.equals(o.artifactId)) {
			return -1;
		}
		return versionCompareTo(o.version);
	}

	int versionCompareTo(String version) {
		Integer[] thatVersion = versionComponents(version);
		Integer[] thisVersion = versionComponents(this.version);
		int length = Math.min(thatVersion.length, thisVersion.length);
		for (int i = 0; i < length; i++) {
			if (thatVersion[i].equals(thisVersion[i])) {
				continue;
			}
			return thisVersion[i].compareTo(thatVersion[i]);
		}
		return thisVersion.length - thatVersion.length;
	}

	Integer[] versionComponents(String version) {
		String[] splitted = StringUtils.delimitedListToStringArray(version, ".");
		if (splitted.length>0 &#038;&#038; splitted[splitted.length-1].endsWith("-SNAPSHOT")) {
			splitted[splitted.length-1]=splitted[splitted.length-1].replaceAll("-SNAPSHOT", "");
		}
		Integer[] components = new Integer[splitted.length];
		for (int i = 0; i < splitted.length; i++) {
			components[i] = (Integer.valueOf(splitted[i]));
		}
		return components;
	}
}
</pre>
<p>While implementing the download part I had learned an important lesson. For downloading and moving large files it is much more efficient to use the Java NIO API.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/05/25/using-nexus-and-the-nexus-rest-api-for-implementing-a-software-update-tool/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java5 Generics are implemented by type erasure, but &#8230;</title>
		<link>http://www.martinahrer.at/2010/05/22/java5-generics-are-implemented-by-type-erasure-but/</link>
		<comments>http://www.martinahrer.at/2010/05/22/java5-generics-are-implemented-by-type-erasure-but/#comments</comments>
		<pubDate>Sat, 22 May 2010 19:51:22 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=443</guid>
		<description><![CDATA[&#8230; still it is possible to get hold of the actual type parameters of parameterized types. Even if classical reflection does not deliver type information, this type parameters can still be extremely useful. I have a few pieces of code (DAO, controllers, etc.) that are parameterized by class objects. An example: public class EntityFormController&#60;T&#62; extends [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230; still it is possible to get hold of the actual type parameters of <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/ParameterizedType.html">parameterized types</a>. Even if classical reflection does not deliver type information, this type parameters can still be extremely useful. I have a few pieces of code (DAO, controllers, etc.) that are parameterized by class objects.<br />
<span id="more-443"></span><br />
An example:</p>
<pre class="brush:java">
public class EntityFormController&lt;T&gt; extends FormController&lt;T&gt; {
	private Class&lt;T&gt; entityClass;

	public String create() {
		Object instance=getEntityClass().newInstance();
		...
	}
}
</pre>
<p>So for every specific instance of the property entityClass must be initialized explicitely by implementing a proper constructor, using dependency injection, etc.</p>
<pre class="brush:java">
public class AddressFormController extends EntityFormController&lt;Address&gt; {
	public class AddressFormController() {
		super(Address.class);
	}
}
</pre>
<p>This is kind of violating the<a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself"> DRY principle</a>. The actual type is already given through the type parameter. So I was trying to eliminate this few lines of code and come up with a solution that can analyze any parameterized type from a class declaration (super class hierarchy + interfaces).</p>
<pre class="brush:java">
/**
 * @author Martin Ahrer
 *
 */
public class GenericTypeArgumentExtractor {
	private final Class&lt;?&gt; candidateClass;

	public GenericTypeArgumentExtractor(Class&lt;?&gt; candidateClass) {
		super();
		this.candidateClass = candidateClass;
	}

	public Class&lt;?&gt;[] getGenericTypeArguments(Class&lt;?&gt; clazz) {
		// TODO: hopefully clazz.getGenericSuperclass() == clazz.getSuperclass()
		Type[] types = Arrays.copyOf(clazz.getGenericInterfaces(), clazz.getGenericInterfaces().length
				+ (clazz.getGenericSuperclass() != null ? 1 : 0));
		if (clazz.getGenericSuperclass() != null) {
			types[types.length - 1] = clazz.getGenericSuperclass();
		}
		// Iterate all types from which the clazz inherits. i.e all generic
		// interfaces and the generic super class.
		for (Type type : types) {
			Class&lt;?&gt;[] arguments = getGenericTypeArguments(type);
			if (arguments != null) {
				return arguments;
			}
		}
		return null;
	}

	private Class&lt;?&gt;[] getGenericTypeArguments(Type type) {
		if (type instanceof ParameterizedType) {
			ParameterizedType parameterizedType = (ParameterizedType) type;
			if (isCandidate(parameterizedType)) {
				Type[] arguments = parameterizedType.getActualTypeArguments();
				Class&lt;?&gt;[] result = new Class&lt;?&gt;[arguments.length];
				for (int i = 0; i &lt; result.length; i++) {
					result[i] = (Class&lt;?&gt;) (arguments[i] instanceof ParameterizedType ? ((ParameterizedType) arguments[i])
							.getRawType()
							: arguments[i]);
				}
				return result;
			}
		} else if (type != null) {
			Class&lt;?&gt;[] result = getGenericTypeArguments((Class&lt;?&gt;) type);
			return result;
		}
		return null;
	}

	/**
	 * Detects the type from which we want the type arguments. Can be overridden
	 * in subclasses.
	 *
	 * @param type
	 *            the parameterized type to check for.
	 * @return true if type arguments of the passed type are reqeusted.
	 */
	protected boolean isCandidate(ParameterizedType type) {
		return candidateClass.isAssignableFrom((Class&lt;?&gt;) type.getRawType());
	}
}
</pre>
<p>Using the above class for extracting type parameters would return an array containing the Address class object.</p>
<pre class="brush:java">
Class<?>[] genericTypeArguments = new GenericTypeArgumentExtractor(EntityFormController.class)
		.getGenericTypeArguments(AddressFormController.class);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/05/22/java5-generics-are-implemented-by-type-erasure-but/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A simple ViewController for JSF</title>
		<link>http://www.martinahrer.at/2010/05/18/a-simple-viewcontroller-for-jsf/</link>
		<comments>http://www.martinahrer.at/2010/05/18/a-simple-viewcontroller-for-jsf/#comments</comments>
		<pubDate>Tue, 18 May 2010 11:14:40 +0000</pubDate>
		<dc:creator>Martin Ahrer</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[JavaServer Faces]]></category>

		<guid isPermaLink="false">http://www.martinahrer.at/?p=428</guid>
		<description><![CDATA[Very early Apache Shale came up with the idea of providing a view controller that allowed to execute dedicated methods (annotated, etc.) during various JSF life-cycle (phase) events for doing initialization work upfront for example. As Shale was hibernated, Apache Orchestra stepped in and added many more useful features. Both frameworks are based on the [...]]]></description>
			<content:encoded><![CDATA[<p>Very early <a href="http://shale.apache.org/">Apache Shale</a> came up with the idea of providing a view controller that allowed to execute dedicated methods (annotated, etc.) during various JSF life-cycle (phase) events for doing initialization work upfront for example. As Shale was hibernated, <a href="http://myfaces.apache.org/orchestra/myfaces-orchestra-core/viewController.html">Apache Orchestra</a> stepped in and added many more useful features.<br />
Both frameworks are based on the idea of having a dedicated page bean (a JSF managed bean per view) that would control important processing steps for the entire view (page).<br />
It requires following naming conventions in order to associate a page bean to a view, optionally a view controller mapper provided the ability of explicitely associating page beans and views.</p>
<p><span id="more-428"></span><br />
In many projects I found the situation that the client didn&#8217;t want to pull in another framework just for that little feature.  Also for distributing responsibilities, I usually have multiple managed beans per view. So another &#8220;artificial&#8221; page bean is required just for controlling the others. So I came up with a fairly simple solution that allows to directly invoke those life-cycle callbacks on any managed bean used on a single page.</p>
<p>The &lt;f:phaseListener&gt; tag allows to bind a JSF <a href="http://javadoc.glassfish.org/v3/apidoc/javax/faces/event/PhaseListener.html">PhaseListener</a>. You can even bind multiple phase listeners (one for each managed bean; you get the idea).</p>
<pre class="brush:xml">
&lt;f:phaseListener binding="#{bean1.viewControllerPhaseListener}" /&gt;
&lt;f:phaseListener binding="#{bean2.viewControllerPhaseListener}" /&gt;
</pre>
<p>So each of the beans would provide a reference to a phase listener that would take care to perform the proper processing for the bean itself. Every bean then would use a set of annotations (like @PreRenderView, @PostRestoreView, @PreInvokeApplication, etc.) to mark those methods that should be called during a specific phase.</p>
<pre class="brush:java">
public class EntityListController&lt;T&gt; {
	private volatile ViewControllerPhaseListener viewControllerPhaseListener;

	public ViewControllerPhaseListener getViewControllerPhaseListener() {
		if (viewControllerPhaseListener == null) {
			viewControllerPhaseListener = new ViewControllerPhaseListener(this);
		}
		return viewControllerPhaseListener;
	}

	@PreRenderView
	public void updateModel() {
		...
	}

	...
</pre>
<p>The code above is partially showing a base class from which many of my managed bean are subclassed. Key is the ViewControllerPhaseListener which hides all of the details of doing the reflection job for finding (and then caching) the annotated methods to invoke during a specific phase. For brevity I&#8217;m showing only the key elements of this class.</p>
<pre class="brush:java">
public class ViewControllerPhaseListener implements PhaseListener {
	private final Object target;

	public ViewControllerPhaseListener(Object target) {
		super();
		this.target = target;
	}

	private final Class&lt;Annotation&gt;[] LIFECYCLE_ANNOTATIONS = new Class[] { PreRenderView.class, PostRestoreView.class,
		PreInvokeApplication.class };

	private final static Map&lt;Class&lt;?&gt;, Map&lt;Class&lt;? extends Annotation&gt;, Set&lt;Method&gt;&gt;&gt; controllerClasses = new ConcurrentHashMap&lt;Class&lt;?&gt;, Map&lt;Class&lt;? extends Annotation&gt;, Set&lt;Method&gt;&gt;&gt;();

	static Map&lt;Class&lt;?&gt;, Map&lt;Class&lt;? extends Annotation&gt;, Set&lt;Method&gt;&gt;&gt; getControllerClasses() {
		return controllerClasses;
	}

	@Override
	public void beforePhase(PhaseEvent event) {
		if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())) {
			invokeAnnotatedMethod(PreRenderView.class, target);
		} else if (PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId())) {
			invokeAnnotatedMethod(PreInvokeApplication.class, target);
		}
	}

	@Override
	public void afterPhase(PhaseEvent event) {
		if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) {
			invokeAnnotatedMethod(PostRestoreView.class, target);
		}
	}

	@Override
	public PhaseId getPhaseId() {
		return PhaseId.ANY_PHASE;
	}

	void invokeAnnotatedMethod(Class&lt;? extends Annotation&gt; annotation, Object targetObject) {
		if (!controllerClasses.containsKey(targetObject.getClass())) {
			registerController(targetObject.getClass());
		}
		for (Method method : controllerClasses.get(targetObject.getClass()).get(annotation)) {
			try {
				method.invoke(targetObject);
			} catch (Exception e) {
				LOGGER.error("Error invoking @{} annotated method {} on {}", new Object[] { annotation.getSimpleName(),
						method, targetObject });
				throw new RuntimeException(e);
			}
		}
	}

	public void registerController(Class&lt;?&gt; controllerClass) {
		// do the reflection work
	}
}
</pre>
<p>In a few projects this has proven to be quite versatile. Currently I have a couple of interesting phase listener components, from the simple DebugPhaseListener to the OpenTransactionInViewPhaseListener. I can just throw them into any view as they are required.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martinahrer.at/2010/05/18/a-simple-viewcontroller-for-jsf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

