Trying it out

Give it a try. Package and deploy the bookshelf-servlet bundle to the releases repository, and then install and start it (deploy -s) in Felix. Remember that the bookshelf-servlet belongs to the Tier 1 Services start level (5).

Checking the logs, you will find entries for the successful creation of the iPOJO instance and the registration of the service:

2010.09.23 17:03:21 INFO -
Bundle: com.packtpub.felix.bookshelf-servlet -
[INFO] BookshelfServletImpl : Instance bookshelf.servlet from
factory BookshelfServletImpl created
2010.09.23 17:03:21 INFO -
Bundle: com.packtpub.felix.bookshelf-servlet -
[javax.servlet.Servlet, java.io.Serializable,

javax.servlet.ServletConfig] - ServiceEvent REGISTERED

If all went well, you should be able to access your new servlet at http://localhost:8080/bookshelf.

Note

The embedded Jetty may take a little time to start and to be ready. Look for a message that looks like Started jetty 6.1.x at port 8080 to know that Jetty is ready to accept requests.

What just happened?

This is a nice example of the collaboration of extenders, each having a separate concern towards a common purpose. From the bundle's point of view, all we've done is define an iPOJO component that is also an Http Servlet and configure it for injection of service instances and properties.

When the bundle is started on the framework, the iPOJO service will instantiate the component and register it as a service. At this point, the whiteboard extension will recognize it as a servlet and publish it with the Http Service.

What just happened?

The preceding flow diagram shows the steps of this activation sequence. They are as follows:

  1. The bundle is installed and started, it is active.
  2. The framework dispatches a bundle STARTED event, one of the listeners is the iPOJO Extender.
  3. The iPOJO Extender inspects the bundle, processes its iPOJO components, and prepares the instances.
  4. The iPOJO Extender registers the Bookshelf Servlet service.
  5. The Framework dispatches a service REGISTERED event, one of the listeners is the Http Whiteboard.
  6. The Http Whiteboard inspects the service and finds it extends HttpServlet.
  7. It registers the servlet with the installed Http Service, using the alias for defining the context.

At this point, the servlet is initialized by the Http Service and made available on the embedded Jetty container.

Pop Quiz

  1. Which design pattern is one where a component requests a service from service registrar?

    a. The whiteboard pattern

    b. The service locator pattern

    c. The dependency injection

  2. How can you register a servlet with an Http Service?

    a. Invoke the registerServlet method

    b. Through the Http Service whiteboard extension and register the servlet with the bundle context

    c. Through the Http Service whiteboard extension and using iPOJO to register the servlet

    d. All of the above

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset