<?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>spiros.blog() &#187; EJB</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/ejb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Sun, 22 Aug 2010 20:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Dependency Injection in Struts2 for stateless EJBs part 2</title>
		<link>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/</link>
		<comments>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/#comments</comments>
		<pubDate>Tue, 03 Jul 2007 10:10:12 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/</guid>
		<description><![CDATA[This is the second, and last part of the Using Dependency Injection in Struts2 for stateless EJBs series of posts. In this post I will present a utility class that can be used to make the creation of Guice bindings for EJB3s easier. Introducing EjbBinder Our goal is to make an easier API, specifically for [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second, and last part of the <strong>Using Dependency Injection in Struts2 for stateless EJBs</strong> series of posts. In this post I will present a utility class that can be used to make the creation of Guice bindings for EJB3s easier.</p>
<h3>Introducing <code>EjbBinder</code></h3>
<p>Our goal is to make an easier API, specifically for creating bindings of EJB3s, and make the specification of the JNDI name optional, when possible. The below code is a new version of the <code>EjbModule</code> class that uses a new class, called <code>EjbBinder</code>, that we are going to present in this post.</p>

<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> EjbModule <span style="color: #000000; font-weight: bold;">implements</span> Module <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span>Binder binder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Bind Context to the default InitialContext.</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">InitialContext</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        EjbBinder ejbs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EjbBinder<span style="color: #009900;">&#40;</span>binder, <span style="color: #000000; font-weight: bold;">new</span> GlassfishEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	ejbs.<span style="color: #006633;">bindRemote</span><span style="color: #009900;">&#40;</span>CurrencyManagerRemote.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ejbs.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>EjbBinder</code> contains two methods named <code>bind</code> and <code>bindRemote</code>. The <code>bind</code> method receives as arguments the expected class and JNDI name and simply uses <code>Binder</code> and <code>JndiItegration</code> to create a binding. The <code>bindRemote</code> method takes as an argument the remote interface of a sesion EJB3 and creates a binding using a JNDI name retrieved from the <code>EjbJndiNameStrategy</code>, that is specified in the <code>EjbBinder</code> constructor (BTW the <code>CurrencyManagerRemote</code> EJB interface in the above code does not relate with the application that we developed in the previous post and it is here to illustrate the <code>bindRemote</code> method).</p>
<p>The definition of the <code>EjbJndiNameStrategy </code>interface:</p>

<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> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The implementation of <code>EjbBinder</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Binder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.binder.ScopedBindingBuilder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">inject</span>.<span style="color: #006633;">jndi</span>.<span style="color: #006633;">JndiIntegration</span>.<span style="color: #006633;">fromJndi</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EjbBinder <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Binder binder<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> EjbJndiNameStrategy jndiNameStrategy<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> EjbBinder<span style="color: #009900;">&#40;</span>Binder binder, EjbJndiNameStrategy nameStrategy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">binder</span> <span style="color: #339933;">=</span> binder<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jndiNameStrategy</span> <span style="color: #339933;">=</span> nameStrategy<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> ScopedBindingBuilder bindRemote<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> bind<span style="color: #009900;">&#40;</span>beanInterface,
                jndiNameStrategy.<span style="color: #006633;">interfaceToJndiName</span><span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> ScopedBindingBuilder bind<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> beanInterface, <span style="color: #003399;">String</span> jndiName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">toProvider</span><span style="color: #009900;">&#40;</span>fromJndi<span style="color: #009900;">&#40;</span>beanInterface, jndiName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Application Server Specific Naming Strategies</h3>
<p>When you create a new EJB and you do not specify a JNDI name, the application server assigns a default name for you. So, if you have an implementation of <code>EjbJndiNameStrategy</code> that uses the naming rules of your application server you could avoid the need to specify a JNDI name when creating the EJB and when creating the binding of the EJB to Guice.</p>
<p>Below we have two implementations of the <code>EjbJndiNameStrategy</code> that can infer the JNDI name of an EJB&#8217;s remote client interface using the remote interface&#8217;s class for the Glassfish and JBoss application servers.</p>
<p>For the Glassfish application server the default global JNDI name of an EJB with a remote interface is the <a href="https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment">fully qualified name of the remote interface</a>.</p>

<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> GlassfishEjbJndiNameStrategy <span style="color: #000000; font-weight: bold;">implements</span> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> beanInterface.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For the JBoss application server the default JNDI name for the remote interface of an EJB is <em>earName/beanName/remote</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//WARNING: Not tested!</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JbossEjbJndiNameStrategy <span style="color: #000000; font-weight: bold;">implements</span> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> earName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JbossEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JbossEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> earName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>earName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">&quot;&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>earName.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">earName</span> <span style="color: #339933;">=</span> earName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> earName <span style="color: #339933;">+</span> interfaceToBeanName<span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/remote&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> interfaceToBeanName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> beanInterface.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">endsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remote&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            name <span style="color: #339933;">=</span> name.<span style="color: #006633;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remote&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;Bean&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Dependency Injection in Struts2 for stateless EJBs part 1</title>
		<link>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/</link>
		<comments>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/#comments</comments>
		<pubDate>Tue, 03 Jul 2007 09:38:10 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/</guid>
		<description><![CDATA[In this post I will present a way to use dependency injection of stateless session beans in Struts 2 actions using Guice. I will only write about EJB3, because they have simpler client lookup code, but you might be able to change the presented code to cover older versions of EJB. We will use off [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will present a way to use dependency injection of stateless session beans in Struts 2 actions using Guice. I will only write about EJB3, because they have simpler client lookup code, but you might be able to change the presented code to cover older versions of EJB.</p>
<p>We will use off the shelf Struts 2 and off the shelf Guice (with the Guice Struts 2 plug-in). No extra &#8220;glue&#8221; code is required to inject stateless EJBs into a Struts actions with Guice. In the next post I will tweak the presented code and make the binding of EJBs in Guice modules simpler.</p>
<p>This post is divided in two sections. In the first section we will create a simple, complete, hello-world type application using Struts 2. The application will consist of only one action that when called will print the current date in a JSP page. Then we will write a stateless session EJB and use Guice to inject the EJB into the action. If you are already familiar with Struts 2 you might want to skip the first section and go directly to the second.</p>
<h2>A basic Struts 2.x application</h2>
<p>Below you will find a series of steps with all code for our mini application.</p>
<h3>1. Project setup</h3>
<p>With your favorite IDE create an enterprise project. You need to create an enterprise project (ear based deployment) and not a web project (war based deployment) because we will create an EJB in the next section.</p>
<p>Now add a web module into your enterprise project and add the following jars to <em>WEB-INF/lib</em>: commons-logging-1.1.jar, freemarker-2.3.8.jar, ognl-2.6.11.jar, struts2-core-2.0.8.jar and xwork-2.0.3.jar.</p>
<p>Although I am a happy Eclipse user I have to admit that Eclipse has very basic support for Java EE 5, so for the code of this post I used Netbeans 6.0 M9 with Glassfish 2 Beta as the application server. Netbeans IMHO is not so good for Java editing but has decent support for the latest Java EE standards. Please keep in mind that the code is only tested with Glassfish 2 Beta.</p>
<h3>2. Edit web.xml</h3>
<p>The below code is the minimum web.xml needed for a Struts 2 application. It contains the Struts 2 filter definition (<code>FilterDispatcher</code>) and a welcome file (<em>index.html</em>) that it is used to redirect the user to the default Struts 2 action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.struts2.dispatcher.FilterDispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>index.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>3. Create struts.xml</h3>
<p>Now we need to create the struts.xml file. This file is located at the root of your classpath. The below code has some basic configuration parameters and includes the <em>dates</em> Struts package that will contain our action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts PUBLIC</span>
<span style="color: #00bbdd;">    &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.enable.DynamicMethodInvocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.devMode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Include the package with our actions --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;dates.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>4. Create the dates Struts package</h3>
<p>Create the dates.xml file at the root of your classpath as we specified in the above struts.xml. In this file we define a package named <em>dates</em> and mapped under <em>&lt;context-root&gt;/dates/*</em> URL.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts PUBLIC</span>
<span style="color: #00bbdd;">        &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">        &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;package</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dates&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;/dates&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;struts-default&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CurrentDate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.tzavellas.dates.web.CurrentDateAction&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/jsp/date.jsp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Add more actions here --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The package contains our only action, named <code>CurrentDate</code> and mapped to the URL <em>&lt;context-root&gt;/dates/CurrentDate.action</em>.</p>
<h3>5. Create the welcome file</h3>
<p>Now we will create our welcome file that redirects the user to the Struts action. This is a simple HTML page named index.html (as defined in web.xml) and located under the context root. The page uses a meta tag to redirect to the URL of the action .</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Refresh&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;0;URL=dates/CurrentDate.action&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Loading ...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>6. Code the action</h3>
<p>Finally it&#8217;s time for some Java code. The below code is a simple Struts 2 action that when called will set the value of the <code>currentDate</code> property to the current date.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.opensymphony.xwork2.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CurrentDateAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        currentDate = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> SUCCESS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> getCurrentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> currentDate<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>/code<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Note that unlike Servlets or Struts 1 actions, in Struts 2 for every HTTP request a new action instance is created so it is safe to mutate an action property (like the <code>currentDate</code> above) with request specific data.</p>
<h3>7. Create the view</h3>
<p>As we defined in the dates package (<em>dates.xml</em>) the view for the <code>CurrentDateAction</code> is a JSP file localed in <em>WEB-INF/jsp/date.jsp</em>. The below code, for the <em>date.jsp</em> page, uses the Struts 2 tag library to display the value of the <code>currentDate</code> property of the action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ page <span style="color: #000066;">contentType</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span> <span style="color: #000066;">pageEncoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;s&quot;</span> <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;/struts-tags&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The current date<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The current date is: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;currentDate&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We&#8217;ve just created a simple application with Struts 2. In the next section we are going to define a stateless session EJB3 and wire it with the above action using Guice.</p>
<p>For more information about the Struts 2 web framework visit the <a href="http://cwiki.apache.org/WW/home.html">Struts 2 wiki</a> pages.</p>
<h2>The EJB and the integration with Guice</h2>
<h3>8. Create the stateless session EJB3</h3>
<p>With your favorite IDE add an EJB module to the enterprise project we created in <em>step 1</em>.</p>
<p>Below we define the SLSB with a remote interface and one method that returns the current date. The bean is mapped to the global JNDI name <em>com.tzavellas.dates.ejb.DatesManagerBean</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.ejb</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Stateless</span><span style="color: #339933;">;</span>
&nbsp;
@Stateless<span style="color: #009900;">&#40;</span>mappedName=<span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DatesManagerBean <span style="color: #000000; font-weight: bold;">implements</span> DatesManagerRemote <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.ejb</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Remote</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399; font-weight: bold;">Remote</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> DatesManagerRemote <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>9. Add the Guice jars in the classpath</h3>
<p>Add guice-1.0.jar and guice-struts2-plugin-1.0.1.jar in <em>WEB-INF/lib</em> directory of your web module. The jar file guice-1.0.jar contains the core Guice implementation and  the guice-struts2-plugin-1.0.1.jar file contains the Guice&#8217;s Struts 2 integration code.</p>
<h3>10. Edit struts.xml to include the Guice plug-in</h3>
<p>Struts 2 is a very flexible framework. One of Struts&#8217; many extensions is the <code>ObjectFactory</code> plug-in interface. The responsibility of an  <code>ObjectFactory</code> is to create the objects managed by Struts. An <code>ObjectFactory</code> is usually a dependency injection container like Spring, Pico and Guice.</p>
<p>To define Guice as the <code>ObjectFactory</code> of our Struts 2 application we have to set the <em>struts.objectFactory</em> property to <em>guice</em> in the struts.xml file and Guice will then be responsible for instantiating all the Struts managed objects (actions, interceptors, etc) .</p>
<p>To continue add the following lines in struts.xml.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Use Guice as the ObjectFactory for this application --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.objectFactory&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;guice&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- When wiring actions use Guice bindings defined in the below module --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;guice.module&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.tzavellas.dates.web.EjbModule&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<h3>11. Code the EjbModule</h3>
<p>In the above struts.xml we defined two properties, <code>struts.objectFactory</code> and <code>guice.module</code>. To explain the <code>guice.module</code> property and the concept of Guice modules we have to explain the basics of Guice.</p>
<p>Guice is a, Java 5 based, dependency injection container that does constructor, field and setter injection (Guice also has features like support for AOP Alliance interceptors that we are not going to talk about in this post). To configure Guice you create a set of modules. A module implements the <code>com.google.inject.Module</code> interface and contains bindings. Bindings are created with the <code>com.google.inject.Binder</code> that is passed to the <code>configure(Binder)</code> method of a module. A binding is a pair composed of a type (key) and an implementation. An implementation can be an object, a class or a provider (factory). The bindings are consulted by the Guice <code>Injector</code> when doing dependency injection.</p>
<p>In the Guice Struts 2 plug-in the Guice <code>ObjectFactory</code> creates an <code>Injector</code> that then reads the module from the <code>guice.module</code> property in the <em>struts.xml</em> file and uses the bindings that are specified in that module to do dependency injection in Struts 2 actions and interceptors.</p>
<p>All we have to do to enable our EJB to be injected into an action is to create a binding for the client interface of the bean. We can&#8217;t create a binding to a class or instance, because the EJB&#8217;s instances must be instantiated by the EJB container and not by Guice, so we will use a provider that will lookup the EJB from JNDI. The Guice library includes a JNDI provider that we are going to use that it is located in the <code>com.google.inject.jndi.JndiIntegration</code> class.</p>
<p>The below code is the implementation of the <code>EJBModule</code>, which has  only two bindings. The first binds the <code>javax.naming.Context</code> type to the <code>javax.naming.InitialContext</code> implementation. This means that when the Guice <code>Injector</code> finds a field or parameter of type <code>javax.naming.Context</code>, that needs to be injected (marked with the <code>@Inject</code> annotation), it will create a new object of type <code>javax.naming.InitialContext</code> and inject it. This binding is needed by the JNDI provider that we are going to use in the next binding.</p>
<p>The second binding binds our EJB&#8217;s remote interface to the JNDI provider. The JNDI provider is returned from the call to the static <code>JndiIntegration.fromJndi(Class, String)</code> method. To get a JNDI provider we have to specify the expected class and JNDI name. This binding tells the Guice <code>Injector</code> that when it finds a field or parameter of type <code>com.tzavellas.dates.ejb.DatesManagerRemote</code>, that needs to be injected (marked with the <code>@Inject</code> annotation), it should call the provider&#8217;s <code>get()</code> method to retrieve an instance to inject.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Binder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Module</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">inject</span>.<span style="color: #006633;">jndi</span>.<span style="color: #006633;">JndiIntegration</span>.<span style="color: #006633;">fromJndi</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.tzavellas.dates.ejb.DatesManagerRemote</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.InitialContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EjbModule <span style="color: #000000; font-weight: bold;">implements</span> Module <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span>Binder binder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Bind Context to the default InitialContext.</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Context</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">InitialContext</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Bind the remote interface to the JNDI name using the JNDI Provider</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">toProvider</span><span style="color: #009900;">&#40;</span>fromJndi<span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For more information on how to do dependency injection with Guice see the <a href="http://docs.google.com/View?docid=dd2fhx4z_5df5hw8">Guice User&#8217;s Guide</a>.</p>
<h3>12. Change the action to use the EJB</h3>
<p>Now that Guice instantiates our actions we can use the normal Guice <code>@Inject</code> annotation to mark an injection point in the action class. We can use any of the constructor, setter and field injection mechanisms. The below code uses field injection because it might be more familiar to EJB3 developers.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Inject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.opensymphony.xwork2.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.tzavellas.dates.ejb.DatesManagerRemote</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CurrentDateAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Guice field injection of the SLSB</span>
    @Inject DatesManagerRemote bean<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        currentDate = bean.<span style="color: #006633;">currentDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> SUCCESS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> getCurrentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> currentDate<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Conclusion</h3>
<p>We are done. We&#8217;ve just coded a simple Struts 2 application that uses Guice to inject stateless session EJB3s to Struts actions. You can follow the instructions outlined above to do the same into your applications.</p>
<h3>Appendix: Quick summary of all the steps</h3>
<p>Assuming that you have a working Struts 2 application, below you will find a series of steps you could follow to do dependency injection of your stateless EJB3s into your Struts 2 actions.</p>
<p>For your project:</p>
<ol>
<li>Include the Guice jars in the <em>WEB-INF/lib</em> directory.</li>
<li>Define a Guice module that will contain the bindings for your EJBs.</li>
<li>Edit the struts.xml to define Guice as your <code>ObjectFactory</code> and include the above module.</li>
</ol>
<p>For every EJB that you want to inject into your Struts actions:</p>
<ol>
<li>Inside the module, define a binding for the EJB using the Guice <code>Provider</code> from <code>com.google.inject.jndi.JndiIntegration</code> class, by specifying the bean&#8217;s client interface and the global JNDI name of the bean (or an ejb-ref if you have one defined in web.xml).</li>
<li>Use the <code>@Inject</code> annotation in the Struts actions where you want the EJBs injected.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Delegate</title>
		<link>http://www.tzavellas.com/techblog/2005/10/24/dynamic-delegate/</link>
		<comments>http://www.tzavellas.com/techblog/2005/10/24/dynamic-delegate/#comments</comments>
		<pubDate>Sun, 23 Oct 2005 22:01:21 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EJB]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=29</guid>
		<description><![CDATA[Dynamic Delegate is my new pet project. Dynamic Delegate is a small library that allows you to create objects that implement the Business Delegate design pattern on the fly. You don&#8217;t have to code a Business Delegate. All you need to do, is make your EJBs implement the Business Interface design pattern and use DynamicDelegateFactory [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tzavellas.com/projects/dyndelegate">Dynamic Delegate</a> is my new pet project.</p>
<p><a href="http://www.tzavellas.com/projects/dyndelegate">Dynamic Delegate</a> is a small library that allows you to create objects that implement the <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html">Business Delegate</a> design pattern on the fly. You don&#8217;t have to code a <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html">Business Delegate</a>. All you need to do, is make your <a href="http://java.sun.com/products/ejb/">EJB</a>s implement the <a href="http://www.ibm.com/developerworks/java/library/j-ejbtip0820/">Business Interface</a> design pattern and use <a href="http://www.tzavellas.com/projects/dyndelegate/apidocs/com/tzavellas/dyndelegate/DynamicDelegateFactory.html"><code>DynamicDelegateFactory</code></a> to create a <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html">Business Delegate</a> that will implement the <a href="http://www.ibm.com/developerworks/java/library/j-ejbtip0820/">Business Interface</a> of the <a href="http://java.sun.com/products/ejb/">EJB</a>.</p>
<p>You can see an <a href="http://www.tzavellas.com/projects/dyndelegate/examples.html">example</a> of Dynamic Delegate <a href="http://www.tzavellas.com/projects/dyndelegate/examples.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/10/24/dynamic-delegate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Book Review: Core J2EE Design Patterns 2nd edition</title>
		<link>http://www.tzavellas.com/techblog/2005/10/13/book-review-core-j2ee-design-patterns-2nd-edition/</link>
		<comments>http://www.tzavellas.com/techblog/2005/10/13/book-review-core-j2ee-design-patterns-2nd-edition/#comments</comments>
		<pubDate>Thu, 13 Oct 2005 17:28:39 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[JavaEE]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=26</guid>
		<description><![CDATA[I read the Core J2EE Design Patterns 2nd edition in July 2005. Below I have written a review for this important book. Core J2EE Design Patterns is one of the most important books on the traditional J2EE application architecture. If you are programming J2EE systems that are heavily based on EJB 2.x then this book [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.tzavellas.com/techblog/techblog-files/CJP2ndEdCoverSmall.jpg" alt="Core J2EE Design Patterns"/></p>
<p>I read the <a href="http://www.corej2eepatterns.com/">Core J2EE Design Patterns 2nd edition</a> in July 2005. Below I have written a review for this important book.</p>
<p><a href="http://www.corej2eepatterns.com/">Core J2EE Design Patterns</a> is one of the most important books on the traditional <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> application architecture. If you are programming <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> systems that are heavily based on <a href="http://java.sun.com/products/ejb/">EJB 2.x</a> then this book is a must read for you. On the other hand if you are programming <a href="http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764558315.html">J2EE (without EJB <img src='http://www.tzavellas.com/techblog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</a> with more lightweight tools, like <a href="http://www.springframework.org/">Spring</a>, don&#8217;t rush to go without considering to read this book. This book, no matter which technology you use to implement enterprise applications provides good information on object oriented design and layering.</p>
<p>Of course, as many people have said before, a lot of the core J2EE design patterns are not really patterns but they just provide ways to overcome several limitations in some of the <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> technologies. Examples of “pseudo-patterns” include <a href="http://corej2eepatterns.com/Patterns2ndEd/CompositeEntity.htm">Composite Entity</a> and <a href="http://corej2eepatterns.com/Patterns2ndEd/TransferObject.htm">Data Transfer Object</a> which try to address some limitations of the EJB Entity Beans. I believe that the reader can also learn a lot from these “pseudo-patterns”. For example the <a href="http://corej2eepatterns.com/Patterns2ndEd/TransferObject.htm">Data Transfer Object</a> can be seen as a way to decouple two layers or as a more general pattern for distributed computing where you aggregate all the information that you want to pass between the nodes in transfer objects and not just as a way with acceptable performance to get multiple fields from an Entity Bean. For example if for some reason you have a physically separated business tier and you use a technology like <a href="http://hibernate.org/">Hibernate</a> for persistence you will probably need to have a remote facade that will return transfer objects with all the information that you need in the presentation tier in order to avoid any <a href="http://www.hibernate.org/hib_docs/api/net/sf/hibernate/LazyInitializationException.html">LazyInitializationException</a> exceptions. I find almost everything in the book useful and educative.</p>
<p>For me a book on design patterns is useful if it has lots of code to illustrate how the patterns can be implemented. The <a href="http://www.corej2eepatterns.com/">Core J2EE Design Patterns</a> not only provides implementations for all the patterns but also provides and analyzes various implementation strategies. You can see multiple ways to implements most of the patterns, learn the trade-offs of each implementation strategy and also get a deeper understanding of the various <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> technologies.</p>
<p>The UML diagrams in the book are very good and very helpful since they help you visualize the interactions in the design pattern and you better understand the responsibilities of the participant objects.</p>
<p>I&#8217;d like to note two things that are specific to the second edition. First the existence of <a href="http://corej2eepatterns.com/Patterns2ndEd/WebServiceBroker.htm">Web Service Broker</a>, a pattern on how to implement web services in <a href="http://java.sun.com/j2ee/1.4/index.jsp">J2EE 1.4</a>, tells us the important role of web services in the <a href="http://java.sun.com/j2ee/1.4/index.jsp">J2EE 1.4</a> update. Second the existence of <a href="http://corej2eepatterns.com/Patterns2ndEd/BusinessObject.htm">Business Object</a> tells us that the procedural style of EJB applications made J2EE programmers forget the basics of object oriented programming. You cannot create a rich in behavior domain model with Entity Beans cause they don&#8217;t support inheritance and they are not reentrant (if Entity Bean “A” calls Entity Bean “B” then in the same call stack B cannot call back to A). So J2EE programmers implemented all the business logic inside a service layer (with Session Beans), with <a href="http://www.martinfowler.com/bliki/AnemicDomainModel.html">anemic domain models</a> using Entity Beans. A service layer can be good for things like transaction management, security and other infrastructure services but when you have complex business logic and an anemic domain model you can easily end up with lots of duplicate code. The <a href="http://corej2eepatterns.com/Patterns2ndEd/BusinessObject.htm">Business Object</a> is a back to the basics design pattern that acknowledges the importance of rich in behavior domain models.</p>
<p>I would also like to comment on the <a href="http://corej2eepatterns.com/Patterns2ndEd/CompositeEntity.htm">Composite Entity</a> design pattern. I have very limited knowledge of Entity Beans. I have used Entity Beans in only one project. Most of the projects that I worked on were using <a href="http://hibernate.org/">Hibernate</a> or <a href="http://java.sun.com/products/jdbc/">JDBC</a>. While reading this pattern I felt very strange since it is very awkward for me to program with Entity Beans. I believe that if you have an exposure to tools like <a href="http://hibernate.org/">Hibernate</a> and you go back to implement persistence using Entity Beans you are continuously asking yourself “Why things have to be so hard to do and so restrictive?”. I am so glad to see the new EJB 3.0 Entity Beans specification, things are so much better now.</p>
<p>On the other hand, the <a href="http://corej2eepatterns.com/Patterns2ndEd/DomainStore.htm">Domain Store</a> is a welcomed addition to the <a href="http://corej2eepatterns.com/Patterns2ndEd/index.htm">J2EE patterns</a>. This pattern provides a good explanation for the generic <a href="http://martinfowler.com/bliki/POJO.html">POJO</a> persistence frameworks like <a href="http://hibernate.org/">Hibernate</a>, <a href="http://java.sun.com/products/jdo/">JDO</a> and <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0 (JSR 220)</a>.</p>
<p>For me, the <a href="http://www.corej2eepatterns.com/">Core J2EE Design Patterns 2nd edition</a> was a very pleasant reading since I expanded my knowledge of <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> and also cleared some things in my mind. I was aware of most of the patterns in this book but reading them from this book made them more clear in my mind. From my experience a lot of J2EE programmers do not really understand most of the <a href="http://corej2eepatterns.com/Patterns2ndEd/index.htm">core J2EE design patterns</a> and misuse them.</p>
<p>My conclusion for <a href="http://www.corej2eepatterns.com/">Core J2EE Design Patterns 2nd edition</a> is that it is a very well written book that is a must read for every <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> developer since not only does it provide valuable information about programming <a href="http://java.sun.com/j2ee/index.jsp">J2EE</a> applications with the traditional architecture but it also defines a widely used vocabulary for common <a href="http://corej2eepatterns.com/Patterns2ndEd/index.htm">J2EE patterns and idioms</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/10/13/book-review-core-j2ee-design-patterns-2nd-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Caching in Service Locator implementations</title>
		<link>http://www.tzavellas.com/techblog/2005/06/26/caching-in-service-locator-implementations/</link>
		<comments>http://www.tzavellas.com/techblog/2005/06/26/caching-in-service-locator-implementations/#comments</comments>
		<pubDate>Sun, 26 Jun 2005 18:50:19 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[EJB]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=15</guid>
		<description><![CDATA[Here you can find an article by Bobby Woolf that describes why caching inside a Service Locator in J2EE 1.3 applications may have unexpected behavior. The article also provides a nice explanation of the EJB resource reference mechanism. Update (19/9/2005): BTW this is why the Adventure Builder (the sample application developed by SUN for J2EE [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www-128.ibm.com/developerworks/websphere/techjournal/0410_woolf/0410_woolf.html">Here</a> you can find an article by <a href="http://www-128.ibm.com/developerworks/blogs/dw_blog.jspa?blog=392">Bobby Woolf</a> that describes why caching inside a <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html">Service Locator</a> in <a href="http://java.sun.com/j2ee/1.3/index.jsp">J2EE 1.3</a> applications may have unexpected behavior.</p>
<p>The <a href="http://www-128.ibm.com/developerworks/websphere/techjournal/0410_woolf/0410_woolf.html">article</a> also provides a nice explanation of the <a href="http://java.sun.com/products/ejb/">EJB</a> resource reference mechanism.</p>
<p><strong>Update</strong> (19/9/2005):</p>
<p>BTW this is why the <a href="https://adventurebuilder.dev.java.net/">Adventure Builder</a> (the sample application developed by <a href="http://www.sun.com/">SUN</a> for <a href="http://java.sun.com/j2ee/1.4/index.jsp">J2EE 1.4</a>) has two implementations for the <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html">Service Locator</a> design pattern.</p>
<p>The <a href="https://adventurebuilder.dev.java.net/source/browse/adventurebuilder/ws/components/servicelocator/src/java/com/sun/j2ee/blueprints/servicelocator/ejb/ServiceLocator.java?rev=1.3&#038;view=markup">first</a> implementation is for the EJB tier and does not have caching to avoid the problem explained in the <a href="ttp://www-128.ibm.com/developerworks/websphere/techjournal/0410_woolf/0410_woolf.html">article</a>.</p>
<p>The <a href="https://adventurebuilder.dev.java.net/source/browse/adventurebuilder/ws/components/servicelocator/src/java/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java?rev=1.4&#038;view=markup">second</a> implementation is for the web tier and uses caching since resource references in the web tier are the per web application and it is safe to cache them in a static variable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2005/06/26/caching-in-service-locator-implementations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
