Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

Tuesday, August 29, 2017

OSGi Dependency Management

OSGi is defining dynamic component development for JAVA. Complex application divided into multiple modules. These modules can communicate each other.  OSGi dependency management provide way to manage their dependencies among each other.

When we break the application into multiple bundles none of the classes or interfaces not available to other bundles until we do some configuration in the bundles. Dependency management define the way to share these classes between the bundles.

Before expose these classes or uses it in other bundles we have to define this information in the OSGi MANIFEST file with some specific headers.

We have mechanism called Export and Import in the OSGi and such way we can export one-bundle classes and we can use these exported classes in other bundle by using import mechanism.

We have following headers, which define the package level dependency in OSGi bundles and these headers need to define in the MENIFEST file.

Export-Package:  

We have to specify the package names with comma separated values, which we wanted to export such way it can be available to other bundles.

Example:


Export-Package: com.liferaysavvy.example.service, com.liferaysavvy.example.service1



 Import-Package:

Import package will use to user other bundle classes that already exported by the other bundles.

Example:


Export-Package: com.liferaysavvy.example.service, org.osgi.framework; version="1.3.0"



 OSGi have several other ways to manage their decencies between bundles. Such a way other classes are visible to the bundles.

The following are different ways.

Boot Classpath

It will define the basic classes and jar file required by the bundles or OSGi container. Example java.* package classes are available in the boot classpath level.

Framework Classpath

Its separate class loader and OSGi framework related classes and packages are available. Specially OSGi framework API and its implementation classes.

Bundle space

Bundle space consist of JAR file, which used by the bundle like dependency jar files, which are used current bundle only.

Imported packages

Imported packages will consist of specific classes imported by the bundles. Let us say bundles have many components but some of components wants to make visible to other bundles and these specific classes are available in Imported packages

In this case, we will use Import-Package header, which will make set of classes visible to other bundles.

There are many headers, which we can define in the OSGi bundle MENIFEST file. The following are the important headers, which are involved in the dependency management.

Export-Package:  

We have to specify the package names with comma separated values, which we wanted to export such way it can be available to other bundles.

Import-Package:

Import package will use to user other bundle classes that already exported by the other bundles.

Bundle-Classpath:

Bundle-ClassPath define list of comma separated jar files or directories which will used by the current bundle.

Export-Service

Export-Service Define specific services that make visible to other bundles.

Import-Service

What are the services exported by bundles can be used by other with Import-Service header.
There are other headers and each header have its own importance in the OSGi MANIFEST file. Get all header information from the following link


When we work with dependency management following are the steps. Usually we have at least two bundles to make work dependency management.

Bundle A

Define your services in the interface
Implement services for the defined interfaces
Add your services package path under Export-Package header in MANIFEST file
Register your Service implementation Object in “Bundle A” Activator class

Bundle B

Define exported package name under Import-Package header in MANIFEST file.
Get Service Object in “Bundle B” Activator class

Service Trackers:


Sometime one interface have multiple implementations and bundles export the same interface package, and each bundle register different Implementation Object in the Bundle Context with same interface class name.  Service Tracker is the way to filter the implantation by specific properties such a way consumer bundle can get right implementation service. 

Author

Recent Posts

Recent Posts Widget

Popular Posts