Hello World using RMI-IIOP Server and IDL Client

The ORB includes:

These allows us to write IDL clients to our RMI-IIOP servers and vice-versa.

In the example below, we will generate IDL from the helloWorld2 RMI-IIOP example and write an IDL client to talk to it.

Generating IDL

To generate IDL from the Remote interface in the helloWorld2 example, run the rmi2idl compiler. The rmi2idl compiler works with compiled Java class files. It is customary to run the compiler against the remote implementation instead of the remote interface. Run ant rmi2idl, which will run the following:

   rmi2idl -ds gensrc -classpath ../../lib -verbose helloWorld2.HelloImpl

The following IDL is generated in the gensrc/helloWorld2/Hello.idl file.

// Wed Jun 09 14:45:48 EDT 2004
                                                                           
#ifndef __helloWorld2_Hello__
#define __helloWorld2_Hello__
                                                                           
#include "orb.idl"
                                                                           
module helloWorld2 {
                                                                           
    interface Hello {
    |                                                                      
    |   ::CORBA::WStringValue sayHello();
    };
                                                                           
    #pragma ID Hello "RMI:helloWorld2.Hello:0000000000000000"
                                                                           
};
                                                                           
#endif

Note that java.lang.String is mapped to ::CORBA::WStringValue. If you look inside the orb.idl file, you will see that it typedefed to the boxed value type containing a wstring.

/*
 * $Id: orb.idl,v 1.2 2002/08/21 00:34:03 bjarne Exp $
 *
 * Copyright 1998-2003 by Novell, Inc.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Novell, Inc. ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with Novell.
 */
                                                                           
#pragma prefix "omg.org"
                                                                           
module CORBA {  
                                                                           
    valuetype StringValue string;
    valuetype WStringValue wstring;
                                                                           
    interface Policy {
    |   long policy_type();
    |   Policy copy();
    |   void destroy();
    };
                                                                           
    typedef sequence <Policy> PolicyList;
                                                                           
    interface Current {};
                                                                           
    native Environment;
};

Compiling the generated IDL

We will now compile the above IDL using the idl2java compiler. Run ant idl2java, which will run the following:

   idl2java -ds gensrc -compile -pkgPrefix helloWorld2 helloWorld3 -verbose  -I ../../../../../idl gensrc/helloWorld2/Hello.idl

Notice that we used the -pkgPrefix argument to the idl2java compiler since we didn't want it to override files in the helloWorld2 package. We also had to pass it the idl directory to pick up the orb.idl file.

Hello World Client

The Client is pretty much the same as the helloWorld example exept that we now have to use the helloWorld3.helloWorld2.Hello interface.

package helloWorld3;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
import helloWorld3.helloWorld2.Hello;
import helloWorld3.helloWorld2.HelloHelper;
                                                                           
public class helloClient
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // read the stringified object reference
    |   |   String helloIOR = Util.readIOR("ior");
    |   |                                                                  
    |   |   // narrow the stringified object
    |   |   Hello hello = HelloHelper.narrow(orb.string_to_object(helloIOR));
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}


Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.