FileDocCategorySizeDatePackage
ProxyBookServiceClient.javaAPI DocExample5339Mon Dec 16 14:46:38 GMT 2002ora.jwsnut.chapter6.client

ProxyBookServiceClient.java

package ora.jwsnut.chapter6.client;

// J2SE imports
import java.net.URL;

// JAX-RPC imports
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Stub;

// Service imports
import ora.jwsnut.chapter6.proxybookservice.BookInfo;
import ora.jwsnut.chapter6.proxybookservice.BookQuery;
import ora.jwsnut.chapter6.proxybookservice.BookServiceException;

public class ProxyBookServiceClient {
    
    private static final int GET_BOOKCOUNT = 0;
    private static final int GET_AUTHOR = 1;
    private static final int GET_EDITOR = 2;
    private static final int GET_PRICE = 3;
    private static final int GET_BOOKS = 4;
    
    private static final String[] commands = {
            "author", "editor", "price", "list"
    };
    
    public static void main(String[] args) {
       
        try {
            if (args.length == 0) {
                usage();
            }
            
            // Make sure that the arguments are legal
            int command = -1;
            String name = null;
            if (args.length != 1) {
                String request = args[1];
                if (args.length > 2) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(args[2]);
                    for (int i = 3; i < args.length; i++) {
                        sb.append(' ');
                        sb.append(args[i]);                    
                    }
                    name = sb.toString();
                    System.out.println("NAME = [" + name + "]");
                }
                for (int i = 0; i < commands.length; i++) {
                    if (request.equals(commands[i])) {
                        command = i + 1;
                        break;
                    }
                }
                
                if (command == -1 || (command != GET_BOOKS && name == null)) {
                    // Not found or no name supplied
                    usage();
                }
            } else {
                command = GET_BOOKCOUNT;
            }
            
            // The WSDL URL is the first command line argument
            URL wsdlURL = new URL(args[0]);
            
            // Form the names of the service and of the port
            QName serviceName = new QName("urn:jwsnut.chapter2.bookservice/wsdl/BookQuery",
                                          "BookService");
            QName portName = new QName("urn:jwsnut.chapter2.bookservice/wsdl/BookQuery",
                                          "BookQueryPort");

            // Get the Service
            ServiceFactory factory = ServiceFactory.newInstance();
            Service service = factory.createService(wsdlURL, serviceName);
            BookQuery bookQuery = 
                           (BookQuery)service.getPort(portName,
                                                         BookQuery.class);

            // Perform the RPC and print the results          
            switch (command) {
            case GET_BOOKCOUNT:
                int bookCount = bookQuery.getBookCount();
                System.out.println("Book count = " + bookCount);
                break;
            
            case GET_AUTHOR:
                String author = bookQuery.getAuthor(name);
                System.out.println(author != null ? author :
                                    "No matching book found");
                break;
                    
            case GET_EDITOR:
                String editor = bookQuery.getEditor(name);
                System.out.println(editor != null ? editor :
                                    "No matching book found");
                break;
                
            case GET_PRICE:
                double price = bookQuery.getPrice(name);
                System.out.println("Price = " + price + " USD.");
                break;  
       
            case GET_BOOKS:
                BookInfo[] books = bookQuery.getBookInfo();
                for (int i = 0; i < books.length; i++) {
                    BookInfo info = books[i];
                    System.out.println(info.getTitle() + " by " +
                                info.getAuthor() + ", edited by " +
                                info.getEditor() + ", price USD " +
                                info.getPrice());
                }
                break;               
            }
            System.exit(0);
        } catch (Throwable t) {
            System.out.println("CLASS: " + t.getClass().getName() + "\n\n");
            System.out.println(t.getMessage());
            t.printStackTrace(System.out);
        }
    }  
    
    private static void usage() {
        System.err.println("Usage: ProxyBookServiceClient URL [args]");
        System.err.println("\tNo arguments - gets the number of known books");
        System.err.println("\tauthor name     Finds the author for a book");
        System.err.println("\teditor name     Finds the editor for a book");
        System.err.println("\tprice name      Finds the price for a book");
        System.err.println("\tlist            Gets a list of all known books");
        System.exit(1);
    }
}