Hello World using Cos Naming

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 cosNaming;
                                                                           
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;
                                                                           
public class Server
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // get the root of the NameService
    |   |   NamingContext root = NamingContextHelper.narrow(
    |   |       orb.resolve_initial_references("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 cosNaming;
                                                                           
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;
                                                                           
public class Client
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // get the root of the NameSpace
    |   |   NamingContext root = NamingContextHelper.narrow(
    |   |       orb.resolve_initial_references("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();
    |   }
    }
}

Hello Applet Client

package cosNaming;
                                                                           
import java.awt.Graphics;
                                                                           
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContext;
import org.omg.CosNaming.NamingContextHelper;
                                                                           
import helloWorld2.Hello;
                                                                           
public class helloApplet extends java.applet.Applet 
{
    private String _message = "";
                                                                           
    public void init() {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(this, null);
    |   |                                                                  
    |   |   // get the root of the NameSpace
    |   |   NamingContext root = NamingContextHelper.narrow(
    |   |       orb.resolve_initial_references("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
    |   |   _message = hello.sayHello();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
                                                                           
    public void paint(Graphics g) 
    {
    |   g.drawString(_message, 25, 50);
    }
}


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