Friday, 27 September 2013

A HelloWorld OSGI Module using Ecipse Equinox OSGI Framework

Objective

To create a HelloWorld OSGI Module using Equinox OSGI Framework.

Environment

  • Eclipse IDE (Juno)
  • OSGI Equinox Framework

Development

  • Open you Eclipse IDE and Workspace.
  • Create a new Plug-in Project (File -> New -> Other -> Plug-in Project).
  • Give Project name as "HelloWorldModule". Select target as "an OSGI Framework: standard.
  • Select Next -> Next and choose template as "Hello OSGI Bundle"; then Next -> Finish.
  • You will see a Java class Activator (implementing BundleActivator) gets created with start() and stop() methods. Both the methods have BundleContext parameter passed, the BundleContext is a way through which we can interact with OSGI framework.
  • The Activator class start() and stop() method provide a way to define what should be done when the bundle is started and stopped while running under an OSGI framework.
  • You will also see few System out messages under start() and stop() method which will help us test when these methods are called.

Testing

  • Right click on project and select Run As -> Run Configurations..
  • Create a new OSGI Configuration by right clicking on OSGI Framework. Give name as HelloWorldModule and select following Bundles
  • Cock on Apply and Run
  • In Eclipse Console view the following message and prompt will appear
Hello World!!
osgi> 
  • Type ss on osgi prompt, this will list the current bundles installed and their status
osgi> ss
"Framework is launched."
id    State       Bundle
0    ACTIVE      org.eclipse.osgi_3.8.2.v20130124-134944
                Fragments=2
1    ACTIVE      org.apache.felix.gogo.command_0.8.0.v201108120515
2    RESOLVED    org.eclipse.equinox.weaving.hook_1.0.200.I20130319-1000
                Master=0
3    ACTIVE      org.apache.felix.gogo.runtime_0.8.0.v201108120515
4    ACTIVE      org.eclipse.equinox.console_1.0.0.v20120522-1841
5    ACTIVE      HelloWorldModule_1.0.0.qualifier
6    ACTIVE      org.apache.felix.gogo.shell_0.8.0.v201110170705

  • Now to see what happens when we stop a bundle; give command osgi> stop <bundl-id>. In our case it would be stop 5 since 5 is the bundle if of HelloWorldModule.
  • We will see following output on console
osgi> stop 5
Goodbye World!!
  • Now to exit from osgi prompt type exit and then y
  • So you noticed the message "Hello World!!" and "Goodby World!!" when the HelloWorldModule bundle started and stopped. In real world application this is where we would write the actual code which we want to execute during start and stop of a module.