OSGi:

Eclipse IDE is one of the extensively used Java development tool and I have been using it from the start of my Java programming career. One of the key features of Eclipse which impresses me a lot is its plugin architecture. To add support for a new feature we just download the relevant plugins, copy them to the plugins folder and after a restart all the required menus are in place and all the new features are available at our disposal.

I always wanted to develop an application on a similar architecture but due to other project priorities couldn’t research a lot on this. After 7 years of my S/W career I was finally struck with an opportunity to develop an application based on the OSGi Framework. As I started my research on OSGi framework I realized this is what I had been waiting for a long time.

What is OSGi?

From my initial discussions you must have got a clue that OSGi development is somewhere related to plugin development. Correct OSGi framework allows us to build highly modular Java applications composed of small components.

To define it more correctly the OSGi Framework provides the standardized specifications that allow applications to be constructed from small, reusable and collaborative components. These components are highly cohesive and loosely coupled. Each component can be individually developed, tested, deployed, updated and managed with minimal or impact on other components.The components can be composed into an application and deployed.

OSGi Bundles

An application module or component is termed as a bundle in OSGi. Bundles are the common Java jar files except that their Manifest file contains more OSGi specific details like a definitive name, version, dependencies and other deployment details.

As opposed to normal Java application which is packaged in a monolithic jar/war file bundles are logical modules that combines to comprise an application. They explicitly define which packages they export and which external packages they depend upon. OSGi framework performs bundle resolution ensuring consistency among bundles with respects to their versions and other constraints.

The OSGi specification defines a life cycle for OSGi bundles and once the bundle is deployed the bundle life cycle determines its status. A bundle can be installed, started, stopped and uninstalled from the framework following the lifecycle prescribed by the OSGi specifications.

OSGi Containers

OSGi based application run within an OSGi Container. Following are some of the popular OSGi containers:

  • Eclipse Equinox
  • Apache Felix
  • Knopflerfish

OSGi Benefits

  • Changes are easy to accommodate:
    Functionalities of a bundle are known only through its published interfaces. This allows changes to the implementation logic without affecting other collaborating bundles.
  • Improved work distribution:
    An OSGi based application is a collection of bundles. Each bundle defines a set of dependencies to other bundles. The development tasks can be distributed easily based on the bundle dependency hierarchy.
  • Improved Testability
    Each bundle can be tested as a cohesive unit in addition to the unit and integration testing.
  • Improved Packaging and Release management
    Application features can be developed under separate bundles. For an installation a deploy time decision can be made to package and deploy only those bundles which are relevant to the application features to be made available.
  • Increased Reusability
    Functionalities of a bundle can be abstracted enough to make them reusable across projects. It is even possible to reassemble module selection of an application to produce a different application.

In this article I touched upon what is OSGi and what benefits it offers. In the next article I would like to touch upon the challenges in developing an OSGi based application and how Spring DM server comes to the rescue.

Following are some of the good reference books for starting with OSGi

  1. Modular Java by Craig Walls
  2. OSGi in Action