Hello World using Java RMI-IIOP

The ORB supports writing CORBA interfaces using Java RMI. With this feature, you can work just with Java and never have to worry about learning and using IDL.

This is a simple example to get you started with Java CORBA programming using simple transient object support in the ORB. Java RMI will be used to define Object interfaces.

Hello RMI Interface

The Hello World interface defined using Java RMI.

package helloWorld2;
                                                                           
public interface Hello extends java.rmi.Remote
{
    String sayHello() throws java.rmi.RemoteException;
}

Hello Implementation

The HelloImpl implements the Hello interface.

package helloWorld2;
                                                                           
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
                                                                           
public class HelloImpl extends PortableRemoteObject implements Hello
{
    public HelloImpl() throws RemoteException {}
                                                                           
    public String sayHello() throws RemoteException
    {
    |   return "Hello World!\n";
    }
}

Hello World Server

The server creates the ORB, creates a Java RMI servant, writes out it's stringified object reference, and then waits for invocations. Notice, that since HelloImpl is not a CORBA object (does not implement org.omg.CORBA.Object,) you need to first get its stub/object reference befor using the object_to_string method.

package helloWorld2;
                                                                           
import util.Util;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string((Object) 
    |   |       PortableRemoteObject.toStub(hello));
    |   |                                                                  
    |   |   // write the stringified object reference
    |   |   Util.writeIOR(helloIOR, "ior", true);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

Hello World Client

The client creates the ORB, reads the stringified object reference of the Hello object, casts it to the Hello type, and then invokes the sayHello method on it.

package helloWorld2;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
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 = (Hello) orb.string_to_object(helloIOR);
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

Note: When an object is unmarshaled or destringified, the ORB automatically finds the right stub for it. This makes it possible to use Java casts instead of using the narrow method on javax.rmi.PortableRemoteObject. The use of narrow method is of course the standard thing to do. For example,

Hello hello = (Hello) orb.string_to_object(helloIOR);

above can be replaced by

Hello hello = (Hello) PortableRemoteObject.narrow(orb.string_to_object(
    helloIOR), Hello.class);

if desired.

Generating Stub and Skeleton

Notice that none of the above code had any dependency on generated code. The ORB still needs the stubs and skeletons to do marshaling and request brokering.

To generate stubs and skeletons, run the rmi2iiop compiler. The rmi2iiop compiler works with compiled Java class files. It is customary to run the compiler against the remote implementation instead of the remote interface.

   rmi2iiop -keepgenerated -ds gensrc -d ../../lib helloWorld2.HelloImpl

The following Java classes are genereted in the helloWorld2 package.

_Hello_Stub     // the Stub
_HelloImpl_Tie  // the delegation based Skeleton

Code Generated from HelloImpl.java

  1. _Hello_Stub: The rmi2iiop compiler by default generates stubs that are enabled for local invocation. For locally running objects, a deep copy of serializable parameters is made to preserve RMI parameter passing semantics (pass by value of non-remote parameters, and pointer sharing) and then IIOP is bypassed to do a direct call on the servant object.

    You can disable copying of parameters by specifying -localnocopy flag on the rmi2iiop compiler. You can skip generation of local stubs all together by using the -nolocal flag.

    // Wed Jun 09 14:46:24 EDT 2004
                                                                               
    package helloWorld2;
                                                                               
                                                                               
    public class _Hello_Stub extends javax.rmi.CORBA.Stub 
        implements helloWorld2.Hello
    {
        private static final String[] __ids = {
        |   "RMI:helloWorld2.Hello:0000000000000000"
        };
                                                                               
        public String[] _ids() { return __ids; }
                                                                               
        public java.lang.String sayHello() 
            throws java.rmi.RemoteException
        {
        |   _local_stub:
        |                                                                      
        |   if (javax.rmi.CORBA.Util.isLocal(this)) {
        |   |                                                                  
        |   |   // get the Servant Object
        |   |   org.omg.CORBA.portable.ServantObject _servObj =
        |   |       _servant_preinvoke("sayHello", helloWorld2.Hello.class);
        |   |   if (_servObj == null) break _local_stub;
        |   |                                                                  
        |   |   try {
        |   |   |   // invoke on the servant
        |   |   |   return ((helloWorld2.Hello) _servObj.servant).sayHello();
        |   |   |                                                              
        |   |   } catch (Throwable ex) {
        |   |   |   // copy the exception
        |   |   |   ex = (Throwable) javax.rmi.CORBA.Util.copyObject(ex, _orb());
        |   |   |                                                              
        |   |   |   throw javax.rmi.CORBA.Util.wrapException(ex);
        |   |   |                                                              
        |   |   } finally {
        |   |   |   _servant_postinvoke(_servObj);
        |   |   }
        |   }
        |                                                                      
        |   org.omg.CORBA_2_3.portable.InputStream in = null;
        |                                                                      
        |   try {
        |   |   try {
        |   |   |   // create an output stream
        |   |   |   org.omg.CORBA_2_3.portable.OutputStream out = 
        |   |   |       (org.omg.CORBA_2_3.portable.OutputStream) 
        |   |   |        _request("sayHello", true);
        |   |   |                                                              
        |   |   |   // do the invocation
        |   |   |   in = (org.omg.CORBA_2_3.portable.InputStream) _invoke(out);
        |   |   |   return (java.lang.String) in.read_value(java.lang.String.class);
        |   |   |                                                              
        |   |   } catch (org.omg.CORBA.portable.ApplicationException ex) {
        |   |   |                                                              
        |   |   |   // get the input stream
        |   |   |   in = (org.omg.CORBA_2_3.portable.InputStream)
        |   |   |       ex.getInputStream();
        |   |   |                                                              
        |   |   |   // read the exception id
        |   |   |   String id = ex.getId();
        |   |   |   in.read_string();
        |   |   |                                                              
        |   |   |   // unexpected exception
        |   |   |   throw new java.rmi.UnexpectedException(id);
        |   |   |                                                              
        |   |   } catch (org.omg.CORBA.portable.RemarshalException rex) {
        |   |   |   return sayHello();
        |   |   |                                                              
        |   |   } finally {
        |   |   |   _releaseReply(in);
        |   |   }
        |   |                                                                  
        |   } catch (org.omg.CORBA.SystemException ex) {
        |   |   throw javax.rmi.CORBA.Util.mapSystemException(ex);
        |   }
        }
                                                                               
    }
    
  2. _HelloImpl_Tie:

    // Wed Jun 09 14:46:24 EDT 2004
                                                                               
    package helloWorld2;
                                                                               
                                                                               
    public class _HelloImpl_Tie
        extends javax.rmi.CORBA.Stub
        implements javax.rmi.CORBA.Tie, helloWorld2.Hello
    {
        private org.omg.CORBA.portable.ObjectImpl _thisObject;
                                                                               
        public synchronized org.omg.CORBA.Object thisObject()
        {
        |   if (_thisObject == null) {
        |   |   try {
        |   |   |   // create the stub
        |   |   |   _thisObject = (org.omg.CORBA.portable.ObjectImpl)
        |   |   |       Class.forName("helloWorld2._Hello_Stub").newInstance();
        |   |   |                                                              
        |   |   |   // set the delegate in the stub
        |   |   |   try {
        |   |   |   |   _thisObject._set_delegate(_get_delegate());
        |   |   |   } catch (org.omg.CORBA.BAD_OPERATION ex) {}
        |   |   |                                                              
        |   |   } catch (java.lang.Exception ex) {
        |   |   |   throw new java.lang.RuntimeException("unable to create stub");
        |   |   }
        |   }
        |                                                                      
        |   return _thisObject;
        }
                                                                               
        public org.omg.CORBA.ORB orb()
        {
        |   return _orb();
        }
                                                                               
        public void orb(org.omg.CORBA.ORB orb)
        {
        |   orb.connect(this);
        }
                                                                               
        private static final String[] __ids = {
        |   "RMI:helloWorld2.Hello:0000000000000000"
        };
                                                                               
        public String[] _ids() { return __ids; }
                                                                               
        public synchronized void deactivate()
        {
        |   _orb().disconnect(this);
        |   _thisObject = null;
        }
                                                                               
        private helloWorld2.HelloImpl _target;
                                                                               
        public void setTarget(java.rmi.Remote target)
        {
        |   _target = (helloWorld2.HelloImpl) target;
        }
                                                                               
        public java.rmi.Remote getTarget()
        {
        |   return _target;
        }
                                                                               
        public org.omg.CORBA.portable.OutputStream _invoke(String method,
            org.omg.CORBA.portable.InputStream in1,
            org.omg.CORBA.portable.ResponseHandler rh)
        {
        |   org.omg.CORBA_2_3.portable.InputStream in =
        |       (org.omg.CORBA_2_3.portable.InputStream) in1;
        |   org.omg.CORBA_2_3.portable.OutputStream out = null;
        |                                                                      
        |   try {
        |   |   if (method.equals("sayHello")) {
        |   |   |   java.lang.String result = _target.sayHello();
        |   |   |   out = (org.omg.CORBA_2_3.portable.OutputStream)
        |   |   |       rh.createReply();
        |   |   |   out.write_value(result, java.lang.String.class);
        |   |   }
        |   |                                                                  
        |   |   else throw new org.omg.CORBA.BAD_OPERATION(method);
        |   |                                                                  
        |   } catch (org.omg.CORBA.SystemException ex) {
        |   |   throw ex;
        |   |                                                                  
        |   } catch (java.lang.Throwable ex) {
        |   |   throw new org.omg.CORBA.portable.UnknownException(ex);
        |   }
        |                                                                      
        |   return out;
        }
                                                                               
                                                                               
        public java.lang.String sayHello() throws java.rmi.RemoteException
        {
        |   return _target.sayHello();
        }
    }
    


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