Extending GlassFish v3 via the NetBeans Platform's OSGi Support
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?
- 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.
- 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.
- 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:
<class-path-extension>
<runtime-relative-path>ext/felix.jar</runtime-relative-path>
<binary-origin>release/modules/ext/felix.jar</binary-origin>
</class-path-extension>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.
- 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 {
@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 {
}
} - 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.AppThe "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.
- 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".
- 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? :-)
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
WaiHo replied on Sat, 2009/02/14 - 12:52pm
Richard D. Jackson replied on Sat, 2009/02/14 - 1:47pm
I have been using NetBeans for OSGi development for a while now. And by far the easist way of doing it is with the Maven Plugin and the maven-bundle-plugin. The only thing missing is that there is not a Maven archtype for OSGi bundles so you have to create a standard project then modify the pom file. I guess when I learn a little more about Maven I should write one. But anyways the basic pom should look something like this:
Once you have the pom setup correctly everything just works as you would expect. Other improvments needed are a way to deploy from the IDE again just need to learn a little more Maven. And of course debugging from the IDE.
Stuart McCulloch replied on Sat, 2009/02/14 - 11:24pm
in response to: rjackson
Actually there are a couple of Maven archetypes for OSGi bundles... there's the Spring-DM archetype:
http://static.springframework.org/osgi/docs/current/reference/html/appendix-archetype.html
and there's the OPS4J Pax-Construct project, which uses archetypes behind a script/plugin system:
http://www.ops4j.org/projects/pax/construct/
HTH
Geertjan Wielenga replied on Sun, 2009/02/15 - 4:44am
Guido Amabili replied on Sun, 2009/02/15 - 11:11am
At work I use NetBeans 6.1 along with bnd to develop my osgi bundles.
I configured felix as shown in this article "How-to integrate Felix in Netbeans" and tweaked my build xml to add additionnal targets such build bundle, deploy bundle.
Guido
Richard D. Jackson replied on Mon, 2009/02/16 - 1:06pm
in response to: mcculls
Richard D. Jackson replied on Mon, 2009/02/16 - 1:13pm
in response to: guidolx
Guido Amabili replied on Tue, 2009/02/17 - 11:58am
in response to: rjackson
Hi Richard,
Around that time(+- august 2008) I started to use OSGI Bundle, and I can tell you, your howto has been a GREAT help!
Thanks a lot!
Nice to hear of your plans of taking OSGI and NetBeans Integration to the next level.
(But that configuration still works perfectly for me :-)
Guido
eppleton replied on Thu, 2009/03/26 - 8:45am
eppleton replied on Thu, 2009/03/26 - 8:51am
timx replied on Tue, 2009/09/15 - 12:39pm
Nice guides, I will try it now.
Thank for share.
Bee Directory | EZ Directory | Ezinez Directory