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


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
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.
\myosgiprojects\MyComputeService\src\main\resources
\myosgiprojects\MyComputeService\src\main\resources\META-INF
\myosgiprojects\MyComputeService\src\main\resources\OSGI-INF
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.
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