Java Reference
In-Depth Information
service.registerResources("/images", "/images", null);
} catch (NamespaceException e) {
s_log.log(
LogService.LOG_WARNING, "Failed to register static content", e);
}
}
D
Unregisters
content
private void unregister(HttpService service) {
service.unregister("/");
service.unregister("/images");
}
}
The i POJO runtime automatically injects the Log Service into the s_log field and uses
the addHttpService() and removeHttpService() methods to bind and unbind
(respectively) the HTTP Service. The ResourceBinder component responds to lifecycle
events B . The real work, for this example, is done when you register content from the
/html directory within your bundle to the root context of the HTTP Service C . In other
words, the file /html/index.html from within your bundle is served as /index.html from
the HTTP Service. You unregister it when the service is removed or the component is
deactivated D . What does the end result look like? Figure 15.2 shows the service- and
bundle-level dependencies of the ResourceBinder component.
To see this example running, go into the chapter15/httpservice/ directory of the
topic's companion code. Type ant to build the example and java -Dorg.osgi.
service.http.port=8080 -jar launcher.jar bundles to execute it. In addition to
using i POJO , this example uses the Apache Felix HTTP Service ( http://felix.
apache.org/site/apache-felix-http-service.html ) and Apache Felix Log Service ( http:
//felix.apache.org/site/apache-felix-log.html ) implementations.
After launching the example, navigate to http://localhost:8080/index.html; you
should see the web page shown in figure 15.3.
HTTP resource bundle
HttpService bundle
ResourceBinder
HttpServiceImpl
HttpService
LogService
LogServiceImpl
Log service bundle
Figure 15.2 The ResourceBinder has a mandatory
dependency on the HTTP Service for providing content
and an optional dependency on the Log Service for
logging errors.
Figure 15.3 Static content served from the
OSGi HTTP Service
Search WWH ::




Custom Search