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

New Cool Tools for OSGi Developers

02.13.2010
| 22761 views |
  • submit to reddit

In a few months, there'll be a bunch of new tools for OSGi developers, specifically for those who want to use Swing as their UI toolkit. (You could end up with advanced Swing applications just like these.) Development builds of the upcoming NetBeans IDE 6.9, which contains these tools, are already available and the screenshots below come from one of the recent development builds.

In short, the NetBeans Platform understands what an OSGi bundle is. As a result, you'll be able to import OSGi bundles into your NetBeans Platform applications. Below, you see a folder in GlassFish is selected. That folder contains the OSGi bundles provided by GlassFish, which (since GlassFish v3) runs on Felix:

Once you've selected the folder containing the OSGi bundles of interest, you're shown all the OSGi bundles contained within the folder:

Above, you can see that I have specified that three of the OSGi bundles in the GlassFish modules folder should be enabled within my application. Lazy loading of OSGi bundles is also supported, which is achieved by choosing "Autoload" instead of "Enabled".

Once the wizard above is completed, I can see a new cluster (i.e., a new set of related modules) added to the overview of the Libraries used by my application:

Now let's create an OSGi bundle that makes use of the OSGi bundles that we've imported. In the wizard where you'd normally create a NetBeans module, you can now also create an OSGi bundle:

At the end of the wizard above, your Manifest will be as follows:

Manifest-Version: 1.0
Bundle-Localization: org/demo/module/displayer/Bundle
Bundle-Name: %OpenIDE-Module-Name
Bundle-SymbolicName: org.demo.module.displayer
Bundle-Version: 1.0
Import-Package: org.osgi.framework

Next, let's add a BundleActivator. Here you go, a wizard to help you:

A BundleActivator skeleton class is created, registered in the manifest automatically (line 2 below):

Manifest-Version: 1.0
Bundle-Activator: org.demo.module.displayer.Installer
Bundle-Localization: org/demo/module/displayer/Bundle
Bundle-Name: %OpenIDE-Module-Name
Bundle-SymbolicName: org.demo.module.displayer
Bundle-Version: 1.0
Import-Package: org.osgi.framework

Let's add some code to our BundleActivator, while making sure to somehow expose the BundleContext to elsewhere within the module:

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

public class Installer implements BundleActivator {

    public static BundleContext bc;

    @Override
    public void start(BundleContext c) throws Exception {
        bc = c;
    }

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

Finally, we'll create an action in our application that will display the activated bundles to the end user. To this end, there's an Action wizard that creates a dummy ActionListener and registers it in the application's file system. 

We'll define the ActionListener as follows:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.osgi.framework.Bundle;

public final class DisplayBundlesAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        StringBuilder sb = new StringBuilder();
        for (Bundle b : Installer.bc.getBundles()) {
            if (b.getState() != Bundle.ACTIVE) {
                continue;
            }
            sb.append(b.getSymbolicName());
            sb.append("\n");
        }
        DialogDisplayer.getDefault().notify(
            new NotifyDescriptor.Message(sb)
        );
    }
   
}

Note that here we're using a plain old ActionListener class within our OSGi bundle. However, we're calling out to a NetBeans Platform module that contains the DialogDisplayer class (basically, a slightly fancier JOptionPane).

Run the application and invoke the action (using the location that you specified in the Action wizard, which registers the position in the file system via the layer.xml file, where you can modify the position, as needed). Now the Felix container starts up, as well as the NetBeans Platform. You can see the currently activated OSGi bundles, running in your Swing-based NetBeans Platform application:

Now that you've gone through this general overview, you're welcome to, for further exploration of all of this, read the ongoing OSGi technical plans in OSGiAndNetBeans.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Guido Amabili replied on Wed, 2010/04/14 - 1:47am

Hello Geertjan,

I have a question. I occasionaly develop OSGi bundles (not swing related development), so will there be some kind of support for it in the upcoming NetBeans ? (wizards like the one above to specify bundle-name,bundle-activator,dependencies).

Until now I had to include in my projects an ant task to generate a correct manifest.

thx,

Guido

Geertjan Wielenga replied on Tue, 2010/04/20 - 11:27am

It will be done automatically, Guido, via a wizard. Read here for more info:

http://netbeans.dzone.com/nb-osgi-web-development 

Mateo Gomez replied on Mon, 2012/07/16 - 5:34am

cool new tools...congratulations on these

mexican salsa recipes

Matt Coleman replied on Tue, 2012/07/17 - 1:31am in response to: Geertjan Wielenga

this being automatic is great..thanks u Geertjan

graphic design buffalo

Carla Brian replied on Sun, 2012/07/22 - 6:13pm

It looks good.  I bet these new tools are effective and reliable. I can't wait how to do this one. Looking forward for this. - Mercy Ministries

Comment viewing options

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