Portable Object Adapter (POA) with the Name Service - CosNaming 
 

Server.java
 

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

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("Exception 1 deep in here " + 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);
      }
      catch (java.lang.Exception ex)
      {
         System.err.println("Exception here " + ex);
         System.exit(1);
      }

      // Name Service

      org.omg.CORBA.Object nsRef = null;
      try {
         nsRef = orb.resolve_initial_references("NameService");
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Resolve init failure " + se);
         System.exit(1);
      }
      catch (org.omg.CORBA.UserException ue)
      {
         System.err.println("Resolve init failure " + ue);
         System.exit(1);
      }

      NamingContextExt initContext = null;
      try {
         initContext = NamingContextExtHelper.narrow(nsRef);
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Context narrow failure " + se);
         System.exit(1);
      }

      // Write object reference to NS

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

         myPOA.the_POAManager().activate();
         System.out.println(carrier + " ready for launch !!!");

         initContext.rebind(initContext.to_name("Nimitz"), initRef);

         orb.run();
      }
      catch (java.lang.Exception exb)
      {
         System.err.println("Exception " + exb);
         System.exit(1);
      }
   }
}
 


Client.java
 

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

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);
      }

      // Use Name Service

      org.omg.CORBA.Object nsRef = null;
      try {
         nsRef = orb.resolve_initial_references("NameService");
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Resolve init failure " + se);
         System.exit(1);
      }
      catch (org.omg.CORBA.UserException ue)
      {
         System.err.println("Resolve init failure " + ue);
         System.exit(1);
      }

      NamingContextExt initContext = null;
      try {
         initContext = NamingContextExtHelper.narrow(nsRef);
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Context narrow failure " + se);
         System.exit(1);
      }

      org.omg.CORBA.Object initRef = null;
      try
      {
         initRef = ((NamingContext)initContext).resolve(initContext.to_name("Nimitz"));
      }
      catch (org.omg.CORBA.SystemException se)
      {
         System.err.println("Context narrow failure " + se);
         System.exit(1);
      }
      catch (java.lang.Exception exb)
      {
         System.err.println("Exception here " + exb);
         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);
      }
   }
}
 


Building the server with VisiBroker:

Create a directory and place Ship.idl, AircraftCarrierImpl.java, and AircraftImpl.java from the previous page into it. Also place this Server.java in the same directory.
 


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:

Three windows will be required.
Start the Smart Agent and Name Service in one window, server in one window, client in another window
 


prompt> osagent
prompt> start nameserv objects
 
prompt> vbj -DSVCnameroot=objects Server
 
prompt> vbj -DSVCnameroot=objects Client "Sundowner 406"
 

Note:

It is usually easier to create three BAT files to contain the commands above.