blob: e08f564073b519abb344a3a4d3cf80b69d38d7aa [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 - Resource 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">
<SCRIPT language="JavaScript" src="http://cwiki.apache.org/confluence/pages/viewpage.action?spaceKey=OPENEJB&title=functions.js" type="text/javascript"></SCRIPT>
<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="index.html" title="Index">Index</A> | <A href="news.html" title="News">News</A> | <A href="faq.html" title="FAQ">FAQ</A> | <A href="download.html" title="Download">Download</A> | <A href="mailing-lists.html" title="Mailing Lists">Lists</A> | <SPAN class="nobr"><A href="http://issues.apache.org/jira/browse/OPENEJB" title="Visit page outside Confluence" rel="nofollow">Issues<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN>
</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="index.html" title="Index">Home</A></LI>
<LI><A href="news.html" title="News">News</A></LI>
<LI><A href="faq.html" title="FAQ">FAQ</A></LI>
<LI><A href="download.html" title="Download">Download</A></LI>
<LI><A href="../OPENEJBx30/examples.html" title="Examples">OPENEJBx30:Examples</A></LI>
<LI><A href="mailing-lists.html" title="Mailing Lists">Mailing Lists</A></LI>
<LI><A href="source-code.html" title="Source Code">Source Code</A></LI>
</UL>
<H3><A name="Navigation-General"></A>General</H3>
<UL class="alternate" type="square">
<LI><A href="../OPENEJBx30/deploy-tool.html" title="Deploy Tool">Deployment</A></LI>
<LI><A href="../OPENEJBx30/startup.html" title="Startup">Startup</A></LI>
<LI><A href="validation-tool.html" title="Validation Tool">Validation</A></LI>
<LI><A href="webadmin.html" title="Webadmin">Webadmin</A></LI>
</UL>
<H3><A name="Navigation-Configuration"></A>Configuration</H3>
<UL class="alternate" type="square">
<LI><A href="configuration.html" title="Configuration">General</A></LI>
<LI><A href="../OPENEJBx30/deployments.html" title="Deployments">Deployments</A></LI>
<LI><A href="containers.html" title="Containers">Containers</A></LI>
</UL>
<H3><A name="Navigation-Servers"></A>Servers</H3>
<UL class="alternate" type="square">
<LI><A href="local-server.html" title="Local Server">Local</A></LI>
<LI><A href="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="geronimo.html" title="Geronimo">Geronimo</A></LI>
<LI><A href="webobjects.html" title="WebObjects">WebObjects</A></LI>
</UL>
<H3><A name="Navigation-Community"></A>Community</H3>
<UL class="alternate" type="square">
<LI><A href="team.html" title="Team">Team</A></LI>
<LI><A href="articles.html" title="Articles">Articles</A></LI>
<LI><A href="annoyances.html" title="Annoyances">Annoyances</A></LI>
<LI><SPAN class="nobr"><A href="index.html" title="Visit page outside Confluence" rel="nofollow">Wiki<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></LI>
<LI><SPAN class="nobr"><A href="irc://irc.freenode.net/#openejb" title="Visit page outside Confluence" rel="nofollow">IRC<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></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 -->
Resource 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="ResourceInjection-Overview"></A>Overview</H1>
<P>This example demonstrates the use of the injection of environment entries using <B>@Resource</B> annotation.</P>
<P> &quot;EJB Core Contracts and Requirements&quot; specification section 16.2.2&quot;</P>
<UL>
<LI>&quot;A &iuml;&not;?eld or method of a bean class may be annotated to request that an entry from the bean's environment<BR>
be injected. Any of the types of resources or other environment entries described in this chapter may<BR>
be injected. Injection may also be requested using entries in the deployment descriptor corresponding to<BR>
each of these resource types.&quot;</LI>
</UL>
<UL>
<LI>&quot;Environment entries may also be injected into the bean through bean methods that follow the<BR>
naming conventions for JavaBeans properties. The annotation is applied to the set method for<BR>
the property, which is the method that is called to inject the environment entry. The JavaBeans<BR>
property name (not the method name) is used as the default JNDI name.&quot;</LI>
</UL>
<P><B>PurchaseOrderBean</B> class shows use of <B>@Resource</B> annotation through a bean <B>field</B>.</P>
<P><B>InvoiceBean</B> class shows the use of <B>@Resource</B> annotation through a <B>setter method</B>.</P>
<P>The source for this example can be checked out from svn:</P>
<P> $ svn co <SPAN class="nobr"><A href="http://svn.apache.org/repos/asf/incubator/openejb/trunk/openejb3/examples/resource-injection/" title="Visit page outside Confluence" rel="nofollow">http://svn.apache.org/repos/asf/incubator/openejb/trunk/openejb3/examples/resource-injection/<SUP><IMG class="rendericon" src="http://cwiki.apache.org/confluence/images/icons/linkext7.gif" height="7" width="7" align="absmiddle" alt="" border="0"></SUP></A></SPAN></P>
<P>To run the example change to the directory containing example and run Maven:<BR>
(openejb3_project_dir is the top level directory of OpenEJB3 project)</P>
<P>$ cd &lt;openejb3_project_dir&gt;/examples/resource-injection </P>
<P>$ mvn clean install</P>
<H1><A name="ResourceInjection-TheCode"></A>The Code</H1>
<H2><A name="ResourceInjection-Injectionthroughfield"></A>Injection through field</H2>
<P><B>maxLineItem</B> field in <B>PurchaseOrderBean</B> class is annotated with <B>@Resource</B> annotation to inject a simple environment entry. Default value of10 is assigned. Deployer can modify the values of the environment entries at deploy time in deployment descriptor.</P>
<H4><A name="ResourceInjection-@Resourceannotationofafield"></A>@Resource annotation of a field</H4>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-java">@Resource
<SPAN class="code-object">int</SPAN> maxLineItems = 10;</PRE>
</DIV></DIV>
<H2><A name="ResourceInjection-Injectionthroughasettermethod"></A>Injection through a setter method</H2>
<P><B>setMaxLineItem</B> method in <B>InvioceBean</B> class is annotated with @Resource annotation to inject a simple environment entry. By default, the JavaBeans property name is combined with the name of the class in which the annotation is used and is used directly as the name in the bean's naming context. JNDI name for this entry would be:</P>
<UL>
<LI>java:comp/env/org.apache.openejb.examples.resource.InvoiceBean/maxLineItems</LI>
</UL>
<H4><A name="ResourceInjection-@Resourceannotationofasettermethod"></A>@Resource annotation of a setter method</H4>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-java">@Resource
<SPAN class="code-keyword">public</SPAN> void setMaxLineItems(<SPAN class="code-object">int</SPAN> maxLineItems) {
<SPAN class="code-keyword">this</SPAN>.maxLineItems = maxLineItems;
}</PRE>
</DIV></DIV>
<H4><A name="ResourceInjection-enventryinejbjar.xml"></A>env-entry in ejb-jar.xml</H4>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-xml"><SPAN class="code-tag">&lt;env-entry&gt;</SPAN>
<SPAN class="code-tag">&lt;description&gt;</SPAN>The maximum number of line items per invoice.<SPAN class="code-tag">&lt;/description&gt;</SPAN>
<SPAN class="code-tag">&lt;env-entry-name&gt;</SPAN>org.apache.openejb.examples.injection.InvoiceBean/maxLineItems<SPAN class="code-tag">&lt;/env-entry-name&gt;</SPAN>
<SPAN class="code-tag">&lt;env-entry-type&gt;</SPAN>java.lang.Integer<SPAN class="code-tag">&lt;/env-entry-type&gt;</SPAN>
<SPAN class="code-tag">&lt;env-entry-value&gt;</SPAN>15<SPAN class="code-tag">&lt;/env-entry-value&gt;</SPAN>
<SPAN class="code-tag">&lt;/env-entry&gt;</SPAN></PRE>
</DIV></DIV>
<H4><A name="ResourceInjection-Using@Resourceannotatedenventry"></A>Using @Resource annotated env-entry</H4>
<TABLE cellpadding="5" width="85%" cellspacing="8px" class="tipMacro" border="0" align="center"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="http://cwiki.apache.org/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B class="strong">Using @Resource annotated env-entry</B><BR>
<P><B>maxLineItems</B> variable is injected by the <B>setMaxLine</B> method above. See also how env-entry is defined in ejb-jar.xml</P></TD></TR></TABLE>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-java"><SPAN class="code-keyword">public</SPAN> void addLineItem(LineItem item) <SPAN class="code-keyword">throws</SPAN> TooManyItemsException {
<SPAN class="code-keyword">if</SPAN> (item == <SPAN class="code-keyword">null</SPAN>) {
<SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> IllegalArgumentException(<SPAN class="code-quote">&quot;Line item must not be <SPAN class="code-keyword">null</SPAN>&quot;</SPAN>);
}
<SPAN class="code-keyword">if</SPAN> (itemCount &lt;= maxLineItems) {
items.add(item);
itemCount++;
} <SPAN class="code-keyword">else</SPAN> {
<SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> TooManyItemsException(<SPAN class="code-quote">&quot;<SPAN class="code-object">Number</SPAN> of items exceeded the maximum limit&quot;</SPAN>);
}
}</PRE>
</DIV></DIV>
<H1><A name="ResourceInjection-JUnitTest"></A>JUnit Test</H1>
<P>Writing an JUnit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods.</P>
<H4><A name="ResourceInjection-Testfixture"></A>Test fixture</H4>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-java"><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>);
properties.setProperty(<SPAN class="code-quote">&quot;openejb.deployments.classpath.include&quot;</SPAN>, <SPAN class="code-quote">&quot;.*resource-injection.*&quot;</SPAN>);
initialContext = <SPAN class="code-keyword">new</SPAN> InitialContext(properties);
}</PRE>
</DIV></DIV>
<H4><A name="ResourceInjection-Testmethods"></A>Test methods</H4>
<DIV class="code"><DIV class="codeContent">
<PRE class="code-java"><SPAN class="code-keyword">public</SPAN> void testAddLineItem() <SPAN class="code-keyword">throws</SPAN> Exception {
Invoice order = (Invoice) initialContext.lookup(<SPAN class="code-quote">&quot;InvoiceBeanBusinessRemote&quot;</SPAN>);
assertNotNull(order);
LineItem item = <SPAN class="code-keyword">new</SPAN> LineItem(<SPAN class="code-quote">&quot;ABC-1&quot;</SPAN>, <SPAN class="code-quote">&quot;Test Item&quot;</SPAN>);
<SPAN class="code-keyword">try</SPAN> {
order.addLineItem(item);
} <SPAN class="code-keyword">catch</SPAN> (TooManyItemsException tmie) {
fail(<SPAN class="code-quote">&quot;Test failed due to: &quot;</SPAN> + tmie.getMessage());
}
}</PRE>
</DIV></DIV>
<H1><A name="ResourceInjection-Running"></A>Running</H1>
<P>Running the example is fairly simple. Just execute the following commands:</P>
<P>$ cd &lt;openejb3_project_dir&gt;/examples/resource-injection<BR>
$ mvn clean test</P>
<DIV class="preformatted"><DIV class="preformattedContent">
<PRE>-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.apache.openejb.examples.injection.InvoiceBeanTest
WARN - Unable use logging config as there are 3 file references containing directories which have not been created. See the list below.
WARN - [0] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/transaction.log
WARN - [1] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/openejb.log
WARN - [2] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/server.log
Apache OpenEJB 3.0-incubating-SNAPSHOT build: 20070120-10:45
http://incubator.apache.org/openejb
INFO - openejb.home = /home/raj/projects/openejb3/examples/resource-injection
INFO - openejb.base = /home/raj/projects/openejb3/examples/resource-injection
WARN - Cannot find the configuration file [OPENEJB:conf/openejb.xml]. Will attempt to create one for the beans deployed.
INFO - Found EjbModule in classpath: /home/raj/projects/openejb3/examples/resource-injection/target/classes
WARN - Cannot find the META-INF/openejb-jar.xml in /home/raj/projects/openejb3/examples/resource-injection/target/classes.
WARN - Auto-creating a container for bean InvoiceBean: Container(type=STATEFUL, id=Default Stateful Container)
WARN - Auto-deploying ejb InvoiceBean: EjbDeployment(deployment-id=InvoiceBean, container-id=Default Stateful Container)
WARN - Auto-deploying ejb PurchaseOrderBean: EjbDeployment(deployment-id=PurchaseOrderBean, container-id=Default Stateful Container)
INFO - Loaded Module: /home/raj/projects/openejb3/examples/resource-injection/target/classes
INFO - OpenEJB ready.
OpenEJB ready.
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.571 sec
Running org.apache.openejb.examples.injection.PurchaseOrderBeanTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] BUILD SUCCESSFUL
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] Total time: 16 seconds
</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="http://cwiki.apache.org/confluence/pages/editpage.action?spaceKey=OPENEJB&title=Resource%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>