Java Reference
In-Depth Information
public interface HttpService {
HttpContext createDefaultHttpContext();
void registerResources(String alias, String name, HttpContext context);
void registerServlet(
String alias, Servlet servlet,
Dictionary initparams, HttpContext context);
void unregister(String alias)
}
Let's look at how you use this interface; you'll start with registering static resources
and then move on to servlets.
REGISTERING RESOURCES
Let's dive into a web application by creating a bundle to register a set of static
resources. You'll reuse your knowledge of components from chapter 12 to build a sim-
ple i POJO component that registers resources with the HTTP Service. Listing 15.1
shows the complete source code for this component. You may wonder why you're
using an i POJO component instead of a simple BundleActivator . The reason is the
complex startup-ordering issues associated with using multiple services, because your
component uses the HTTP Service and the Log Service. You could do this without a
component framework, but using one makes life simpler.
Listing 15.1 ResourceBinder component class
@Component
public class ResourceBinder {
@Requires(optional=true)
private LogService s_log;
@Requires(id="http")
private HttpService s_http;
@Bind(id="http")
protected void addHttpService(HttpService service) {
register(service);
}
@Unbind(id="http")
protected void removeHttpService(HttpService service) {
unregister(service);
}
@Validate
protected void start() {
register(s_http);
}
B
Responds to
lifecycle events
@Invalidate
protected void stop() {
unregister(s_http);
}
C
Registers content
from /html
private void register(HttpService service) {
try {
service.registerResources("/", "/html", null);
 
Search WWH ::




Custom Search