FileDocCategorySizeDatePackage
NamingTest.javaAPI DocExample2551Wed Apr 05 11:25:42 BST 2000None

NamingTest.java

/*
 * This example is from the book "Java Enterprise in a Nutshell".
 * Copyright (c) 1999 by O'Reilly & Associates.  
 * You may distribute this source code for non-commercial purposes only.
 * You may study, modify, and use this example for any purpose, as long as
 * this notice is retained.  Note that this example is provided "as is",
 * WITHOUT WARRANTY of any kind either expressed or implied.
 */

import org.omg.CosNaming.*;
import org.omg.CORBA.*;

public class NamingTest {
  static public void main(String argv[]) {
    ORB myORB = ORB.init(argv, null);
    try {
      org.omg.CORBA.Object ref = myORB.resolve_initial_references("NameService");
      // Get the root context, as before
      NamingContext rootNC = NamingContextHelper.narrow(ref);
      // Create the components to the sub-context name
      NameComponent comp1 = new NameComponent("servers", "");
      NameComponent ttComp = new NameComponent("ThisOrThat", "");
      NameComponent otherComp= new NameComponent("misc", "");
      // Create each sub-context within the root context, and bind them
      // to their appropriate names
      // Create a new context, bind it to the name "servers", off of the root 
      NamingContext context1 = rootNC.new_context();
      NameComponent path1[] = { comp1 };
      rootNC.bind_context(path1, context1);
      // Create another context, bind it to the name "servers, ThisOrThat" 
      NamingContext ttDir = rootNC.new_context();
      NameComponent path2_1[] = { comp1, ttComp };
      rootNC.bind_context(path2_1, ttDir);
      // Create another context, bind it to the name "servers, misc" 
      NamingContext otherDir = rootNC.new_context();
      NameComponent path2_2[] = { comp1, otherComp };
      rootNC.bind_context(path2_2, otherDir);
      // Now we can bind servers to a name within any of the new sub-contexts
      org.omg.CORBA.Object ttRef = new ThisOrThatServerImpl();
      org.omg.CORBA.Object otherRef = new ThisOrThatServerImpl();
      // Bind the ThisOrThatServer to the appropriate branch under "servers"
      NameComponent tt1Comp = new NameComponent("server1", "");
      NameComponent ttPath[] = { comp1, ttComp, tt1Comp };
      rootNC.bind(ttPath, ttRef);
      // Bind the other server to the "misc" branch of the "servers" dir.
      NameComponent yetAnotherComp = new NameComponent("SomeOtherServer", "");
      NameComponent otherPath[] = { comp1, otherComp, yetAnotherComp };
      rootNC.bind(otherPath, otherRef);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}