| |
| <!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"> |
| <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"> </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"> </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"> </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>The EJB 3.0 specification (<B>EJB Core Contracts and Requirements</B>) section 16.2.2 reads:</P> |
| |
| <P><EM>A field or method of a bean class may be annotated to request that an entry from the bean's environment be injected. Any of the types of resources or other environment entries<A href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=OPENEJBx30&title=88&linkCreation=true&fromPageId=34895" class="createlink">88</A> described in this chapter may be injected. Injection may also be requested using entries in the deployment descriptor corresponding to each of these resource types.</EM></P> |
| |
| <P><EM>Environment entries may also be injected into the bean through bean methods that follow the naming conventions for JavaBeans properties. The annotation is applied to the set method for the property, which is the method that is called to inject the environment entry. The JavaBeans property name (not the method name) is used as the default JNDI name.</EM></P> |
| |
| <P>The <B>PurchaseOrderBean</B> class shows use of field-level <B>@Resource</B> annotation.</P> |
| |
| <P>The <B>InvoiceBean</B> class shows the use of method-level <B>@Resource</B> annotation.</P> |
| |
| <P>The source for this example can be checked out from svn:</P> |
| |
| <P> $ svn co <A href="http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/injection-of-env-entry" class="external-link" rel="nofollow">http://svn.apache.org/repos/asf/openejb/trunk/openejb3/examples/injection-of-env-entry</A></P> |
| |
| <P>To run it change your working directory to the directory <B>injection-of-env-entry</B> and run the following maven2 commands:</P> |
| |
| <P>$ cd injection-of-env-entry</P> |
| |
| <P>$ mvn clean install</P> |
| |
| <H1><A name="ResourceInjection-TheCode"></A>The Code</H1> |
| |
| <H2><A name="ResourceInjection-Injectionthroughfield%28fieldlevelinjection%29"></A>Injection through field (field-level injection)</H2> |
| |
| <P>The <B>maxLineItem</B> field in <B>PurchaseOrderBean</B> class is annotated with <B>@Resource</B> annotation to inform the EJB container <EM>the place</EM> where in the code the injection of a simple environment entry should take place. The default value of 10 is assigned. Deployer can modify the value of the environment entries at deployment time using deployment descriptor (ejb-jar.xml).</P> |
| |
| <H4><A name="ResourceInjection-@Resourceannotationofafield"></A>@Resource annotation of a field</H4> |
| |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <PRE class="code-java"> |
| @Resource |
| <SPAN class="code-object">int</SPAN> maxLineItems = 10; |
| </PRE> |
| </DIV></DIV> |
| |
| <H2><A name="ResourceInjection-Injectionthroughasettermethod%28methodlevelinjection%29"></A>Injection through a setter method (method-level injection)</H2> |
| |
| <P>The <B>setMaxLineItem</B> method in <B>InvoiceBean</B> class is annotated with <B>@Resource</B> annotation to inject the simple environment entry. Only setters can be used as a way to inject environment entry values. </P> |
| |
| <P>You could look up the env-entry using JNDI lookup() method and the following name:</P> |
| |
| <UL> |
| <LI>java:comp/env/org.apache.openejb.examples.resource.InvoiceBean/maxLineItems</LI> |
| </UL> |
| |
| |
| <P>The pattern is to combine the fully-qualified class name and the name of a instance field (or a name of the setter method without <EM>set</EM> prefix and the first letter lowercased).</P> |
| |
| <H4><A name="ResourceInjection-@Resourceannotationofasettermethod"></A>@Resource annotation of a setter method</H4> |
| |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <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 panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <PRE class="code-xml"> |
| <SPAN class="code-tag"><env-entry></SPAN> |
| <SPAN class="code-tag"><description></SPAN>The maximum number of line items per invoice.<SPAN class="code-tag"></description></SPAN> |
| <SPAN class="code-tag"><env-entry-name></SPAN>org.apache.openejb.examples.injection.InvoiceBean/maxLineItems<SPAN class="code-tag"></env-entry-name></SPAN> |
| <SPAN class="code-tag"><env-entry-type></SPAN>java.lang.Integer<SPAN class="code-tag"></env-entry-type></SPAN> |
| <SPAN class="code-tag"><env-entry-value></SPAN>15<SPAN class="code-tag"></env-entry-value></SPAN> |
| <SPAN class="code-tag"></env-entry></SPAN> |
| </PRE> |
| </DIV></DIV> |
| |
| <H4><A name="ResourceInjection-Using@Resourceannotatedenventry"></A>Using @Resource annotated env-entry</H4> |
| |
| <DIV class="panelMacro"><TABLE class="tipMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/confluence/images/icons/emoticons/check.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD><B>Using @Resource annotated env-entry</B><BR><B>maxLineItems</B> variable is injected by the <B>setMaxLine</B> method above. See also how env-entry is defined in ejb-jar.xml</TD></TR></TABLE></DIV> |
| <DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <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">"Line item must not be <SPAN class="code-keyword">null</SPAN>"</SPAN>); |
| } |
| |
| <SPAN class="code-keyword">if</SPAN> (itemCount <= 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">"<SPAN class="code-object">Number</SPAN> of items exceeded the maximum limit"</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 panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <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">"org.apache.openejb.client.LocalInitialContextFactory"</SPAN>); |
| properties.setProperty(<SPAN class="code-quote">"openejb.deployments.classpath.include"</SPAN>, <SPAN class="code-quote">".*resource-injection.*"</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 panel" style="border-width: 1px;"><DIV class="codeContent panelContent"> |
| <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">"InvoiceBeanBusinessRemote"</SPAN>); |
| assertNotNull(order); |
| LineItem item = <SPAN class="code-keyword">new</SPAN> LineItem(<SPAN class="code-quote">"ABC-1"</SPAN>, <SPAN class="code-quote">"Test Item"</SPAN>); |
| |
| <SPAN class="code-keyword">try</SPAN> { |
| order.addLineItem(item); |
| } <SPAN class="code-keyword">catch</SPAN> (TooManyItemsException tmie) { |
| fail(<SPAN class="code-quote">"Test failed due to: "</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 injection-of-env-entry<BR> |
| $ mvn clean test</P> |
| |
| <DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent"> |
| <PRE>------------------------------------------------------- |
| T E S T S |
| ------------------------------------------------------- |
| Running org.superbiz.injection.PurchaseOrderBeanTest |
| Apache OpenEJB 3.0.0-SNAPSHOT build: 20071218-01:41 |
| http://openejb.apache.org/ |
| INFO - openejb.home = c:\oss\openejb3\examples\injection-of-env-entry |
| INFO - openejb.base = c:\oss\openejb3\examples\injection-of-env-entry |
| WARN - Cannot find the configuration file [conf/openejb.xml]. Will attempt to create one for the beans deployed. |
| 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: c:\oss\openejb3\examples\injection-of-env-entry\target\classes |
| INFO - Configuring app: c:\oss\openejb3\examples\injection-of-env-entry\target\classes |
| INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container) |
| INFO - Auto-creating a container for bean InvoiceBean: Container(type=STATEFUL, id=Default Stateful Container) |
| INFO - Loaded Module: c:\oss\openejb3\examples\injection-of-env-entry\target\classes |
| INFO - Assembling app: c:\oss\openejb3\examples\injection-of-env-entry\target\classes |
| INFO - Jndi(name=InvoiceBeanRemote) --> Ejb(deployment-id=InvoiceBean) |
| INFO - Jndi(name=PurchaseOrderBeanRemote) --> Ejb(deployment-id=PurchaseOrderBean) |
| INFO - Created Ejb(deployment-id=InvoiceBean, ejb-name=InvoiceBean, container=Default Stateful Container) |
| INFO - Created Ejb(deployment-id=PurchaseOrderBean, ejb-name=PurchaseOrderBean, container=Default Stateful Container) |
| INFO - Deployed Application(path=c:\oss\openejb3\examples\injection-of-env-entry\target\classes) |
| INFO - OpenEJB ready. |
| OpenEJB ready. |
| Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.859 sec |
| Running org.superbiz.injection.InvoiceBeanTest |
| Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec |
| |
| Results : |
| |
| Tests run: 4, 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"> </TD> |
| <TD class="Col2"> </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=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"> </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> |