Portable Object Adapter (POA) with the Interoperable Object Reference (IOR) 
 

Downloads:

The following examples were tested with Javasoft JDK 1.3 and Inprise VisiBroker for Java 4.1 on a Win98 machine.

Getting Started:

Win98 system environment settings for JavaSoft JDK1.3 for directory C:\jdk1.3:
 


REM
SET PATH=C:\jdk1.3\bin;%PATH%
SET CLASSPATH=C:\progra~1\javasoft\jre\1.3\lib\rt.jar;%CLASSPATH%
REM
 

Win98 system environment settings for Inprise VisiBroker for Java 4.1 (including Naming and Event Service). This assumes that the installation directory is C:\Inprise\vbroker:
 

REM VisiBroker for Java 4.1
REM
SET VBROKER_ADM=C:\Inprise\vbroker\adm
SET PATH=C:\Inprise\vbroker\bin;%PATH%
REM
 

Introduction:

This example uses a very basic IDL file.

Ship.idl
 


// Ship.idl

module Ship {

  interface Aircraft {
    string codeNumber();
  };

  interface AircraftCarrier {
    Aircraft launch(in string name);
  };

};
 


Server.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Jan 2000

import java.io.*;
// import org.omg.CosNaming.*;

public class Server
{
   public static void main(String[] args)
   {
      org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

      org.omg.CORBA.Object objPOA = null;
      try {
         objPOA = orb.resolve_initial_references("RootPOA");
      }
      catch (org.omg.CORBA.ORBPackage.InvalidName ex) {
      }

      org.omg.PortableServer.POA rootPOA = null;
      rootPOA = (org.omg.PortableServer.POA) objPOA;

      org.omg.PortableServer.POA  myPOA = null;

      try {
         myPOA = rootPOA.create_POA("personalPOA", rootPOA.the_POAManager(),
            new org.omg.CORBA.Policy[] {
            rootPOA.create_id_assignment_policy(org.omg.PortableServer.IdAssignmentPolicyValue.USER_ID)
            });
      }
      catch (java.lang.Exception ex)
      {
         System.err.println("Create POA Exception " + ex);
         System.exit(1);
      }

      org.omg.PortableServer.Servant carrier = null;
      try {
         carrier = new AircraftCarrierImpl(myPOA);
         myPOA.activate_object_with_id("Nimitz".getBytes(), carrier);
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Activate failure " + se);
         System.exit(1);
      }
      catch (org.omg.CORBA.UserException ue)
      {
         System.err.println("Activate failure " + ue);
         System.exit(1);
      }

      // Write object reference to an IOR file

      org.omg.CORBA.Object initRef = null;
      try {
         initRef = myPOA.servant_to_reference(carrier);

         FileWriter output = new FileWriter("ns.ior");
         output.write(orb.object_to_string(initRef));
         output.close();
         System.out.println("Wrote IOR to file: ns.ior");

         myPOA.the_POAManager().activate();
         System.out.println(carrier + " ready for launch !!!");
         orb.run();
      }
      catch (java.lang.Exception exb)
      {
         System.err.println("Exception Last deep in here " + exb);
         System.exit(1);
      }
   }
}
 


Client.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Jan 1998

import java.io.*;
import org.omg.CosNaming.*;

public class Client
{
   public static void main(String[] args)
   {
      org.omg.CORBA.ORB orb = null;
      try {
          orb = org.omg.CORBA.ORB.init(args, null);
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("ORB init failure " + se);
         System.exit(1);
      }

      // Read IOR from file

      org.omg.CORBA.Object initRef = null;
      try
      {
         LineNumberReader input = new LineNumberReader(new FileReader("ns.ior"));
         initRef = orb.string_to_object(input.readLine());
      }
      catch(java.io.IOException e)
      {
         System.out.println("Exception: " + e);
         System.exit(1);
      }

      Ship.AircraftCarrier carrier = null;
      try {
         carrier = Ship.AircraftCarrierHelper.narrow(initRef);
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("AircraftCarrier narrow failure " + se);
         System.exit(1);
      }

      // Standard program continues

      String flight = args.length > 0 ? args[0]: "Ghost Rider 101";

      Ship.Aircraft aircraft = null;
      try {
         aircraft = carrier.launch(flight);
         System.out.println("Aircraft has been launched" );
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Carrier launch failure " + se);
         System.exit(1);
      }

      String designation = null;
      try {
         designation = aircraft.codeNumber();
         System.out.println("Aircraft " + designation + " is airborne");
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Get name failure " + se);
         System.exit(1);
      }
   }
}
 


AircraftCarrierImpl.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Sep 2000

public class AircraftCarrierImpl extends Ship.AircraftCarrierPOA
{

   private org.omg.PortableServer.POA _rootPOA;

   public AircraftCarrierImpl(org.omg.PortableServer.POA rootPOA)
   {
      _rootPOA = rootPOA;
   }

   public Ship.Aircraft launch(String name)
   {

      org.omg.PortableServer.Servant aircraft = new AircraftImpl(name);
      try {
         _rootPOA.activate_object_with_id("name".getBytes(), aircraft);
      }
      catch (java.lang.Exception ex)
      {
         System.err.println("Exception 2 " + ex);
         System.exit(1);
      }

      System.out.println(name + " on Catapult 2");

      Ship.Aircraft _aircraft = null;
      try {
         _aircraft = Ship.AircraftHelper.narrow(_rootPOA.create_reference_with_id("name".getBytes(),
            aircraft._all_interfaces(null, null)[0]));
      }
      catch (java.lang.Exception ex)
      {
         System.err.println("Exception 3 " + ex);
         System.exit(1);
      }
      return _aircraft;
   }
}
 


AircraftImpl.java
 

// Building Distributed Object Applications with CORBA
// Infowave (Thailand) Co., Ltd.
// http://www.waveman.com
// Sep 2000

public class AircraftImpl extends Ship.AircraftPOA
{
   private String _codeNumber;

   public AircraftImpl()
   {}

   public AircraftImpl(String codeNumber)
   {
      _codeNumber = codeNumber;
   }

   public String codeNumber()
   {
      return _codeNumber;
   }
}
 


Building the server with VisiBroker:

Create a directory and place Ship.idl, Server.java, AircraftCarrierImpl.java, and AircraftImpl.java into it.
 


prompt> idl2java Ship.idl
prompt> vbjc Server.java
 

Building the client with VisiBroker:

Place Client.java into the same directory as above
 


prompt> idl2java Ship.idl
prompt> vbjc Client.java
 

Running the program:

Now start the Smart Agent and server in one window, client in another window
 


prompt> osagent
prompt> vbj Server
 
prompt> vbj Client "Jolly 700"