Over a million developers have joined DZone.

New Cool Tools for OSGi Developers

DZone's Guide to

New Cool Tools for OSGi Developers

· Java Zone
Free Resource

Managing a MongoDB deployment? Take a load off and live migrate to MongoDB Atlas, the official automated service, with little to no downtime.

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;

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

    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 {

    public void actionPerformed(ActionEvent e) {
        StringBuilder sb = new StringBuilder();
        for (Bundle b : Installer.bc.getBundles()) {
            if (b.getState() != Bundle.ACTIVE) {
            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.


MongoDB Atlas is the easiest way to run the fastest-growing database for modern applications — no installation, setup, or configuration required. Easily live migrate an existing workload or start with 512MB of storage for free.


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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


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

{{ parent.tldr }}

{{ parent.urlSource.name }}