Extending GlassFish v3 via the NetBeans Platform's OSGi Support

DZone 's Guide to

Extending GlassFish v3 via the NetBeans Platform's OSGi Support

· Java Zone ·
Free Resource

The first problem you encounter when trying to use NetBeans IDE to extend GlassFish is that GlassFish, while extendable (hurray), is only extendable via OSGi bundles. And NetBeans IDE doesn't let you create OSGi bundles. Or... doesn't it?

  1. Read NetBeans and OSGi, publicly available on the NetBeans Wiki. Then use the Mercurial command "hg http://hg.netbeans.org/netigso/" to download the OSGi-supporting version of the NetBeans sources. Then build those sources via "ant build-nozip" in the download's root directory. Then start up the IDE that is created in the "nbbuild" folder. This procedure is very simple, though it may take some time for the download to complete.

  2. So, while you're waiting for the download to complete, read From OSGi to GlassFish in 5 Steps. We're going to do the same thing as described there, except that we'll be using NetBeans IDE to create our OSGi bundle.

  3. Create a new NetBeans module project in NetBeans IDE. Switch to the IDE's Files window and put "felix.jar", which is in your GlassFish distribution, into  a newly created folder structure, "release/modules/ext". Add this right above the closing </data> tag in your project.xml file:

    Then add this line to the project.properties file:

    cp.extra= release/modules/ext/felix.jar

    Now the OSGi classes from the felix.jar are available to your module.

  4. Create your Java class/es that extend GlassFish in some way. For this example, we'll simply create a "org.osgi.framework.BundleActivator" implementation that will produce some data when GlassFish starts, as done in the article referred to above:
    package org.demo.gfext;

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

    public class App implements BundleActivator {

    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("User Name: " + userName);
    System.out.println("User Dir: " + userDir);
    System.out.println("User Home: " + userHome);

    public void stop(BundleContext context) throws Exception {

  5. Now rewrite the Manifest.mf so that it has exactly this content (no more and no less than exactly this):
    Manifest-Version: 1.0
    Bundle-SymbolicName: org.demo.gfext
    Export-Package: org.demo.gfext
    Bundle-Activator: org.demo.gfext.App

    The "Bundle-SymbolicName" is the code name base of the module; the "Export-Package" is the package containing our BundleActivator; "Bundle-Activator" is the FQN to the bundle activator.

  6. Build the module and look in the Files window, inside "build/cluster/modules", where you'll find your JAR. That JAR is your OSGi bundle, thanks to the Manifest entry "Bundle-SymbolicName".

  7. Put the JAR into the folder described in From OSGi to GlassFish in 5 Steps and register it in the config.properties file, which is also described in the article.

Now start GlassFish (which automatically starts up on Felix) and you'll see the messages from the BundleActivator.

And this is my 200th article on DZone, it seems! Is there a better way to celebrate that number than to write about OSGi and NetBeans Platform integration? :-) 



Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}