Locating OSGi applications

You can use classes in the org.apache.aries.blueprint package to extend the OSGi application programming model; this third-party SPI is provided through the blueprint-1.0 server feature. You must access OSGi application bundles in to apply your extensions. In Liberty, OSGi applications run as Subsystems. To locate an OSGi application you can create a ServiceTracker in a user feature.

About this task

This topic describes how the developer of a user feature can locate running OSGi applications. This task is often required for user features that provide programming model extensions to OSGi applications. For example, a new user feature might provide such extensions by implementing a new bundle extender, often referred to as a container, or, more simply, by tracking and invoking services published from within certain OSGi applications.

Such user features must use the BundleContext of a particular running OSGi application to create new BundleTracker and ServiceTracker instances. This BundleContext can be obtained from the org.osgi.service.subsystem.Subsystem that is associated with the OSGi application. The following procedure describes how to obtain that SubSystem service.

Procedure

To locate an OSGi application by creating a ServiceTracker in a user feature, complete the following steps:

  1. Construct an org.osgi.framework.Filter that is targeted to the Subsystem that you want to locate.
  2. Create an org.osgi.util.tracker.ServiceTracker that uses the Filter from step 1 to obtain the org.osgi.service.subsystem.Subsystem service that is associated with the OSGi application that you want to locate.
    This Subsystem service instance provides everything you require to work with the OSGi application.

Example

The following example shows how to locate an application with symbolic name my.app by using a ServiceTracker in a user feature:
import org.osgi.framework.BundleContext;
import org.osgi.service.subsystem.Subsystem;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
In the following code extract, the variable ctx is the BundleContext of one of the bundles of the user feature:
String SERVICE_FILTER = "(&(objectClass=org.osgi.service.subsystem.Subsystem)
                           (subsystem.type=osgi.subsystem.application)(subsystem.symbolicName=my.app))"
    
      org.osgi.framework.Filter filter = ctx.createFilter(SERVICE_FILTER);
The last 'null' parameter can be replaced with an instance of a class that implements ServiceTrackerCustomizer<Subsystem, Subsystem>:
org.osgi.util.tracker.ServiceTracker<Subsystem, Subsystem> str = new ServiceTracker<Subsystem, Subsystem>(ctx, filter, null);
The SERVICE_FILTER can be constructed to make use of such constants as:
org.osgi.framework.Constants.OBJECTCLASS;
org.osgi.service.subsystem.SubsystemConstants.SUBSYSTEM_SYMBOLICNAME_PROPERTY;
org.osgi.service.subsystem.SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION;
org.osgi.service.subsystem.SubsystemConstants.SUBSYSTEM_TYPE_PROPERTY;