The bookshelf-log-impl
will provide a BookshelfLogHelper
implementation that keeps a reference to a Log Service and forwards log requests to it after processing their message formatting.
Start another project, the bookshelf-log-impl
bundle:
The following are the project identification information:
com.packtpub.felix
com.packtpub.felix.bookshelf-log-impl
1.10.0
bundle
This bundle will have the bookshelf-log-api
as a dependency because it implements the service interface defined in it. It also has a dependency on the org.osgi.compendium
bundle, which defines the LogService
interface.
Therefore, dependencies of this bundle are as follows:
<dependencies> <dependency> <groupId>com.packtpub.felix</groupId> <artifactId>com.packtpub.felix.bookshelf-log-api</artifactId> <version>1.10.0</version> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> <version>4.2.0</version> </dependency> </dependencies>
This bundle also uses iPOJO for the injection of the LogService
instance, as well as the publishing of the service. We will look at the iPOJO configuration after implementing the service.
Notice that neither of the previous dependencies is Felix-specific. Even though we have selected the "Apache Felix Log Service" as a Log Service implementation, the bundle that uses it does not depend on it. This bundle can be used on any framework with any Log Service implementation.
The implementation of the BookshelfLogHelper
interface will be named BookshelfLogHelperImpl
and defined in the package com.packtpub.felix.bookshelf.log.impl
as follows:
public class BookshelfLogHelperImpl implements BookshelfLogHelper { LogService log; public void debug(String pattern, Object[] args) { String message = MessageFormat.format(pattern, args); this.log.log(LogService.LOG_DEBUG, message); }
The caller passes a message pattern as a string and an array of arguments. Those are used to construct a log message, which is mapped to the right LogService
method signature.
Here we've used the java.text.MessageFormat
Java class, which allows flexible formatting for message text.
The pattern is encoded with placeholders that are used to insert the formatted arguments. For example, the placeholder {n}
is used for the insertion of the nth argument:
String pattern = "Expecting integer, got ''{0}''."; Object[] args = new Object[] { "value" }; System.out.println(MessageFormat.format(pattern, args);
This would produce:
Expecting integer, got 'value'.
It also provides some additional cool formatting features refer to the API Javadocs for a detailed description. (http://download.oracle.com/javase/1.4.2/docs/api/java/text/MessageFormat.html)
The remaining methods are similar, each calling a method from the LogService
interface using the appropriate log level.
The iPOJO configuration for this service is as expected:
<ipojo> <component classname= "com.packtpub.felix.bookshelf.log.impl.BookshelfLogHelperImpl" name="BookshelfLogHelperImpl"> <provides /> <requires field="log" /> </component> <instance component="BookshelfLogHelperImpl" name="bookshelf.log-helper.impl" /> </ipojo>
Complete the project configuration and then package and deploy it to the releases repository.
We are now ready to make changes to the bookshelf-service
and bookshelf-inventory-impl-mock
bundles to use this newly created service.