HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"JDK Naming Services: COSnaming and JNDI"
Volume: 3 Issue: 7, p. 56

	

Listing 1.
 
// The package containing our stubs. 
import HelloApp.*;  
// HelloServer will use the naming service. 
import org.omg.CosNaming.*;  
// Special exceptions 
import org.omg.CosNaming.NamingContextPackage.*;  
// All CORBA applications need these classes. 
import org.omg.CORBA.*;  

public class HelloServer 
{ 
 public static void main(String args[]) { 
  try{ 
   // Create and initialize the ORB 
   ORB orb = ORB.init(args, null); 

   // Create the servant and register it with the ORB 
   HelloServant helloRef = new HelloServant(); 
   orb.connect(helloRef); 

   // Get the root naming context 
   org.omg.CORBA.Object objRef = 
     orb.resolve_initial_references("NameService"); 
   NamingContext ncRef = NamingContextHelper.narrow(objRef); 

   // Bind the object reference in naming 
   NameComponent nc = new NameComponent("Hello", " "); 
   NameComponent path[] = {nc}; 
   ncRef.rebind(path, helloRef); 

   // Wait for invocations from clients 
   java.lang.Object sync = new java.lang.Object(); 
   synchronized(sync){ 
    sync.wait(); 
   } 

  } 
  catch(Exception e) { 
   System.err.println("ERROR: " + e); 
   e.printStackTrace(System.out); 
  } 
 } 
} 

class HelloServant extends _HelloImplBase { 
 public String sayHello() { 
  return "\nHello world!!\n"; 
 } 
} 

Listing 2.
 
import HelloApp.*;           // The package containing our stubs. 
import org.omg.CosNaming.*;   // HelloClient will use the naming service. 
import org.omg.CORBA.*;       // All CORBA applications need these classes. 

public class HelloClient 
{ 
 public static void main(String args[]) { 
  try{ 
   // Create and initialize the ORB 
   ORB orb = ORB.init(args, null); 

   // Get the root naming context 
   org.omg.CORBA.Object objRef = 
     orb.resolve_initial_references("NameService"); 
   NamingContext ncRef = NamingContextHelper.narrow(objRef); 

   // Resolve the object reference in naming 
   NameComponent nc = new NameComponent("Hello", " "); 
   NameComponent path[] = {nc}; 
   Hello helloRef =HelloHelper.narrow(ncRef.resolve(path)); 

   // Call the Hello server object and print results 
   String Hello = helloRef.sayHello(); 
   System.out.println(Hello); 
  } 
  catch(Exception e) { 
   System.out.println("ERROR : " + e); 
   e.printStackTrace(System.out); 
  } 
 } 
} 

Listing 3.
 
• java.naming.provider.url represents the name of the initial context to use. 
• java.naming.corba.orb represents the ORB to use. 
• java.naming.ldap.policies sets the LDAP policies. 

Examples of setting properties in JNDI 1.1 include: 
To use the default LDAP provider: 
-Djava.naming.factory.initial= 
   com.sun.jndi.ldap.ldapCtxFactory 
-Djava.naming.provider.url= 
   ldap://servername/o=widget,c=us 
To use the default NIS provider: 
-Djava.naming.factory.initial= 
   com.sun.jndi.nis.NISCtxFactory 
-Djava.naming.provider.url=nis://servername/domain 
To use the default DNS provider: 
-Djava.naming.factory.initial= 
   com.sun.jndi.dns.DNSCtxFactory 
-Djava.naming.provider.dns=dns://dnsserver/dnsdomain 

To use the default CosNaming provider: 
-Djava.naming.factory.initial= 
   com.sun.jndi.CosNaming.CNCtxFactory 

Listing 4.
 
import HelloApp.*; 
import org.omg.CORBA.ORB; 
import javax.naming.InitialContext; 
import javax.naming.Context; 
import java.util.Hashtable; 

public class HelloServer2 { 
 public static void main(String args[]) { 
  try{ 
   // create and initialize the ORB 
      ORB orb = ORB.init(args, null); 

      // create servant and register it with the ORB 
      HelloServant2 HelloRef = new HelloServant2(); 
      orb.connect(HelloRef); 

      Hashtable env = new Hashtable(); 
      env.put(“java.naming.corba.orb”, orb); 

      // get the root naming context 
      Context ic = new InitialContext(env); 

      // bind the Object Reference using JNDI 
      ic.rebind("Hello", HelloRef); 

      // wait for invocations from clients 
      java.lang.Object sync = new java.lang.Object(); 
      synchronized (sync) { 
    sync.wait(); 
      } 
  } 
  catch (Exception e) { 
      System.err.println("ERROR: " + e); 
      e.printStackTrace(System.out); 
  } 
 } 
} 

class HelloServant2 extends _HelloImplBase { 
 public String sayHello() { 
  return "\nHello world !!\n"; 
    } 
} 

Listing 5.
 
import HelloApp.*; 
import org.omg.CORBA.ORB; 
import org.omg.CORBA.Object; 
import javax.naming.InitialContext; 
import javax.naming.Context; 
import java.util.Hashtable; 

public class HelloClient2 { 
 public static void main(String args[]) { 
  try{ 
      // create and initialize the ORB 
      ORB orb = ORB.init(args, null); 

      Hashtable env = new Hashtable(5, 0.75f); 
      env.put("java.naming.corba.orb", orb); 

      // get the root naming context 
      Context ic = new InitialContext(env); 

   // resolve the Object Reference using JNDI 
   Hello HelloRef = 
   HelloHelper.narrow((org.omg.CORBA.Object) 
      ic.lookup("Hello")); 

   // call the Hello server object and print results 
   String Hello = HelloRef.sayHello(); 
   System.out.println(Hello); 

  } 
  catch (Exception e) { 
   System.out.println("ERROR : " + e) ; 
   e.printStackTrace(System.out); 
  } 
 } 
} 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.