FileDocCategorySizeDatePackage
BookServiceClient.javaAPI DocExample4340Sat Feb 01 14:21:48 GMT 2003ora.jwsnut.chapter2.client

BookServiceClient

public class BookServiceClient extends Object

Fields Summary
private static final int
GET_BOOKS
private static final int
GET_AUTHOR
private static final int
GET_EDITOR
private static final int
GET_PRICE
private static final String[]
commands
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

    
         
        
        try {
            // Must at least have the service address
            if (args.length == 0 || args.length == 2) {
                usage();
            }
            
            // Make sure that the arguments are legal
            int command = -1;
            String name = null;
            if (args.length != 1) {
                String request = args[1];
                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) {
                    // Not found
                    usage();
                }
            } else {
                command = GET_BOOKS;
            }

            // Get a reference to the stub and set the service address
            BookService_Impl service = new BookService_Impl();
            BookQuery bookQuery = (BookQuery)service.getBookQueryPort();
            ((Stub)bookQuery)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);

            // Perform the RPC and print the results          
            switch (command) {
            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;                
            
            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;                
            }
            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 voidusage()

        System.err.println("Usage: BookServiceClient address [args]");
        System.err.println("\tNo arguments - lists all 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.exit(1);