On Jul 9, 2009, at 12:41 AM, Allan Lykke Christensen wrote:
public ContentService getRepository(String jndiName) throws
NamingException {
Context ctx = new InitialContext();
ContentService cs = (ContentService) ctx.lookup(jndiName);
return cs;
}
In my unit test I bind some "ContentService" objects to the
InitialContext
@Test
public void testGetRepository() {
Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
InitialContext initialContext = new InitialContext(p);
initialContext.bind("cs/MyObject", new ContentService("Something
something"));
ContentFacadeLocal bean = (ContentFacadeLocal)
getInitialContext().lookup(BEAN_INTERFACE);
try {
ContentService cs = bean.getRepository("cs/MyObject");
assertNotNull(cs);
} catch (NamingException ex) {
fail(ex.getMessage());
}
}
When I run the test a NamingException is thrown with the message
"javax.naming.NoInitialContextException: Need to specify class name
in environment or system property, or as an applet parameter, or in
an application resource file: java.naming.factory.initial"
Hi Allen,
Hmm.. I'm not sure I know how to advise. Binding things into JNDI and
looking them up inside beans is definitely non-compliant and not
portable. It can be done in OpenEJB with certain limitations, but I
wouldn't go there as my first choice. How exactly do you setup and
use this ContentService in your application when run outside OpenEJB?
Who normally creates these ContentService objects?
In terms of simplicity, not having things injected and instead looking
them up doesn't really change anything from a setup perspective.
Injection is really just syntactic sugar over JNDI, a JNDI lookup is
still done under the covers in exactly the same way as if you were to
look it up in the bean.
Looking forward to your feedback.
-David