Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

From OSGi to GlassFish in 5 Steps

07.23.2008
| 20454 views |
  • submit to reddit

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.

AttachmentSize
extending-glassfish-osgi.png113.58 KB
extending-glassfish-osgi-2.png71.02 KB
extending-glassfish-osgi-3.png29.81 KB
Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Arun Gupta replied on Thu, 2008/07/24 - 6:56am

Nice entry Geertjan! The development builds are already using Felix by default. The workspace can be checked out and built as explained at:

http://blogs.sun.com/arungupta/entry/totd_33_building_glassfish_v3

William Houghtaling replied on Mon, 2011/07/04 - 6:39am

Nice tips and the steps are well instructed. It is really nice that they announce that there will be a connection between the two because of what I know that there are some codes the enables Glassfish to run on an OSGi R4 platform. Gauthier, Houghtaling and Williams

Carla Brian replied on Mon, 2012/05/07 - 9:34am

If you develop enterprise Java applications, being a GlassFish community member gives you an early opportunity to develop those applications and test them for conformance to Java EE 5.  - Paul Perito

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.