In this example, we will redo the client and server for the Hello World using JNDI but this time we will let the JNDI/COS provider create an ORB implicitly.
Notice that in the build file, 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
The Initial context factory implicitly creates the ORB so no explicit
ORB.init() is required. Also when we created an RMI-IIOP object,
the ORB started a keep alive thread that ensures that the VM does not exit when the
"main thread" exits. So we do not need an explicit ORB.run() either.
package jndiCOS2;
import util.Util;
import helloWorld2.Hello;
import helloWorld2.HelloImpl;
import javax.naming.Context;
import javax.naming.InitialContext;
public class helloServer
{
public static void main(String[] args)
{
| try {
| |
| | // create a servant
| | Hello hello = new HelloImpl();
| |
| | // get the root naming context
| | Context ctx = new InitialContext(null);
| |
| | // bind the hello objref using JNDI
| | ctx.rebind("hello", hello);
| |
| | // wait for invocations
| | System.out.println("waiting for invocations ...");
| |
| } catch (Exception ex) {
| | ex.printStackTrace();
| }
}
}
The initial context factory implicitly creates the ORB
so no explicit ORB.init() is required.
package jndiCOS2;
import util.Util;
import helloWorld2.Hello;
import javax.naming.Context;
import javax.naming.InitialContext;
public class helloClient
{
public static void main(String[] args)
{
| try {
| |
| | // get the root naming context
| | Context ctx = new InitialContext(null);
| |
| | // 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();
| }
}
}
package jndiCOS2;
import java.awt.Graphics;
import java.util.Hashtable;
import helloWorld2.Hello;
import javax.naming.Context;
import javax.naming.InitialContext;
public class helloApplet extends java.applet.Applet
{
private String _message = "";
public void init() {
| try {
| |
| | // get the root naming context
| | Context ctx = new InitialContext(null);
| |
| | // lookup the hello objref
| | Hello hello = (Hello) ctx.lookup("hello");
| |
| | // 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.