You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by ManojS <ma...@yahoo.co.in> on 2008/05/03 07:19:59 UTC

Service locator problem while EJB lookup ...

Hi,

I have an issue while looking up an EJB using service locator pattern. I
have a "ServiceLocator" class which is being used everywhere in my code to
lookup any EJB. But now when I use openEJB (version 3.0) it is throwing a
"javax.naming.NamingException" with message, "Name GenConfigFacadeEJB not
found".

My service locator class is as follows:
//imports I removed for here ...
public class ServiceLocator {
	private static Context context = null;
	static {
		System.setProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
"org.openejb.client.LocalInitialContextFactory");
		System.setProperty( javax.naming.Context.URL_PKG_PREFIXES,
"org.openejb.client" );
		context =  new InitialContext();
	}
	public static EJBHome getEJB(String ejbName, Class homeClass) throws
NamingException {
		Object objref = null;
		try {
			objref = context.lookup(ejbName);
		} catch (NamingException e) {
			objref = context.lookup("java:comp/env/" + ejbName);
		}
		EJBHome home = (EJBHome)PortableRemoteObject.narrow(objref, homeClass);
		return home;
	}
}

In other places wherever I need to access the EJB, I will be calling this
API as follows:
	ServiceLocator.getEJB("GenConfigFacadeEJB",GenConfigFacadeHome.class);

My ejb-jar.xml is as follows:

<ejb-jar>
	<enterprise-beans>
	<session>
		<ejb-name>GenConfigFacadeEJB</ejb-name>
		<home>com.company.GenConfigFacadeHome</home>
		<remote>com.company.GenConfigFacade</remote>
		<local-home>com.company.GenConfigFacadeLocalHome</local-home>
		<local>com.company.GenConfigFacadeLocal</local>
		<ejb-class>com.company.GenConfigFacadeEJB</ejb-class>
		<session-type>Stateless</session-type>
		<transaction-type>Container</transaction-type>
	</session> 
	........
	</enterprise-beans>
</ejb-jar>

And my openejb-jar.xml configuration is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<openejb-jar>
	  <properties>
	     openejb.deploymentId.format = {ejbName}
	     openejb.jndiname.format = {deploymentId}
	  </properties>
</openejb-jar>

I am using openEJB embedded in Tomcat 6.0.16. While starting the tomcat, it
is showing that this respective EJB is deployed, as follows:
34391 INFO  [main] OpenEJB.startup - Created
Ejb(deployment-id=GenConfigFacadeEJB, ejb-name=GenConfigFacadeEJB,
container=Default Stateless Container)

Can anyone help me to find out why the service locator class is failing to
find the EJB objects ?

Thanks in advance.

Manoj.

-- 
View this message in context: http://www.nabble.com/Service-locator-problem-while-EJB-lookup-...-tp17031873p17031873.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: Service locator problem while EJB lookup ...

Posted by ManojS <ma...@yahoo.co.in>.
David,

Thank you for the solution. It worked.

However, I have got an another issue (not an issue, but really a
requirement). I can ask that question here for your opinion, meanwhile I
will understand openejb better and experiment in my own way to solve that
problem. I was trying to integrate an application into openejb. That
application have different modules and one module is compiled in java 1.4
and have its own session beans (ofcourse, the code is not with me, and that
module jar provided will not have an ejb-jar.xml inside, instead, the main
application add the required beans to its ejb-jar.xml). Now the problem is,
I want to use the datasources configured in the openejb.xml file in those
session beans also. Because that module is already coded to do a jndi lookup
and get the datasource.

Any suggestion/help/advice from anyone are highly appreciated.

Manoj.

-- 
View this message in context: http://www.nabble.com/Service-locator-problem-while-EJB-lookup-...-tp17031873p17064611.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: Service locator problem while EJB lookup ...

Posted by David Blevins <da...@visi.com>.
On May 2, 2008, at 10:19 PM, ManojS wrote:

>
> Hi,
>
> I have an issue while looking up an EJB using service locator  
> pattern. I
> have a "ServiceLocator" class which is being used everywhere in my  
> code to
> lookup any EJB. But now when I use openEJB (version 3.0) it is  
> throwing a
> "javax.naming.NamingException" with message, "Name  
> GenConfigFacadeEJB not
> found".
>
> My service locator class is as follows:
> //imports I removed for here ...
> public class ServiceLocator {
> 	private static Context context = null;
> 	static {
> 		System.setProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
> "org.openejb.client.LocalInitialContextFactory");
> 		System.setProperty( javax.naming.Context.URL_PKG_PREFIXES,
> "org.openejb.client" );
> 		context =  new InitialContext();
> 	}

It's likely that the vm has already pulled and cached the value of  
INITIAL_CONTEXT_FACTORY, it looks for the default only once and is  
probably pointing to Tomcat.  If the NameNotFoundException stack trace  
doesn't show any openejb classes, this is definitely what happened.

Best bet is to construct your InitialContext like so:

   static {
       Properties p = new Properties();
       p.setProperty( javax.naming.Context.INITIAL_CONTEXT_FACTORY,
                      "org.openejb.client.LocalInitialContextFactory");
       context =  new InitialContext(p);
   }

I left the URL_PKG_PREFIXES property out as I know that one is pulled  
and cached very early in startup and Tomcat owns the "java:" prefix,  
which is good.

That should fix the issue.

I like your service locator approach.  Some recommendations for you on  
that.

updated openejb-jar.xml:

   <?xml version="1.0" encoding="UTF-8"?>
   <openejb-jar>
     <properties>
       openejb.deploymentId.format = {ejbName}
       openejb.jndiname.format = {deploymentId}/{interfaceClass}
     </properties>
   </openejb-jar>

updated service locator using generics:

     public static <T extends EJBHome> T getEJB(String ejbName,  
Class<T> homeClass) throws NamingException {
         Object objref = null;
         try {
             objref = context.lookup(ejbName + "/" +  
homeClass.getName());
         } catch (NamingException e) {
             objref = context.lookup("java:comp/env/" + ejbName);
         }
         T home = (T) PortableRemoteObject.narrow(objref, homeClass);
         return home;
     }

then it can be used with no cast as:

     GenConfigFacadeHome home =  
ServiceLocator.getEJB("GenConfigFacadeEJB",GenConfigFacadeHome.class);


Reason for the updated openejb.jndiname.format is that you have two  
interface one local and one remote and with the format set to  
{deploymentId} only one interface will be bound.  Since your locator  
uses ejbName and home interface class, using the same pattern in your  
jndiname.format has advantages.

The updated getEJB method signature with <T extends EJBHome> is  
identical to having the method return EJBHome, which is why I left it  
that way when making it generic.  But, if you yank the "extends  
EJBHome" you can use the same method to lookup your EJBLocalHome  
interaces as well.  So that'd be:

     public static <T> T getEJB(String ejbName, Class<T> homeClass)  
throws NamingException {...}


-David