<?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>AtomicGamer Dev Blog &#187; Warp-persist</title>
	<atom:link href="http://www.atomicgamer.com/dev/tag/warp-persist/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.atomicgamer.com/dev</link>
	<description>The Trials and Errors of AtomicGamer</description>
	<lastBuildDate>Sun, 22 Nov 2009 19:33:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Wicket, Guice 2.0, Warp-persist 2.0</title>
		<link>http://www.atomicgamer.com/dev/2009/10/wicket-guice-2-0-warp-persist-2-0/</link>
		<comments>http://www.atomicgamer.com/dev/2009/10/wicket-guice-2-0-warp-persist-2-0/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 03:37:56 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[AtomicGamer]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Warp-persist]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://www.atomicgamer.com/dev/?p=31</guid>
		<description><![CDATA[When I&#8217;m in development mode, I dislike using frameworks that will be out-of-date by the time I release. I will install beta (or alpha or simply development) builds of the software I&#8217;m choosing to use for my project. The downside (besides framework bugs) is that I&#8217;m usually on my own when trying to figure out [...]]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m in development mode, I dislike using frameworks that will be out-of-date by the time I release. I will install beta (or alpha or simply development) builds of the software I&#8217;m choosing to use for my project. The downside (besides framework bugs) is that I&#8217;m usually on my own when trying to figure out how to integrate the projects together as most web tutorials aren&#8217;t up-to-date.</p>
<p>I&#8217;ve spent some time getting Wicket (just 1.4.2), Guice 2, and Warp-persist 2.0 running together. Everything seems to be running happily but I haven&#8217;t tried anything complicated. I can inject my Hibernate session and start a transaction though. Just remember that Warp-persist 2.0 is in active development and wicket-guice doesn&#8217;t mention Guice 2.0 at all. Use it this all together at your own peril.</p>
<p>It&#8217;s really not that much of a stretch from the current 1.0 releases of Guice and Warp-persist. And really, most of what I did I got from <a href="http://www.mularien.com/blog/2007/11/28/bleeding-edge-transactional-wicket-web-applications-with-warp-and-guice/">blog 1</a> and <a href="http://richard-wilkinson.co.uk/2008/04/19/wicket-guice-and-warp-persist/">blog 2</a>. The <a href="http://code.google.com/docreader/#p=warp-persist">current Warp-persist 2.0 docs</a> are a work-in-progress.</p>
<p>First, let&#8217;s setup the Maven 2 dependencies under the assumption you got Wicket and Hibernate already configured appropriately.</p>
<p>We&#8217;ll need wicket-guice which is dependent on Guice 1.0. We&#8217;ll remove that dependency.</p>
<pre class="brush: xml">
        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.wicket&lt;/groupId&gt;
            &lt;artifactId&gt;wicket-guice&lt;/artifactId&gt;
            &lt;version&gt;${wicket.version}&lt;/version&gt;
            &lt;exclusions&gt;
                &lt;exclusion&gt;
                    &lt;groupId&gt;com.google.code.guice&lt;/groupId&gt;
                    &lt;artifactId&gt;guice&lt;/artifactId&gt;
                &lt;/exclusion&gt;
            &lt;/exclusions&gt;
        &lt;/dependency&gt;
</pre>
<p>Then, we&#8217;ll add a dependency for Guice 2.0.</p>
<pre class="brush: xml">
         &lt;dependency&gt;
            &lt;groupId&gt;com.google.inject&lt;/groupId&gt;
            &lt;artifactId&gt;guice&lt;/artifactId&gt;
            &lt;version&gt;2.0&lt;/version&gt;
        &lt;/dependency&gt;
</pre>
<p>Lastly, we&#8217;ll add our dependency for Warp-persist 2.0. Unfortunately, there aren&#8217;t any public Maven repositories that have a recent build of Warp-persist 2.0 stored. We&#8217;ll need to download the most recent public build (you can get the source and build your own snapshot, but I leave that as an exercise to the ambitious) and manually install it our local Maven repository.</p>
<p>Head to the <a href="http://warp-persist.googlecode.com/svn/trunk/warp-persist/dist/">public dist</a> and download <strong>warp-persist-2.0-20090214.zip</strong>. The snapshot is out-of-date (as of 2009/10/12)! Extract the warp-persist-2.0-20090214\warp-persist-2.0-20090214.jar located in the zip file to some local directory. Then run the following Maven command to install this jar into your local repository.</p>
<p><code><br />
mvn install:install-file  -Dfile=warp-persist-2.0-20090214.jar -DgroupId=com.wideplay.warp -DartifactId=warp-persist -Dversion=2.0-20090214 -Dpackaging=jar -DgeneratePom=true<br />
</code></p>
<p>With any luck, you&#8217;ll be able to add the last Maven dependency.</p>
<pre class="brush: xml">
        &lt;dependency&gt;
            &lt;groupId&gt;com.wideplay.warp&lt;/groupId&gt;
            &lt;artifactId&gt;warp-persist&lt;/artifactId&gt;
            &lt;version&gt;2.0-20090214&lt;/version&gt;
        &lt;/dependency&gt;
</pre>
<p>Maven is exciting. </p>
<p>The SessionPerRequestFilter from Warp-persist 1.0 has been deprecated. In our web.xml, we&#8217;ll have to use the new PersistenceFilter instead. It&#8217;s pretty straightforward. Just make sure you define the PersistenceFilter mapping first. This&#8217;ll get us towards our open session in view goal.</p>
<pre class="brush: xml">
	&lt;filter&gt;
		&lt;filter-name&gt;wicket.web&lt;/filter-name&gt;
 		&lt;filter-class&gt;org.apache.wicket.protocol.http.WicketFilter&lt;/filter-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;applicationClassName&lt;/param-name&gt;
			&lt;param-value&gt;com.atomicgamer.web.WebApplication&lt;/param-value&gt;
 		&lt;/init-param&gt;
 	&lt;/filter&gt;

    &lt;filter&gt;
        &lt;filter-name&gt;warpPersistFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;com.wideplay.warp.persist.PersistenceFilter&lt;/filter-class&gt;
    &lt;/filter&gt;

    &lt;filter-mapping&gt;
        &lt;filter-name&gt;warpPersistFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;

    &lt;filter-mapping&gt;
        &lt;filter-name&gt;wicket.web&lt;/filter-name&gt;
	    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
</pre>
<p>We&#8217;re almost there. In our subclasses WebApplication, we need to configure Warp-persist&#8217;s PersistenceService, tell Guice about it, and tell Wicket-guice about everything. One difference from Warp-persist 1.0 is that we don&#8217;t need to manually start the PersistenceService. The PersistenceFilter takes care of that for us.</p>
<pre class="brush: java">
        //We&#039;ll be wanting to use Hibernate
        Module warpModule = PersistenceService.usingHibernate().across(UnitOfWork.REQUEST).buildModule();
        //And use the Warp-persist module followed by our own module for Guice.
        Injector injector = Guice.createInjector(warpModule, buildWebModule());
        //And we&#039;ll probably be wanting to use some injecting inside of our Wicket classes
        addComponentInstantiationListener(new GuiceComponentInjector(this, injector));
</pre>
<p>For completeness, here&#8217;s the buildWebModule() method inside my WebApplication. There&#8217;s no magic here. I&#8217;m using a Hibernate annotation configuration and telling it about my DAO classes.</p>
<pre class="brush: java">
    protected Module buildWebModule()
    {
        return new Module()
        {
            public void configure(Binder binder)
            {
                AnnotationConfiguration configuration = new AnnotationConfiguration().configure();
                binder.bind(Configuration.class).toInstance(configuration);
                configuration.addAnnotatedClass(Game.class);

                binder.bind(GameDAO.class).to(GameHibernateDAO.class);
            }
        };
    }
</pre>
<p>With this all set, I can take those DAO and use them finally! Let&#8217;s get a session.</p>
<pre class="brush: java">
public abstract class HibernateDAO&lt;T, ID extends Serializable&gt; implements DAO&lt;T, ID&gt;
{
    @Inject
    protected Provider&lt;Session&gt; session;
</pre>
<p>And more importantly, let&#8217;s start a transaction.</p>
<pre class="brush: java">
    @Transactional
    Game getGame(long id)
    {
        return gameDAO.findById(id);
    }
</pre>
<p>And that&#8217;s all. If you&#8217;re already running 1.0 versions of everything, it&#8217;s not too bad to upgrade to 2.0. Practically everything is backward compatible and just deprecated at this point. So if you want to be on the bleeding edge of Hibernate 3.5, Guice 2.0, Warp-persist 2.0, Wicket 1.4.2, well, there you go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.atomicgamer.com/dev/2009/10/wicket-guice-2-0-warp-persist-2-0/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
