Hi David,

Thanks for your response. I realised that I was trying to over- engineer my code by having dynamic binding and look-up of objects using JNDI. It was not needed for the purpose of my application. Instead I've gone back to using a Resource Adapter for connecting to a Java Content Repository (JCR). Specifically, I'm using the Jackrabbit Content Repository. A JCA component was already provided (although a pain to install on Mac because of filename case issues) and I've managed to install it on GlassFish. This got me back to my original test case. Using the Resource Adapter (JCA 1.0), requires me to set-up a a Connector connection pool and resource which I bound to a JNDI name. The repository would then be available using the following DI:

@Resource(mappedName="jcr/repository", type=javax.jcr.Repository.class) private Repository rep;

But how do I tell OpenEJB about the resource so that I can add my own dummy repository before running my tests? Is it possible?

Thanks,
 Allan

On 14/07/2009, at 01.15, David Blevins wrote:


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


Reply via email to