Time for action - implementing the service activator

The bundle activator for this service is straightforward. We basically register the service with the framework on start and keep a reference to it for being able to unregister it on stop.

In Chapter 9, The Bookshelf: Second Stab with iPOJO, we'll look at how to declare services using annotations, and, in some cases, remove the need for an activator altogether.

It's also useful, for now, to include a part of the code that tests the service, which would be kicked off at service start. This test code will be removed in the next chapter, in which we'll add shell service integration and start testing it using the text console.

The package containing the activator class, BookshelfServiceImplActivator, will be com.packtpub.felix.bookshelf.service.impl.activator:

public class BookshelfServiceImplActivator
implements BundleActivator
{
ServiceRegistration reg = null;
public void start(BundleContext context) throws Exception
{
this.reg = context.registerService(
BookshelfService.class.getName(),
new BookshelfServiceImpl(context), null);
testService(context);
}
public void stop(BundleContext context) throws Exception {
if (this.reg!=null) {
context.ungetService(reg.getReference());
}
}

Let's write the testService method now.

Framework service lookup

Since we have not yet implemented any way of interacting with the service, but we still want to make sure it's working right, we've added a method call that executes on bundle start testService(), which will add a few books. Then perform a search and display the search results on the standard output.

private void testService(BundleContext context)
{
// retrieve service
String name = BookshelfService.class.getName();
ServiceReference ref = context.getServiceReference(name);
if (ref==null) {
throw new RuntimeException(
"Service not registered: " + name);
}
BookshelfService service =
(BookshelfService) context.getService(ref);

This BookshelfService lookup seems to be unnecessary, because we've just instantiated the bookshelf service implementation previously. We could have kept a reference to it and used it. However, this allows you to move this test method to a separate unit testing class later without changing it.

Let's continue with our test method. So we first login (the credentials will be hardcoded admin / admin):

// authenticate and get session
String sessionId;
try
{
System.out.println("
Signing in. . .");
sessionId =
service.login("admin", "admin".toCharArray());
}
catch (InvalidCredentialsException e)
{
e.printStackTrace();
return;
}

Then we can add a few books using the service interface. The goal of the test is to validate the addBook and the searchBooksByAuthor methods, so we'll add books with attributes that fit the purpose.

// add a few books
try
{
System.out.println("
Adding books. . .");
service.addBook(sessionId, "123-4567890100",
"Book 1 Title", "John Doe", "Group 1", 0);
service.addBook(sessionId, "123-4567890101",
"Book 2 Title", "Will Smith", "Group 1", 0);
service.addBook(sessionId, "123-4567890200",
"Book 3 Title", "John Doe", "Group 2", 0);
service.addBook(sessionId, "123-4567890201",
"Book 4 Title", "Jane Doe", "Group 2", 0);
}
catch (BookAlreadyExistsException e)
{
e.printStackTrace();
return;
}
catch (InvalidBookException e)
{
e.printStackTrace();
return;
}

The search will look for authors that end with "Doe". The search string will therefore be "%Doe".

// and test search
String authorLike = "%Doe";
System.out.println(
"Searching for books with author like: "+authorLike);
Set<String> results = service.searchBooksByAuthor(
sessionId, authorLike);
for (String isbn : results)
{
try
{
System.out.println(
" - " + service.getBook(sessionId, isbn));
}
catch (BookNotFoundException e)
{
System.err.println(e.getMessage());
}
}
}
}

This will do for validation until we get the user interfaces up. The testService() method and references to it will be removed in the latter chapters.

Note

Outside the scope of this book, but nonetheless important to know about, is that you can also include pieces of code dedicated for unit and integration testing as part of the code structure. These pieces can be used in the context of a testing framework (such as JUnit) and automatically kicked off by Maven as part of the relevant phase of the build lifecycle.

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

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