blob: 82a2574f6903c956294311b20a72589c4d714289 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<!-- $PAGETITLE -->
<TITLE>OpenEJB - Custom Injection</TITLE>
<LINK href="http://openejb.apache.org/all.css" rel="stylesheet" type="text/css">
<!--[if IE]><link rel="stylesheet" type="text/css" media="screen, projection" href="openejb.apache.org/ie.css"><![endif]-->
<LINK rel="SHORTCUT ICON" href="http://openejb.apache.org/images/favicon.ico">
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
</HEAD>
<BODY>
<!-- Delay the loading of the external javascript file needed for labels (as it takes too long to load and visibly holds loading of the page body) -->
<!-- To do this without javascript errors over undefined functions, we need to declare stubs here (that are overrided later by the proper implementations) -->
<SCRIPT language="JavaScript" type="text/javascript">
function doAddLabel(hideTextfieldAfterAddParam)
{
// stub
}
function onAddLabel()
{
// stub
}
function showLabelsInput()
{
// stub
}
</SCRIPT>
<A name="top"></A>
<TABLE class="frameTable" cellpadding="0" cellspacing="0" border="0">
<TR class="Row1">
<TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col2"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col3"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
</TR>
<TR class="Row2">
<TD class="Col1"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3" id="breadcrumbs">
<!-- $TOP_NAV_BAR -->
<A href="../OPENEJB/index.html" title="Index">Home</A> | <A href="../OPENEJB/download.html" title="Download">Download</A> | <A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Lists</A> | <A href="http://issues.apache.org/jira/browse/OPENEJB" class="external-link" rel="nofollow">Issues</A>
</TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</TD>
</TR>
<TR class="Row3">
<TD class="Col1"><IMG alt="" class="Row3Img" id="thinLine" src="http://openejb.apache.org/images/line_sm.gif"></TD>
<TD class="Col2"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col3"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col4"><IMG alt="" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5"><IMG alt="" class="Row3Img" src="http://openejb.apache.org/images/dotTrans.gif"></TD>
</TR>
<TR class="Row4">
<TD class="Col1">
<SPAN id="Navigation">
<H3><A name="Navigation-Overview"></A>Overview</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/index.html" title="Index">Home</A></LI>
<LI><A href="../OPENEJB/news.html" title="News">News</A></LI>
<LI><A href="../OPENEJB/faq.html" title="FAQ">FAQ</A></LI>
<LI><A href="../OPENEJB/download.html" title="Download">Download</A></LI>
<LI><A href="index.html" title="Index">Documentation</A></LI>
<LI><A href="../OPENEJB/examples.html" title="Examples">Examples</A></LI>
<LI><A href="http://cwiki.apache.org/confluence/display/OPENEJB/Lightening%20Demos" class="external-link" rel="nofollow">Lightning Demos</A></LI>
<LI><A href="../OPENEJB/mailing-lists.html" title="Mailing Lists">Mailing Lists</A></LI>
<LI><A href="../OPENEJB/source-code.html" title="Source Code">Source Code</A></LI>
<LI><A href="http://blogs.apache.org/openejb" class="external-link" rel="nofollow">Project Blog</A></LI>
</UL>
<H3><A name="Navigation-Servers"></A>Servers</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/local-server.html" title="Local Server">Local</A></LI>
<LI><A href="../OPENEJB/remote-server.html" title="Remote Server">Remote</A></LI>
</UL>
<H3><A name="Navigation-Integrations"></A>Integrations</H3>
<UL class="alternate" type="square">
<LI><A href="tomcat.html" title="Tomcat">Tomcat</A></LI>
<LI><A href="../OPENEJB/geronimo.html" title="Geronimo">Geronimo</A></LI>
<LI><A href="../OPENEJB/webobjects.html" title="WebObjects">WebObjects</A></LI>
</UL>
<H3><A name="Navigation-Community"></A>Community</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/team.html" title="Team">Team</A></LI>
<LI><A href="../OPENEJB/articles.html" title="Articles">Articles</A></LI>
<LI><A href="http://webchat.freenode.net/?channels=openejb" class="external-link" rel="nofollow">IRC</A></LI>
</UL>
<H3><A name="Navigation-RelatedProjects"></A>Related Projects</H3>
<UL class="alternate" type="square">
<LI><A href="http://activemq.apache.org/" class="external-link" rel="nofollow">ActiveMQ</A></LI>
<LI><A href="http://openjpa.apache.org/" class="external-link" rel="nofollow">OpenJPA</A></LI>
<LI><A href="http://cxf.apache.org/" class="external-link" rel="nofollow">CXF</A></LI>
</UL>
<H3><A name="Navigation-Index"></A>Index</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJB/space-index.html" title="Space Index">Site Index</A></LI>
<LI><A href="space-index.html" title="Space Index">Doc Index</A></LI>
</UL>
<H3>
<A name="Navigation-Feeds"></A>
Feeds
</H3>
<UL class="feeds">
<LI>
<A href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">
<IMG src="http://openejb.apache.org/images/rss.gif"></A>
<A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/rss.action?key=OPENEJB&newPages=false">Site</A>
</LI>
<LI><A href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">
<IMG src="http://openejb.apache.org/images/rss.gif"></A>
<A class="feedsText" href="http://cwiki.apache.org/confluence/spaces/blogrss.action?key=OPENEJB">News</A>
</LI>
</UL>
</SPAN>
</TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3">
<TABLE id="PageHeader" border="0" width="100%">
<TR>
<TD>
<A href="http://openejb.org/">
<IMG hspace="0" src="http://openejb.apache.org/images/logo_openejb.gif" vspace="0">
</A>
</TD>
<TD align="right">
<A href="http://www.apache.org/">
<IMG src="http://www.apache.org/images/asf-logo.gif" width="258" height="66">
</A>
</TD>
</TR>
<TR>
<TD id="page_title">
<!-- $TITLE -->
Custom Injection
</TD>
<TD align="right">
<BR><BR>
<!-- Google CSE Search Box Begins -->
<FORM id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT name="q" type="text" size="25">
<INPUT type="submit" name="sa" value="Search">
</FORM>
<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>
<!-- Google CSE Search Box Ends -->
</TD>
</TR>
</TABLE>
<P>
<!-- $BODY -->
<DIV id="PageContent">
<H1><A name="CustomInjection-Overview"></A>Overview</H1>
<P>As noted in the <A href="injection-of-env-entry-example.html" title="Injection of env-entry Example">Injection of env&#45;entry Example</A>, the EJB 3.0 supported env-entry types are fairly limited. Also the use of several &lt;env-entry&gt; tags in an ejb-jar.xml can get a bit verbose.</P>
<P>OpenEJB does not restrict you to just these data types or require you to use an ejb-jar.xml to declare them.</P>
<UL class="alternate" type="square">
<LI><B>@Resource</B> can be used on any type for which there is <B>java.beans.PropertyEditor</B></LI>
<LI>You may <B>install your own</B> PropertyEditors and package them with your app.</LI>
<LI>Java <B>Generics</B> are supported (e.g. List&lt;URI&gt; myURIs)</LI>
<LI>You may use a <B>META-INF/env-entries.properties</B> file as an alternative to an ejb-jar.xml</LI>
</UL>
<P>See <A href="built-in-type-converters.html" title="Built-in Type Converters">Built&#45;in Type Converters</A> for a full list of supported env-entry types.</P>
<P><EM>The source for this example is the &quot;custom-injection&quot; directory located in the <A href="../OPENEJB/download.html" title="Download">openejb-examples.zip</A> available on the download page.</EM></P>
<H1><A name="CustomInjection-TheCode"></A>The Code</H1>
<H2><A name="CustomInjection-BeanClass"></A>Bean Class</H2>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">@Stateless
<SPAN class="code-keyword">public</SPAN> class StratocasterImpl <SPAN class="code-keyword">implements</SPAN> Stratocaster {
@Resource(name = <SPAN class="code-quote">&quot;pickups&quot;</SPAN>)
<SPAN class="code-keyword">private</SPAN> List&lt;Pickup&gt; pickups;
@Resource(name = <SPAN class="code-quote">&quot;style&quot;</SPAN>)
<SPAN class="code-keyword">private</SPAN> Style style;
@Resource(name = <SPAN class="code-quote">&quot;dateCreated&quot;</SPAN>)
<SPAN class="code-keyword">private</SPAN> Date dateCreated;
@Resource(name = <SPAN class="code-quote">&quot;guitarStringGuages&quot;</SPAN>)
<SPAN class="code-keyword">private</SPAN> Map&lt;<SPAN class="code-object">String</SPAN>, <SPAN class="code-object">Float</SPAN>&gt; guitarStringGuages;
@Resource(name = <SPAN class="code-quote">&quot;certificateOfAuthenticity&quot;</SPAN>)
<SPAN class="code-keyword">private</SPAN> File certificateOfAuthenticity;
<SPAN class="code-keyword">public</SPAN> Date getDateCreated() {
<SPAN class="code-keyword">return</SPAN> dateCreated;
}
/**
* Gets the guage of the electric guitar strings
* used in <SPAN class="code-keyword">this</SPAN> guitar.
* @param string
* @<SPAN class="code-keyword">return</SPAN>
*/
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">float</SPAN> getStringGuage(<SPAN class="code-object">String</SPAN> string){
<SPAN class="code-keyword">return</SPAN> guitarStringGuages.get(string);
}
<SPAN class="code-keyword">public</SPAN> List&lt;Pickup&gt; getPickups() {
<SPAN class="code-keyword">return</SPAN> pickups;
}
<SPAN class="code-keyword">public</SPAN> Style getStyle() {
<SPAN class="code-keyword">return</SPAN> style;
}
<SPAN class="code-keyword">public</SPAN> File getCertificateOfAuthenticity() {
<SPAN class="code-keyword">return</SPAN> certificateOfAuthenticity;
}
}
</PRE>
</DIV></DIV>
<H2><A name="CustomInjection-TheMETAINF%2Fenventries.propertiesfile"></A>The META-INF/env-entries.properties file</H2>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-none">guitarStringGuages=E1=0.052\nA=0.042\nD=0.030\nG=0.017\nB=0.013\nE=0.010
certificateOfAuthenticity=/tmp/strat-certificate.txt
dateCreated=1962-03-01
pickups=S,S,S
style=VINTAGE
</PRE>
</DIV></DIV>
<H2><A name="CustomInjection-TheCustomTypeandEditor"></A>The Custom Type and Editor</H2>
<P>Support for java.lang.Enum types is already built-in, but we've decided we'd like to allow abbreviated versions of the enum constants to be usable. We do this by creating a custom PropertyEditor for our Pickup enum like so:</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">public</SPAN> class PickupEditor <SPAN class="code-keyword">extends</SPAN> java.beans.PropertyEditorSupport {
<SPAN class="code-keyword">public</SPAN> void setAsText(<SPAN class="code-object">String</SPAN> text) <SPAN class="code-keyword">throws</SPAN> IllegalArgumentException {
text = text.trim();
<SPAN class="code-keyword">if</SPAN> (text.equalsIgnoreCase(<SPAN class="code-quote">&quot;H&quot;</SPAN>)) setValue(Pickup.HUMBUCKER);
<SPAN class="code-keyword">else</SPAN> <SPAN class="code-keyword">if</SPAN> (text.equalsIgnoreCase(<SPAN class="code-quote">&quot;S&quot;</SPAN>)) setValue(Pickup.SINGLE_COIL);
<SPAN class="code-keyword">else</SPAN> <SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> IllegalStateException(<SPAN class="code-quote">&quot;H and S are the only supported Pickup aliases&quot;</SPAN>);
}
}
</PRE>
</DIV></DIV>
<P>We cleverly install this PropertyEditor in a static block in the Pickup class that will be executed should someone actually reference the Pickup type.</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">import</SPAN> java.beans.PropertyEditorManager;
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-keyword">enum</SPAN> Pickup {
HUMBUCKER,
SINGLE_COIL;
<SPAN class="code-comment">// Here's the little magic where we register the PickupEditor
</SPAN> <SPAN class="code-comment">// which knows how to create <SPAN class="code-keyword">this</SPAN> object from a string.
</SPAN> <SPAN class="code-comment">// You can add any of your own Property Editors in the same way.
</SPAN> <SPAN class="code-keyword">static</SPAN> {
PropertyEditorManager.registerEditor(Pickup.class, PickupEditor.class);
}
}
</PRE>
</DIV></DIV>
<H1><A name="CustomInjection-TestCase"></A>Test Case</H1>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java"><SPAN class="code-keyword">public</SPAN> class StratocasterTest <SPAN class="code-keyword">extends</SPAN> TestCase {
<SPAN class="code-keyword">private</SPAN> InitialContext initialContext;
<SPAN class="code-keyword">protected</SPAN> void setUp() <SPAN class="code-keyword">throws</SPAN> Exception {
Properties properties = <SPAN class="code-keyword">new</SPAN> Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, <SPAN class="code-quote">&quot;org.apache.openejb.client.LocalInitialContextFactory&quot;</SPAN>);
initialContext = <SPAN class="code-keyword">new</SPAN> InitialContext(properties);
}
<SPAN class="code-keyword">public</SPAN> void test() <SPAN class="code-keyword">throws</SPAN> Exception {
Stratocaster strat = (Stratocaster) initialContext.lookup(<SPAN class="code-quote">&quot;StratocasterImplLocal&quot;</SPAN>);
Date date = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US).parse(<SPAN class="code-quote">&quot;Mar 1, 1962&quot;</SPAN>);
assertEquals(<SPAN class="code-quote">&quot;Strat.getDateCreated()&quot;</SPAN>, date, strat.getDateCreated());
List&lt;Pickup&gt; pickups = asList(Pickup.SINGLE_COIL, Pickup.SINGLE_COIL, Pickup.SINGLE_COIL);
assertEquals(<SPAN class="code-quote">&quot;Strat.getPickups()&quot;</SPAN>, pickups, strat.getPickups());
assertEquals(<SPAN class="code-quote">&quot;Strat.getStyle()&quot;</SPAN>, Style.VINTAGE, strat.getStyle());
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>E1\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.052F, strat.getStringGuage(<SPAN class="code-quote">&quot;E1&quot;</SPAN>));
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>A\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.042F, strat.getStringGuage(<SPAN class="code-quote">&quot;A&quot;</SPAN>));
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>D\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.030F, strat.getStringGuage(<SPAN class="code-quote">&quot;D&quot;</SPAN>));
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>G\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.017F, strat.getStringGuage(<SPAN class="code-quote">&quot;G&quot;</SPAN>));
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>B\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.013F, strat.getStringGuage(<SPAN class="code-quote">&quot;B&quot;</SPAN>));
assertEquals(<SPAN class="code-quote">&quot;Strat.getStringGuage(\&quot;</SPAN>E\<SPAN class="code-quote">&quot;)&quot;</SPAN>, 0.010F, strat.getStringGuage(<SPAN class="code-quote">&quot;E&quot;</SPAN>));
File file = <SPAN class="code-keyword">new</SPAN> File(<SPAN class="code-quote">&quot;/tmp/strat-certificate.txt&quot;</SPAN>);
assertEquals(<SPAN class="code-quote">&quot;Strat.getCertificateOfAuthenticity()&quot;</SPAN>, file, strat.getCertificateOfAuthenticity());
}
}
</PRE>
</DIV></DIV>
<H1><A name="CustomInjection-Runningit"></A>Running it</H1>
<P>Running the example is fairly simple. In the &quot;custom-injection&quot; directory of the <A href="../OPENEJB/download.html" title="Download">examples zip</A>, just run:</P>
<BLOCKQUOTE>
<P>$ mvn clean install</P></BLOCKQUOTE>
<P>Which should create output like the following.</P>
<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent">
<PRE>-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.superbiz.enventries.StratocasterTest
Apache OpenEJB 3.1-SNAPSHOT build: 20080409-12:05
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb3/examples/custom-injection
INFO - openejb.base = /Users/dblevins/work/openejb3/examples/custom-injection
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default JDK 1.3 ProxyFactory)
INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes
INFO - Configuring app: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean StratocasterImpl: Container(type=STATELESS, id=Default Stateless Container)
INFO - Loaded Module: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes
INFO - Assembling app: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes
INFO - Jndi(name=StratocasterImplLocal) --&gt; Ejb(deployment-id=StratocasterImpl)
INFO - Created Ejb(deployment-id=StratocasterImpl, ejb-name=StratocasterImpl, container=Default Stateless Container)
INFO - Deployed Application(path=/Users/dblevins/work/openejb3/examples/custom-injection/target/classes)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.705 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
</PRE>
</DIV></DIV>
</DIV>
</P>
</TD>
<TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">
</TD>
</TR>
<TR class="Row5">
<TD class="Col1">&nbsp;</TD>
<TD class="Col2">&nbsp;</TD>
<TD class="Col3">
<BR>
<BR>
<IMG width="100%" height="1" src="http://openejb.apache.org/images/line_light.gif">
<TABLE width="100%">
<TR>
<TD>
<SPAN class="bodyGrey">
<SMALL>
<NOTICE><!-- $FOOTER -->
Apache OpenEJB is an project of The Apache Software Foundation (ASF)
</NOTICE>
<BR>
Site Powered by
<A href="http://atlassian.com/">Atlassian</A>
<A href="http://atlassian.com/confluence/">Confluence</A>
.
</SMALL>
</SPAN>
</TD>
<TD align="right">
<A style="color:#999;font-size:small;font-weight:normal;" href="https://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJBx30&title=Custom%20Injection">[ edit ]</A>
</TD>
</TR>
</TABLE>
<BR>
</TD>
<TD class="Col4"><IMG src="http://openejb.apache.org/images/dotTrans.gif"></TD>
<TD class="Col5">&nbsp;</TD>
</TR>
</TABLE>
<!-- Needed for composition plugin -->
<!-- delay the loading of large javascript files to the end so that they don't interfere with the loading of page content -->
<SPAN style="display: none">
<SCRIPT type="text/javascript" language="JavaScript" src="http://cwiki.apache.org/confluence/labels-javascript"></SCRIPT>
<SCRIPT src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</SCRIPT>
<SCRIPT type="text/javascript">
_uacct = "UA-2717626-1";
urchinTracker();
</SCRIPT>
</SPAN>
</BODY>
</HTML>