Hello World using IDL

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

1 Hello IDL Interface

Below is the IDL for the Hello World Application. The interface Hello has one method, sayHello which takes no parameters and returns a string. The IDL serves as a contract between the object implementation and its client.
// hello.idl
module helloWorld
{
    interface Hello
    {
    |   string sayHello();
    };
};

2 Generating the Java Bindings

Run the idl2java compiler on the hello.idl file to generate the Java bindings from the above IDL.
idl2java -compile -ds gensrc -d ../../lib hello.idl
The idl2java compiler, generates the Java code to the gensrc directory, and compiles the generated Java code to the ../../lib directory. The following classes are created in the helloWorld Java package. Notice that all the Java class names are derived from the IDL interface Hello's name.
Hello               // Java interface corresponding to IDL interface
HelloOperations     // methods on the Java interface
HelloHelper         // helper methods for narrow, read, write
HelloHolder         // holder for out and inout parameters
_HelloImplBase      // inheritance based Skeleton
_HelloTie           // delegation based Skeleton
_HelloStub          // Stub

3 Hello Implementation

The servant extends the _HelloImplBase class and implements the sayHello method.
package helloWorld;
                                                                           
public class HelloImpl extends _HelloImplBase
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}
Notice that by extending _HelloImplBase, the servant also implements the org.omg.CORBA.Object interface. So, you can use the servant also as an object reference.

4 Hello World Server

The server creates the ORB, creates a CORBA object, writes out it's stringified object reference, and then waits for invocations.
package helloWorld;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant and register with ORB
    |   |   Hello hello = new HelloImpl();
    |   |   orb.connect(hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(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();
    |   }
    }
}
The orb.connect(hello) statement is optional. If you try to marshal or stringify the servant/objref that hasn't been connected, the ORB will automatically connects it up.

5 Hello World Client

The client creates the ORB, reads the stringified object reference of the Hello object, narrows it to the Hello type, and then invokes the sayHello method on it.
package helloWorld;
                                                                           
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 = HelloHelper.narrow(orb.string_to_object(helloIOR));
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

6 TIE Approach

The idl2java compiler generates both inheritance as well as delegation based skeletons. Above, we saw how to write the servant using the inheritance based skeleton. Here we will use the delegation based skeleton. It is often referred as the TIE approach.

The advantage of the inheritance based skeleton is that fewer objects are created at runtime. The main disadvantage is that it takes up the single implementation inheritance slot which may be needed for other things.

6.1 Hello Implementation

The servant implements the HelloOperations interface.
package helloWorld;
                                                                           
public class HelloImpl2 implements HelloOperations
{
    public String sayHello()
    {
    |   return "Hello World!\n";
    }
}

6.2 Hello World Server

The server, as before, creates the ORB, creates a CORBA object, writes out it's stringified object reference, and then waits for invocations.

Here, creating the servant step is different than before. Notice, that the TIE object actually extends _HelloImplBase.

package helloWorld;
                                                                           
import util.Util;
import org.omg.CORBA.ORB;
                                                                           
public class helloServer2
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a TIEd servant and register with ORB
    |   |   Hello hello = new _HelloTie(new HelloImpl2());
    |   |   orb.connect(hello);
    |   |                                                                  
    |   |   // create a stringified object reference
    |   |   String helloIOR = orb.object_to_string(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();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc. All rights reserved.