I was introduced to OSGi quite by coincidence. I had to give a presentation and a colleague suggested to look into OSGi. And once I started looking into it, I was quite surprised to find the ease with which it provided the solutions to some of the problems that we face in the web application development. There are so many good articles and blogs which provide a great amount of material regarding OSGi. They are mentioned in the reference.
One of the concepts of OSGi that really intrigued me was how it allows bundles to export services that can be consumed by other bundles without knowing anything about the exporting bundle. OSGi takes care of this by introducing Service Registry where the exporting bundle registers the interfaces that it want to expose and any other bundle which wants to use those interface can just look up in the registry to use the implementation. The other concept of OSGi which I also found interesting was how OSGi uses version management to allow different versions of the same java class to be used within the project. So I created this blog to explore these concepts in greater detail. At this point, it would be important to mention that the intended audience for this blog are people who are new to OSGi. Also the example used are very basic and intend to show just the flow of control from one package to another.
To show how OSGi uses the Service Registry, I have created 2 bundles (Service and Dao) which register their interfaces. There are 2 more bundles (ServiceServlet and DaoServlet) which reference the registry to look for these interfaces respectively. I have created servlets to call the methods of the interface to show how we can create a dynamic web enabled project using OSGi. Finally to show the version management. I have created 2 different bundles having the same package and the same java class but different methods. These bundles would have different versions and we will see how these 2 methods can coexist.
Environment Setup:
Prerequisites:
For this setup, I am using Eclipse Europa which comes with the Equinox framework required for running OSGi. You can also download the latest version of the eclipse. The OSGi container Equinox comes inbuilt with Eclipse 3.2 or later. Incase you are using Eclipse 3.1 or earlier , you would need to download Eclipse Equinox plugin.
For this setup, we are using Jetty as the server. But Equinox can be configured to work with Tomcat.
Setup the Execution Environment
Make sure that you have installed the JVMs you wish to use and that you have mapped each OSGi Execution Environment to one of the JVMs. Go to Window --> Preferences --> Installed JREs --> Execution Environments.

You can download the source. Import the packages into eclipse and skip right to the First Scenario.
public interface MyDao { public List retrieveAllObjects(); }
public class Activator implements BundleActivator { private ServiceRegistration daoregistration; public void start(BundleContext context) throws Exception { System.out.println("inside sampleDao bundle"); MyDao mydao = new MyDaoImpl(); daoregistration = context.registerService(MyDao.class.getName(), mydao, null); } public void stop(BundleContext context) throws Exception { System.out.println("Outside sampleDao bundle"); daoregistration.unregister(); } }
public class MyDaoImpl implements MyDao { public List retrieveAllObjects() { //Test test= new Test(); System.out.println("Inside retrieveAllObjects of MyDao"); //test.testMethod2(); return null; } }
public interface MyService { public List findAllObjects(); }
public class Activator implements BundleActivator { private ServiceRegistration registration; public static MyDao myDao; ServiceReference daoServiceReference; public void start(BundleContext context) throws Exception { System.out.println("Inside sampleservice bundle"); MyService service = new MyServiceImpl(); registration = context.registerService (MyService.class.getName(), service, null); daoServiceReference= context.getServiceReference(MyDao.class.getName()); myDao =(MyDao)context.getService(daoServiceReference); } public void stop(BundleContext context) throws Exception { System.out.println("Outside sampleservice bundle"); registration.unregister(); context.ungetService(daoServiceReference); } }
Again make sure to add the activator in the Overview Tab of the Manifest.MF. Also go to the Dependenies Tab of the Manifest.MF and import com.sample.sampledao package.

public class MyServiceImpl implements MyService { MyDao myDao; public List findAllObjects() { // Test test = new Test(); myDao = Activator.myDao; System.out.println("Inside findAllObjects of MyService"); // test.testMethod1(); return (myDao == null) ? null : myDao.retrieveAllObjects(); } }
public class ServletActivator implements BundleActivator { public static MyService myService; ServiceReference serviceReference; public void start(BundleContext context) throws Exception { System.out.println("Inside ServiceServlet"); serviceReference= context.getServiceReference (MyService.class.getName()); myService =(MyService)context.getService(serviceReference); } public void stop(BundleContext context) throws Exception { System.out.println("Outside Service Servlet"); context.ungetService(serviceReference); } }
public class MyServlet extends HttpServlet { MyService myService; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter pw = new PrintWriter (resp.getOutputStream()); pw.println("This servlet is running in an Service Servlet using OSGi."); pw.println("Welcome to my Service Servlet"); pw.println(""); myService.findAllObjects(); pw.close(); } //@Override public void init() throws ServletException { System.out.println("inside init method"); myService = ServletActivator.myService; } }
This servlet is calling the method findAllObjects of the MyService Interface.
public class DaoActivator implements BundleActivator { public static MyDao myDao; ServiceReference daoServiceReference; public void start(BundleContext context) throws Exception { System.out.println("Inside DaoServlet"); daoServiceReference= context.getServiceReference(MyDao.class.getName()); myDao =(MyDao)context.getService(daoServiceReference); } public void stop(BundleContext context) throws Exception { System.out.println("Outside Dao Servlet"); context.ungetService(daoServiceReference); } }
public class MyServlet extends HttpServlet { MyService myService; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter pw = new PrintWriter (resp.getOutputStream()); pw.println("This servlet is running in an Dao Servlet using OSGi."); pw.println("Welcome to my Dao Servlet"); pw.println(""); myService.retreiveAllObjects(); pw.close(); }
Dao Servlet is calling the method retrieveAllObjects of MyDao.
First Scenario: We have a method findAllObjects() in MyService interface. This method calls retrieveAllObjects() of MyDao Interface. Both of the interfaces are registered in the service registry.
Its time to test the work we have done so far. But before we do that, there are some more steps that need to be done .


osgi> Jun 23, 2008 10:32:41 AM org.mortbay.http.HttpServer doStart
INFO: Version Jetty/5.1.x
Jun 23, 2008 10:32:41 AM org.mortbay.util.Container start
INFO: Started org.mortbay.jetty.servlet.ServletHandler@1081d2e
Jun 23, 2008 10:32:41 AM org.mortbay.util.Container start
INFO: Started HttpContext[/,/]
Jun 23, 2008 10:32:41 AM org.mortbay.http.SocketListener start
INFO: Started SocketListener on 0.0.0.0:8081
osgi>
This servlet is running in an Service Servlet using OSGi.
Welcome to my Service Servletin the OSGi console, you should see the output:
osgi> Inside findAllObjects of MyService
Inside retrieveAllObjects of MyDao
This servlet is running in an Dao Servlet using OSGi.
Welcome to my Dao Servlet
osgi> Inside retrieveAllObjects of MyDao
Version Management To see how OSGi manages the 2 different versions of the same class, we will create 2 bundles, with the same package and having the same class.
public class Test { public void testMethod1() { System.out.println("Inside Method 1 of Test"); } }

public class Test { public void testMethod2() { System.out.println("Inside Method 2 of Test"); } }
Final Scenario We can check to see how the same class with 2 different methods and different versions can be called from inside a package.
This servlet is running in an Service Servlet using OSGi.
Welcome to my Service Servletin the OSGi console, you should see the output:osgi> inside init method
Inside findAllObjects of MyService
test Objectcom.sample.test.Test@194d372
Inside Method 1 of Test
Inside retrieveAllObjects of MyDao
Inside Method 2 of Test
This servlet is running in an Dao Servlet using OSGi.
Welcome to my Dao Servlet
inside init method
Inside retrieveAllObjects of MyDao
Inside Method 2 of TestIn this post, you learned how to setup the OSGi execution environment and how OSGi uses the service registry to handle dependencies and also about version management. In my next post, I will try to continue from here and write about some other cool features of OSGi.
REFERENCES:
Filed under OSGi | 10 Comments »
[...] Ruchika Goyal (Xebia Hollande) qui relate lui aussi son expérience OSGi dans Experimenting with OSGi on Server Side [...]
[...] Experimenting with OSGi on Server Side | Xebia Blog [...]
>the intended audience for this blog are people who are new to OSGi.
But then this Einstein writes:
>You must already be familiar with how to create a bundle and how to use OSGi commands.
Incredible.
I am looking for some idea and stumble upon your posting
decide to wish you Thanks. Eugene
Gene,
I think you may want to leave the fine tuning part on the blog. The content is really good for beginner level OSGI user otherwise. This is definitely not advanced OSGI stuff and that’s what she meant.
Great post!
Be careful when using context.getServiceReference and context.getService-IIRC, your bundles do not track services that appear and disappear.
Check out the ServiceTracker for another option.
You might be interested in OSGi articles by Neil Bartlett – http://neilbartlett.name/blog/osgi-articles/
Great for people just getting started.
Also have a look at OPS4J Pax tools – http://wiki.ops4j.org/confluence/display/ops4j/Pax, especially Pax Construct and Pax Runner. They make your life a little easier when developing with OSGi.
I am just curious – is Xebia using OSGi in its projects?
Sonny,
In Xebia we have all kinds of OSGi developers. Some are already using it in their projects and rest of the people always start learning at some point of time. This blog works for people who have just started learning OSGi. Thanks for sharing other references.
Take a look on working thing http://sevenhats.org/