FileDocCategorySizeDatePackage
BindingExampleINS.javaAPI DocExample4244Thu Dec 15 21:49:02 GMT 2005com.oreilly.jent.corba

BindingExampleINS.java

package com.oreilly.jent.corba;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

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

public class BindingExampleINS {
  static public void main(String argv[]) {
    try {
      // Initialize our ORB reference
      ORB myORB = ORB.init(argv, null);
      // Get the root Portable Object Adapter (POA)
      POA rootPOA = (POA) myORB.resolve_initial_references("RootPOA");
      // Activate the POA manager
      rootPOA.the_POAManager().activate();

      // Get the root naming context, using the INS interface
      org.omg.CORBA.Object ref =
        myORB.resolve_initial_references("NameService");
      NamingContextExt rootNC = NamingContextExtHelper.narrow(ref);

      String currURL = null;

      // Create a new context
      NamingContext ctx = rootNC.new_context();
      // Construct the name URL for the new context, and convert it to a
      // Name (a NameComponent array) using the INS utility method to_name()
      currURL = "bankBranches";
      NameComponent path[] = rootNC.to_name(currURL);
      // Bind the context to the name
      rootNC.rebind_context(path, ctx);
      System.out.println("Bound new context to " + currURL);

      // Create another context, bind it to the path "bankBranches, Cambridge" 
      NamingContext cambridgeCtx = rootNC.new_context();
      currURL = "bankBranches/Cambridge";
      path = rootNC.to_name(currURL);
      rootNC.rebind_context(path, cambridgeCtx);
      System.out.println("Bound new context to " + currURL);

      // Create another context, bind it to the path "bankBranches, Boston" 
      NamingContext bostonCtx = rootNC.new_context();
      currURL = "bankBranches/Boston";
      path = rootNC.to_name(currURL);
      rootNC.rebind_context(path, bostonCtx);
      System.out.println("Bound new context to " + currURL);

      // Now we can bind Accounts to a name within any of the new sub-contexts
      // Create a few Account server objects, and get usable client references
      // from the POA
      AccountImplPOA johnAcct = new AccountImplPOA("JohnSmith");
      Account johnRef =
        AccountHelper.narrow(rootPOA.servant_to_reference(johnAcct));
      AccountImplPOA maryAcct = new AccountImplPOA("MarkJones");
      Account maryRef =
        AccountHelper.narrow(rootPOA.servant_to_reference(maryAcct));
      // Bind each Account to a name in the appropriate branch path.  Assume
      // that John has his account out of the Cambridge branch, Mary has hers
      // out of the Boston branch.
      currURL = "bankBranches/Cambridge/Smith,J";
      NameComponent johnPath[] = rootNC.to_name(currURL);
      rootNC.rebind(johnPath, johnRef);
      System.out.println("Bound new account to " + currURL);
      currURL = "bankBranches/Boston/Jones,M";
      NameComponent maryPath[] = rootNC.to_name(currURL);
      rootNC.rebind(maryPath, maryRef);
      System.out.println("Bound new account to " + currURL);

      // Wait for client requests
      myORB.run();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}