FileDocCategorySizeDatePackage
ProxyHandlerBookServiceClient.javaAPI DocExample3318Tue Dec 17 23:21:54 GMT 2002ora.jwsnut.chapter6.client

ProxyHandlerBookServiceClient.java

package ora.jwsnut.chapter6.client;

// J2SE imports
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

// JAX-RPC imports
import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Stub;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.HandlerRegistry;
import javax.xml.rpc.holders.StringHolder;

// Service imports
import ora.jwsnut.chapter6.handlerbookservice.HandlerBookQuery;
import ora.jwsnut.chapter6.handlerbookservice.HandlerBookServiceException;

public class ProxyHandlerBookServiceClient {
    
    public static void main(String[] args) {
        
        final String NS_URI = "urn:jwsnut.chapter6.handlerbookservice/wsdl/HandlerBookQuery";
        try {
            if (args.length != 1) {
                usage();
            }
            
            // The WSDL URL is the only command line argument
            URL wsdlURL = new URL(args[0]);
            
            // Form the names of the service and of the port
            QName serviceName = new QName(NS_URI, "HandlerBookService");
            QName portName = new QName(NS_URI, "HandlerBookQueryPort");

            // Get the Service 
            ServiceFactory factory = ServiceFactory.newInstance();
            Service service = factory.createService(wsdlURL, serviceName);


		// Build a handler chain with one handler
		QName[] headers = new QName[] {
						new QName(NS_URI, "auth"),
                                    new QName(NS_URI, "time")
                              };
		HashMap map = new HashMap();
		map.put("debug", "true");
		HandlerInfo info = new HandlerInfo(ClientHandler.class, map, headers);
		ArrayList handlerList = new ArrayList();
		handlerList.add(info);

            // Add the handler chain the HandlerRegistry
            HandlerRegistry handlerRegistry = service.getHandlerRegistry();
		handlerRegistry.setHandlerChain(portName, handlerList);
            
            // Now get the dynamic proxy
            HandlerBookQuery bookQuery = 
                           (HandlerBookQuery)service.getPort(portName,
                                                         HandlerBookQuery.class);

            // Get info for each book.
            StringHolder stringHolder = new StringHolder();

            int count = bookQuery.getBookCount();
            System.out.println("Book count = " + count);
            
            for (int i = 0; i < count; i++) {
                String title = bookQuery.getBookTitle(i);
                bookQuery.getBookAuthor(title, stringHolder);
                System.out.println("Title: " + title + ", author: " + stringHolder.value);                
            }
            
            // Log success
            bookQuery.log("ProxyHandlerBookServiceClient: success"); 

            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: ProxyHandlerBookServiceClient wsdlURL");
        System.exit(1);
    }
}