FileDocCategorySizeDatePackage
SerializerBookServiceClient.javaAPI DocExample2589Tue Oct 29 13:56:34 GMT 2002ora.jwsnut.chapter6.client

SerializerBookServiceClient.java

package ora.jwsnut.chapter6.client;

import java.util.HashMap;
import java.util.Iterator;

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

// Service imports
import ora.jwsnut.chapter6.serializerbookservice.BookInfo;
import ora.jwsnut.chapter6.serializerbookservice.ElectronicBookInfo;
import ora.jwsnut.chapter6.serializerbookservice.SBookQuery;
import ora.jwsnut.chapter6.serializerbookservice.BookServiceException;

// Import for service
import ora.jwsnut.chapter6.serializerbookservice.SerializerBookService_Impl;

public class SerializerBookServiceClient {
        
    public static void main(String[] args) {
        
        try {
            // Must have the service address
            if (args.length != 1) {
                usage();
            }
            
           // Get a reference to the stub and set the service address
            SerializerBookService_Impl service = new SerializerBookService_Impl();
            SBookQuery bookQuery = (SBookQuery)service.getSBookQueryPort();
            ((Stub)bookQuery)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, args[0]);

            // Get all of the books
            BookInfo[] info = bookQuery.getBookInfo();
            System.out.println("Books from getBookInfo()");
            for (int i = 0; i < info.length; i++) {
                boolean eBook = info[i] instanceof ElectronicBookInfo;
                System.out.println("\tTitle: " + info[i].getTitle() + 
                    (eBook ? "; URL: " + ((ElectronicBookInfo)info[i]).getURL() : "; not E-Book")); 
            }
            
            // Get the books in a HashMap
            HashMap map = bookQuery.getBookMap();
            System.out.println("Books from getBookMap()");
            Iterator iter = map.values().iterator();
            while (iter.hasNext()) {
                BookInfo book = (BookInfo)iter.next();
                boolean eBook = book instanceof ElectronicBookInfo;
                System.out.println("\tTitle: " + book.getTitle() + 
                    (eBook ? "; URL: " + ((ElectronicBookInfo)book).getURL() : "; not E-Book")); 
            }
            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: SerializerBookServiceClient address");
        System.exit(1);
    }
}