FileDocCategorySizeDatePackage
NamingSubdirs.javaAPI DocExample2764Wed Apr 05 11:25:42 BST 2000None

NamingSubdirs.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.CORBA.*;
import org.omg.CORBA.ORBPackage.*;
import org.omg.CosNaming.*;
import java.util.*;

public class NamingSubdirs {
  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("Primary naming service unavailable.");
      invN.printStackTrace();
      System.exit(1);
    }
    NamingContext localNC = NamingContextHelper.narrow(ref);
    System.out.println("Got primary Naming Service.");
    NameComponent nodeName = new NameComponent("RemoteContexts", "");
    NameComponent sub1 = new NameComponent("sub1", "foo");
    NameComponent sub2 = new NameComponent("sub1", "bar");
    NameComponent path1[] = {nodeName};
    NameComponent path2[] = {nodeName, sub1};
    NameComponent path2_1[] = {nodeName, sub2};
    NameComponent fullPath[] = {nodeName, sub1,
                                new NameComponent("object", "")};
    NameComponent fullPath2[] = {nodeName, sub2,
                                 new NameComponent("object", "")};
    NameComponent fullPath2v2[] = {nodeName,
                                   new NameComponent("sub1", ""),
                                   new NameComponent("foo", ""),
                                   new NameComponent("object", "")};
    try {
      NamingContext node = localNC.bind_new_context(path1);
      node = localNC.bind_new_context(path2);
      node = localNC.bind_new_context(path2_1);
      ServerImpl server = new ServerImpl();
      localNC.bind(fullPath, server);
      localNC.bind(fullPath2, server);
    }
    catch (Exception e) {
      System.out.println("Failed to bind objects/context to primary.");
      e.printStackTrace();
    }

    // Try getting name context
    try {
      ref = localNC.resolve(fullPath2v2);
      ref = localNC.resolve(path2);
      NamingContext nc = NamingContextHelper.narrow(ref);
      System.out.println("Got context...");
      NameComponent relPath[] = {new NameComponent("object", "")};
      ref = nc.resolve(relPath);
      Server server = ServerHelper.narrow(ref);
      System.out.println("Got server...");
    }
    catch (Exception e2) {
      System.out.println("Couldn't resolve subdir.");
      e2.printStackTrace();
    }
  }
}