Building an OSGi declarative service with Maven using Netbeans

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.

 

OSGi defined

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.

  • cd to Glassfish bin directory;
    • On Windows: <glassfish-installation-directory>\sges-v3\bin
      On Solaris/UNIX/Mac OS X: <glassfish-installation-directory>/sges-v3/bin
  • Run the command
    • asadmin deploy --type osgi $HOME\org.apache.felix.scr-1.4.0.jar. Where $HOME is the directory to which you downloaded the org.apache.felix.scr-1.4.0.jar file.

Note: If you encounter RuntimeException and CommandException,

  • cross check the command you entered.
  • the org.apache.felix.scr-1.4.0.jar file is not already in Glassfish directory before you run the command.
  • Restart Glassfish before you try the command again.

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.

 

In Netbeans:

 

 

Start a new Maven project

Maven archetype in Netbeans

Netbeans project name screenshot

Note: the following directory structure in Netbeans
\myosgiprojects\MyComputeService
\myosgiprojects\MyComputeService\src
\myosgiprojects\MyComputeService\src\main
\myosgiprojects\MyComputeService\src\main\java
\myosgiprojects\MyComputeService\src\main\java\com
\myosgiprojects\MyComputeService\src\main\java\com\mycompany
\myosgiprojects\MyComputeService\src\main\java\com\mycompany\mycomputeservice
\myosgiprojects\MyComputeService\src\main\java\com\mycompany\mycomputeservice\impl

 

 

Write the interface.

 

Create a Java interface ComputeService.java and copy the following code into it.

  1. package com.mycompany.mycomputeservice;
  2.  
  3. public interface ComputeService{
  4.    public int add(int x, int y);
  5.    public int multiply(int x, int y);
  6. }

 

Write the interface implementation.

Create a Java class ComputeServiceImpl.java in the package com.mycompany..mycomputeservice.impl and copy the following code into it.

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package com.mycompany.mycomputeservice.impl;
  7.  
  8. import com.mycompany.mycomputeservice.ComputeService;
  9.  
  10. /**
  11.  *
  12.  * @author Kayode Odeyemi
  13.  */
  14. public class ComputeServiceImpl implements ComputeService {
  15.  
  16.     private int x = 10;
  17.     private int y = 6;
  18.  
  19.     public int add(int x, int y) {
  20.         this.x = x;
  21.         this.y = y;
  22.         return x + y;
  23.     }
  24.  
  25.     public int multiply(int x, int y) {
  26.         this.x = x;
  27.         this.y = y;
  28.         return x * y;
  29.     }
  30.  
  31. }

 

In your file system, create the following directories

\myosgiprojects\MyComputeService\src\main\resources
\myosgiprojects\MyComputeService\src\main\resources\META-INF
\myosgiprojects\MyComputeService\src\main\resources\OSGI-INF

 

 

Create an XML document.

In the OSGI-INF directory, create an XML document computeservice.xml and add the following code;

  1. <?xml version="1.0" encoding="windows-1252"?>
  2.  
  3. <!--
  4.    Document   : computeservice.xml
  5.    Created on : January 20, 2010, 2:20 PM
  6.    Author     : Kayode Odeyemi
  7.    Description:
  8.        A Service Component descriptor for Computation Service.
  9. -->
  10.  
  11. <component name="decl-compservice" immediate="true">
  12.     <implementation class="com.mycompany.mycomputeservice.impl.ComputeServiceImpl" />
  13.     <service>
  14.         <provide interface="com.mycompany.mycomputeservice.ComputeService"/>
  15.     </service>
  16.  
  17. </component>

Note: Take note of the name of this component (decl-compservice). It is used as an identifier for a client application.

 

Create a Manifest file

In the META-INF directory, create a manifest file MANIFEST.MF to add the metadata about the bundle, and add the following code;

  1. Bundle-Version: 1.0
  2. Bundle-SymbolicName: com.mycompany.mycomputeservice.declarative
  3. Bundle-Name: Computation Service
  4. Export-Package: com.mycompany.mycomputeservice
  5. Service-Component: OSGI-INF/computeservice.xml
  6. Bundle-ManifestVersion: 2

 

Modify the pom.xml file

 

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

  1. <build>
  2.       <plugins>
  3.           <plugin>
  4.               <groupId>org.apache.maven.plugins</groupId>
  5.               <artifactId>maven-jar-plugin</artifactId>
  6.               <configuration>
  7.                   <archive>
  8.                       <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
  9.                   </archive>
  10.               </configuration>
  11.           </plugin>
  12.       </plugins>
  13.   </build>

Clean and build the project

 

 

Make sure the application builds successfully.

Check the output console

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

 

Great article, thanks.

Thank you for this tutorial it's just what I've been looking for. Ana

Your blog is my favorite,

Your blog is my favorite, congratulates. Often I'll look here, a lot of information and nice site.

I would like to thank you for

I would like to thank you for sharing this great information with us. I am really glad to learn about this because it helps me to increase my knowledge.

awesome

Awesome blog, thanks! Keep up the great work! Je vous souhaite une bonne continuation. Your blog is my favorite, congratulates. Often I'll look here, a lot of information and nice site.

account money

Saw your site bookmarked on Reddit.I love your site and marketing strategy.Your site is very useful for me .I bookmarked your site! My Home account money account money

Hi, all

Not bad article, but I really miss that you didn't express your opinion, but ok you just have different approach

My opinion

Basically, if I may say something about my opinion, it will have to do with the fact that I can get all these done within Netbeans. OSGi is a great technology with lot's of possibilities. I now hardly write any Java application without modularizing it.

Hi, Everyone!

You have really great taste on catch article titles, even when you are not interested in this topic you push to read it

free trial

After reading you site, Your site is very useful for me .I bookmarked your site!

Post new comment

The content of this field is kept private and will not be shown publicly.