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.
About this task
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 that are 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 that are provided by shared libraries.
Procedure
-
Create a mylib/osgi directory in the
servers/defaultServer directory under
${WLP_USER_DIR}
directory.
For example: wlp/usr/servers/defaultServer/mylib/osgi.
-
Copy the
osgi-lib.jar
and commons-lang.jar
files into the new
folder.
-
Configure the shared library for the application so that the library is loaded. In the
server.xml
file, or an included file, define the library by adding the following
code:
<library id="mylib">
<fileset dir="${server.config.dir}/mylib/osgi" includes="*.jar" scanInterval="5s"/>
</library>
Note: The library
element can also take a filesetRef
attribute
with a comma-separated list of fileset
element IDs.
-
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:
<osgiLibrary libraryRef="myLib"/>
-
Optional: Configure the list of packages to make them available for access from OSGi
applications. Packages that are contained in the shared library can be accessed by OSGi applications
when the library is configured by 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.