Wednesday, November 10, 2010

How to use Gemini Naming with Eclipse Virgo (Part 1)

First, let me get some definitions down.

What is Gemini Naming? It is a bridge to use JNDI in OSGi runtime. More information is available on the gemini naming site. You can also read up in OSGi Enterprise Specification section 126

What is Eclipse Virgo? This is a donated SpringSource dmServer. A really nice modular OSGi runtime/app server.

    Now let me explain why would I, being in sound state of mind would try to use JNDI in OSGi.

    Long version: I am very lazy and I also like to use other peoples stuff. That stuff is usually not OSGi aware and I am currently trying to use it in Virgo. Most of the time other peoples stuff also needs to be configured with a database connection information. Well, at least stuff that I wanted to use. That configuration can be conveniently provided with 5 to 10 entries in the property file or a single JNDI location string. I already have 5 to 10 entries in the property file to maintain, so I did not want to duplicate it to another property file to be missed during deployment.

    Short version: Because I can and hacking/learning stuff is fun.

    So without further delay, lets me explain how to deploy Gemini Naming and use it to look up entries from a traditional java app in the Virgo Web Server.

    Some preparation is needed:
    2. Unzip that bad boy to a location that we will call $VIRGO_HOME

    3. Download Gemini Naming (M1 at the time of the post - so artifact names might change)

    4. Unzip that bad boy as well.

    5. Copy org.eclipse.gemini.naming.impl-1.0.0.M01-incubation.jar to $VIRGO_HOME/lib/kernel


    6. Download APIs for OSGi enterprise bundle from the maven repo and save them to $VIRGO_HOME/lib/kernel

    Now we need to modify a few files. Use your editor of choice and modify $VIRGO_HOME/lib/org.eclipse.virgo.kernel.launch.properties to add the OSGi enterprise api bundle to the list of launcher bundles:

    launcher.bundles =\
    ....
    file:lib/kernel/org.osgi.enterprise-4.2.0.jar@start,\
    file:lib/kernel/org.apache.felix.configadmin-1.2.4.jar@start,\
    ....
    
    This takes care of the kernel region (Read on for more info on Virgo regions more info on Virgo regions). Now lets take care of user region.

    We need to modify $VIRGO_HOME/config/org.eclipse.virgo.kernel.userregion.properties.

    1. Let's add Gemini naming to the list of baseBundles in user region

    baseBundles = \
    ... ,\
    file:lib/kernel/org.eclipse.gemini.naming.impl-1.0.0.M01-incubation.jarr@start
    

    2. We need to import a package into the user region from the kernel and that is done by modifying the packageImports property:

    packageImports =\
    ..... ,\
    org.osgi.service.jndi;version="0"
    

    This package comes from the osgi enterprise bundle deployed in the kernel region and allows us to keep a single version of the bundle deployed in both regions vs. each having an individual copy.

    3. Let's enable osgi console at the same time so we can examine the list of services:

    osgi.console=2401
    

    Now you can start the server and see the JNDI services deployed.

    cd $VIRGO_HOME/bin/
    ./startup.sh -clean
    

    Connect to telnet console:

    $ telnet localhost 2401
    osgi> vsh service list
    

    You should see entries:

    ....
    51 javax.naming.spi.ObjectFactory                                    4
    52 javax.naming.spi.InitialContextFactoryBuilder                     4
    53 org.osgi.service.jndi.JNDIContextManager                          4
    54 org.osgi.service.jndi.JNDIProviderAdmin                           4
    

    That is it. Now you can use JNDIContextManager and JNDIProviderAdmin to get a reference to InitialContext or look up services with "osgi:service/" schema name.

    Well... You almost can. But that is topic for part 2. Stay tuned.  Hint Tomcat gets in the way (see here)

    No comments: