Hello World using JNDI

In this example, we will redo the client and server for the Hello World using RMI-IIOP but this time we will use JNDI to publish and retrieve the hello object reference instead of directly using COS Naming.

Notice that in the Makefile, we have set the JNDI Naming Factory for initial contexts to the JNDI/COS provider from JavaSoft.

-Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory

1 Hello Server

package jndiCOS;
                                                                           
import util.Util;
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
                                                                           
public class helloServer
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create a servant
    |   |   Hello hello = new HelloImpl();
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable();
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the root naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // bind the hello objref using JNDI
    |   |   ctx.rebind("hello", hello);
    |   |                                                                  
    |   |   // wait for invocations
    |   |   System.out.println("waiting for invocations ...");
    |   |   orb.run();
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}

2 Hello Client

package jndiCOS;
                                                                           
import util.Util;
import helloWorld2.Hello;
                                                                           
import org.omg.CORBA.ORB;
                                                                           
import java.util.Hashtable;
                                                                           
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
                                                                           
public class helloClient
{
    public static void main(String[] args)
    {
    |   try {
    |   |                                                                  
    |   |   // create the jBroker ORB
    |   |   ORB orb = ORB.init(args, null);
    |   |                                                                  
    |   |   // create the JNDI Environment
    |   |   Hashtable env = new Hashtable(5, 0.75f);
    |   |   env.put("java.naming.corba.orb", orb);
    |   |                                                                  
    |   |   // get the root naming context
    |   |   Context ctx = new InitialContext(env);
    |   |                                                                  
    |   |   // lookup the hello objref
    |   |   Hello hello = (Hello) ctx.lookup("hello");
    |   |                                                                  
    |   |   // invoke method on the object
    |   |   System.out.println(hello.sayHello());
    |   |                                                                  
    |   } catch (Exception ex) {
    |   |   ex.printStackTrace();
    |   }
    }
}
Copyright © 2000-2003, Novell, Inc. All rights reserved.