Chapter 7. The Bookshelf: First Stab

The bookshelf service is the business logic component of our case study. It will stand as a middle tier between the inventory (or data layer), which we've implemented in Chapter 4, Let's Get Started: The Bookshelf Project, and the presentation layer, which we will implement in later chapters.

In this chapter, we will define the service's API and implement it. We will also create an activator for the service to register it.

Since we do not yet have a presentation layer, we will also make the activator perform a few test actions on the service at startup to ensure that it's working as expected.

You will:

  • Define the BookshelfService interface and implement it
  • Learn how to get access to a registered service (the inventory service)
  • Install the bookshelf service bundle onto you Felix framework, running the first test

The Bookshelf Service bundle

In the previous chapters, when we have worked on the Bookshelf Inventory API and mock implementation, we separated the API bundle from that holding the implementation.

In general, it is a good practice to separate them as it enforces the loose coupling between components and prevents the developer from making assumptions about the specifics of the implementation.

As we've seen, this loose coupling makes it easier to replace the specific implementation of the API without impacting the components that depend on it

An added benefit is that it limits the strict dependencies to those required by the API bundle. For example, our bookshelf-inventory-api bundle has no dependencies while the bookshelf-inventory-impl-mock bundle does. Keeping them separate simplifies the dependency structure.

However, this separation has the downside of increasing the number of bundles we're working with. The lose coupling also adds the overhead of identifying the implementation and installing it separately.

To make the previous points clearer, we will define the API and the implementation in the same bundle for the Bookshelf Service. This way, you'd have seen both cases in action and you can choose which one fits your needs best.

Let's start with the boiler-plate project preparation.

Have a go hero preparing the bookshelf-service project

We've already gone through those steps before for the bookshelf-inventory-api and bookshelf-inventory-impl-mock bundles. Let's see if you can go through them on your own. The following is the information that you'll need during your setup.

The bundle identification is:

  • Group Id: com.packtpub.felix
  • Artifact Id: com.packtpub.felix.bookshelf-service
  • Version: 1.7.0
  • Packaging: bundle

The project will have dependencies to:

  • com.packtpub.felix.bookshelf-inventory-api (1.5.0)
  • org.osgi.core (4.2.0)

The Java packages will be:

  • com.packtpub.felix.bookshelf.service.api for the API interfaces and classes
  • com.packtpub.felix.bookshelf.service.impl for the implementation

The activator will be:

  • BookshelfServiceImplActivator in the package com.packtpub.felix.bookshelf.service.impl.activator

This information should be all you need to set up the project base structure.

Once you're done with the setup, download the accompanying code and double-check if you've got it all right.

Next, we'll write the API and implementation classes.

Define the main Bookshelf Service interfaces

The BookshelfService interface provides the bookshelf business logic and functionality to components that require it. It will perform a few checks before delegating to the inventory tier.

In our example, and for simplicity, the business logic tier also includes the authentication functionality. The authentication functionality is defined by the Authentication interface, which the BookshelfService interface extends.

Define the main Bookshelf Service interfaces
..................Content has been hidden....................

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