Part 3 – OSGi: Writing the first OSGi bundle

< Part 2 – OSGi: Creating a workspace | OSGi tutorial home

Now we can make our hands dirty with a bit of OSGi code. Writing  a bundle is very easy. we need to write a BundleActivator which is the starting point while the bundle get activated or deactivated.

package subin.osgi.bundle.simple;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class SimpleBundleActivator
implements BundleActivator
{
    public void start(final BundleContext context) throws Exception
    {
        System.out.println("Hello World !");
    } 

    public void stop(final BundleContext context) throws Exception
    {
        System.out.println("Good Bye !");
    }
}

The start() will get called by the framework when the bundle is activated. Similarly, stop() on deactivation.


Manifest – OSGi Bundle
Again a bundle is nothing but the same old JAR with additional set of archive parameters. The following is the manifest file I wrote for the bundle.

Manifest-Version: 1.0
Bundle-Name: My Bundle
Bundle-SymbolicName: subin.osgi.simple
Bundle-Version: 1.0.0
Bundle-Description: a Simple bundle
Bundle-Vendor: subin.co.in
Bundle-Activator: subin.osgi.bundle.simple.SimpleBundleActivator
Import-Package: org.osgi.framework

Bundle-SymbolicName is the unique symbolic name for the bundle which will be used to refer it from other bundles & with in the framework.
Bundle-Version is the version of the bundle.
Bundle-Activator is the name of the activator class.
Import-Package defines the imported packages for the bundle.

Creating the bundle
Once the BundleActivator class and the manifest is ready, create a JAR. It’s better we use Bundle-SymbolicName as the JAR name.
How to create a JAR ?


How to run my bundle ?
All the bundles need to be installed with the framework to use it. And an installed bundle need to be activated.

See the attached screen shot.

Figure 1: Running the bundle for the very first time

osgi_console_first_bundle

Commands – OSGi console
ss – status of bundles in the framework
install <bundle URL> – installs a bundle from the specified URL
start <bundle ID> – starts the bundle with the specified ID. A unique ID will be assigned to each bundle during installation.
stop <bundle ID> – stops the specified bundle


Some basic configurations
Once we install a bundle the framework will load the bundle automatically when the framework starts up next time. But the framework won’t start the bundle automatically.

Figure 2: Framework restart

osgi_console_first_bundle_restart


Enabling bundle auto-start

We can enable the bundle auto-start  by doing some changes in the configuration/config.ini. Add these configuration properties in the file.

osgi.bundles=org.eclipse.equinox.common@2:start, org.eclipse.update.configurator@3:start, subin.osgi.simple@4:start
eclipse.ignoreApp=true
osgi.noShutdown=true

Figure 3: Framework restart after configuration changes

osgi_console_first_bundle_auto_start

Advertisements