OSGi bundles
An OSGi bundle is a Java™ archive file that contains Java code, resources, and a manifest that describes the bundle and its dependencies. The bundle is the unit of deployment for an application.
Types of bundles
- Application bundles
- Application bundles are bundles that you create specifically for your application. They are instance-specific or isolated; that is, they are not intended to be shared. They are referenced in the application manifest in the Application-Content header.
- Shared bundles
- Shared bundles are not application-specific. A single instance of a package from a shared bundle can be used by many applications. Shared bundles cannot import packages or services from application bundles. Shared bundles in an application must be provided by reference rather than contained directly in an application.
- The enterprise bundle archive (EBA) file for the application.
- A server internal OSGi bundle repository.
- External OSGi bundle repositories.
Application bundles can be stored either in the EBA file or in a repository. Shared bundles are stored in a repository (otherwise they cannot be shared).
- Referenced bundles
- A referenced bundle is a bundle that is referenced in the application manifest, and stored in a repository.
- Dependency bundles
- A dependency bundle is a bundle that is not referenced in the application manifest, but that is used by bundles that are referenced in the application manifest, or by other dependency bundles.
Application bundles that are not directly contained in the EBA file are instance-specific referenced bundles. Use bundles are shared referenced bundles. Provision bundles are shared dependency bundles.
OSGi bundle manifest file
An OSGi bundle JAR file contains a JAR manifest file. This file contains metadata that enables the OSGi Framework to process the modular aspects of the bundle.
META-INF/MANIFEST.MF
:Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: MyService bundle
Bundle-SymbolicName: com.sample.myservice
Bundle-Version: 1.0.0
Bundle-Activator: com.sample.myservice.Activator
Import-Package: org.apache.commons.logging;version="1.0.4"
Export-EJB: ExampleBean
Export-Package: com.sample.myservice.api;version="1.0.0"
The metadata in this manifest file includes the following
key properties:- Bundle-Version
- Describes the version of the bundle and enables multiple versions of a bundle to be active concurrently in the same framework instance.
- Bundle-Name
- Provides a human readable name for a bundle.
- Bundle-SymbolicName
- Uniquely identifies a bundle in the framework. It does not replace the need for a Bundle-Name header.
- Bundle-Activator
- Receives notification from the framework about the bundle lifecycle
changes. This property specifies the class that implements the
org.osgi.framework.BundleActivator
interface. - Import-Package
- Declares the external dependencies of the bundle that are used
by the OSGi Framework for bundle resolution. Specific versions or
version ranges for each package can be declared. In this example manifest
file, the
org.apache.commons.logging
package is required at Version 1.0.4 or later. - Export-EJB:
- If an EJB project is converted to an OSGi bundle, and Export-EJB section is added to the manifest. Use this section to list EJBs that you want to expose. EJBs can be added to this section by editing manually or by right-clicking a converted EJB project and selecting .
- Export-Package
- Declares the packages that are visible outside the bundle. Any package not declared here has visibility only within the bundle.
Bundle lifecycle
The framework manages the lifecycle of bundles. As you install and run a bundle, it goes through various states.
- INSTALLED
- The bundle is installed, but not all of the dependencies of the bundle have been met. The bundle requires packages that have not been exported by any currently installed bundle.
- RESOLVED
- The bundle is installed and the dependencies of the bundle have been met, but it is not running. If a bundle is started and all of the dependencies of the bundle are met, the bundle skips this state.
- STARTING
- A temporary state that the bundle goes through while the bundle is starting.
- ACTIVE
- The bundle is running.
- STOPPING
- A temporary state that the bundle goes through while the bundle is stopping.
- UNINSTALLED
- The bundle no longer exists in the framework.