FileDocCategorySizeDatePackage
ContextBookServiceServant.javaAPI DocExample5527Sat Oct 19 12:35:52 BST 2002ora.jwsnut.chapter6.contextbookservice

ContextBookServiceServant

public class ContextBookServiceServant extends Object implements ContextBookQuery, javax.xml.rpc.server.ServiceLifecycle
Implementation class for the context-handling book web service.

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

        boolean allowed = true;
        if (userName != null) {
            // Authentication is configured.
            Principal principal = endpointContext.getUserPrincipal();
            allowed = principal != null && userName.equals(principal.getName());
        }
        return allowed;            
    
public voiddestroy()
Called when the service instance is no longer required.

        // 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
ContextBookServiceException if the book title is unknown

        String authorName = ContextBookServiceServantData.getBookAuthor(title);
        if (authorName == null || !checkAccess()) {
            throw new ContextBookServiceException("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 = ContextBookServiceServantData.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 = ContextBookServiceServantData.getBookTitles();        
        if (bookTitles == null || index < 0 || index >= bookTitles.length || !checkAccess()) {
            return null;
        }
        return isUpperCase() ? bookTitles[index].toUpperCase() : bookTitles[index];
    
public voidinit(java.lang.Object context)

    
    /* -- Implementation of the ServiceLifeCycle interface -- */
          
        endpointContext = (ServletEndpointContext)context;
        
        // Get the authorized user name from the init parameters
        ServletContext servletContext = endpointContext.getServletContext();
        userName = servletContext.getInitParameter("UserName");
        
        // Get alphabetic sorting flag from JNDI
        try {
            InitialContext namingCtx = new InitialContext();
            Object value = namingCtx.lookup("java:comp/env/sorted");
            sorted = value instanceof Boolean ? ((Boolean)value).booleanValue() : false; 
            ContextBookServiceServantData.setSorted(sorted);
        } catch (NamingException ex) {
            servletContext.log("Exception while accessing naming context", ex);
        }
    
public booleanisUpperCase()
Returns whether book titles are being forced to upper case.

return
true if book titles are forced to upper case, false if not.

        HttpSession session = endpointContext.getHttpSession();
        if (session != null) {
            Boolean upperCase = (Boolean)session.getAttribute(UPPER_CASE); 
            return upperCase == null ? false : upperCase.booleanValue();
        }      
        
        // No session - upper case mode not allowed
        return false;
    
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);
        }
    
public voidsetUpperCase(boolean cond)
Sets whether book titles returned by the getBookTitle() method should be in upper case.

param
cond true if all book titles should be in upper case, false for title case.

        HttpSession session = endpointContext.getHttpSession();
        if (session != null) {
            session.setAttribute(UPPER_CASE, cond ? Boolean.TRUE : Boolean.FALSE);            
        }