Over a million developers have joined DZone.

From NetBeans Modules to OSGi and Back!

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The big blocker for some in relation to the NetBeans Platform is its lack of OSGi support. OSGi is used widely as the modular basis of applications, enabling incorporation of a large set of libraries created by developers all over the world. A simple requirement for application extensibility is that that extensibility be based on the most widely accepted standard. That being the case, the ability for the NetBeans Platform to make use of OSGi bundles is not only an indication of its having adopted the generally accepted standard but also an indication of the versatility of the NetBeans Platform, since backward compatibility is holy to the NetBeans engineers.

So, how can OSGi be supported by the NetBeans Platform while not undermining backward compatibility? By enabling the NetBeans modular architecture to understand OSGi, in such a way that both/either can be used within desktop applications. Why not simply use Eclipse RCP instead if OSGi is what you want? Because Swing, on top of which the NetBeans Platform is based, is the other generally accepted standard, while not being native to Eclipse RCP.

Hence, there is the Netigso build of the NetBeans IDE:

http://hudson.apidesign.org/job/netigso

It lets you combine NetBeans modules with OSGi bundles. (The name "Netigso" consists of "Net" from NetBeans, attached to "OSGi" spelled backwards.) For quite some time, that build has been broken. No more. It works now.

  1. Download the build above and then use it to create a new NetBeans module. In the manifest, change the "OpenIDE-Module" key to "Bundle-SymbolicName". Now, when you build the module, you will have an OSGi bundle because the NetBeans build system will create an OSGi bundle when the above key is set in the manifest.

  2. Let's now use some of the NetBeans API classes inside our OSGi bundle! Use the New Action wizard and then fill out the generated class like this:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import org.openide.DialogDisplayer;
    import org.openide.NotifyDescriptor;

    public final class SomeAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    NotifyDescriptor dd = new NotifyDescriptor.Message("Hello from " + getClass().getClassLoader());
    DialogDisplayer.getDefault().notify(dd);
    }

    }

    The New Action wizard registers the above automatically in the layer.xml file. (Isn't it cool that you can use a standard ActionListener to define your actions, rather than one that is specific to the NetBeans APIs?) Note also that the above implies that your module has a dependency on the NetBeans Dialogs API.

  3. Now run the module. A new NetBeans Platform application will start up and your OSGi bundle will be installed into it. That means that the NetBeans Platform's runtime container understands OSGi because it treats the OSGi bundle just like any other NetBeans module. Then, when you invoke the action provided by the OSGi bundle, causing the dialog to appear, you will see that OSGi is able to interact with the NetBeans Platform, since it needs to do so to be able to call into the NetBeans module that provides the Dialogs API.

Hurray. Now you're able to create complex desktop applications on top of OSGi and Swing, in a completely natural way, using NetBeans IDE. The result could be a robust and pluggable application, such as any of these. Meanwhile, backward compatibility is assured since NetBeans modules can be used exactly as before. 

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

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 }}