/*

 * @author : Simeon M. Greene
 * @version 1.0
 *
 * Development Environment : Oracle9i JDeveloper
 *
 * Name of the File        : RpcLitTopdownClient.java

 *
 * Creation / Modification History
 *    Simeon            05-May-2003       Created
 *    Chandar          22-Aug-2003        Modified
 *
 *  Overview :
 *  This class is the client for RPC literal Web Service. It uses the Web Service

 * stub class to invokes methods of the Web Service.
 */

package oracle.demo.topdownrpc;

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

import javax.xml.rpc.holders.StringHolder;
import javax.xml.rpc.holders.IntHolder;
import javax.xml.rpc.holders.FloatHolder;

import java.net.URL;
import java.util.logging.*;

// IO related imports
import java.io.StringWriter;

import java.io.PrintWriter;

import oracle.demo.topdownrpc.types.*;
import oracle.demo.topdownrpc.stubs.*;

public class RpcLitTopdownClient {

  // define various int, float and String variable and assign some values to them
    private static float VAL_FLOAT = 2.009f;


    private static int VAL_INT = 75;

    private static String VAL_STRING = "April is the cruelest month.";

    private static float VAL_FLOAT1 = 3.8776f;

    private static int VAL_INT1 = 200;

    private static String VAL_STRING1 = "Turning and turning in the widening gyre\n"+
                                          " The falcon cannot hear the falconer; ";


    private static String VAL_STRING2 = "Things fall apart";

    private String m_serviceURL = null;

    private ServiceFactory m_factory;

    private static String RESOURCE_BUNDLE = "oracle.demo.topdownrpc.RpcLitTopdownClient";

   // create an anonymous logger using properties file as resource bundle
    private static Logger m_loggerDefault = Logger.getAnonymousLogger(RESOURCE_BUNDLE);

    private Logger m_logger;


   /**
   *  Construction definition
   */
    public RpcLitTopdownClient(String serviceURL) throws Exception{

        // initialize the web service URL
        m_serviceURL = serviceURL;

        // create an instance of Service Factory
        m_factory = ServiceFactory.newInstance();

        // get a logger object using the properties file as the resource bundle

        m_logger = Logger.getLogger("oracle.demo.topdownrpc",RESOURCE_BUNDLE);

        // set the level to which the messaages should be logged
        m_logger.setLevel(Level.ALL);

        //get a SimpleFormatter to format and log  the messages
        SimpleFormatter formatter = getFormatter();

         // parent handler for logging messages should not be used
         m_logger.setUseParentHandlers(false);

         // Add a log Handler to receive logging messages.
        m_logger.addHandler(new StreamHandler(System.out,formatter){
            public synchronized void publish(LogRecord record){


                // Format and publish a LogRecord.
                super.publish(record);
                flush();
            }
        });
    }

   /**
    *  This method returns a SimpleFormatter that can be used by Logger object
    *  to format messages.
   */
    private SimpleFormatter getFormatter() {
        return new SimpleFormatter(){
            // define method to format the log record

            public synchronized String format(LogRecord record){
                StringBuffer sb = new StringBuffer();

                // Localize and format the message string from  log record
                String message = formatMessage(record);

                //append the level of record
                sb.append(record.getLevel().getLocalizedName());
                sb.append(": ");
                sb.append(message);
                sb.append(System.getProperty("line.separator"));
                if(record.getThrown() != null){
                    try{
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        record.getThrown().printStackTrace(pw);

                        pw.close();
                    }
                    catch(Exception ex){  // catch exceptions

                    }
                }
                return sb.toString();
            }
        };
    }

    /**
    *  This main method which starts the execution of client class
   */
    public static void main(String args[]){
        String serviceURL = null;
        // read the input serice URL argument

        if(args.length > 0){
            serviceURL = args[0];
        }
        else{
            //log error message
            m_loggerDefault.log(Level.SEVERE,"demo.error.nourl");
            return;
        }
        try{

            //instantiate the client
            RpcLitTopdownClient demo = new RpcLitTopdownClient(serviceURL);
            // call Web Service methods
            demo.runDemo();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }

    }

   /**
    *  This method calls various method to invoke Web Service methods
   */
   private void runDemo() throws Exception{
         demoEchoStruct();
          demoEchoStructArray();
          demoEchoString();
          demoEchoIntegerArray();
          demoEchoStringArray();
          demoEchoBoolean();
          demoEchoStructAsSimpleTypes();
          demoEchoNestedArray();
          demoEchoNestedStruct();
    }

  /**
   *  This method calls various method to invoke Web Service methods

  */
  public SoapTestPortTypeRpc initializeService() throws Exception{

        WhiteMesaSoapRpcLitTestSvc service = (WhiteMesaSoapRpcLitTestSvc)
                                         m_factory.loadService(new URL(m_serviceURL),
                                                     WhiteMesaSoapRpcLitTestSvc.class, null);

        SoapTestPortTypeRpc port = service.getSoap11TestRpcLitPort();
        ((Stub)port)._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY,m_serviceURL);

    return port;
  }


   /**
    *  This method creates a nested struct and passes it as a paramter to
      *  echoNestedStruct method  of the Web Service. It also logs the elements
      *  of nested struct received from the Web Service
   */
    private void demoEchoNestedStruct() throws Exception{

        m_logger.log(Level.INFO,"demo.echoNestedStruct.enter");

       //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

        // create the struct object
        SOAPStruct struct1 = new SOAPStruct();

        // set values of struct elements
        struct1.setVarFloat(VAL_FLOAT1);
        struct1.setVarInt(VAL_INT1);
        struct1.setVarString(VAL_STRING1);

        // created nested struct using above struct object
        SOAPStructStruct ss = new SOAPStructStruct(VAL_STRING,VAL_INT,VAL_FLOAT,struct1);

        // call Web Service method and get the returned nested struct
        ss = port.echoNestedStruct(ss);

        // get the struct object  from nested struct
        struct1 = ss.getStructItem();


        // log the values of top level struct attributes
        m_logger.log(Level.INFO,"demo.echoNestedStruct.msg",new Object[]{
            new String() + ss.getVarString(),
            new String() + ss.getVarInt(),
            new String() + ss.getVarFloat(),
        });

        // log the values of nested struct attributes
         m_logger.log(Level.INFO,"demo.echoNestedStruct.msg1",new Object[]{
            new String() + struct1.getVarString(),
            new String() + struct1.getVarInt(),
            new String() + struct1.getVarFloat(),
        });
        m_logger.log(Level.INFO,"demo.echoNestedStruct.exit");
    }

   /*
   * This method invokes echoNestedArray method on web service passing
   *  user defined type containing nested array as an attribute
   **/
    private void demoEchoNestedArray() throws Exception{

        m_logger.log(Level.INFO,"demo.echoNestedArray.enter");

        SOAPArrayStruct arrayStruct = new SOAPArrayStruct();

       //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

        //create user defined type containg String array
        ArrayOfstring aos = new ArrayOfstring(new String[]{
            VAL_STRING1,VAL_STRING2
        });

        //set values of various struct elements
        arrayStruct.setVarArray(aos);
        arrayStruct.setVarFloat(VAL_FLOAT);
        arrayStruct.setVarInt(VAL_INT);
        arrayStruct.setVarString(VAL_STRING);
        arrayStruct = port.echoNestedArray(arrayStruct);

        // call Web Service method
        aos = arrayStruct.getVarArray();

        // log the values from nested array struct object returned by Web Service

        m_logger.log(Level.INFO,"demo.echoNestedArray.msg",new Object[]{
            new String() + arrayStruct.getVarString(),
            new String() + arrayStruct.getVarInt(),
            new String() + arrayStruct.getVarFloat(),
            new String() + aos.getStringItem()[0],
            new String() + aos.getStringItem()[1]
        });
        m_logger.log(Level.INFO,"demo.echoNestedArray.exit");
    }

   /*
   * This method invokes echoStructAsSimpleTypes method on Web Service passing
   *  a struct type as parameter. It also send Holder objects as parameters and logs
   *  the values filled by Web Service in these Holder objects
   **/
    private void demoEchoStructAsSimpleTypes() throws Exception{
        m_logger.log(Level.INFO,"demo.echoStructAsSimpleTypes.enter");

       //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

       // create user defined struct object
        SOAPStruct struct = new SOAPStruct();


        // set the values of struct elements
        struct.setVarFloat(VAL_FLOAT);
        struct.setVarInt(VAL_INT);
        struct.setVarString(VAL_STRING);

        // create a StringHolder object
        StringHolder strHolder = new StringHolder();

        // create an IntHolder object
        IntHolder intHolder = new IntHolder();

        // create a FloatHolder object
        FloatHolder floatHolder = new FloatHolder();

        // call the method on Web Service
        port.echoStructAsSimpleTypes(struct,strHolder,intHolder,floatHolder);

        // log the values returned by Web Service in Holder objects
        m_logger.log(Level.INFO,"demo.echoStructAsSimpleTypes.msg",new Object[]{
            new String() + strHolder.value,
            new String() + intHolder.value,
            new String() + floatHolder.value
        });
        m_logger.log(Level.INFO,"demo.echoStructAsSimpleTypes.exit");
    }


   /*
   * This method invokes echoBoolean method on Web Service
   **/
    private void demoEchoBoolean() throws Exception{
        m_logger.log(Level.INFO,"demo.echoBoolean.enter");

        //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

       // call method on Web Service
        boolean bVal = port.echoBoolean(true);

        // log the value return by the Web Service
        m_logger.log(Level.INFO,"demo.echoBoolean.msg",new Boolean(bVal));
        m_logger.log(Level.INFO,"demo.echoBoolean.exit");
    }

   /*
   *  This method invokes echoIntegerArray method on Web Service passing user defined
   *  type containing int array as an attribute.
   **/
    private void demoEchoIntegerArray() throws Exception{
        m_logger.log(Level.INFO,"demo.echoIntegerArray.enter");

       //initialize Service and get the handle of remote Web Service

        SoapTestPortTypeRpc port = initializeService();

       // create an int array
        int array[] = new int[]{VAL_INT,VAL_INT1};

       // create user defined type
        ArrayOfint aoi = new ArrayOfint(array);

       // invoke Web Service method
       aoi = port.echoIntegerArray(aoi);

       // get int array from object returned by Web Service
        array = aoi.getInteger();

        // log the values of integer array elements
        m_logger.log(Level.INFO,"demo.echoIntegerArray.msg",new Object[]{
            new String() + array[0],
            new String() + array[1]
        });
        m_logger.log(Level.INFO,"demo.echoIntegerArray.exit");
    }

   /*
   * This method invokes echoString method on Web Service
   **/
    private void demoEchoString() throws Exception{
        m_logger.log(Level.INFO,"demo.echoString.enter");


        //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

       // call method on  Web Service
        String ret = port.echoString(VAL_STRING);

        // log value returned by the Web Service
        m_logger.log(Level.INFO,"demo.echoString.msg",ret);
        m_logger.log(Level.INFO,"demo.echoStructArray.exit");
    }

   /*
   *  This method invokes echoIntegerArray method on Web Service passing user defined
   *  type containing String array as an attribute.
   **/
    private void demoEchoStringArray() throws Exception{
        m_logger.log(Level.INFO,"demo.echoStringArray.enter");

        //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

       // create object of user defined type containing String array as an attribute
        ArrayOfstring aos = new ArrayOfstring(new String[]{  VAL_STRING1,VAL_STRING2  });

       // call method on Web Service
        aos = port.echoStringArray(aos);


      // log the values of elements in String array return by the Service
         m_logger.log(Level.INFO,"demo.echoStringArray.msg",new Object[]{
                new String() + aos.getStringItem()[0],
                new String() + aos.getStringItem()[1]
            });

        m_logger.log(Level.INFO,"demo.echoStringArray.exit");
    }


   /*
   *  This method invokes echoStructArray method on Web Service an array of user defined
   *  struct as input paramter
   **/
    private void demoEchoStructArray() throws Exception{
        m_logger.log(Level.INFO,"demo.echoStructArray.enter");

        //initialize Service and get the handle of remote Web Service
        SoapTestPortTypeRpc port = initializeService();

       // create an object of user defined struct
        SOAPStruct struct = new SOAPStruct();

       // set values of struct attributes
        struct.setVarFloat(VAL_FLOAT);
        struct.setVarInt(VAL_INT);
        struct.setVarString(VAL_STRING);

       // create another object of struct type

        SOAPStruct struct1 = new SOAPStruct();
        struct1.setVarFloat(VAL_FLOAT1);
        struct1.setVarInt(VAL_INT1);
        struct1.setVarString(VAL_STRING1);

        // create object containing array of struct types
        ArrayOfSOAPStruct array = new ArrayOfSOAPStruct(new SOAPStruct[]{
            struct,struct1
        });

        // call method on Web Service and get the returned array
        array = port.echoStructArray(array);

        // get the struct array from returned object
        SOAPStruct[] resp = array.getStructItem();

        // log the values of first struct element in the array
        m_logger.log(Level.INFO,"demo.echoStructArray.msg1",new Object[]{
            new String("float=") + resp[0].getVarFloat(),
            new String("int=") + resp[0].getVarInt(),
            new String("string=") + resp[0].getVarString()
        });

        // log the values of second struct element in the array
        m_logger.log(Level.INFO,"demo.echoStructArray.msg1",new Object[]{
            new String("float=") + resp[1].getVarFloat(),
            new String("int=") + resp[1].getVarInt(),
            new String("string=") + resp[1].getVarString()
        });
        m_logger.log(Level.INFO,"demo.echoStructArray.exit");

    }

   /*
   *  This method invokes echoStruct method on Web Service passing user defined
   *  struct as input paramter.
   **/
    private void demoEchoStruct() throws Exception{
        m_logger.log(Level.INFO,"demo.echoStruct.enter");

        //initialize Service and get the handle of remote Web Service
       SoapTestPortTypeRpc port = initializeService();

        // create an object of user defined struct type
        SOAPStruct struct = new SOAPStruct();

        // set the values of struct attributes
        struct.setVarFloat(VAL_FLOAT);
        struct.setVarInt(VAL_INT);
        struct.setVarString(VAL_STRING);

        // call method on  Web Service and get the struct object back
        struct = port.echoStruct(struct);

       // log the values of attributes of returned struct object
        m_logger.log(Level.INFO,"demo.echoStruct.msg",new Object[]{
            new String("float=") + struct.getVarFloat(),
            new String("int=") + struct.getVarInt(),
            new String("string=") + struct.getVarString()
        });
        m_logger.log(Level.INFO,"demo.echoStruct.exit");
    }


}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy