Previous Topic

Next Topic

Book Contents

Book Index

Package Admin service

The PackageAdmin service allows bundles of administrative type to trace what packages are exported/imported and by which bundles. Other bundles can access the PackageAdmin service through its registered interfaces: org.osgi.service.packageadmin.PackageAdmin, which is defined by OSGi Package Admin specification and com.prosyst.mbs.framework.packages.PackageAdmin, an extension which adds methods for remote transfer of information about available packages in the framework.

The service basic concepts and terms are specified by the OSGi Alliance in the Package Admin Service Specification.

In general, the PackageAdmin service allows to retrieve:

Registering bundle

This service is registered by the System bundle.

Using the OSGi Package Admin service

The following listing shows how you can use the PackageAdmin service to find the name of the bundle that exports a given package, as well as the ones that import it.

import org.osgi.service.packageadmin.PackageAdmin;
  import org.osgi.service.packageadmin.ExportedPackage;
  import org.osgi.framework.BundleActivator;
  import org.osgi.framework.BundleContext;
  import org.osgi.framework.Bundle;
  import org.osgi.framework.ServiceReference;

  public class PackageAdminTester implements BundleActivator {
    ServiceReference pAdminRef = null;
    PackageAdmin pAdmin = null;
    String servName = "org.osgi.service.packageadmin.PackageAdmin";
    
    public void start(BundleContext bc) {
      // Calling the Package Admin service
      pAdminRef = bc.getServiceReference(servName);
      if(pAdminRef != null) {
        pAdmin = (PackageAdmin) bc.getService(pAdminRef);
      }
      // Getting all exported packages in the framework
      ExportedPackage[] packages = pAdmin.getExportedPackages((Bundle)null);
      for(int i=0; i< packages.length; i++) {
        // Searching for the org.osgi.service.http package
        if((packages[i].getName()).equals("org.osgi.service.http")) {
          // Operations on a package match
          listPackages(packages[i]);
        }
      }
    }

    public void stop(BundleContext bc) {
      bc.ungetService(pAdminRef);
    }
  
    // This method prints in the system output the bundle that
    // exports the passed package as well as the bundles that import it
    public void listPackages(ExportedPackage ePackage) {
      Bundle httpBundle = ePackage.getExportingBundle();
      System.out.println("Bundle that exports the "
                         + ePackage.getName() + " package:");
      System.out.println("\t" + httpBundle.getHeaders().get("Bundle-Name"));
      Bundle[] userBundles = ePackage.getImportingBundles();
      for(int j=0; j<userBundles.length; j++) {
        System.out.println("Bundle that imports the "
                            + ePackage.getName() + " package: ");
        System.out.println("\t" + userBundles[j].getHeaders().get("Bundle-Name"));
      }
    }
  }

For more information about the OSGi Package Admin service, refer to the OSGi Package Admin Service Specification, and for the extension – in the API documentation.