<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>
                                <td>
                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>
                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
EJB reference resolvers
</h3>
<span style="margin-bottom: 10px;">
modified by <a href="http://community.jboss.org/people/jaikiran">jaikiran pai</a> in <i>EJB 3.0 Development</i> - <a href="http://community.jboss.org/docs/DOC-15746">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><h1><span>Purpose of this document:</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>This wiki briefly explains the EJB reference resolvers in the JBoss EJB3 project. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Background</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Assuming a @EJB has to injected into a component or a component has a ejb-ref, this reference needs to be resolved to a JNDI name. The JNDI name can then be used by various service providers (like EJB containers, Weld or any other framework which does the injection) to lookup the proxy of the reference EJB. </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>A short example:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@Stateless
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> A
<font color="navy">{</font>
 
     <font color="darkgreen">// local business interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> BLocal otherBeanLocal;
     
     <font color="darkgreen">// remote business interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> BRemote otherBeanRemote;
     
     <font color="darkgreen">// no-interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> B nointerfaceViewOfSomeOtherBean;
     
     ....
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@Stateless
@LocalBean
@Local(BLocal.class)
@Remote (BRemote.class)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> B
<font color="navy">{</font>
 
     
     ....
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>As can be seen above, the "A" SLSB is being injected with various different views of "B" bean. Each of these @EJB will internally have to be resolved, by containers, to a jndi-name to inject the appropriate proxies.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Details:</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Each of the @EJB, above, can be represented as a <a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/spi/src/main/java/org/jboss/ejb3/ejbref/resolver/spi/EjbReference.java">EjbReference</a>. The EjbReference can then be passed to a <a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/spi/src/main/java/org/jboss/ejb3/ejbref/resolver/spi/EjbReferenceResolver.java">EjbReferenceResovler</a> to resolve the jndi-name. The EjbReferenceResolver has one simple method:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>package</b></font> org.jboss.ejb3.ejbref.resolver.spi;
 
<font color="navy"><b>import</b></font> org.jboss.deployers.structure.spi.DeploymentUnit;
 
<font color="darkgreen">/**
* EjbReferenceResolver
*
* @author ALR* @version $Revision: $
*/</font>
<font color="navy"><b>public</b></font> <font color="navy"><b>interface</b></font> EjbReferenceResolver
<font color="navy">{</font>
 
   <font color="darkgreen">// --------------------------------------------------------------------------------||</font>
   <font color="darkgreen">// Contracts ----------------------------------------------------------------------||</font>
   <font color="darkgreen">// --------------------------------------------------------------------------------||</font>
 
   <font color="darkgreen">/**
     * Returns the JNDI Name of the proxy described by the specified
     * arguments.
     *
     * @param du The DeploymentUnit in question
     * @param reference The EJB reference used
     * @throws UnresolvableReferenceException If the reference cannot be resolved within scope
     * @return
     */</font>
   String resolveEjb(DeploymentUnit du, EjbReference reference) <font color="navy"><b>throws</b></font> UnresolvableReferenceException;
 
<font color="navy">}</font>
 
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>As can be seen the "resolveEjb" method accepts the EjbReference and a org.jboss.deployers.structure.spi.DeploymentUnit. The org.jboss.deployers.structure.spi.DeploymentUnit represents a deployment unit in a MC based deployment framework. Implementations ot EjbReferenceResolver can use the passed deployment unit to resolve the jndi name of the EjbReference.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Out-of-box implementations:</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><ul><li level="1" type="ul"><p>EJB 3.0:</p></li></ul><p>     </p><ul><ul><li level="2" type="ul"><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/ejb30-impl/src/main/java/org/jboss/ejb3/ejbref/resolver/ejb30/impl/FirstMatchEjbReferenceResolver.java">org.jboss.ejb3.ejbref.resolver.ejb30.impl.FirstMatchEjbReferenceResolver</a> </p></li></ul></ul><p>     </p><ul><ul><li level="2" type="ul"><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/ejb30-impl/src/main/java/org/jboss/ejb3/ejbref/resolver/ejb30/impl/ScopedEJBReferenceResolver.java">org.jboss.ejb3.ejbref.resolver.ejb30.impl.ScopedEJBReferenceResolver</a> </p></li></ul></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The above 2 EjbReferenceResolver implementations are meant for EJB 3.0 deployments. These resolvers have no notion of EJB3.1 nointerface views (or for that matter any EJB3.1 specific semantics). As such these resolvers cannot be used to resolve the jndi-name for the EjbReference of a no-interface view of a bean. Specifically, in the previous code example, these 2 resolvers wont be able to resolve:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">     <font color="darkgreen">// no-interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> B nointerfaceViewOfSomeOtherBean;        
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><ul><li level="1" type="ul"><p>EJB 3.1:</p></li></ul><p>     </p><ul><ul><li level="2" type="ul"><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/ejb31-impl/src/main/java/org/jboss/ejb3/ejbref/resolver/ejb31/impl/FirstMatchEjbReferenceResolver.java">org.jboss.ejb3.ejbref.resolver.ejb31.impl.FirstMatchEjbReferenceResolver</a></p></li></ul></ul><p>     </p><ul><ul><li level="2" type="ul"><p><a class="jive-link-external-small" href="http://github.com/jbossejb3/jboss-ejb3-ejbref-resolver/blob/master/ejb31-impl/src/main/java/org/jboss/ejb3/ejbref/resolver/ejb31/impl/ScopedEJBReferenceResolver.java">org.jboss.ejb3.ejbref.resolver.ejb31.impl.ScopedEJBReferenceResolver</a></p></li></ul></ul><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>(Even though the resolver classnames are the same, note the difference in the package names for EJB 3.0 and EJB 3.1).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>These 2 EJB3.1 EjbReferenceResolver implementations can be used for both EJB3.0 and EJB3.1 deployments. As such these resolvers can be used to resolve even the EjbReference of no-interface views of beans. So in the previous code example, these 2 resolvers will be able to resolve the jndi-name of all those @EJB references.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Usage example:</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@Stateless
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> A
<font color="navy">{</font>
 
     <font color="darkgreen">// local business interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> BLocal otherBeanLocal;
     
     <font color="darkgreen">// remote business interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> BRemote otherBeanRemote;
     
     <font color="darkgreen">// no-interface view of B bean</font>
     @EJB
     <font color="navy"><b>private</b></font> B nointerfaceViewOfSomeOtherBean;
     
     ....
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">@Stateless
@LocalBean
@Local(BLocal.class)
@Remote (BRemote.class)
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> B
<font color="navy">{</font>
 
     
     ....
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><pre class="jive-pre"><code class="jive-code jive-java">
<font color="navy"><b>import</b></font> org.jboss.deployers.structure.spi.DeploymentUnit;
<font color="navy"><b>import</b></font> org.jboss.ejb3.ejbref.resolver.ejb31.impl.FirstMatchEjbReferenceResolver;
<font color="navy"><b>import</b></font> org.jboss.ejb3.ejbref.resolver.spi.EjbReference;
<font color="navy"><b>import</b></font> org.jboss.ejb3.ejbref.resolver.spi.EjbReferenceResolver;
 
<font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> Usage
<font color="navy">{</font>
 
     <font color="navy"><b>public</b></font> <font color="navy"><b>void</b></font> resolveEjbReferenceForDeployment(DeploymentUnit du)
     <font color="navy">{</font>
     
          <font color="darkgreen">// Note, the process of getting hold of the DeploymentUnit is out of scope</font>
          <font color="darkgreen">// of this article.</font>
          
          <font color="darkgreen">// Let's just assume that the passed DeploymentUnit corresponds to a .jar</font>
          <font color="darkgreen">// containing the A and B beans show above</font>
          
          
          <font color="darkgreen">// 1. Let's first resolve the jndi-name for:</font>
          <font color="darkgreen">// local business interface view of B bean</font>
          <font color="darkgreen">// @EJB</font>
          <font color="darkgreen">// private BLocal otherBeanLocal;</font>
          
          <font color="darkgreen">// first let's create the EjbReference for the BLocal ejb reference</font>
          EjbReference ejbRefToBLocal = <font color="navy"><b>new</b></font> EjbReference(null, BLocal.class.getName(), <font color="navy"><b>null</b></font>);
          
          <font color="darkgreen">// now let's use the (EJB3.1) FirstMatchEjbReferenceResolver to resolve the ejb reference</font>
          EjbReferenceResolver ejbRefResolver = <font color="navy"><b>new</b></font> FirstMatchEjbReferenceResolver();
          String jndiNameToBLocal = ejbRefResolver.resolve(du,ejbRefToBLocal);
          
          <font color="darkgreen">// just print out the jndi-name</font>
          System.out.println(<font color="red">"jndi-name for @EJB to BLocal is: "</font> + jndiNameToBLocal);
          
          
          <font color="darkgreen">// 2. Let's now resolve the jndi-name for:</font>
          <font color="darkgreen">// remote business interface view of B bean</font>
          <font color="darkgreen">// @EJB</font>
          <font color="darkgreen">// private BRemote otherBeanRemote;          </font>
          
          <font color="darkgreen">// first let's create the EjbReference for the BRemote ejb reference</font>
          EjbReference ejbRefToBRemote = <font color="navy"><b>new</b></font> EjbReference(null, BRemote.class.getName(), <font color="navy"><b>null</b></font>);
          
          <font color="darkgreen">// Let's use the same (EJB3.1) FirstMatchEjbReferenceResolver to resolve the ejb reference</font>
          String jndiNameToBRemote = ejbRefResolver.resolve(du,ejbRefToBRemote);
          <font color="darkgreen">// just print out the jndi-name</font>
          System.out.println(<font color="red">"jndi-name for @EJB to BRemote is: "</font> + jndiNameToBRemote);
          
          
          <font color="darkgreen">// 3. Let's now resolve the jndi-name for:</font>
          <font color="darkgreen">// no-interface view of B bean</font>
          <font color="darkgreen">// @EJB</font>
          <font color="darkgreen">// private B nointerfaceViewOfSomeOtherBean;</font>
          
          <font color="darkgreen">// first let's create the EjbReference for the no-interface view of the ejb reference</font>
          <font color="darkgreen">// Note that for the no-interface view, the second parameter (i.e. the beanInterfaceName) to EjbReference</font>
          <font color="darkgreen">// is the fully qualified class name of the EJB implementation class</font>
          EjbReference ejbRefToBNoInterfaceView = <font color="navy"><b>new</b></font> EjbReference(null, B.class.getName(), <font color="navy"><b>null</b></font>);
          
          <font color="darkgreen">// Let's use the same (EJB3.1) FirstMatchEjbReferenceResolver to resolve the ejb reference</font>
          String jndiNameToBNoInterfaceView = ejbRefResolver.resolve(du,ejbRefToBNoInterfaceView);
          <font color="darkgreen">// just print out the jndi-name</font>
          System.out.println(<font color="red">"jndi-name for @EJB to B (no-interface view) is: "</font> + jndiNameToBNoInterfaceView);
          
     <font color="navy">}</font>
 
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Maven co-ordinates:</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The EjbReference and EjbReferenceResolver SPI reside in org.jboss.ejb3.ejbref.resolver:ejbref-resolver-spi artifact. The EJB3.0 implementations reside in org.jboss.ejb3.ejbref.resolver:ejbref-resolver-ejb30-impl artifact. The EJB3.1 implementations reside in org.jboss.ejb3.ejbref.resolver:ejbref-resolver-ejb31-impl artifact.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><h1><span>Outdated references</span></h1><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Before the org.jboss.ejb3.ejbref.resolver project was created, similar EjbReference resolution resided in org.jboss.ejb3:jboss-ejb3-common artifact. The EjbReference, EjbReferenceResolver and the implementations from org.jboss.ejb3:jboss-ejb3-common are now deprecated and will not be supported in the long term. JBoss EJB3 and other projects are encouraged to use the new org.jboss.ejb3.ejbref.resolver:* artifacts.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">  </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-15746">going to Community</a></p>
        <p style="margin: 0;">Create a new document in EJB 3.0 Development at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2030">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>