CORBA & IDL in Java  last reivision Nov 22 / 2000   Peter Komisar


1. Define the interface for the services and structure of the remote Corba object

module vote_poll{
    interface votes{
         long get_Liberal( );
         long get_Tory( );
         };
      }; 


2. Save to an .idl file and compile it with the idltojava compiler
 

idltojava -fno-cpp vote_poll.idl   // leaving out the no pre-proccessor flag resulted in a fatal error

A new directory called vote_poll is created (in the current dir),  containing the java files
votes.java, _votesStub.java,  _votesImplBase.java, votesHolder.java,  votesHelper.java

 Here what you get. . .
 
 votes.java       //   The  java  interface extending org.omg.CORBA..Object and IDLEntity 


/*
 * File: ./VOTE_POLL/VOTES.JAVA
 * From: VOTE_POLL.IDL
 * Date: Mon Nov 22 15:04:16 1999
 *   By: C:\JDK12~1.2\BIN\IDLTOJ~1.EXE Java IDL 1.2 Aug 18 1998 16:25:34
 */
package vote_poll;
public interface votes
 extends org.omg.CORBA.Object,  org.omg.CORBA.portable.IDLEntity

// notice extensions added by idltojava

    int get_Liberal( );
    int get_Tory( );
}

_votesImplBase.java  // this implementation class which is mercifully extendable. Take a look!


  _votesStub.java    // the stub for the client acts as a proxy to make calls on the remote Corba object

  votesHolder.java          // assists in method calls which are not supported by java


votesHelper.java           // provides methods to help the client resolve the remote object


3. Compile the generated java files

javac vote_poll \ *.java



Implementation Class

4. Create the implementation class extending the _nameImplBase


package vote_poll;

public class votesImpl extends _votesImplBase{
   static int l = 0;
   static int t = 0;
   public votesImpl( ){       // recommended no-arg, empty constructor
   }
     /* method implementations */
public int get_Liberal( ){
   l ++ ;
  return l ;
  }
public int get_Tory( ){
  t ++ ;
  return t;
  }
}
                 // The class compiled but there were a lot of method deprecation 



Corba Server

5. Writing the server.
    Looking at a several examples, code for the corba server  is all quite similar


import vote_poll.*;
import org.omg.CosNaming.* ;
import org.omg.CosNaming.NamingContextPackage.* ;
import org.omg.CORBA.* ;

public class vote_poll_server {
public static void main( String args[] )
{
  try{
     // initialize ORB instance
     ORB orb = ORB.init(args,null);

    // instantiate implementation object and register with ORB
     votesImpl vI = new votesImpl( );
     orb.connect(vI);

     // reference the name service
     org.omg.CORBA.Object corba_object = orb.resolve_initial_references("NameService");
     NamingContext naming_context = NamingContextHelper.narrow(corba_object);

    // Bind the implementation object reference to a naming component
     NameComponent namecomponent = new NameComponent("Vote", "");
     NameComponent path[] = { namecomponent };
     naming_context.rebind(path, vI);

      // Synch on Object object and wait for client invocations
     java.lang.Object sync = new java.lang.Object( );
     synchronized (sync) {
     sync.wait( );
     }
     }
     catch(Exception e){
       System.err.println("Error " + e);
       }
   }
 }
 



Corba Client

6. Writing the client


import vote_poll.* ;
import org.omg.CosNaming.* ;
import org.omg.CosNaming.NamingContextPackage.* ;
import org.omg.CORBA.* ;

public class vote_poll_client{
 public static void main( String args[] ){

  if (args.length!=1){
     System.out.println("Vote by specifying a party, Liberal or Tory. ");
     System.exit(1);
     }
     String party_vote=args[0];
  try{
   // Create and initialize an instance of an ORB
      ORB orb = ORB.init(args, null);

   // Obtain a Name Service reference
      org.omg.CORBA.Object  corba_object = orb.resolve_initial_references("NameService");
      NamingContext naming_context = NamingContextHelper.narrow(corba_object);

   // Creates a name component object for String "Vote" , then an array of one of it
      NameComponent nc = new NameComponent( "Vote", "");
      NameComponent path[] = { nc };

 /*    The object returned by the Helper class narrow method  is assigned  to the implementation
         interface. The object returned  is actually a stub object (_votesStub generated by the idltojava
         compiler) which acts as a proxy or representative for the actual implementation referenced
        back at the server
  */
        votes vI = votesHelper.narrow(naming_context.resolve(path));

  // reference the interface methods against the implementation stub object, vI
      if (party_vote.equals("Liberal"))
         System.out.println( "The vote is Liberal and the new tally is " + vI.get_Liberal());
      else
          if (party_vote.equals("Tory"))
            System.out.println( "The vote is Tory and the new tally is " + vI.get_Tory());
          else
            System.out.println("Your ballot has been discounted");
   }
   catch(Exception e)
   {
   System.out.println("ERROR : " + e);
   }
 }
}


7. Compile the Server and the Client  and the Implementation class

    // cd to the directory above vote_poll

    javac vote_poll \ vote_poll_client.java
    javac  vote_poll \ voteImpl.java
    javac  vote_poll \ vote_poll_server.java


8. Run the name server

Enter tnameserv at the command line



// Move the client and the server to a directory above the vote_poll directory.
// This allows the contents of the directory corresponding to vote_poll to be imported

9. Run the server

    java vote_poll_server


10. Run the client

   java vote_poll_client