blob: 9c152228a4b2c673606381c04c745c649d89ed79 [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 - Service Locator</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 -->
Service Locator
</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">
<P>The functionality of the <A href="jndi-names.html" title="JNDI Names">openejb.jndiname.format</A> allows for writing some really fun service locator code. Creating the exact layout you want using the exact data you want means you can create robust libraries for pulling things out of JNDI.</P>
<H1><A name="ServiceLocator-Lookupexamples"></A>Lookup examples</H1>
<P>To get the creative juices flowing here are a few examples of lookup methods you could create for your service locator, the jndi name formats that would work with those lookups, and examples of client code using the service locator. For simplicity, we'll assume all the lookup examples start with this basic class that has a built-in lookup allowing for a common prefix to be optionally applied to the beginning of all lookup strings.</P>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeHeader panelHeader" style="border-bottom-width: 1px;"><B>MyLocator.java</B></DIV><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> class MyLocator {
<SPAN class="code-keyword">private</SPAN> <SPAN class="code-keyword">final</SPAN> Context context;
<SPAN class="code-keyword">public</SPAN> MyLocator() <SPAN class="code-keyword">throws</SPAN> NamingException {
<SPAN class="code-keyword">this</SPAN>(<SPAN class="code-keyword">null</SPAN>);
}
<SPAN class="code-keyword">public</SPAN> MyLocator(<SPAN class="code-object">String</SPAN> commonPrefix) <SPAN class="code-keyword">throws</SPAN> NamingException {
Properties properties = <SPAN class="code-keyword">new</SPAN> Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, <SPAN class="code-quote">&quot;org.apache.openejb.client.RemoteInitialContextFactory&quot;</SPAN>);
properties.put(Context.PROVIDER_URL, <SPAN class="code-quote">&quot;ejbd:<SPAN class="code-comment">//localhost:4201/&quot;</SPAN>);
</SPAN> <SPAN class="code-keyword">this</SPAN>.context = <SPAN class="code-keyword">new</SPAN> InitialContext(properties);
}
<SPAN class="code-keyword">public</SPAN> <SPAN class="code-object">Object</SPAN> lookup(<SPAN class="code-object">String</SPAN> name) {
<SPAN class="code-keyword">try</SPAN> {
<SPAN class="code-keyword">if</SPAN> (commonPrefix != <SPAN class="code-keyword">null</SPAN>) name = commonPrefix + <SPAN class="code-quote">&quot;/&quot;</SPAN> +name;
<SPAN class="code-keyword">return</SPAN> context.lookup(name);
} <SPAN class="code-keyword">catch</SPAN> (NamingException e) {
<SPAN class="code-keyword">throw</SPAN> <SPAN class="code-keyword">new</SPAN> IllegalArgumentException(e);
}
}
}
</PRE>
</DIV></DIV>
<H2><A name="ServiceLocator-Justtheinterface"></A>Just the interface</H2>
<P>Usable with JNDI name formats ending in the full class name of the interface such as:</P>
<UL class="alternate" type="square">
<LI>{interfaceClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> &lt;T&gt; T lookup(<SPAN class="code-object">Class</SPAN>&lt;T&gt; type) {
<SPAN class="code-keyword">return</SPAN> (T) lookup(type.getName());
}
</PRE>
</DIV></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator();
Widget widget = locator.lookup(Widget.class);
</PRE>
</DIV></DIV>
<P>Or with a common prefix or with a common prefix supplied in constructor such as:</P>
<UL class="alternate" type="square">
<LI>{moduleId}/{interfaceClass}</LI>
<LI>ejb/{moduleId}/{interfaceClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator(<SPAN class="code-quote">&quot;ejb/superbiz&quot;</SPAN>);
Widget widget = locator.lookup(Widget.class);
Store store = locator.lookup(Store.class);
</PRE>
</DIV></DIV>
<H2><A name="ServiceLocator-Interfaceclassandaprefix"></A>Interface class and a prefix</H2>
<P>Usable with JNDI name formats including a varying prefix such as ejbName or deploymentID<BR>
and ending in the full class name of the interface</P>
<UL class="alternate" type="square">
<LI>{ejbName}/{interfaceClass}</LI>
<LI>{deploymentId}/{interfaceClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> &lt;T&gt; T lookup(<SPAN class="code-object">String</SPAN> prefix, <SPAN class="code-object">Class</SPAN>&lt;T&gt; type) {
<SPAN class="code-keyword">return</SPAN> (T) lookup(prefix + <SPAN class="code-quote">&quot;/&quot;</SPAN> + type.getName());
}
</PRE>
</DIV></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator();
Widget redWidget = locator.lookup(<SPAN class="code-quote">&quot;RedWidgetBean&quot;</SPAN>, Widget.class);
Widget blueWidget = locator.lookup(<SPAN class="code-quote">&quot;BlueWidgetBean&quot;</SPAN>, Widget.class);
</PRE>
</DIV></DIV>
<P>Or with a common prefix or with a common prefix supplied in constructor such as:</P>
<UL class="alternate" type="square">
<LI>{moduleId}/{ejbName}/{interfaceClass}</LI>
<LI>ejb/{moduleId}/{deploymentId}/{interfaceClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator(<SPAN class="code-quote">&quot;accountingApp&quot;</SPAN>);
Widget widget = locator.lookup(<SPAN class="code-quote">&quot;RedWidgetBean&quot;</SPAN>, Widget.class);
Store store = locator.lookup(<SPAN class="code-quote">&quot;StoreBean&quot;</SPAN>, Store.class);
</PRE>
</DIV></DIV>
<H2><A name="ServiceLocator-Interfaceclassandejbclass"></A>Interface class and ejb class</H2>
<P>Usable with JNDI name formats comprised of the interfaceClass and ejbClass</P>
<P>For variation, the interface class is the prefix and the ejb class is the<BR>
suffix. This is neat as the the prefix (the interface class name) becomes<BR>
a jndi context with one binding in it for each implementing ejb class.</P>
<P>Works with:</P>
<UL class="alternate" type="square">
<LI>{interfaceClass}/{ejbClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> &lt;T&gt; T lookup(<SPAN class="code-object">Class</SPAN>&lt;T&gt; type, <SPAN class="code-object">Class</SPAN> ejbClass) {
<SPAN class="code-keyword">return</SPAN> (T) lookup(type.getName() + <SPAN class="code-quote">&quot;/&quot;</SPAN> + ejbClass.getName());
}
</PRE>
</DIV></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator();
Widget widget = locator.lookup(Widget.class, BlueWidgetBean.class);
</PRE>
</DIV></DIV>
<P>Or with a common prefix or with a common prefix supplied in constructor such as:</P>
<UL class="alternate" type="square">
<LI>{moduleId}/{interfaceClass}/{ejbClass}</LI>
<LI>ejb/{moduleId}/{interfaceClass}/{ejbClass}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator(<SPAN class="code-quote">&quot;ejb/purchasingApp&quot;</SPAN>);
Widget widget = locator.lookup(Widget.class, RedWidgetBean.class);
Store store = locator.lookup(Store.class, StoreBean.class);
</PRE>
</DIV></DIV>
<H2><A name="ServiceLocator-Interfaceclassandejbclasswithsimplenames"></A>Interface class and ejb class with simple names</H2>
<P>Similar to the above example but using the simple name of the classes resulting<BR>
in a JNDI tree that's a bit more human readable.</P>
<UL class="alternate" type="square">
<LI>{ejbClass.simpleName}/{interfaceClass.simpleName}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
<SPAN class="code-keyword">public</SPAN> &lt;T&gt; T lookup(<SPAN class="code-object">Class</SPAN> ejbClass, <SPAN class="code-object">Class</SPAN>&lt;T&gt; type) {
<SPAN class="code-keyword">return</SPAN> (T) lookup(ejbClass.getSimpleName() + &quot;&quot; + type.getSimpleName());
}
</PRE>
</DIV></DIV>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator();
Widget widget = locator.lookup(RedWidgetBean.class, Widget.class);
</PRE>
</DIV></DIV>
<P>Or with a common prefix or with a common prefix supplied in constructor such as:</P>
<UL class="alternate" type="square">
<LI>{moduleId}/{ejbClass.simpleName}/{interfaceClass.simpleName}</LI>
<LI>ejb/{moduleId}/{ejbClass.simpleName}/{interfaceClass.simpleName}</LI>
</UL>
<DIV class="code panel" style="border-width: 1px;"><DIV class="codeContent panelContent">
<PRE class="code-java">
MyLocator locator = <SPAN class="code-keyword">new</SPAN> MyLocator(<SPAN class="code-quote">&quot;shippingApp&quot;</SPAN>);
Widget widget = locator.lookup(GreenWidgetBean.class, Widget.class);
Store store = locator.lookup(SuperStoreBean.class, Store.class);
</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=Service%20Locator">[ 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>