OSGi Hello World with NetBeans 6.9

Since version 6.9, NetBeans offers the possibility to work with OSGi bundles. The creation of the project is relatively simple. You just need to go to new Project > Maven > Maven OSGi Bundle like this :

The generated pom.xml begins this way:

<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>net.stephou.osgi</groupId>
 <artifactId>HelloWorld</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>bundle</packaging>
 <name>OSGiTest OSGi Bundle</name>

 <dependencies>
  <dependency>
   <groupId>org.apache.felix</groupId>
   <artifactId>org.osgi.core</artifactId>
   <version>1.4.0</version>
  </dependency>
 </dependencies>
...

We notice there is only one dependency to the OSGi core and that the packaging is “bundle”. The rest of the pom is the run-on-felix profile configuration for NetBeans.

To display a HelloWorld in Apache Felix console, we need to add a HelloWorld java class. So, right-click on the source package and go to New > Other > OSGI > Bundle Activator like this :

Here is the code for the class after adding something for the console :

package net.stephou.osgi;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

/**

*

* @author Stephou

*/

public class HelloWorld implements BundleActivator {

   public void start(BundleContext context) throws Exception {

     System.out.println("Hello OSGi World");

   }

   public void stop(BundleContext context) throws Exception { 

     System.out.println("Goodbye OSGi World");

   }
}

In the pom.xml, you can see the Bundle-Activator line has been added for the maven-bundle-plugin :

<plugin>
 <groupId>org.apache.felix</groupId>
 <artifactId>maven-bundle-plugin</artifactId>
 <version>2.0.1</version>
 <extensions>true</extensions>
 <configuration>
 <instructions>
   <Bundle-Activator>net.stephou.osgi.helloworld.HelloWorldActivator</Bundle-Activator>
 </instructions>
 </configuration>
 </plugin>

Just Build and Run your project in NetBeans and you’ll see the following in the console :

Welcome to Felix
================

Hello OSGi World

Congratulation 😉

Advertisements

One thought on “OSGi Hello World with NetBeans 6.9

  1. Hi Stephou

    Thanks for the simple example. Unfortunately I encountered some show-stoppers using Netbeans v6.9 more or less out of the box. The most visible difference was that the ‘current’ OSGi bundle template only added a HelloWorld to the section and didn’t make a HelloWorldActivator

    My package was called lexicon.felixicon …

    org.vogue.lexicon.felixicon.HelloWorld

    So I did some digging but I still didn’t find the templates yet in the configuration area: Tools / Templates (as yet).

    The second set of problems comes with the generated POM file. It has a set of Warnings from the initial build/generate with invalid reference or macro values; such as “${project.build.directory}” (e.g.)

    [WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 63,column 57] : ${project.build.directory} is not a valid reference.
    [WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 66,column 57] : ${plugins} is not a valid reference.
    [WARNING] org.apache.velocity.runtime.exception.ReferenceException: reference : template = archetype-resources/pom.xml [line 67,column 56] : ${project.build.directory} is not a valid reference.

    Again, I didn’t find how to dig-out where the template is kept (via tools). But also do you know where the values for “${project.build.directory}” can be managed? I looked through Options, didn’t find any.

    I’m wondering if they belong in the local Felix config file or something non-Netbeans and/or something in the Maven or OSGi arena? Any ideas?

    One final point, was that the Felix version ‘inside’ Netbeans is quite mature. Is there some method you know of to build a runtime for a current Felix release and bundles for that as a application? Perhaps a pointer to a tutorial, or something for getting started?

    I like the start this simple example gave me, it at least left me with a small manageable set of problems to resolve. Thanks again.

    Cheers,
    Will

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s