FileDocCategorySizeDatePackage
DIISimpleClient.javaAPI DocExample2568Wed Apr 05 11:25:42 BST 2000None

DIISimpleClient.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

//
// CORBA example 11: Calling a remote method using DII.  Note that no
//   interface is loaded for the remote object.
//

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import com.sun.CORBA.iiop.*;

public class DIISimpleClient {
  public static void main(String argv[]) {
    org.omg.CORBA.ORB myORB = org.omg.CORBA.ORB.init(argv, null);
    org.omg.CORBA.ORB singleORB = org.omg.CORBA.ORB.init();
    try {
      org.omg.CORBA.Object ncRef = 
        myORB.resolve_initial_references("NameService");
      NamingContext nc = NamingContextHelper.narrow(ncRef);

      NameComponent nComp = new NameComponent("ThisOrThatServer", "");
      NameComponent[] path = {nComp};
      org.omg.CORBA.Object objRef = nc.resolve(path);

      // Make a dynamic call to the doThis method

      // First build the argument list.  In this case, there's a single String
      // argument to the method.
      NVList argList = myORB.create_list(1);
      Any arg1 = myORB.create_any();
      arg1.insert_string("something");
      NamedValue nvArg = 
        argList.add_value("what", arg1, org.omg.CORBA.ARG_IN.value);

      // Create an Any object to hold the return value of the method
      Any result = myORB.create_any();
      result.insert_string("dummy");
      NamedValue resultVal = 
        myORB.create_named_value("result", result, 
                                 org.omg.CORBA.ARG_OUT.value);

      // Get the local context from the ORB
      // NOTE: This call does not work in Java 2, and will return a
      //   NOT_IMPLEMENTED exception.  To make this work in Java 2, simply
      //   remove this call to get_default_context(), and pass a null pointer
      //   into the _create_request() call below.
      Context ctx = myORB.get_default_context();

      // Create the method request and invoke the method
      Request thisReq = 
        objRef._create_request(ctx, "doThis", argList, resultVal);
      thisReq.invoke();

      // Get the return value
      result = thisReq.result().value();
      System.out.println("doThis() returned: " + result.extract_string());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}