OSGi is a standard with many different implementations. Embedded systems often use OSGi. Also the development environment Eclipse is based on OSGi, and many Java desktop applications use the Eclipse framework. OSGi defines a modularization within the JVM (Java Virtual Machine). Even though Java enables a division of code into classes or packages, there is no modular concept for larger units.
The OSGi Module System
OSGi supplements Java by such a module system. To do so OSGi introduces bundles into the Java world. Bundles are based on Java’s JAR files, which comprise code of multiple classes. Bundles have a number of additional entries in the file META-INF/ MANIFEST.MF, which each JAR file should contain. These entries define which classes and interfaces the bundle exports. Other bundles can import these classes and interfaces. Therefore OSGi extends Java with a quite sophisticated module concept without inventing entirely new concepts.
Listing 14.1 OSGi MANIFEST.MF Bundle-Name: A service
Bundle-SymbolicName: com.ewolff.service Bundle-Description: A small service Bundle-ManifestVersion: 2 Bundle-Version: 1.0.0
Bundle-Acltivator: com.ewolff.service.Activator Export-Package: com.ewolff.service.interfaces;version="1.0.0" Import-Package: com.ewolff.otherservice.interfaces; version="1.3.0"
Listing 14.1 shows an example of a MANIFEST.MF file. It contains the description and name of the bundle and the bundle activator. This Java class is executed upon the start of the bundle and can initialize the bundle. Export-Package indicates which Java packages are provided by this bundle. All classes and interfaces of these packages are available to other bundles. Import-Package serves to import packages from another bundle. The packages can also be versioned.
In addition to interfaces and classes bundles can also export services. However, an entry in MANIFEST.MF is not sufficient for this. Code has to be written. Services are only Java objects in the end. Other bundles can import and use the services. Also calling the services happens in the code.
Bundles can be installed, started, stopped, and uninstalled at runtime. Therefore, bundles are easy to update: Stop and uninstall the old version, then install a new version and start. However, if a bundle exports classes or interfaces and another bundle uses these, an update is not so simple anymore. All bundles that use classes or interfaces of the old bundle and now want to use the newly installed bundle have to be restarted.