Java Reference
In-Depth Information
HTML
Servlet
JSP
GWT
Blah, blah, blah,
blah, blah, blah,
blah, blah
Web
server
Web service client
Figure 15.1
In this chapter you'll
build a simple web application
hosted on a single OSGi framework
that calls out to a number of back-
end OSGi frameworks using web-
services protocols.
Web service
Web service
Web service
There are two main routes into the
OSG
i framework for web applications: the
OSG
i
HTTP
Service specification and the Web Applications specification. The
HTTP
Service
specification is an
OSG
i Compendium specification. It enables programmatic registra-
tion of servlets and static resources. The Web Applications specification is one of the
R4.2 Enterprise specifications and defines a web application bundle (
WAB
). A
WAB
is a
special web archive (
WAR
) file that supplies
OSG
i metadata and relies on the
OSG
i
framework's lifecycle layer to control when its resources are made available. We'll look
at
HTTP
Service first.
15.1.1
Using the HTTP Service specification
If you're starting a web application from scratch, the simplest way of providing it in
OSG
i is to use the
HTTP
Service, which is represented by the
org.osgi.service.
http.HttpService
interface. You find the
HTTP
Service like any other
OSG
i service,
by looking in the service registry using the
BundleContext
:
String name = HttpService.class.getName();
ServiceReference ref = ctx.getServiceReference(name);
if (ref != null) {
HttpService svc = (HttpService) ctx.getService(ref);
if (svc != null) {
// do something
}
}
Having found the
HTTP
Service, what can you do with it? The
HttpService
interface
provides methods to register and unregister static resources (for example, images or
HTML
pages) and Java servlets. The
HttpService
interface is defined as follows: