tomcatservletsosgiequinoxembedded-osgi

JNDI lookup from OSGI equinox bundle deployed on tomcat (bridged mode)


I have my datasource configured in tomcat\context.xml. I have deployed a bridge.war provided by Eclipse to get servletbridge environment.

I have developed another osgi bundle which contains servlet registation code and my aim is to do JNDI look up for datasource from this servlet bundle.

However I get

javax.naming.NoInitialContextException:Cannot instantiate class: org.apache.naming.java.javaURLContextFactory (root cause classnotfound for org.apache.naming.java.javaURLContextFactory)

when I try following code in my OSGI bundle

        Context initContext = new InitialContext();
        Context envContext = (Context) initContext.lookup("java:/comp/env");
        DataSource ds = (DataSource) envContext.lookup("jdbc/TestDB");

I have added catalina.jar in my osgi container an also as a dependency in my osgi bundle.

My launch.ini of bridge.war looks like this:

osgi.*=@null
org.osgi.*=@null
eclipse.*=@null

osgi.parentClassloader=ext
osgi.contextClassLoaderParent=ext
org.osgi.framework.system.packages.extra=org.apache.naming.java

Can someone help me with it?


Solution

  • The parent of the embedded OSGi container is ext: The Java extension class loader

    To be able to see the classes of Tomcat, the parent classloader must be changed in launch.ini to fwk: The OSGi framework class loader.

    By doing that, the parent classloader of the embedded OSGi container be the classloader of the web application. If a bundle in the embedded OSGi container wants to use a class from the webapp or Tomcat directly, you might have to list the package of the class with org.osgi.system.packages.extra setting.

    More information about parent classloaders of embedded Equinox: http://wiki.eclipse.org/FAQ_How_do_I_add_a_library_to_the_classpath_of_a_plug-in%3F

    More information about the class loader hierarchy of Tomcat: http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html