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

Extending GlassFish v3 via the NetBeans Platform's OSGi Support

02.16.2009
| 22388 views |
  • submit to reddit

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:
    <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.

  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 {

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

    }
  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? :-) 

 

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

Comments

Wai Ho replied on Sat, 2009/02/14 - 12:52pm

Excellent!

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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jhserv</groupId>
<artifactId>OsgiTesterModule</artifactId>
<packaging>bundle</packaging>
<version>1.0-SNAPSHOT</version>
<name>OsgiTesterModule</name>
<description>Just a test Module to test things out.</description>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.3</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Private-Package>org.jhserv.OsgiTesterModule.*</Private-Package>
<Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
<Bundle-Activator>org.jhserv.OsgiTesterModule.Activator</Bundle-Activator>
</instructions>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

 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: Richard D. Jackson

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

Personally, I haven't swallowed the Maven pill yet. :-) Though, I can see its advantages.

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: Stuart Mcculloch

Thanks for the links. I knew about the construct one but have not had the time to use it yet. I did not know about the Spring one though and it looks to be more what I would be interested in. I will defenatly take a look at it.

Richard D. Jackson replied on Mon, 2009/02/16 - 1:13pm in response to: Guido Amabili

Glad you liked that article (I wrote that when I first started to learn OSGi). And I do agree that there are some advantages to that aproch as well. But there are some disadvantages as well. After I get finished with some of the stuff I'm working on I really plan on digging into getting Netbeans to work better with OSGi. Mostly every thing works with Maven but I would really like to have things like deploying bundles, stopping and starting them. And of course debugging them with out having to go through the hopes you have to now or dropping down to the command line.

Guido Amabili replied on Tue, 2009/02/17 - 11:58am in response to: Richard D. Jackson

 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

 

 

Toni Epple replied on Thu, 2009/03/26 - 8:45am

You don't need to export your package in this example, do you?

Toni Epple replied on Thu, 2009/03/26 - 8:51am

I like the idea of reusing NetBeans Module Projects for creating OSGi Modules. NetBeans Modules and OSGi Bundles are so similar that you could probably also reuse the NB dependency management UI support for OSGi with only limited changes... (like adding some Ant Targets)

Mateo Gomez replied on Tue, 2012/06/19 - 2:49am in response to: Toni Epple

this explains it all Toni..im less confuse now  corn salsa recipe

Matt Coleman replied on Thu, 2013/01/10 - 12:10am in response to: Geertjan Wielenga

me too...i agre with you Geertjan!

buffalo freelance web designer 

Cata Nic replied on Mon, 2013/09/02 - 3:58am

 Is difficult to use this extension? I want to have a stable solution and I don't know if this one deserve my attention.

Comment viewing options

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