Using Initial References Service

In this example, we will redo the client and server for the Hello World using RMI-IIOP but this time we will use the Cos Name Service to publish and retrieve the hello object reference instead of using the stringified object references from the disk.

Hello Server

Here the server will first get the root NamingContext using the ORB.resolve_initial_references method with NameService as a parameter. It wil then construct a Name and publish the object reference to the hello object. Remember from the Hello World lesson, we needed to get the Stub for the hello servant before we could use it on an API that expected a CORBA Object.

package initialRefs;
                                                                           
import util.Util;
                                                                           
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CORBA.Object;
                                                                           
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
import com.sssw.jbroker.api.bootstrap.InitialReferences;
import com.sssw.jbroker.api.bootstrap.InitialReferencesService;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   InitialReferences         initialRefs;
    |   InitialReferencesService  insService;
    |                                                                      
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // get the initial references object
    |   |   insService = (InitialReferencesService) orb.
    |   |       resolve_initial_references("InitialReferencesService");
    |   |   initialRefs = insService.getInitialReferences("localhost", 2506);
    |   |                                                                  
    |   |   // get the root of the NameService
    |   |   NamingContext root = NamingContextHelper.narrow(
    |   |       initialRefs.get("NameService"));
    |   |                                                                  
    |   |   // publish the hello object reference
    |   |   NameComponent nc = new NameComponent("hello", "");
    |   |   NameComponent[] name = new NameComponent[] { nc };
    |   |   root.rebind(name, (Object) PortableRemoteObject.toStub(hello));
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

Hello Client

The client also gets the root NamingContext and then resolves the name and gets back an object reference.

package initialRefs;
                                                                           
import util.Util;
                                                                           
import helloWorld2.Hello;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
                                                                           
import javax.rmi.PortableRemoteObject;
                                                                           
import com.sssw.jbroker.api.bootstrap.InitialReferences;
import com.sssw.jbroker.api.bootstrap.InitialReferencesService;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   InitialReferences         initialRefs;
    |   InitialReferencesService  insService;
    |                                                                      
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // get the initial references object
    |   |   insService = (InitialReferencesService) orb.
    |   |       resolve_initial_references("InitialReferencesService");
    |   |   initialRefs = insService.getInitialReferences("localhost", 2506);
    |   |                                                                  
    |   |   // get the root of the NameSpace
    |   |   NamingContext root = NamingContextHelper.narrow(initialRefs.
    |   |       get("NameService"));
    |   |                                                                  
    |   |   // get the hello object from the NameService
    |   |   NameComponent nc = new NameComponent("hello", "");
    |   |   NameComponent[] name = new NameComponent[] { nc };
    |   |   Hello hello = (Hello) root.resolve(name);
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}


Copyright © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.