Time for action - writing BookshelfServiceImpl

We won't go through the whole BookshelfService implementation. Let's implement the first part together and then you can go on with the rest of it on your own.

public class BookshelfServiceImpl implements BookshelfService
{
private String sessionId;
private BundleContext context;

The service receives a BundleContext through its constructor, handed to it by the activator (see later). It will keep a reference of it in order to use it for the BookInventory component lookup when it needs the inventory functionality.

public BookshelfServiceImpl(BundleContext context)
{
this.context = context;
}

The login, logout, and checkSession methods are plain mocks, only one set of credentials is recognized and only one session is supported at a time. Also, no concurrency checks are made, therefore, we would expect this code to misbehave in a multi-threaded usage.

public String login(String username, char[] password)
throws InvalidCredentialsException
{
if ("admin".equals(username) &&
Arrays.equals(password, "admin".toCharArray()))
{
this.sessionId =
Long.toString(System.currentTimeMillis());
return this.session;
}
throw new InvalidCredentialsException(username);
}
public void logout(String sessionId) {
checkSession(sessionId);
this.sessionId = null;
}

The session check is a simple one: we only allow one active session at a time. If the sessionId is set, it must match the one that's passed, otherwise the check fails.

public boolean sessionIsValid(String sessionId) {
return this.sessionId!=null
&& this.sessionId.equals(sessionId);
}
protected void checkSession(String sessionId) {
if (!sessionIsValid(sessionId)) {
throw new SessionNotValidRuntimeException(sessionId);
}
}

We will call this method before every operation of the bookshelf service.

Let's carry onto the implementation of the BookshelfService methods.

To retrieve a Book from the repository, after session validation, we get an instance of the inventory service to which we delegate the book load request.

public Book getBook(String sessionId, String isbn)
throws BookNotFoundException
{
checkSession(sessionId);
BookInventory inventory = lookupBookInventory();
return inventory.loadBook(isbn);
}

The lookupBookInventory method will use the stored context reference to retrieve the BookInventory service instance from the framework's registered services and then return it.

private BookInventory lookupBookInventory() {
String name = BookInventory.class.getName();
ServiceReference ref =
this.context.getServiceReference(name);
if (ref == null)
{
throw new
BookInventoryNotRegisteredRuntimeException(name);
}
return (BookInventory) this.context.getService(ref);
}

Since we have loose coupling between the bookshelf service and the inventory component, we need to make sure that there is an implementation registered for the BookInventory interface. This is why we check if ref is null before using it.

Have a go hero -complete service implementation

You have a general idea about this now, right? How about completing the implementation on your own?

I've included the methods missing from the implementation of the BookshelfService below for quick reference:

public MutableBook getBookForEdit(String sessionId, String isbn)
throws BookNotFoundException;
public void addBook(
String sessionId, String isbn, String title,
String author, String group, int grade)
throws BookAlreadyExistsException, InvalidBookException;
public void modifyBookCategory(
String sessionId, String isbn, String group)
throws BookNotFoundException, InvalidBookException;
public void modifyBookRating(
String sessionId, String isbn, int grade)
throws BookNotFoundException, InvalidBookException;
public Set<String> getCategories(String sessionId);
public void removeBook(String sessionId, String isbn)
throws BookNotFoundException;
public Set<String> searchBooksByAuthor(
String sessionId, String authorLike);
public Set<String> searchBooksByCategory(
String sessionId, String categoryLike);
public Set<String> searchBooksByTitle(
lookupBookInventory methodimplementation, completingString sessionId, String titleLike);
public Set<String> searchBooksByRating(
String sessionId, int gradeLower, int gradeUpper);
}

We'll get the chance to test some of those in a bit to make sure they're working as expected before adding the client interaction bits.

But before getting there, we still need to write the bundle activator code.

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

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