Michael Elhadad

Software Engineering - Fall 1999


Lecture 8: Corba (Part 1) -- Corba and Java

Motivation

Read the Java tutorial on IDL. This page uses the same example (Hello World).

Hello World in Java/Corba

To get the pieces together, start with simplest example. Everything starts with an IDL definition:
module HelloApp
{
  interface Hello
  {
    string sayHello();
  };
};

IDL to Java

Run idltojava to convert this IDL to a set of files:
% idltojava hello.idl
% ls -l HelloApp/*.java

-rw-r--r--   247	HelloApp/Hello.java
-rw-r--r--  1771	HelloApp/HelloHelper.java
-rw-r--r--   781	HelloApp/HelloHolder.java
-rw-r--r--  1452	HelloApp/_HelloImplBase.java
-rw-r--r--   960	HelloApp/_HelloStub.java
The files created are:
  1. A Java package for each IDL module
  2. A Java Interface for each IDL interface
  3. A Helper Java class for each IDL interface
  4. A Holder Java class for each IDL interface
  5. A skeleton class (ImplBase) for each IDL interface to use on the server.
  6. A stub (proxy) class for each IDL interface to use on the client.

A Java CORBA Client

A typical Java/Corba client does the following:
  1. Import the IDL generated package
  2. Connect to the orb (ORB.init)
  3. Connect to the Naming service (orb.resolve_initial_references)
  4. Obtain an object reference from the naming service (ncRef.resolve)
  5. Narrow the object reference to a stub interface (use the Helper class)
  6. Use the object reference (use the Stub class)
This is illustrated in this program:
import HelloApp.*;           // The package containing our stubs.
import org.omg.CosNaming.*;  // HelloClient will use the naming service.
import org.omg.CORBA.*;      // All CORBA applications need these classes.


public class HelloClient
{
  public static void main(String args[])
  {
    try{
      // Create and initialize the ORB
      ORB orb = ORB.init(args, null);
      
      // Get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext ncRef = NamingContextHelper.narrow(objRef);
      
      // Resolve the object reference in naming
      NameComponent nc = new NameComponent("Hello", "");
      NameComponent path[] = {nc};
      Hello helloRef = HelloHelper.narrow(ncRef.resolve(path));
      
      // Call the Hello server object and print results
      String Hello = helloRef.sayHello();
      System.out.println(Hello);      
    } catch(Exception e) {
        System.out.println("ERROR : " + e);
        e.printStackTrace(System.out);
      }  
  }
}

A Java CORBA Server

A Java/Corba server does the following:
  1. Import the IDL generated package
  2. Connect to the orb (ORB.init)
  3. Create a servant object for each implemented IDL interface (new HelloServant)
  4. Connect the servants with the orb (orb.connect)
  5. Connect to the Naming service (orb.resolve_initial_references)
  6. Register (bind) the servant objects with the naming service (ncRef.Rebind)
  7. Enter in a loop to wait for client connections
In addition, for each supported IDL interface, a Servant class is implemented. The servant class can be implemented using the Inheritance or the TIE model. The choice must be given to the idltojava translator. By default, the inheritance model is used.

If inheritance is used, the servant class extends the ImplBase class generated by idltojava and it only needs to implement the methods declared in the Java interface.

This is illustrated in the this program:

import HelloApp.*;		// The package containing our stubs.
import org.omg.CosNaming.*;	// HelloServer will use the naming service.
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;		// All CORBA applications need these classes.


public class HelloServer 
{
  public static void main(String args[])
  {
    try{
      // Create and initialize the ORB
      ORB orb = ORB.init(args, null);
      
      // Create the servant and register it with the ORB
      HelloServant helloRef = new HelloServant();
      orb.connect(helloRef);
      
      // Get the root naming context
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      NamingContext ncRef = NamingContextHelper.narrow(objRef);
      
      // Bind the object reference in naming
      NameComponent nc = new NameComponent("Hello", "");
      NameComponent path[] = {nc};
      ncRef.rebind(path, helloRef);
      
      // Wait for invocations from clients
      java.lang.Object sync = new java.lang.Object();
      synchronized(sync){
        sync.wait();
      }
    } catch(Exception e) {
        System.err.println("ERROR: " + e);
        e.printStackTrace(System.out);
    }  
  }
}

class HelloServant extends _HelloImplBase
{
  public String sayHello()
  {
    return "\nHello world!!\n";
  }
}

Running the Example

The files needed are:
Last modified June 6th, 1999 Michael Elhadad