/*
* 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.
*/
//
// CORBA code listing, "Adding Objects to a Naming Context": Registering
// objects with a Naming Service in various sub-directories.
//
import org.omg.CORBA.*;
import org.omg.CORBA.ORBPackage.*;
import org.omg.CosNaming.*;
import java.util.*;
public class SubdirTest {
public static void main(String argv[]) {
ORB orb = ORB.init(argv, null);
org.omg.CORBA.Object ref = null;
try {
ref = orb.resolve_initial_references("NameService");
}
catch (InvalidName invN) {
System.out.println("No primary NameService available.");
System.exit(1);
}
try {
// 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
NamingContext context1 = rootNC.new_context();
NameComponent path1[] = { comp1 };
rootNC.bind_context(path1, context1);
NamingContext ttDir = rootNC.new_context();
NameComponent path2_1[] = { comp1, ttComp };
rootNC.bind_context(path2_1, ttDir);
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);
System.out.println("Bound first server");
// 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);
System.out.println("Bound second server");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|