Configure libraries for OSGi applications

Each OSGi application can access a set of provided APIs and its own internal classes. Shared libraries can also be configured to provide access to extra packages from shared libraries.

Each OSGi application has its own set of OSGi bundles in a running Liberty server. Each OSGi bundle specifies the packages that it needs and the packages it provides for use by other OSGi bundles. Bundles within an OSGi application can access any packages provided by other bundles within the same OSGi application. Additionally, OSGi bundles within an OSGi application can access API packages provided by the Liberty server. Shared libraries can also be used to provide API packages for use by OSGi applications.

Libraries can be shared across multiple OSGi applications. All the applications, including Java EE applications, can use the same classes at runtime provided by shared libraries.


Steps

  1. Create a mylib/osgi directory

      mkdir wlp/usr/servers/defaultServer/mylib/osgi

  2. Copy osgi-lib.jar and commons-lang.jar files into the new directory.

  3. In server.xml, or an included file, define the library...

    Note that the library element can also take a filesetRef attribute with a comma-separated list of fileset element IDs.

  4. Reference the library as an OSGi library so that OSGi applications can access the packages provided by the library and share a single copy of the library. In the server.xml file, or an included file, add the following code:

  5. Optional: Configure the list of packages to make them available for access from OSGi applications.

    Packages contained in the shared library can be accessed by OSGi applications when the library is configured using the osgiLibrary element. Packages can also be listed to give more control over what packages are accessible by OSGi applications. The package syntax uses the OSGi Export-Package header syntax to define each package. To list the packages in the server.xml file, or an included file, add the following code:

      <osgiLibrary libraryRef="myLib">  
          <package>org.example.osgi.lib.pkg1; version=1.0</package>  
          <package>org.example.osgi.lib.pkg2; version=1.1</package>
      </osgiLibrary>

    Note: When no package elements are used, the library is scanned to find the packages that the library provides. Each package that is discovered gets the default version of 0.0.0.


Parent topic: Administer Liberty manually