Tagged: osgi Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 10:02 am on March 10, 2009 Permalink |
    Tags: bundle, console, , , osgi   

    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


    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


    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

    Figure 3: Framework restart after configuration changes


  • Subinkrishna Gopi 9:25 am on March 6, 2009 Permalink |
    Tags: , , , osgi   

    Part 2 – OSGi: Creating a workspace 

    < Part 1 –  OSGi: What, Why & How | OSGi tutorial home

    To implement my OSGi based applications I am using Equinox (Eclipse Foundation). If you are using Eclipse IDE (which is based on OSGi) you don’ t need to download the OSGi core APIs as you can find it in the Eclipse_Home/plugins directory. If you are using Eclipse Ganymede, you will find a JAR org.eclipse.osgi_3.4.0.v20080605-1900.jar or a higher version. That’s all what we want (for now).

    Equinox download: http://download.eclipse.org/eclipse/equinox/.

    Workspace directory structure

    The directory structure should look like this.

    |-- configuration
    |  |-- config.ini               - Framework configuration
    |-- org.eclipse.osgi_3.4.2.jar  - Framework
    |-- subin.orgi.simple.jar       - My bundle
    |-- <other bundles, archives>

    The config.ini is the place where we can configure the framework and bundle behavior. We can talk about it later.

    Running the Equinox core

    Copy org.eclipse.osgi JAR in to your workspace directory. You can start the OSGi framework by running the org.eclipse.osgi JAR file. Use the following command:

    your/workspace/dir> java -jar org.eclipse.osgi_3.4.2.jar -console

    This will start an OSGi console for us using which we can start, install, uninstall OSGi bundles.

    osgi console

    In the above screen shot you can see the OSGi console with the list of installed bundles. (I am using Equinox 3.4.2.)

    Configuring the Equinox workspace
    We can configure Equinox in various ways. I think its very early to mention all those things here. If you are really interested in knowing that, check this link.

    Quick start guide: http://www.eclipse.org/equinox/documents/quickstart.php

    Starting Equinox faceless
    When we start Equinox core using the above command, it will launch an OSGi console. There is a way to start Equinox faceless (with out console). Try this command.

    java -jar org.eclipse.osgi_3.4.2.jar -console 8281 -noExit

    This will make Equinox to launch the OSGi console in port 8281 which we can access using Telnet on port 8281. Try this.

    telnet localhost 8281

    osgi console telnet

    Read more on starting Equinox faceless: http://www.eclipsezone.com/eclipse/forums/t93976.rhtml



  • Subinkrishna Gopi 3:30 pm on March 3, 2009 Permalink |
    Tags: , , , felix, , knoplerfish, osgi   

    Part 1 – OSGi: What, Why & How 

    What is OSGi?
    OSGi is a Java-based service platform that can be remotely managed. OSGi has been developed by the OSGi Alliance (formerly known as the Open Services Gateway initiative) is an open standards organization founded in March 1999. The OSGi framework defines an application life cycle management model, a service registry, an Execution environment and Modules.

    Read More: http://www.osgi.org/About/WhatIsOSGi
    Download the specification: http://www.osgi.org/Release4/Download

    OSGi Architecture (Source: Wikipedia)


    • Bundles
      Bundles are normal jar components with extra manifest headers. The bundles are loosely coupled & highly cohesive.
    • Services
      The services layer connects bundles in a dynamic way by offering a publish-find-bind model for plain old Java objects (POJO).
    • Services Registry
      The API for management services.
    • Life-Cycle
      The API for life cycle management (install, start, stop, update, and uninstall bundles).
    • Modules
      The layer that defines encapsulation and declaration of dependencies (how a bundle can import and export code).
    • Security
      The layer that handles the security aspects by limit bundle functionality to pre-defined capabilities.
    • Execution Environment
      Defines what methods and classes are available in a specific platform.

    Why should we use OSGi?
    OSGi offers extreme modularity for the developer as the developers will be developing the modules as Bundles which can be plugged in to the application anytime from anywhere. This also enables the reusability and maintainability of the modules. We can even remove some of the modules for upgrade/bug fixes with out disturbing other modules.

    Read more: http://www.osgi.org/About/WhyOSGi

    How can we develop OSGi based applications?
    We can develop an OSGi based application using any of the OSGi implementations. Three of the most widely used implementations are, Equinox (Eclipse Foundation), Apache Felix & Knopflerfish.


Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc