package org.apache.soap.transport.jms;

import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.*;
import org.apache.soap.transport.*;
import org.apache.soap.rpc.*;

/**
 * This is just a humble client to test out SOAP over JMS.
 *
 * @author Hari Nam Singh (hsingh@elite.com)
 */
public class JMSTestClient {

    /**
     * Starts the app.
     */
    public static void main(String[] args) {
        if ((args.length > 6) || (args.length < 5)) {
            System.err.println("Usage: java " + JMSTestClient.class.getName() + "\\"
                               + " routerURL requestQueueName responseQueueName"
                               + " jndiFactory jndiURL [message]");
            System.exit(1);
        }
        try {
            // give the command line args nice names
            URL routerURL          = new URL(args[0]);
            String requestQueue    = args[1]
                   , responseQueue = args[2]
                   , jndiFactory   = args[3]
                   , jndiURL       = args[4]
                   , textMess      = null;
            if (args.length == 6) {
                textMess = args[5];
            }

            // build call
            SOAPTransport ss = new SOAPJMSConnection(requestQueue
                                                     , responseQueue
                                                     , jndiFactory
                                                     , jndiURL);
            Call call = new Call();
            call.setSOAPTransport(ss);
            call.setTargetObjectURI("swifta");
            if (textMess == null) {
                call.setMethodName("soapRead");
            } else {
                call.setMethodName("soapSend");
            }
            call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
            Vector params = new Vector();
            params.addElement(new Parameter("queue", String.class, "testqueue3@router1", null));
            if (textMess != null) {
                params.addElement(new Parameter("message", String.class, textMess, null));
            }
            call.setParams(params);

            Response resp = call.invoke(routerURL, "");

            // check for fault
            if (resp == null) {
                System.err.println("response is null???");
                System.exit(1);
            }
            if (resp.generatedFault()) {
                Fault fault = resp.getFault();
                if (fault == null) {
                    System.out.println("fault is null...");
                    System.exit(1);
                }
                System.err.println("Ouch, the call failed: ");
                System.err.println("  Fault Code = " + fault.getFaultCode());
                System.err.println("  Fault String = " + fault.getFaultString());
                System.exit(1);
            } else {
                Parameter result = resp.getReturnValue();
                if (result == null) {
                    System.out.println("result is null");
                    System.exit(1);
                }/* else {
                    System.out.println(result.getValue());
                }*/
                System.out.println(result.getValue());
                if (textMess != null) {
                    System.out.println("MESSAGE SENT SUCCESSFULLY");
                } else {
                    System.out.println("THE MESSAGE RETRIEVED IS: " + result.getValue());
                }
                System.exit(0);
            }

        } catch(Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }

    }

}