Use OSGi HTTP Whiteboard in Liberty
We can use the httpWhiteboard-1.0 feature to enable the development and deployment of modular Web applications written using servlet technologies.
Stabilized feature: Support for OSGi applications is stabilized. Java Platform, Enterprise Edition (Java EE) 8 technologies are not enabled for OSGi applications and features that support OSGi development. As an alternative, develop applications using the Java Platform, Enterprise Edition (Java EE) or Microprofile features. For more information, see Stabilized Liberty features and feature capabilities. To use the httpWhiteboard-1.0 feature, we must have Liberty and the httpWhiteboard-1.0 feature installed.
- Install the latest version of Liberty with the OSGi application programming model capabilities. We can install Liberty either using one of the zip install packages (ZIP file) or by extracting the Java archive (JAR) package. For more information about installing Liberty, see Install Liberty.
- Install the httpWhiteboard-1.0 feature. If the feature is not available as part
of the downloaded ZIP archive files, then install the feature from the Liberty repository using the following command:
bin\installUtility install httpWhiteboard-1.0
We will see the following messages as the installation progresses:
Step 1 of 3: Downloading httpWhiteboard-1.0... Step 2 of 3: Install httpWhiteboard-1.0... Step 3 of 3: Cleaning up temporary files...
After the feature is installed successfully, we can use the feature.
- Configure the httpWhiteboard-1.0 feature in server.xml.
<featureManager> <feature>httpWhiteboard-1.0</feature> </featureManager>
What to do next
- Write a service.
The Http Whiteboard implementation looks for a particular type of OSGi services that are registered, for example javax.servlet.Servlet. So to use the Http Whiteboard feature, we can use any supported OSGi component model, such as Blueprint, to register the service and the service is automatically picked up. The following Blueprint example shows a simple servlet implementation, com.my.MyWhiteboardServlet being registered in the OSGi service registry under the javax.servlet.Servlet interface. It is also registered under a standard Http Whiteboard service property, org.http.whiteboard.servlet.pattern, that provides a relative URL location for the servlet.
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="MyWhiteboardServletBean" class="com.my.MyWhiteboardServlet" /> <service id="MyWhiteboardServletBeanService" ref="MyWhiteboardServletBean" interface="javax.servlet.Servlet" /> <service-properties> <entry key="osgi.http.whiteboard.servlet.pattern" value="/mywhiteboardservlet" /> </service-properties> </service> </blueprint>
[AUDIT ] CWWKE0001I: The server defaultServer has been launched. [AUDIT ] CWWKZ0058I: Monitoring dropins for applications. [AUDIT ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http [AUDIT ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http/shared [AUDIT ] CWWKT0016I: Web application available (default_host): http://localhost:9080/osgi/http/ [AUDIT ] CWWKN2000A: HTTP Whiteboard context root added: /osgi/http/MyHttpWhiteboardApp [AUDIT ] CWWKZ0001I: Application MyHttpWhiteboardApp started in 0.424 seconds.
In this example, there are three HTTP Whiteboard context root added messages. The last message shows where the context root for the application, MyHttpWhiteboardApp, is added. The application also contains the servlet defined in the previous blueprint example. We can access a servlet by combining the Web application URL that ends in /osgi/http/ and the relative URL for the Whiteboard context root (merging the intersecting /osgi/http/), and then appending the location of the servlet specified in osgi.http.whiteboard.servlet.pattern. For example, merging the Web application URL (http://localhost:9080/osgi/http/) and whiteboard context root (/osgi/http/MyHttpWhiteboardApp) for the application gives:
http://localhost:9080/osgi/http/MyHttpWhiteboardApp
Add the value of osgi.http.whiteboard.servlet.pattern will result in the following URL:
http://localhost:9080/osgi/http/MyHttpWhiteboardApp/mywhiteboardservlet
The Http Whiteboard specification is based around the OSGi services. This means the feature integrates with other native OSGi services and component models. For example, Blueprint can be used to dynamically inject other services. Blueprint name space handlers and configuration admin can be used to dynamically inject configuration values into the servlet allowing the application configuration to be changed if required.