<?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>Sonatype Blog &#187; guice</title>
	<atom:link href="http://blog.sonatype.com/people/tag/guice/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Thu, 16 May 2013 18:53:09 +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>Guicing up Hudson: Making life easier for developers with JSR-330</title>
		<link>http://blog.sonatype.com/people/2011/02/guicing-up-hudson-making-life-easier-for-developers-with-jsr-330/</link>
		<comments>http://blog.sonatype.com/people/2011/02/guicing-up-hudson-making-life-easier-for-developers-with-jsr-330/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 16:00:30 +0000</pubDate>
		<dc:creator>Jason van Zyl</dc:creator>
				<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[Hudson]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=7429</guid>
		<description><![CDATA[Today we started rolling out the first of our proposed JSR-330 Dependency Injection changes to Hudson back into the Hudson community. We&#8217;re giving it back because we think it is going to make a huge difference for Hudson&#8217;s future development. As more and more libraries move to JSR-330, we&#8217;re going to see a lot of [...]]]></description>
				<content:encoded><![CDATA[<p>Today we started rolling out the first of our proposed JSR-330 Dependency Injection changes to Hudson back into the Hudson community.  We&#8217;re giving it back because we think it is going to make a huge difference for Hudson&#8217;s future development.   As more and more libraries move to JSR-330, we&#8217;re going to see a lot of possibilities open up because of these changes.   With today&#8217;s donation, we&#8217;re making it easier to extend Hudson, we&#8217;re reducing the effort required to write a Hudson plugin, and we&#8217;re helping to put in a new foundation for the next level of Hudson interoperability and performance.</p>

<h3>What does this mean for you as an end-user?</h3>

<p>Guice is emerging as a lightweight Dependency Injection standard. We&#8217;ve moved the core of Maven to Guice over the past two years and it has dramatically increased performance and opened up possibility for integration with other tools and libraries.    Since Guice is implementing JSR-330 standards, what we&#8217;re really doing with this effort is moving Hudson to a more standard, more maintainable architecture.   As an end-user, you will likely notice increased stability as the core becomes more modular, easier to maintain and test.   You should also expect greater integration with other tools that can speak the JSR-330 standard.   This includes components that use both Guice and the Spring Framework.</p>

<p><span id="more-7429"></span></p>

<h3>Our Plan for JSR-330 Integration Going Forward</h3>

<p>We&#8217;ve been working on decoupling both our JSR-330 and JAXRS work from our commercial version of Hudson for a couple of months.  Our current focus is to allow Hudson plugins to take advantage of the JSR-330 Dependency Injection standard and then work toward using JSR-330, via Guice, in the core of Hudson itself. We have opened the <a href="https://github.com/sonatype/hudson-jsr330">Github repository containing the Hudson JSR-330 work</a> which also contains a small example of a [Hudson Plugin written using JSR-330].</p>

<p>We&#8217;ve been using this new Hudson JSR-330 mechanism to create our JAX-RS (RESTful Web) integration, and our Maven 3.x integration. We will be working as fast as we can to propose and deliver our JSR-330 integration, JAX-RS integration, and our Maven 3.x integration back to the Hudson Community. Stuart McCulloch, who has been working on our Guice strategy, will be doing this work and following up with proposals on the Hudson Development list. Jeanfrancois Arcand is working on a REST framework, based on Jersey, that we will be proposing for integration into Hudson which is how we see webservices working in Hudson going forward.</p>

<p>Our GWT-based UI for our Maven integration relies upon these REST services, and our GWT-based Maven 3.x builder also uses JAX-RS as a communication channel back to the server.  While it is unlikely that we will be able to stop using Jelly completely in the near future, this work will set the stage for a Hudson that is based entirely on RESTful web services.    This isn&#8217;t easy work, we&#8217;re trying to set the foundation for future UI while maintaining compatibility for plugins that continue to use Jelly.   Jeanfrancois is also looking at the integration of Atmosphere to make the standard pages in Hudson more efficient with respect to communication to the server.</p>

<p>So this is just the start of what we said we would deliver. More pieces will be released as they are ready.   If you are interested, please take a look at the JSR-330 integration work we&#8217;ve done and look forward to more posts about JSR330 next week!   If you have any feedback, please provide your comments or email questions.  We&#8217;re excited about the work that has been done and the work yet to come.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2011/02/guicing-up-hudson-making-life-easier-for-developers-with-jsr-330/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Next Generation Maven Development Stack @ JFokus</title>
		<link>http://blog.sonatype.com/people/2010/01/next-generation-maven-development-stack/</link>
		<comments>http://blog.sonatype.com/people/2010/01/next-generation-maven-development-stack/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 05:21:05 +0000</pubDate>
		<dc:creator>Jason van Zyl</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[m2eclipse]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[GIT]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[Hudson]]></category>
		<category><![CDATA[jfokus]]></category>
		<category><![CDATA[peaberry]]></category>
		<category><![CDATA[polyglot]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=4113</guid>
		<description><![CDATA[For my talk today at JFokus today I&#8217;ve taken the liberty of starting some notes for folks interested in attending. There&#8217;s a lot to cover and so I thought I would try the approach of providing some material up front so the session can be more of a dialog. I&#8217;m going to attempt to cover [...]]]></description>
				<content:encoded><![CDATA[<p>For my talk today at JFokus today I&#8217;ve taken the liberty of starting some notes for folks interested in attending. There&#8217;s a lot to cover and so I thought I would try the approach of providing some material up front so the session can be more of a dialog. I&#8217;m going to attempt to cover everything in the picture below and save the demos folks might want to see for the Sonatype booth. Happy to chat with folks and do any demos before and after the presentation. Just stop by!</p>

<p><img src="http://www.sonatype.com/people/wp-content/uploads/2010/01/Stack1.png" alt="Stack.png" border="0" /></p>

<h3>Maven Stack Infrastructure</h3>

<p>I&#8217;m going to talk about some of the under pinnings of the technologies we&#8217;re using as part of our Maven work. Why we selected the technologes and some of the current work that&#8217;s happening.
<span id="more-4113"></span></p>

<ul>
<li><a href="http://code.google.com/p/google-guice/">Guice</a></li>
<li><a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/">From Plexus to Guice (#1): Why Guice?</a></li>
<li><a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-2-the-guiceplexus-bridge-and-custom-bean-injection/">From Plexus to Guice (#2): The Guice/Plexus Bridge and Custom Bean Injection</a></li>
<li><a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/">From Plexus to Guice (#3): Creating a Guice Bean Extension Layer</a></li>
<li><a href="http://code.google.com/p/peaberry/">Peaberry</a></li>
<li><a href="http://incubator.apache.org/shiro/">Apache Shiro</a></li>
<li><a href="http://enunciate.codehaus.org/">Enunciate</a></li>
</ul>

<h3>Maven</h3>

<p>There are several develops going on in the various Maven projects. Maven 3.x is on the way, but we have some very interesting work happing with OSGi within our Tycho project. The Flexmojos project and NAR (the C/++ framework for Maven) are also popular.</p>

<h4>Tycho</h4>

<ul>
<li><a href="http://tycho.sonatype.org/">Tycho Site</a></li>
<li><a href="http://github.com/sonatype/sonatype-tycho">Tycho @ Github</a></li>
<li><a href="http://github.com/sonatype/m2eclipse-tycho">Tycho/PDE Integration</a></li>
<li><a href="http://wiki.eclipse.org/Equinox_p2">P2 Site</a></li>
</ul>

<h4>Flexmojos</h4>

<ul>
<li><a href="http://flexmojos.sonatype.org/">Flexmojos Site</a></li>
</ul>

<h4>NAR</h4>

<ul>
<li><a href="http://duns.github.com/maven-nar-plugin/">NAR Site</a></li>
<li><a href="http://github.com/sonatype/maven-nar-plugin">NAR @ Github</a></li>
</ul>

<h3>M2Eclipse</h3>

<p>The primary IDE integration we work on at Sonatype. The most important thing to talk about in M2Eclipse is the configuration framework.</p>

<ul>
<li><a href="http://m2eclipse.sonatype.org/">M2Eclipse Site</a></li>
</ul>

<h3>Hudson</h3>

<p>Most of the work we&#8217;ve been doing on Hudson is still not really good enough for public consumption but we&#8217;re testing the changes we&#8217;re making on the Sonatype grid.</p>

<p><a href="https://hudson.dev.java.net/">Hudson Site</a></p>

<h3>Nexus</h3>

<ul>
<li><a href="http://nexus.sonatype.org">Nexus Site</a></li>
<li><a href="http://www.sonatype.com/people/2010/01/nexus-oss-ecosystem/">Nexus: Improving Maven Central and Supporting the Maven Ecosystem</a></li>
<li><a href="http://www.sonatype.com/people/2010/01/10-questions/">Selecting OSS Software: 10 Questions Answered for Sonatype Nexus</a></li>
<li><a href="http://nexus.sonatype.org/oss-repository-hosting.html">OSS Repository Hosting</a></li>
<li><a href="http://bundles.sonatype.org/index.html#welcome">OSGi Central</a></li>
<li>Ruby Central (We&#8217;re working on making that publicly available</li>
</ul>

<h3>Proviso</h3>

<p>Proviso is not publicly released yet, but Alin and I have been working together to get our provisioning framework ready for a public release.</p>

<h3>Git</h3>

<p>We are starting to use Git heavily and soon likely exclusively. We are helping out on the EGit, and JGit projects at Eclipse and we&#8217;re trying to put a little Git server together based on JGit, MINA, and Apache Shiro.</p>

<ul>
<li><a href="http://www.eclipse.org/jgit/">JGit Site</a></li>
<li><a href="http://www.eclipse.org/egit/">EGit Site</a></li>
<li><a href="http://aniszczyk.org/2010/01/25/egit-and-jgit-builds-available/">Tycho building EGit/JGit</a></li>
<li><a href="http://github.com/sonatype/sshjgit">SShJGit @ Github</a></li>
</ul>

<h3>Maven Extensions</h3>

<p>I&#8217;ll chat about these in the talk.</p>

<ul>
<li><a href="http://polyglot.sonatype.org/">Polyglot Maven Site</a></li>
<li><a href="http://github.com/sonatype/polyglot-maven">Polyglot Maven @ Github</a></li>
<li><a href="http://mvnsh.sonatype.org">Maven Shell</a></li>
<li><a href="https://github.com/sonatype/mvnsh">Maven Shell @ Github</a></li>
</ul>

<p>I&#8217;ll likely keep adding to the entry leading up the talk, but I thought I would get the ball rolling!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2010/01/next-generation-maven-development-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From Plexus to Guice (#3): Creating a Guice Bean Extension Layer</title>
		<link>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/</link>
		<comments>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 09:00:53 +0000</pubDate>
		<dc:creator>Stuart McCulloch</dc:creator>
				<category><![CDATA[Maven]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[plexus]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=3981</guid>
		<description><![CDATA[In the first two articles in this series you learned why Sonatype has decided to move our development efforts from Plexus to Guice and you&#8217;ve also been introduced to Sonatype&#8217;s efforts to create a Guice/Plexus integration library to make the migration from Plexus to Guice seamless.   In this post, I&#8217;m going to dive into [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png"><img class="alignright size-full wp-image-4024" title="from-plexus-to-guice" src="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png" alt="" width="250" height="62" /></a>In the first two articles in this series you learned why<a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/"> Sonatype has decided to move our development efforts from Plexus to Guice</a> and you&#8217;ve also been introduced to Sonatype&#8217;s efforts to create a <a href="http://www.sonatype.com/people/2010/01/from-plexus-to-guice-2-the-guiceplexus-bridge-and-custom-bean-injection/">Guice/Plexus integration library</a> to make the migration from Plexus to Guice seamless.   In this post, I&#8217;m going to dive into the details of bean injection in Guice, and I&#8217;m going to demonstrate how you can use the general-purpose bean injection layer in the Guice/Plexus integation library to intialize a bean.  This post is highly technical and is meant for developers interested in the details of our attempts to bridge Plexus and Guice.<span id="more-3981"></span></p>

<p><strong>Guice-Bean Extension Layer</strong></p>

<p>Our goal is to create a general-purpose bean injection layer on top of the raw <a title="2. Customizing Guice Injection" href="https://docs.sonatype.com/display/OSGI/2.+Customizing+Guice+Injection">custom injection</a> API provided by Guice. This layer will take care of registering the necessary <tt>MembersInjectors</tt> as well as scanning class hierarchies for potential bean properties. All the client needs to do is decide whether or not to supply a binding for a given property.  The primary class in this layer is <tt>BeanListener</tt>:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.BeanListener</strong></div>
<div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BeanListener
    <span style="color: #000000; font-weight: bold;">implements</span> TypeListener
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> BeanListener<span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">final</span> BeanBinder beanBinder <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...etc...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



</div>
</div>

<p>This is a <tt>TypeListener</tt> implementation that you can register with Guice to provide bean injection for any type. It accepts a single argument of type <tt>BeanBinder</tt>, which the client is expected to implement:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.BeanBinder</strong></div>
<div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> BeanBinder
<span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> PropertyBinder bindBean<span style="color: #009900;">&#40;</span> TypeLiteral<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> type, TypeEncounter<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>B<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> encounter <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



</div>
</div>

<p>The single <tt>bindBean</tt> method should supply a <tt>PropertyBinder</tt> based on the type of bean being injected. This means clients can customize or cache information on a per-type basis, or simply provide the same <tt>PropertyBinder</tt> implementation for all bean types. Returning <tt>null</tt> will disable bean injection for that type.  The <tt>PropertyBinder</tt> interface does the same job as <tt>BeanBinder</tt>, but at the property level:</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.PropertyBinder</strong></div>
<div>
<pre>public interface PropertyBinder
{
    &lt;T&gt; PropertyBinding bindProperty( BeanProperty&lt;T&gt; property );
}</pre>
</div>
</div>

<p>This is the point where clients decide whether or not to bind a particular property. They can either return an implementation of <tt>PropertyBinding</tt> which should inject the property, or return <tt>null</tt> to ignore this property. This process continues until all the bean properties have been scanned &#8211; unless the client returns the <tt>LAST_BINDING</tt> constant from the <tt>PropertyBinder</tt> API, which will immediately stop the scanning for the current bean. <tt>LAST_BINDING</tt> is supposed to be used by binders who know they can&#8217;t supply any more bindings, because then there&#8217;s no point in continuing the scan.  So what does a <tt>PropertyBinding</tt> look like?</p>

<div>
<div><strong>org.sonatype.guice.bean.inject.PropertyBinding</strong></div>
<div>
<pre>public interface PropertyBinding
{
    &lt;B&gt; void injectProperty( B bean );
}</pre>
</div>
</div>

<p>Well, not much as you can see! It has a single method <tt>injectProperty</tt> which is the trigger for injecting a value into the appropriate property of the given bean. How this value is created and how it is injected into the bean is up to the client, but most clients will use the <tt>BeanProperty</tt> supplied in the original <tt>bindProperty</tt> method to do the actual injection:</p>

<div>
<div><strong>org.sonatype.guice.bean.reflect.BeanProperty</strong></div>
<div>
<pre>public interface BeanProperty&lt;T&gt;
{
    &lt;A extends Annotation&gt; A getAnnotation( Class&lt;A&gt; annotationType );

    TypeLiteral&lt;T&gt; getType();

    String getName();

    &lt;B&gt; void set( B bean, T value );
}</pre>
</div>
</div>

<p>Specifically the <tt>set</tt> method lets you inject a value into this property for a given bean instance. Notice how you can also query the generic type, name, as well as any annotations attached to the bean property. You can use any or all of this information to determine what value to inject into the property: for example by querying XML metadata, runtime annotations, maybe even pulling values out of a database.</p>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>Property names are normalized &#8211; a setter method called &#8220;setValue&#8221; has a property name of &#8220;value&#8221;</td>
</tr>
</tbody>
</table>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>The <tt>BeanProperty</tt> currently doesn&#8217;t tell you whether it&#8217;s backed by a field or setter method. This is because we don&#8217;t need this information in our use-case and don&#8217;t believe others will, but some sort of query method could be added in the future if necessary.</td>
</tr>
</tbody>
</table>
</div>

<p>Enough talk, let&#8217;s actually try and use this extension layer in a small example!</p>

<h3>Example</h3>

<p>We begin by creating an example Maven project:</p>

<div>
<div>
<pre>mvn archetype:create "-DgroupId=example.bean.guice" "-DartifactId=guice-bean-example"

cd guice-bean-example/</pre>
</div>
</div>

<p>Remove the example App files, as we&#8217;ll be adding our own files soon:</p>

<div>
<div>
<pre>rm src/main/java/example/bean/guice/App.java
rm src/test/java/example/bean/guice/AppTest.java</pre>
</div>
</div>

<p>Now edit the POM, first we need to add a dependency to the <tt>guice-bean-inject</tt> library:</p>

<div>
<div><strong>pom.xml</strong></div>
<div>
<pre>  &lt;dependencies&gt;
    &lt;!-- ... --&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.sonatype.spice.inject&lt;/groupId&gt;
      &lt;artifactId&gt;guice-bean-inject&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;!-- ... --&gt;
  &lt;/dependencies&gt;</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>This will also pull in the <tt>guice-bean-reflect</tt> and <tt>guice-patches</tt> libraries</td>
</tr>
</tbody>
</table>
</div>

<p>We also need to tell Maven to compile at the Java5 level, because this is the minimum required by Guice:</p>

<div>
<div><strong>pom.xml</strong></div>
<div>
<pre>  &lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
        &lt;configuration&gt;
          &lt;source&gt;1.5&lt;/source&gt;
          &lt;target&gt;1.5&lt;/target&gt;
        &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
  &lt;/build&gt;</pre>
</div>
</div>

<p>We&#8217;re now ready to add our example code, let&#8217;s start with a very simple &#8220;do nothing&#8221; binder:</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B&gt; PropertyBinder bindBean( TypeLiteral&lt;B&gt; type, TypeEncounter&lt;B&gt; encounter )
  {
    return null;
  }
}</pre>
</div>
</div>

<p>Well that&#8217;s fairly useless! We should at least return some property binders which in turn return some property bindings, otherwise nothing will be injected. To save on typing we&#8217;ll put our injection code in a couple of nested anonymous classes, but we could just as well have created named classes. Let&#8217;s also use the bean property to do the actual injection (for the moment we will just inject <tt>null</tt>).</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B&gt; PropertyBinder bindBean( TypeLiteral&lt;B&gt; type, TypeEncounter&lt;B&gt; encounter )
  {
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            property.set( bean, null );
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>We made the <tt>property</tt> parameter <tt>final</tt> so we could use it inside the anonymous class</td>
</tr>
</tbody>
</table>
</div>

<p>Next we need to provide actual values for our bean properties. We&#8217;re free to get these values from anywhere we want, but to keep this example simple we&#8217;ll just get them from the Guice injector. So how do we access the injector while we&#8217;re still configuring bindings? Well the <tt>TypeEncounter</tt> has methods to look up the <tt>Provider</tt> for a given binding <tt>Key</tt>. But be careful &#8211; we can&#8217;t use these providers during the property binding call because the injector is still being initialized. We can only use them once the injection phase starts, such as in the <tt>injectProperty</tt> method.</p>

<p>Our example bean binder takes the simple name of the bean class, appends the property name, and turns it into a JSR 330 binding annotation like <tt>@Named("PersonBean.name")</tt>. <em>In practice you&#8217;ll also want to include the package name to avoid collisions.</em> The appropriate value provider is supplied by the <tt>TypeEncounter</tt> using a key based on the binding annotation and property type. We store the provider in a final variable so we can access it later on when we need to inject the property value into a bean.</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import javax.inject.Named;
import javax.inject.Provider;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.util.Jsr330;

public class SimpleBeanBinder
  implements BeanBinder
{
  public &lt;B1&gt; PropertyBinder bindBean( final TypeLiteral&lt;B1&gt; type, final TypeEncounter&lt;B1&gt; encounter )
  {
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        // simple mapping id, not guaranteed to be unique as we don't include the package namespace
        Named valueId = Jsr330.named( type.getRawType().getSimpleName() + "." + property.getName() );
        final Provider&lt;T&gt; valueProvider = encounter.getProvider( Key.get( property.getType(), valueId ) );

        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            property.set( bean, valueProvider.get());
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<p>We now have a working bean binder that maps bean properties to JSR 330 named bindings. To finish things off, let&#8217;s add a bit of logging so we can see what the binder is doing during the test:</p>

<div>
<div><strong>src/main/java/example/bean/guice/SimpleBeanBinder.java</strong></div>
<div>
<pre>package example.bean.guice;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Named;
import javax.inject.Provider;

import org.sonatype.guice.bean.inject.BeanBinder;
import org.sonatype.guice.bean.inject.PropertyBinder;
import org.sonatype.guice.bean.inject.PropertyBinding;
import org.sonatype.guice.bean.reflect.BeanProperty;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.util.Jsr330;

public class SimpleBeanBinder
  implements BeanBinder
{
  private final Logger LOGGER = Logger.getLogger( SimpleBeanBinder.class.getName() );

  void log( String message )
  {
    // blank method+class to remove clutter <img src='http://blog.sonatype.com/people/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> 
    LOGGER.logp( Level.INFO, "", "", message );
  }

  public &lt;B1&gt; PropertyBinder bindBean( final TypeLiteral&lt;B1&gt; type, final TypeEncounter&lt;B1&gt; encounter )
  {
    log( "Binding bean &lt;" + type + "&gt;" );
    return new PropertyBinder()
    {
      public &lt;T&gt; PropertyBinding bindProperty( final BeanProperty&lt;T&gt; property )
      {
        log( "Binding property [" + property.getName() + "]" );

        // simple mapping id, not guaranteed to be unique as we don't include the package namespace
        Named valueId = Jsr330.named( type.getRawType().getSimpleName() + "." + property.getName() );
        final Provider&lt;T&gt; valueProvider = encounter.getProvider( Key.get( property.getType(), valueId ) );

        return new PropertyBinding()
        {
          public &lt;C&gt; void injectProperty( C bean )
          {
            T value = valueProvider.get();
            log( "Injecting property [" + property.getName() + "] with \"" + value + "\"" );
            property.set( bean, value );
          }
        };
      }
    };
  }
}</pre>
</div>
</div>

<p>OK so we have our simple bean binder, now we need a bean to test it with. How about the following:</p>

<div>
<div><strong>src/main/java/example/bean/guice/PersonBean.java</strong></div>
<div>
<pre>package example.bean.guice;

public class PersonBean
{
  private int age;

  private String name;

  public void setName( String name )
  {
    this.name = name.toUpperCase();
  }

  public String getName()
  {
    return name;
  }

  public int getAge()
  {
    return age;
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/forbidden.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>This is a trivial bean that mixes field and setter injection, it is definitely not meant to represent good coding practice!</td>
</tr>
</tbody>
</table>
</div>

<p>Finally we need to write a test that exercises our bean binder. The following jUnit test bootstraps a Guice injector that uses our binder along with a couple of constant bindings for each of the bean properties. Guice will handle converting the constant strings to the right types. The injector is used to inject the example bean instance into the test, where its properties are checked against the expected values.</p>

<div>
<div><strong>src/test/java/example/bean/guice/PersonBeanTest.java</strong></div>
<div>
<pre>package example.bean.guice;

import javax.inject.Inject;

import junit.framework.TestCase;

import org.sonatype.guice.bean.inject.BeanListener;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.util.Jsr330;

public class PersonBeanTest
  extends TestCase
{
  @Inject
  PersonBean personBean;

  @Override
  protected void setUp()
  {
    Guice.createInjector( new AbstractModule()
    {
      @Override
      protected void configure()
      {
        bindListener( Matchers.only( TypeLiteral.get( PersonBean.class ) ),
                      new BeanListener( new SimpleBeanBinder() ) );

        bindConstant().annotatedWith( Jsr330.named( "PersonBean.name" ) ).to( "John Smith" );
        bindConstant().annotatedWith( Jsr330.named( "PersonBean.age" ) ).to( "42" );
      }
    } ).injectMembers( this );
  }

  public void testNameBean()
  {
    // check the name was capitalized by the setter
    assertEquals( "JOHN SMITH", personBean.getName() );
    assertEquals( 42, personBean.getAge() );
  }
}</pre>
</div>
</div>

<div>
<table><colgroup> <col width="24"></col> <col></col> </colgroup>
<tbody>
<tr>
<td valign="top"><img src="https://docs.sonatype.com/images/icons/emoticons/information.gif" border="0" alt="" width="16" height="16" align="absmiddle" /></td>
<td>If you are doing a lot of Guice testing, you should take a look at the <a rel="nofollow" href="http://code.google.com/p/atunit/">atunit<sup><img src="https://docs.sonatype.com/images/icons/linkext7.gif" border="0" alt="" width="7" height="7" align="absmiddle" /></sup></a> library</td>
</tr>
</tbody>
</table>
</div>

<p>We&#8217;re now ready to test our example bean binder! Type the following command to kick things off:</p>

<div>
<div>
<pre>mvn clean install</pre>
</div>
</div>

<p>You should see the usual Maven output followed by a successful test, which should look something like this:</p>

<div>
<div>
<pre>-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running example.bean.guice.PersonBeanTest
Jan 8, 2010 12:27:07 AM
INFO: Binding bean &lt;example.bean.guice.PersonBean&gt;
Jan 8, 2010 12:27:08 AM
INFO: Binding property [name]
Jan 8, 2010 12:27:08 AM
INFO: Binding property [age]
Jan 8, 2010 12:27:08 AM
INFO: Injecting property [age] with "42"
Jan 8, 2010 12:27:08 AM
INFO: Injecting property [name] with "John Smith"
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.785 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0</pre>
</div>
</div>

<p>Congratulations, you&#8217;ve just used Guice to initialize a plain old bean with no annotations at all!</p>

<h3>Next: Reflection</h3>

<p>Interested in how the <tt>BeanListener</tt> finds bean properties? We&#8217;ll take a closer look at this in the next post in this series which will cover Generic Bean reflection.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-3-creating-a-guice-bean-extension-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From Plexus to Guice (#1): Why Guice?</title>
		<link>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/</link>
		<comments>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/#comments</comments>
		<pubDate>Tue, 19 Jan 2010 19:00:55 +0000</pubDate>
		<dc:creator>Jason van Zyl</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Nexus]]></category>
		<category><![CDATA[peaberry]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=3970</guid>
		<description><![CDATA[When we started the Maven project, dependency injection was still developing. Spring was just starting out and the Avalon project at Apache was really the only IoC framework around. While the concept seems second-nature by 2010, in 2002, it wasn&#8217;t a primary focus of the initial efforts of the Maven community but it was something [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png"><img class="alignright size-full wp-image-4024" title="from-plexus-to-guice" src="http://www.sonatype.com/people/wp-content/uploads/2010/01/from-plexus-to-guice1.png" alt="" width="250" height="62" /></a>When we started the Maven project, dependency injection was still developing. Spring was just starting out and the Avalon project at Apache was really the only IoC framework around. While the concept seems second-nature by 2010, in 2002, it wasn&#8217;t a primary focus of the initial efforts of the Maven community but it was something I felt had to be in place for the development of Maven 2. We knew we needed some sort of component framework, some standard mechanism to instantiate plugins and configure them based on a set of configuration points, and, at the time, Plexus filled the gap. Plexus was exactly what we needed because it evolved with the requirements of Maven, and I think that Plexus served us well for the past few years but it&#8217;s time to let go. I never felt compelled to switch until <a href="http://code.google.com/p/google-guice/wiki/Guice20">Guice 2.0</a>.   Guice has the capabilities and adaptability we require in Maven.</p>

<p>For all new development, we&#8217;ve decided to focus on <a href="http://code.google.com/p/google-guice/">Guice</a> and build a compatibility layer for existing components. In this post, I&#8217;m going to discuss the various factors that went into the decision to move to Guice. All of Sonatype&#8217;s product are currently developed using Guice and the Guice/Plexus integration libraries that Stuart will describe in the articles that will follow over the next few weeks, and future work on Maven 3 will be based on Guice.<span id="more-3970"></span></p>

<h3>Why Guice?</h3>

<p>We ultimately went with Guice because it provided a system that could be configured and customized at runtime. It was very close to the model that Plexus was using, and we felt that it would be easy for us to build a compatibility layer between the two. The primary motiviation in this decision was our ability to write that compatibility layer atop whatever framework we selected that would allow for backward-compatibility. You see, it isn&#8217;t good enough in a community this large to just move to a new framework and expect everyone to just scramble to rewrite Maven plugins. We had to find a way to support existing plugins, and existing extensions. We had to find a way to work with all of the existing code that was coded to Plexus and adapt that to a new engine.</p>

<p>Guice is highly adaptable. With Guice we don&#8217;t need to configure all of our components ahead of time, we can instantiate them as needed and modify the &#8220;context&#8221; with a Guice/Plexus integration layer which Stuart will describe in the following articles. The answer to &#8220;why Guice?&#8221; is explained in the code that was required to write this compatibility layer. The only way we found to make this work was by using Guice.     Other reasons included <a href="http://java.dzone.com/news/dependency-injection-and-type-">type safety</a>, <a href="http://code.google.com/p/garbagecollected/wiki/SpringAndGuiceErrors">easy-to-read error messages</a>, and <a href="http://code.google.com/p/peaberry/">OSGi support using peaberry</a>.    All of our development uses the <a href="http://code.google.com/p/atinject/">JSR330 standard @Inject anotations</a>.   We&#8217;re using JSR330 instead of Guice-specific annotations to make sure that our work is more standard and portable going forward.</p>

<h3>Focusing the Community</h3>

<p>Aside from the technical reasons to move to Guice, there are reasons that involve the community. If our audacious goal is to provide world-class build and development infrastructure support, we need to remove unnecessary distractions from our environment. We just were not focused on Plexus, and issues would come up in Plexus that needed to be supported which distracted from the core effort. We&#8217;re not just <em>using</em> Guice. Because we are starting to rely on it, we appreciate the need to work closely with a project like Guice and contribute back to that community.  Stuart is a committer, and we wanted to collaborate with others and use a framework used widely by others.</p>

<p>Maven&#8217;s goal was never to create a dependency injection framework, it was always focused on builds and the infrastructure that supports development. Not long ago, we were contemplating what it would take to improve Plexus, we looked at the effort required to document and support Plexus going forward, and it just didn&#8217;t make any sense for us to continue to apply precious time and effort to a problem that had already been solved by several others. It makes sense to focus the community, and moving to Guice will allow us to devote more of our own resources to supporting the central projects and products.</p>

<h3>Making it Easier to Extend</h3>

<p>When your project or production uses a standard tool, you increase the audience that can customize it. If the goal of Maven is to create a solid foundation of infrastructure for others to build upon, it only makes sense to adopt something more standard. Maven and products like Nexus should be judged by the number and variety of plugins that have been developed for them, and this is something that attract most new users to Maven (the abundance and variety of Maven plugins). Anything we can do to make it easier to extend these systems is the right thing to do for the &#8220;ecosystem&#8221; I described last week.</p>

<p>If you wanted to write a plugin for the initial version of Nexus two years ago, you would have had to know a lot about Plexus. There was nothing particularly wrong with Plexus, it had a well-defined interface and it served its purpose, but there wasn&#8217;t very much documentation, and requiring some knowledge of Plexus for plugin developers was reducing the size of potential developers. In the past year, we&#8217;ve made a course correction, and you can write a Nexus plugin without having to learn any dependency injection framework. What has made this transition possible is the flexibility of Guice.</p>

<h3>Summary</h3>

<p>Stuart&#8217;s going to dive into the technical details in the next five parts of this series. If you are a developer interested in the implementation of Plexus and Guice, you&#8217;ll find these posts to be a good starting point for learning about we&#8217;ve implemented this compatibility layer.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2010/01/from-plexus-to-guice-1-why-guice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
