2.15. Open Services Gateway Initiative (OSGi)

OSGi (Open Services Gateway Initiative) is a platform for component applications. An OSGi application consists of components called bundles that export and import packages and potentially also services. The OSGi platform provides means for resolving dependencies between bundles and controlling the lifecycle of bundles, and also implements some standard services.

2.15.1. Bundles

An OSGi bundle is a JAR file that contains a manifest file describing the bundle and the class files implementing the bundle, possibly with other resources the implementation might require. The manifest file identifies the bundle, describes the interface of the bundle in terms of its exported and imported packages, and specifies the dependencies on other bundles for situations where package dependencies are not suitable.

For dependency specification purposes, bundles are identified by their names and versions. Names are unique strings that follow the common reverse domain naming conventions. Versions are triplet of integers with the usual major.minor.micro semantics. When installed, bundles are also assigned a unique numerical identifier.

Exported and imported packages are connected using class loader hierarchy. A bundle can only use code that it implements or imports. Other bundles can only use code that a bundle exports.

Once a bundle is installed and its dependencies resolved, it can be started and stopped. The framework starts a bundle before use and stops a bundle after use by calls to its activator interface. The bundle is provided with a bundle context that allows it to access the various framework functions as required.

OSGi Bundle States

interface Bundle {
    // Bundle state constants
    int UNINSTALLED = 0x00000001;
    int INSTALLED = 0x00000002;
    int RESOLVED = 0x00000004;
    int STARTING = 0x00000008;
    int STOPPING = 0x00000010;
    int ACTIVE = 0x00000020;

    int getState ();

    ...
}

OSGi Bundle Activator Interface

interface BundleActivator {
    void start (BundleContext context) throws Exception;
    void stop (BundleContext context) throws Exception;
}

OSGi Bundle Context Interface (Bundle Related)

interface BundleContext {
    // Access to framework properties
    String getProperty (String key);

    // Access to objects representing bundles
    Bundle getBundle ();
    Bundle getBundle (long id);
    Bundle getBundle (String location);
    Bundle [] getBundles ();

    // Support for bundle management
    Bundle installBundle (String location, InputStream input) throws BundleException;
    Bundle installBundle (String location) throws BundleException;

    // Support for bundle lifecycle notifications
    void addBundleListener (BundleListener listener);
    void removeBundleListener (BundleListener listener);

    // Support for framework event notifications
    void addFrameworkListener (FrameworkListener listener);
    void removeFrameworkListener (FrameworkListener listener);

    // Support for persistent storage
    File getDataFile (String filename);

    ...
}

2.15.2. Services

A bundle can dynamically register and unregister services. A service is identified by its interface and by arbitrary additional properties specified during service registration. The framework keeps track of available services and distributes events whenever service availability or service properties change.

OSGi Bundle Context Interface (Service Related)

interface BundleContext {

    ...

    // Support for service management
    ServiceRegistration registerService (String [] clazzes, Object service, Dictionary properties);
    ServiceRegistration registerService (String clazz, Object service, Dictionary properties);

    Filter createFilter (String filter) throws InvalidSyntaxException;
    ServiceReference [] getServiceReferences (String clazz, String filter) throws InvalidSyntaxException;
    ServiceReference [] getAllServiceReferences (String clazz, String filter) throws InvalidSyntaxException;

    ServiceReference getServiceReference (String clazz);
    Object getService (ServiceReference reference);
    boolean ungetService (ServiceReference reference);

    // Support for service lifecycle notifications
    void addServiceListener (ServiceListener listener, String filter) throws InvalidSyntaxException;
    void addServiceListener (ServiceListener listener);
    void removeServiceListener (ServiceListener listener);
}

Some services are standardized. Among framework related services are the Package Admin Service, Start Level Service, Permission Admin Service. Among general purpose services are the Log Service, HTTP Service, XML Parser Service.