Welcome to my first topic on OSGi. This article is focused on using Netbeans to create an OSGi declarative service deployed on Glassfish which is of course built on Apache Felix - an OSGi framework implementation.
According to Craig Walls in the book "Modular Java - Creating Flexible Applications with OSGi and Spring", OSGi is a component framework specification that brings modularity to the Java platform. OSGi enables the creation of highly cohesive, loosely coupled modules that can be composed into larger applications. Each module can be individually developed, tested, deployed, updated and managed with minimal or no impact to the other modules. Hence, OSGi is a solution to Java's modular incapabilities.
OSGi Declarative service
The knowledge of the OSGi Declarative service is not that far fetched. The basics of this topic can be found at Jerome's blog and Neil Bartlett blog. Like I said earlier, this article is focused on using Netbeans to perform the same task created with Eclipse by Neil Bartlett, but with an Apache Felix flavour.
Use of Maven
The Apache Maven "build tool" or "project management tool" is the primary tool I will be using for this application. More information about Apache Maven can be found here
Howto with Netbeans
So, getting started with this, this example assumes you are using Netbeans 6.8. The latest Glassfish is bundled with the Apache Felix OSGi runtime. But the OSGi declarative support is not. So firstly, download the Apache Felix SCR plugin here.
Before we start to write the application, let's deploy the downloaded scr plugin to Glassfish.
Note: If you encounter RuntimeException and CommandException,
To confirm that you have successfully deployed the org.apache.felix.scr-1.4.0.jar plugin, check the Glassfish autostart directory at <path to Glassfish>/modules/autostart
Now, let's write a simple OSGi service and a client to access the bundle.
Note: the following directory structure in Netbeans
Create a Java interface ComputeService.java and copy the following code into it.
Create a Java class ComputeServiceImpl.java in the package com.mycompany..mycomputeservice.impl and copy the following code into it.
In the OSGI-INF directory, create an XML document computeservice.xml and add the following code;
Note: Take note of the name of this component (decl-compservice). It is used as an identifier for a client application.
In the META-INF directory, create a manifest file MANIFEST.MF to add the metadata about the bundle, and add the following code;
Modify the pom.xml file by adding the maven-jar-plugin plugin. The pom.xml(POM meaning Project Object Model) file stores information about the project. There's no Maven without a pom.xml file. Inside the pom.xml file, add the following code within the <project></project>;
Check the output console
This concludes the first phase of the project. In the next, we will write a client that will utilize this service and it's methods.