FileDocCategorySizeDatePackage
FirstDynamicClient.javaAPI DocExample2629Mon Dec 16 13:26:38 GMT 2002ora.jwsnut.chapter6.client

FirstDynamicClient.java

package ora.jwsnut.chapter6.client;

import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

public class FirstDynamicClient {
      
    // Namespace for the service
    private static final String SERVICE_URI = 
                     "urn:jwsnut.chapter6.smallbookservice/wsdl/SmallBookQuery";
     
    public static void main(String[] args) {
        
        if (args.length != 1) {
            usage();
        }

        try {  
            QName serviceName = new QName(SERVICE_URI, "SmallBookService");
            QName portName = new QName(SERVICE_URI, "SmallBookQueryPort");
            ServiceFactory factory = ServiceFactory.newInstance();
            Service service = factory.createService(new URL(args[0]), serviceName); 
            
            // Get a Call object for the getBookCount() method
            Call call = service.createCall(portName, new QName(SERVICE_URI, "getBookCount"));
            Object result = call.invoke(null);
            int bookCount = ((Integer)result).intValue();
            System.out.println("Number of books: " + bookCount);

            // Get Call objects for the getBookTitle() and getBookAuthor methods
            Call bookTitleCall = service.createCall(portName, new QName(SERVICE_URI, "getBookTitle"));
            Call bookAuthorCall = service.createCall(portName, new QName(SERVICE_URI, "getBookAuthor"));
            for (int i = 0; i < bookCount; i++) {
                // Get the book title
                result = bookTitleCall.invoke(new Object[] { new Integer(i) });
                String title = (String)result;
                System.out.print("Book title is [" + title + "], ");

                // Get the book's author.
                bookAuthorCall.invoke(new Object[] { title, null });
                List list = bookAuthorCall.getOutputValues();
                String author = (String)list.get(0);
                System.out.println("author is [" + author + "]");
            }
            
            // Log successful completion
            Call logCall = service.createCall(portName, new QName(SERVICE_URI, "log"));
            logCall.invokeOneWay(new Object[] { "Successful completion." });
        } catch (Exception ex) {
            System.out.println(ex);
            ex.printStackTrace(System.out);
        }
    }
    
    public static void usage() {
        System.err.println("Usage: java FirstDynamicClient WSDLAddress");
        System.exit(1);
    }
}