FileDocCategorySizeDatePackage
HeaderBookServiceServant.javaAPI DocExample4192Tue Dec 17 22:54:08 GMT 2002ora.jwsnut.chapter6.headerbookservice

HeaderBookServiceServant.java

package ora.jwsnut.chapter6.headerbookservice;

import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.ServletContext;
import javax.xml.rpc.holders.CalendarHolder;
import javax.xml.rpc.holders.StringHolder;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.server.ServiceLifecycle;
import ora.jwsnut.chapter6.headerbookservice.Authentication;

/**
 * Implementation class for the small book web service.
 */
public class HeaderBookServiceServant implements HeaderBookQuery, ServiceLifecycle {
    
    // ServletEndpointContext object
    private ServletEndpointContext endpointContext;
    
    // Name of an authorized user.
    private String userName;
        
    // Password for the authorized user.
    private String password;

    
    /* -- Implementation of the ServiceLifeCycle interface -- */
    public void init(Object context) throws ServiceException {
        endpointContext = (ServletEndpointContext)context;
        
        // Get the authorized user name and password from the init parameters
        ServletContext servletContext = endpointContext.getServletContext();
        userName = servletContext.getInitParameter("UserName");
        password = servletContext.getInitParameter("Password");
    }
    
    public void destroy() {
        // Nothing to do
    }
    
    /* -- Implementation of the HeaderBookQuery interface -- */    
    /**
     * Gets the number of books known to the service
     * @return the number of books known to the service.
     */
    public int getBookCount(Authentication auth, CalendarHolder calendarHolder) {
        calendarHolder.value = new GregorianCalendar();
        String[] titles = HeaderBookServiceServantData.getBookTitles();
        return titles == null || !checkAccess(auth) ? 0 : titles.length;
    }

    /**
     * Gets the title of a given book.
     * @param index the index of the book whose title is required
     * @return the title of the given book, or <code>null</code> if
     * the index is not valid.
     */
    public String getBookTitle(int index, Authentication auth, CalendarHolder calendarHolder) {
        calendarHolder.value = new GregorianCalendar();
        String[] bookTitles = HeaderBookServiceServantData.getBookTitles(); 
        if (bookTitles == null || index < 0 || index >= bookTitles.length || !checkAccess(auth)) {
            return null;
        }
        return bookTitles[index];
    }
    
    /**
     * Gets the author for a books with a given title
     * @param title the titles of the book
     * @param author an output parameter that will be set to the author of the given book
     * @throws HeaderBookServiceException if the book title is unknown
     */
    public void getBookAuthor(String title, StringHolder author, 
                         Authentication auth, CalendarHolder calendarHolder) throws HeaderBookServiceException {
        calendarHolder.value = new GregorianCalendar();
        String authorName = HeaderBookServiceServantData.getBookAuthor(title);
        if (authorName == null || !checkAccess(auth)) {
            throw new HeaderBookServiceException("Unknown author: " + title);
        }
        author.value = authorName;
    }  
    
    /**
     * Makes a log entry.
     * @param value the value to be logged.
     */
    public void log(String value, Authentication auth, CalendarHolder calendarHolder) {
        calendarHolder.value = new GregorianCalendar();
        if (checkAccess(auth)) {
            endpointContext.getServletContext().log(new Date() + ": " + value);
        }
    }
       
    /**
     * Check whether the calling user is authenticated.
     */
    private boolean checkAccess(Authentication auth) {
        boolean allowed = false;
        if (userName != null && password != null && auth != null) {
            // Authentication is configured.
            return userName.equals(auth.getUserName()) &&
                    password.equals(auth.getPassword());
        }
        return allowed;            
    }
}