Configure JNDI Names Using Open EJB
I am trying to (unit) test an EJB class without having to start my websphere environment. I'm using Open EJB now , but there are some JNDI name resolution issues for other EJBs being used in my EJB ... and there is no way for me to inject mocking classes from my test right now.
Getting InitialContext
final Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
properties.setProperty("log4j.category.OpenEJB.options ", "debug");
properties.setProperty("log4j.category.OpenEJB.startup ", "debug");
properties.setProperty("log4j.category.OpenEJB.startup.config ", "debug");
properties.setProperty("MyOwnDatasource.JdbcDriver ", "com.ibm.as400.access.AS400JDBCDriver");
properties.setProperty("MyOwnDataSource.JdbcUrl ", "jdbc:as400:MYHOSTNAME;database name=MYDATABASE;libraries=MYDEFAULTTABLE");
ic = new InitialContext(properties);
There is a search inside my tested class java:comp/env/ejb/PrefixEjbNameLocalHome
and I cannot install Open EJB to generate JNDI names in this format.
Additional property for JNDI name format
I tried to set the formatting rule like this:
properties.setProperty("openejb.jndiname.format ", "comp/env/ejb/{interfaceClass}");
Properties not used?
Also, the registration configuration is not used. I only see INFO and WARN messages from Open EJB, although I have installed log4j.category.OpenEJB.*
and the like in DEBUG or TRACE.
a source to share
This is the "java:" part that messed up your test case. Basically Context.INITIAL_CONTEXT_FACTORY and "java:" are mutually exclusive. The InitialContext class has a special understanding of "java:" or any "foo:" lookup, and if they are at the beginning of the name, it will not use the INITIAL_CONTEXT_FACTORY you specified. A somewhat disappointing part of JNDI.
If you are looking for the name exactly as printed in the magazine, it will work. So for example this log message:
INFO - Jndi(name=WidgetBeanRemote) --> Ejb(deployment-id=WidgetBean)
Then in code:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
// set any other properties you want
Context context = new InitialContext(p);
Object o = context.lookup("WidgetBeanRemote");
a source to share