<?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()</title>
	<atom:link href="http://www.tzavellas.com/techblog/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>Tue, 08 Dec 2009 22:49:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using self types for trait composition</title>
		<link>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/</link>
		<comments>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 15:01:37 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[trait]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=227</guid>
		<description><![CDATA[While using traits in a small Scala project, I finally understood when to use self types. Self types are often characterized by Scala newbies as an incomprehensible language feature with no obvious usage. After coding with traits for a while I understood that self types are an essential information hiding tool for composing traits.
I am [...]]]></description>
			<content:encoded><![CDATA[<p>While using traits in a small Scala project, I finally understood when to use self types. Self types are often characterized by Scala newbies as an <a href="http://www.naildrivin5.com/scalatour/wiki_pages/ExplcitlyTypedSelfReferences">incomprehensible language feature</a> with no obvious usage. After coding with traits for a while I understood that self types are an essential information hiding tool for composing traits.</p>
<p>I am coding a small JMX library in Scala, similar to <a href="http://static.springsource.org/spring/docs/2.5.x/reference/jmx.html">Spring&#8217;s JMX</a> module and to the <a href="http://github.com/martint/jmxutils">jmxutils</a> library. In my project I have a <code>MBeanInfoAssembler</code> trait, that is a strategy interface, whose role is to create a <code>ModelMBeanInfo</code> from a <code>Class</code>. Implementations of that trait will use reflection to discover the attributes and operations that will be used to export instances of the <code>Class</code> to JMX.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>  
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The first implementation of the trait was the <code>SimpleMBeanInfoAssembler</code> class which uses reflection and some conventions to discover the attributes and operations.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, 
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>In the above code I have removed the implementations of the <code>attributes</code> and <code>operations</code> methods because they are not important for this example. What is important is the <code>createMBeanInfo</code> method which just calls the <code>ModelMBeanInfoSupport</code> constructor with the results of the <code>attributes</code> and <code>operations</code> methods. If we leave the code like this, when we try to provide a second implementation of the <code>MBeanInfoAssembler</code> trait we will probably copy-paste that method&#8217;s code.</p>
<p>In Scala, traits can have concrete methods so our first though is to move the implementation of the <code>createMBeanInfo</code> method to the <code>MBeanInfoAssembler</code> trait. After doing that our trait becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, 
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>and the implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>This refactoring has the obvious advantage of removing the repetition that would be needed to create multiple implementations of <code>MBeanInfoAssembler</code> but also one major disadvantage over our previous code.</p>
<p>In order to support the implementation of the <code>createMBeanInfo</code> method the <code>MBeanInfoAssembler</code> trait gets updated with two new <em>public abstract methods</em> (<code>attributes</code> and <code>operations</code>). Those methods are useless to the clients of the trait, who only want to create a <code>ModelMBeanInfo</code> from a <code>Class</code>, and are there only to support the trait&#8217;s implementation. To correct this defect in our design we need to do another refactoring by introducing a new trait and connecting the two traits together using a self type annotation.</p>
<p>If you are new to Scala you might wonder what is a self type? In the Scala documentation self types are described as a way to <em>&#8220;declare the type of the value <code>this</code> explicitly&#8221;</em> which might be accurate but sounds at least confusing for a newbie like me.</p>
<p>In a well designed object-oriented system we achive reuse and flexibility when our system has lots of small objects that have a single responsibility and are explicitely connected using interfaces that represent the role of the connection. Usually those connections are made using constructor arguments. The power of such system comes from the testability and reusability of the small objects and the flexibility of composing those objects to solve a particular problem.</p>
<p>Since traits in Scala can have code (method implementations), if we want to create an equally well designed and flexible system using traits we need to be able to do the same. The problem is that traits do not have constructors so we need a new construct to declare trait dependencies and also a way to compose traits together. The solution to the first problem are the self type annotations and the solution to the second is multiple inheritance.</p>
<p>In the previous example the <code>MBeanInfoAssembler</code> trait had many responsibilities assigned to it. It must be able to create a <code>ModelMBeanInfo</code> given a <code>Class</code> and it must also extract any metadata in order to find the right attributes and operations for the given <code>Class</code>. We decide to move the second responsibility to a new trait called <code>MBeanMetadataExtractor</code> that has the <code>attributes</code> and <code>operations</code> methods.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanMetadataExtractor <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Now we need to update the <code>MBeanInfoAssembler</code> trait and to somehow connect it to the <code>MBeanMetadataExtractor</code> trait so it can use the <code>attributes</code> and <code>operations</code> methods. To do this we will use a self type annotation.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> MBeanInfoAssembler <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #000080;">:</span> MBeanMetadataExtractor <span style="color: #000080;">=&gt;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> createMBeanInfo<span style="color: #F78811;">&#40;</span>clazz<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> ModelMBeanInfo <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">new</span> ModelMBeanInfoSupport<span style="color: #F78811;">&#40;</span>clazz.<span style="color: #000000;">getName</span>,
                              clazz.<span style="color: #000000;">getSimpleName</span>,
                              attributes<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>,
                              operations<span style="color: #F78811;">&#40;</span>clazz<span style="color: #F78811;">&#41;</span>,
                              Array<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The syntax of the self type annotation is <code>this: MBeanMetadataExtractor =></code> and it tells the compiler that a <code>MBeanInfoAssembler</code> can be implemented in a class only if that class also implements the <code>MBeanMetadataExtractor</code> trait. Using this assumption the compiler makes the methods of the <code>MBeanMetadataExtractor</code> available for use in the <code>MBeanInfoAssembler</code> (in our case the <code>attributes</code> and <code>operations</code> methods).</p>
<p>The class <code>SimpleMBeanInfoAssembler</code> now has to implement both <code>MBeanInfoAssembler</code> and <code>MBeanMetadataExtractor</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> SimpleMBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">extends</span> MBeanInfoAssembler <span style="color: #0000ff; font-weight: bold;">with</span> MBeanMetadataExtractor <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> attributes<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanAttributeInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the attributes from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> operations<span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Class<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>ModelMBeanOperationInfo<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">//find the operations from the class using reflection</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>By doing the above changes we were able to hide the <code>attributes</code> and <code>operations</code> methods from the clients of the <code>MBeanInfoAssembler</code> trait. Obviously those methods exist in the public interface of <code>SimpleMBeanInfoAssembler</code> but in a well designed system other classes will connect with <code>SimpleMBeanInfoAssembler</code> by using one of its traits (interfaces) so this is not a problem.</p>
<p>In conclusion, if we want to move method implementations inside traits then we risk polluting the interface of those traits with abstract methods that support the implementation of the concrete methods and are unrelated with the main responsibility of the trait. A solution to this problem is to move those abstract methods in other traits and compose the traits together using self type annotations and multiple inheritance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2009/12/07/using-self-types-for-trait-composition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Statically weaving JPA entities for EclipseLink using Maven</title>
		<link>http://www.tzavellas.com/techblog/2008/10/17/statically-weaving-jpa-entities-for-eclipselink-using-maven/</link>
		<comments>http://www.tzavellas.com/techblog/2008/10/17/statically-weaving-jpa-entities-for-eclipselink-using-maven/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 06:31:07 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EclipseLink]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=124</guid>
		<description><![CDATA[EclipseLink provides advanced JPA features such as lazy-loading, change tracking and fetch groups using bytecode weaving. To use bytecode weaving you can either dynamically instrument your entity classes at runtime (via a jvm agent) or use a tool to statically process the .class files after compilation. In this post we will present how to use [...]]]></description>
			<content:encoded><![CDATA[<p><a title="EclipseLink" href="http://eclipse.org/eclipselink">EclipseLink</a> provides advanced JPA features such as lazy-loading, change tracking and fetch groups using bytecode weaving. To use bytecode weaving you can either <a href="http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#How_to_Configure_Dynamic_Weaving_for_JPA_Entities_Using_the_EclipseLink_Agent">dynamically</a> instrument your entity classes at runtime (via a jvm agent) or use a tool to <a href="http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#How_to_Configure_Static_Weaving_for_JPA_Entities">statically</a> process the <em>.class</em> files after compilation. In this post we will present how to use EclipseLink&#8217;s static weaver in a Maven project.</p>
<p>To enable static weaving in a Maven based project we have to add the Eclipselink weaver in the <em>process-classes</em> phase of the <a href="http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html">Maven&#8217;s build life cycle</a>. The <em>process-classes</em> phase happens after the <em>compile</em> phase and allows the post-processing of files generated in compile phase. In our case, the EclipseLink weaver will post-process the <em>.class</em> files produced by the compiler to add extra bytecodes that implement the desired JPA functionality (lazy-loading, etc).</p>
<p>In the below code we use the <a href="http://maven.apache.org/plugins/maven-antrun-plugin/">Maven AntRun plugin</a> to call (via the <a href="http://ant.apache.org/manual/CoreTasks/java.html">java ANT task</a>) the command line version of the EclipseLink&#8217;s static weaver. Please note that the class name of the weaver is <strong>org.eclipse.persistence.tools.weaving.jpa.StaticWeave</strong> and not <strong>org.eclipse.persistence.tools.weaving.StaticWeave</strong> as the <a href="http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#How_to_Configure_Static_Weaving_for_JPA_Entities">EclipseLink JPA Extensions</a> wiki page says.</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;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-antrun-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>process-classes<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phase<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tasks<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;java</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;org.eclipse.persistence.tools.weaving.jpa.StaticWeave&quot;</span></span>
<span style="color: #009900;">                   <span style="color: #000066;">classpathref</span>=<span style="color: #ff0000;">&quot;maven.runtime.classpath&quot;</span> <span style="color: #000066;">fork</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;-loglevel FINE -persistenceinfo src/main/resources target/classes target/classes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/java<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/tasks<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>run<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goal<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/goals<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/execution<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/executions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In addition to the <em>pom.xml</em> changes in order to use static weaving in EclipseLink you have to set the <strong>eclipselink.weaving</strong> property to <strong>static</strong> in the <em>META-INF/peristence.xml</em> file.</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;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;eclipselink.weaving&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;static&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>For more information about EclipseLink&#8217;s static weaving see the <a href="http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)#How_to_Configure_Static_Weaving_for_JPA_Entities">How to Configure Static Weaving for JPA Entities</a> section of the <a href="http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_(ELUG)">Using EclipseLink JPA Extensions</a> wiki page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2008/10/17/statically-weaving-jpa-entities-for-eclipselink-using-maven/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Using EclipseLink with Maven</title>
		<link>http://www.tzavellas.com/techblog/2008/10/17/using-eclipselink-with-maven/</link>
		<comments>http://www.tzavellas.com/techblog/2008/10/17/using-eclipselink-with-maven/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 06:30:59 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EclipseLink]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/?p=113</guid>
		<description><![CDATA[The EclipseLink/Maven wiki page of the EclipseLink project has information on how to setup your Maven dependencies in order to use EclipseLink in a Maven based project. The problem with that page is that it contains typos in all artifactIds of the OSGi enabled artifacts. For example the page lists org.eclipse.peristence.jpa as the artifactId of [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://wiki.eclipse.org/EclipseLink/Maven">EclipseLink/Maven</a> wiki page of the <a href="http://eclipse.org/eclipselink">EclipseLink project</a> has information on how to setup your Maven dependencies in order to use EclipseLink in a Maven based project. The problem with that page is that it contains typos in all artifactIds of the OSGi enabled artifacts. For example the page lists <strong>org.eclipse.peristence.jpa</strong> as the artifactId of the JPA bundle instead of <strong>org.eclipse.persistence.jpa</strong>. Below I have the code that you have to add to your <em>pom.xml</em> in order to use EclipseLink as a JPA provider in your project.</p>
<p>First include the <a href="http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo">EclipseLink repository</a> in your Maven repositories by adding the following XML snippet.</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;repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>EclipseLink<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.eclipse.org/downloads/download.php?r=1<span style="color: #ddbb00;">&amp;amp;</span>nf=1<span style="color: #ddbb00;">&amp;amp;</span>file=/rt/eclipselink/maven.repo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/repositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then add the following dependencies in the dependencies section of pom.xml</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;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence.jpa<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence.core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence.asm<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.persistence.antlr<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>runtime<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Remember that the above dependencies are for using EclipseLink as a JPA provider. You might also need to include the MOXY or the SDO bundle if you want to use XML mapping or SDO. Also if you want to use EclipseLink&#8217;s JPA extensions in your code (like the <strong>@Cache</strong> annotation for example) change the scope of the <strong>org.eclipse.persistence.jpa</strong> artifact from <strong>runtime</strong> to <strong>compile</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2008/10/17/using-eclipselink-with-maven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Seattle Conference on Scalability</title>
		<link>http://www.tzavellas.com/techblog/2007/07/07/seattle-conference-on-scalability/</link>
		<comments>http://www.tzavellas.com/techblog/2007/07/07/seattle-conference-on-scalability/#comments</comments>
		<pubDate>Sat, 07 Jul 2007 10:39:03 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/07/07/seattle-conference-on-scalability/</guid>
		<description><![CDATA[Finally, the talks from the Google&#8217;s scalability conference are available online.
]]></description>
			<content:encoded><![CDATA[<p>Finally, the talks from the Google&#8217;s <a href="http://www.google.com/events/scalability_seattle/">scalability conference</a> are available <a href="http://video.google.com/videosearch?q=user%3A%22Google+engEDU%22+Seattle+Conference+on+Scalability&#038;hl=en">online</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/07/07/seattle-conference-on-scalability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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 creating bindings [...]]]></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>
	</channel>
</rss>
