«^»
3.2. Using Sun's JavaIDL

At the present time, Sun's JavaIDL is not part of the JDK: however, it can be downloaded from Sun's JavaIDL page at http://java.sun.com/products/jdk/idl/ and used with JDK 1.0.2 or JDK 1.1.x. JavaIDL will form part of JDK 1.2, and a beta release of JDK 1.2 is available from http://java.sun.com/products/jdk/1.2/. A version of JavaIDL is also supplied with Sun's Solaris 2.6 operating system.

The use of JavaIDL will be demonstrated by using the same example as was used for RMI:

As before, we will need to provide an interface for our object: this time it is provided in IDL:

0069: module CountApp {
0070:    interface Count {
0071:       long getCount();
0072:       void setCount(in long vCount);
0073:    };
0074: };

When JavaIDL's idltojava command is used to translate this file into Java, it will also produce a class called _CountImplBase in a subdirectory called CountApp. We can implement the above interface by extending the class _CountImplBase:

0075: package IDL.Count;                                      // CountServant.java
0076: import IDL.Count.CountApp._CountImplBase;
0077: public class CountServant extends _CountImplBase {
0078:    public CountServant() {
0079:       super();
0080:       System.out.println("CountServant: constructor called");
0081:       iCount = 0;
0082:    }
0083:    public synchronized int getCount() {
0084:       System.out.println("CountServant: getCount called");
0085:       return iCount;
0086:    }
0087:    public synchronized void setCount(int vCount) {
0088:       System.out.println("CountServant: setCount called with " + vCount);
0089:       iCount = vCount;
0090:    }
0091:    private int iCount;
0092: }

It is on the server that we have an object that is of the class CountServant. Here is a Java application that creates an object of this class and then asks the ORB's Naming Service to bind the name second Count to the object:

0093: package IDL.Count;                                       // CountServer.java
0094: import org.omg.CosNaming.NameComponent;
0095: import org.omg.CosNaming.NamingContext;
0096: import org.omg.CosNaming.NamingContextHelper;
0097: import org.omg.CORBA.ORB;
0098: public class CountServer {
0099:    public static void main(String[] args) {
0100:       try{
0101:          // create and initialize the ORB
0102:          ORB orb = ORB.init(args, null);
0103:          // create a CountServant object and register it with the ORB
0104:          CountServant realCount = new CountServant();
0105:          orb.connect(realCount);
0106:          // get the root naming context
0107:          org.omg.CORBA.Object objRef = 
0108:                     orb.resolve_initial_references("NameService");
0109:          NamingContext ncRef = NamingContextHelper.narrow(objRef);
0110:          // bind the Object Reference in Naming
0111:          NameComponent nc = new NameComponent("second Count", "");
0112:          NameComponent path[] = {nc};
0113:          ncRef.rebind(path, realCount);
0114:          // wait for invocations from clients
0115:          java.lang.Object sync = new java.lang.Object();
0116:          synchronized (sync) {
0117:             sync.wait();
0118:          }
0119:       }
0120:       catch (Exception rException) {
0121:          rException.printStackTrace();
0122:       }
0123:    }
0124: }

Finally, here is a client program. This is the program that accesses the methods of the object on the server:

0125: package IDL.Count;                                       // CountClient.java
0126: import IDL.Count.CountApp.Count;
0127: import IDL.Count.CountApp.CountHelper;
0128: import java.io.DataInputStream;
0129: import org.omg.CosNaming.NameComponent;
0130: import org.omg.CosNaming.NamingContext;
0131: import org.omg.CosNaming.NamingContextHelper;
0132: import org.omg.CORBA.ORB;
0133: public class CountClient {
0134:    public static void main(String[] args) {
0135:       DataInputStream stdin = new DataInputStream(System.in);
0136:       try{
0137:          // create and initialize the ORB
0138:          ORB orb = ORB.init(args, null);
0139:          // get the root naming context
0140:          org.omg.CORBA.Object objRef = 
0141:                     orb.resolve_initial_references("NameService");
0142:          NamingContext ncRef = NamingContextHelper.narrow(objRef);
0143:          // resolve the Object Reference in Naming
0144:          NameComponent nc = new NameComponent("second Count", "");
0145:          NameComponent path[] = {nc};
0146:          Count stubCount = CountHelper.narrow(ncRef.resolve(path));
0147:          // enter a loop which processes requests from the keyboard
0148:          System.out.println("before loop");
0149:          while (true) {
0150:             String stdinLine = stdin.readLine();
0151:             if ( stdinLine.equals("quit") ) break;
0152:             if ( stdinLine.equals("show") )
0153:                System.out.println(stubCount.getCount());
0154:             else {
0155:                int intVal = Integer.parseInt(stdinLine);
0156:                stubCount.setCount(intVal);
0157:             }
0158:          }
0159:          System.out.println("after loop");
0160:       } 
0161:       catch (Exception rException) {
0162:          rException.printStackTrace();
0163:       }
0164:    }
0165: }