FileDocCategorySizeDatePackage
HandlerBookServiceServant.javaAPI DocExample3981Tue Dec 17 23:14:52 GMT 2002ora.jwsnut.chapter6.handlerbookservice

HandlerBookServiceServant

public class HandlerBookServiceServant extends Object implements HandlerBookQuery, javax.xml.rpc.server.ServiceLifecycle
Implementation class for the small book web service.

Fields Summary
private javax.xml.rpc.server.ServletEndpointContext
endpointContext
private String
userName
private String
password
Constructors Summary
Methods Summary
private booleancheckAccess()
Check whether the calling user is authenticated.

        boolean allowed = false;
        
        // Get the username and password from the MessageContext
        MessageContext context = endpointContext.getMessageContext();
        String callingUser = (String)context.getProperty(HandlerBookServiceConstants.USERNAME_PROPERTY);
        String callingPwd = (String)context.getProperty(HandlerBookServiceConstants.PASSWORD_PROPERTY);
        
        if (userName != null && password != null) {
            // Authentication is configured.
            return userName.equals(callingUser) &&
                    password.equals(callingPwd);
        }
        return allowed;            
    
public voiddestroy()

        // Nothing to do
    
public voidgetBookAuthor(java.lang.String title, javax.xml.rpc.holders.StringHolder author)
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
HandlerBookServiceException if the book title is unknown

        String authorName = HandlerBookServiceServantData.getBookAuthor(title);
        if (authorName == null || !checkAccess()) {
            throw new HandlerBookServiceException("Unknown author: " + title);
        }
        author.value = authorName;
    
public intgetBookCount()
Gets the number of books known to the service

return
the number of books known to the service.

        String[] titles = HandlerBookServiceServantData.getBookTitles();
        return titles == null || !checkAccess() ? 0 : titles.length;
    
public java.lang.StringgetBookTitle(int index)
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 null if the index is not valid.

        String[] bookTitles = HandlerBookServiceServantData.getBookTitles(); 
        if (bookTitles == null || index < 0 || index >= bookTitles.length || !checkAccess()) {
            return null;
        }
        return bookTitles[index];
    
public voidinit(java.lang.Object context)

        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 voidlog(java.lang.String value)
Makes a log entry.

param
value the value to be logged.

        if (checkAccess()) {
            endpointContext.getServletContext().log(new Date() + ": " + value);
        }