FileDocCategorySizeDatePackage
SecondDynamicClient.javaAPI DocExample4326Tue Dec 17 19:10:42 GMT 2002ora.jwsnut.chapter6.client

SecondDynamicClient.java

package ora.jwsnut.chapter6.client;

import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.encoding.XMLType;
import javax.xml.soap.SOAPConstants;

public class SecondDynamicClient {
      
    // 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 {  
            // Create a service without WSDL
            QName serviceName = new QName(SERVICE_URI, "SmallBookService");
            QName portName = new QName(SERVICE_URI, "SmallBookQueryPort");
            ServiceFactory factory = ServiceFactory.newInstance();
            Service service = factory.createService(serviceName); 
            
            // Get a Call object for the getBookCount() method
            Call call = service.createCall(portName, new QName(SERVICE_URI, "getBookCount"));
            
            // Tailor the call object. Just set the return type,
            // the encoding and the target endpoint address 
            call.setReturnType(XMLType.XSD_INT, java.lang.Integer.class);
            call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, SOAPConstants.URI_NS_SOAP_ENCODING);
            call.setTargetEndpointAddress(args[0]);
            
            // Make the call
            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"));
            bookTitleCall.addParameter("int_1", XMLType.XSD_INT, java.lang.Integer.class, ParameterMode.IN); 
            bookTitleCall.setReturnType(XMLType.XSD_STRING, java.lang.String.class);
            bookTitleCall.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, SOAPConstants.URI_NS_SOAP_ENCODING);
            bookTitleCall.setTargetEndpointAddress(args[0]);
            
            Call bookAuthorCall = service.createCall(portName, new QName(SERVICE_URI, "getBookAuthor"));
            bookAuthorCall.addParameter("String_1", XMLType.XSD_STRING, java.lang.String.class, ParameterMode.IN);
            bookAuthorCall.addParameter("String_2", XMLType.XSD_STRING, java.lang.String.class, ParameterMode.OUT);
            bookAuthorCall.setReturnType(null);
            bookAuthorCall.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, SOAPConstants.URI_NS_SOAP_ENCODING);
            bookAuthorCall.setTargetEndpointAddress(args[0]);
            
            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 });
                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.addParameter("String_1", XMLType.XSD_STRING, java.lang.String.class, ParameterMode.IN);
            logCall.setReturnType(null);
            logCall.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, SOAPConstants.URI_NS_SOAP_ENCODING);
            logCall.setTargetEndpointAddress(args[0]);            
            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 SecondDynamicClient targetAddress");
        System.exit(1);
    }
}