FileDocCategorySizeDatePackage
BookServiceEJB.javaAPI DocExample4115Wed Jan 29 23:48:24 GMT 2003ora.jwsnut.chapter2.ejbbookservice

BookServiceEJB.java

package ora.jwsnut.chapter2.ejbbookservice;

import java.util.HashMap;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import ora.jwsnut.chapter2.bookservice.BookInfo;
import ora.jwsnut.chapter2.bookservice.BookServiceException;

/**
 * Implementation class for the books web service
 * hosted by a stateless session bean.
 */
public class BookServiceEJB implements SessionBean {

    private SessionContext sessionContext;
    
    /**
     * Provides the bean with access to state
     * from the container.
     */ 
    public void setSessionContext(SessionContext sessionContext) throws EJBException {
        this.sessionContext = sessionContext;
    }

    // SessionBean methods
    public void ejbCreate() throws EJBException {
        // Nothing to do in this example
    }
    
    public void ejbRemove() throws EJBException {
        // Nothing to do in this example
    }

    /**
     * Activation/passivations methods are also no-ops
     */
    public void ejbActivate() throws EJBException {
    }
    
    public void ejbPassivate() throws EJBException {
    }
    
    /**
     * Gets the number of books known to the service
     * @return the number of books known to the service.
     */
    public int getBookCount() {
        return BookServiceServantData.getBookInfo().length;
    }
    
    /**
     * Get the author for a book with a given name.
     * @param name the name of the book.
     * @return the author of the book, or <code>null</code>
     * if no matching book can be found.
     */
    public String getAuthor(String name) {
        BookInfo book = findBook(name);
        return book == null ? null : book.getAuthor();
    }
    
    /**
     * Gets the name of the editor for a book.
     * @param name the name of the book.
     * @return the editor of the book, or <code>null</code>
     * if no matching book can be found.
     */
    public String getEditor(String name) {
        BookInfo book = findBook(name);
        return book == null ? null : book.getEditor();
    }
    
    /**
     * Gets the price of a book with a given name. If the given
     * name does not match with a known book, a BookServiceException
     * is thrown.
     * @param name the name of the book.
     * @return the price of the book.
     */
    public double getPrice(String name) throws BookServiceException {
        BookInfo book = findBook(name);
        if (book == null) {
            // No such book - throw an exception
            throw new BookServiceException("No matching book for '" + name + "'");
        }
        return book.getPrice();
    }
    
    /**
     * Gets information for all known books, in the form of an array.
     * @return an array of <code>BookInfo</code> objects with one
     * entry for each book.
     */
    public BookInfo[] getBookInfo() {        
        return BookServiceServantData.getBookInfo();
    }
    
    /** 
     * Returns all of the books known to the book
     * service in the form of a <code>HashMap</code> where the
     * key to each entry is the book title in upper case
     * and the value is a <code>BookInfo</code> object.
     */
    public HashMap getBookMap() {
        return BookServiceServantData.getBookInfoHashMap();
    }
    
    /* -- Implementation details -- */
    /**
     * Looks for a book whose title matches a given string.
     * The comparison is case-insensitive.
     * @param name the string to match against the book name
     * @return the <code>BookInfo</code> object for the first
     * matching book, or <code>null</code> if no match exists.
     */
    private BookInfo findBook(String name) {
        BookInfo[] books = BookServiceServantData.getBookInfo();
        for (int i = 0; i < books.length; i++) {
            if (books[i].getTitle().equalsIgnoreCase(name)) {
                // Found a match
                return books[i];
            }
        }
        return null;        // No match
    }
    
}