7.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.

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.

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.

interface BundleActivator {
  void start (BundleContext context) throws Exception;
  void stop (BundleContext context) throws Exception;
}
interface BundleContext {
  // Access to framework properties
  String getProperty (String key);

  // Access to objects representing bundles
  Bundle getBundle ();
  Bundle getBundle (long id);
  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);

  ...
}