Basic Extensions using Liberty Libraries1.0
This feature enables the configuration of Basic Extensions using Liberty Libraries (BELL).
The BELL feature enables libraries to define the implementation of an Open Liberty SPI or API with Java ServiceLoader
configuration files. These files are included in the /META-INF/services/
directory of the library JAR file.
With the BELL feature, a library declares a /META-INF/services/
resource with the name of the fully qualified API or SPI interface, and provides a class that implements the interface. The feature processes the /META-INF/services/
resources and provides the library interface implementation to the Open Liberty runtime.
Enabling this feature
To enable the Basic Extensions using Liberty Libraries 1.0 feature, add the following element declaration into your server.xml
file, inside the featureManager
element:
<feature>bells-1.0</feature>
Examples
Configuring a servlet filter for applications
In certain scenarios, a servlet filter implementation must be applied to all applications that are deployed to Open Liberty. For example, a servlet filter might be used to add new headers to each request. The ServletContainerInitializer
interface provides an onStartup
method that can be used to add a servlet filter implementation class to all applications.
The BELL feature can be used to configure a library that provides a ServletContainerInitializer
implementation as a service. The ServletContainerInitializer
service onStartup
method is called by Open Liberty for each application that starts. In the following example, a ServletContainerInitializer
implementation is configured to add a servlet filter implementation with each ServletContext
object in a Java file. The ServletContext.addFilter(String, Filter)
method is used by the onStartup
method to add the servlet filter implementation:
public class SCI implements ServletContainerInitializer {
@Override
public void onStartup(Set<Class<?>> arg0, ServletContext arg1)
throws ServletException {
arg1.addFilter("myFilter", MyFilter.class).addMappingForUrlPatterns(null, false, "/*");
}
}
To register the ServletContainerInitializer
interface that adds the servlet filter for an application, a class name is specified in the library JAR file. The library JAR file contains a META-INF/services/javax.servlet.ServletContainerInitializer
file that specifies the fully qualified ServletContainerInitializer
implementation class name that is contained in the library. The following example uses the file
element to add the path/to/jar
JAR file to the library:
<library id="init">
<file name="path/to/jar"/>
</library>
<bell libraryRef="init"/>
Adding a custom user registry from a shared library
The BELL feature can load a custom user registry with a custom user registry JAR file that is stored in a shared library on the server. The following example adds the resources/sharedLib
shared library that contains the bellsCur.jar
JAR file in the server.xml
file. The bell
element references this library to load the custom user registry:
<library id="bellsCurLib" name="bellsCurLib">
<file name="${server.config.dir}/resources/sharedLib/bellsCur.jar"></file>
</library>
<bell libraryRef="bellsCurLib"></bell>