Over a million developers have joined DZone.

From OSGi to GlassFish in 5 Steps

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Sometime ago the new connection between GlassFish and OSGi was announced: you will be able to extend the upcoming GlassFish V3 via OSGi bundles. Though V3 isn't final yet, one can already get acquainted with it and, already, with its OSGi extensibility. The information on the practical steps of extending GlassFish via OSGi is currently not centralized, so here is a quick start, describing the absolute basics, from which one can extrapolate more meaningful scenarios.

At the end of this intro, you will (unlike everyone else in the world who does not have the OSGi bundle that you will create here) see your user name, user directory, and user home, whenever GlassFish starts up:

We will basically follow the instructions provided by Arun Gupta, which mostly worked for me, using the Maven Bundle Plugin.

  1. Create the POM.xml and other artifacts from the command line:
    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes 
    -DgroupId=org.glassfish.samples.osgi.helloworld -DartifactId=helloworld

  2. Open the pom.xml in the IDE of your choice, tweak it according to step 3 of Arun's instructions, and code the basic OSGi bundle's content:

    In the end, this was my class:

    package org.glassfish.samples.osgi.helloworld;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class App implements BundleActivator {

    @Override
    public void start(BundleContext context) throws Exception {
    String userName = context.getProperty("user.name");
    String userDir = context.getProperty("user.dir");
    String userHome = context.getProperty("user.home");
    System.out.println("------------------------------------------------------");
    System.out.println("User Name: " + userName);
    System.out.println("User Dir: " + userDir);
    System.out.println("User Home: " + userHome);
    System.out.println("------------------------------------------------------");
    }

    @Override
    public void stop(BundleContext context) throws Exception {
    }

    }
  3.  

  4. Run the "Install" goal (in NetBeans IDE, that means right-clicking the project and then choosing Custom | Goals and then running the Install goal) and put the JAR created by this process into this folder:
    GLASSFISH_HOME/glassfish/felix/bundle


    So, here you see my JAR, which is called "Greeter-0.1-SNAPSHOT.jar":

  5.  

  6. Open GLASSFISH_HOME/glassfish/felix/conf/config.properties and register the JAR there under "felix.auto.start", i.e., look at the final line below:
    felix.auto.start.1= \
    ${com.sun.aas.installRootURI}/modules/tiger-types-osgi-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/auto-depends-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/config-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/hk2-core-0.2.1.jar \
    ${com.sun.aas.installRootURI}/modules/osgi-adapter-0.2.1.jar \
    ${com.sun.aas.installRootURI}/felix/bundle/Greeter-0.1-SNAPSHOT.jar
  7.  

  8. Soon (very soon!) GlassFish will start in Felix mode by default, as shown in Arun's blog entry. However, for now, run the following from the GLASSFISH_HOME/glassfish folder, i.e., for now, be aware of the "DGlassFish_Platform" switch:
    java -DGlassFish_Platform=Felix -jar modules/glassfish-10.0-tp-2-SNAPSHOT.jar

 

Then GlassFish starts up in Felix mode and the OSGi bundle that you registered earlier will simply be picked up as part of the start up process. And then you will see the messages you sent to yourself in the bundle.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}