Search code examples
javaapache-camelosgiguicejndi

How to create multiple InitialContext(s) in a single jvm runtime?


Anyone know if this is possible???

I have a situation where an application is booted using google Guice. By default it creates an InitialContext using org.apache.camel.guice.jndi.GuiceInitialContextFactory, which is burried in camel-guice startup logic.

I'm running it in karaf, which has it's own way of creating an InitialContext via

javax.naming.spi.NamingManager calling org.apache.aries.jndi.OSGiInitialContextFactoryBuilder etc

What happens is , when I call InitialContext = new InitialContext() from a bundle Activator , i get a reference to karaf's context, BUT if I do NOT do this and just create a org.apache.camel.guice.Main to start the app, so long as org.apache.camel.guice.jndi.GuiceInitialContextFactory is visible to classloader, it will create guice context correctly, start the app, but I will not be able to "see" karaf's initial context from "guiced" classes.

So what I need to be able to do is to have Guice startup using it's GuiceInitialContextFactory as described above , but also be able to get a reference to kararf's InitialContext... I'm not too familiar with the APIs for doing this.


Solution

  • This can be done BUT in osgi only. Note that this is impossible in a "normal" java environment because InitialContext initialization is static (1 per JVM runtime)

    In case you want to do this in your osgi environemnt, you need to implement the InitialContextFactoryBuilder interface , create and return initial context that you require using the methods provided. Please see osgi compendium 5 spec, specifically section 126 "JNDI Services Specification" for instructions.