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.
 */

/**
 * AccountInitPOA: Initialize a remote object, register it with a naming
 * service, and generate a stringified IOR.
 */

import org.omg.CORBA.*;
import org.omg.CosNaming.*;
import org.omg.PortableServer.POA;
import java.net.InetAddress;

public class AccountInitPOA {
  public static void shutdown(org.omg.CORBA.ORB orb) {
    System.out.println("Shutting down");
  }
  public static void main(String[] args) {
    try {
      // Initialize an ORB reference
      ORB myORB = ORB.init(args, null);

      // Create an instance of an Account server implementation
      AccountImplPOA acct = new AccountImplPOA(args[0]);

      // Get the root Portable Object Adapter (POA)
      POA rootPOA = (POA) myORB.resolve_initial_references("RootPOA");
      // Activate the POA manager
      rootPOA.the_POAManager().activate();
      // Activate our servant, so that direct requests (through a corbaloc
      // or IOR URL) will work immediately
      rootPOA.activate_object(acct);

      // Get an object reference from the implementation
      org.omg.CORBA.Object obj = rootPOA.servant_to_reference(acct);
      Account acctRef = (Account) AccountHelper.narrow(obj);

      // Get the root name context (use the INS interface, so that we can use
      // the simpler name construction process)
      org.omg.CORBA.Object objRef =
        myORB.resolve_initial_references("NameService");
      NamingContextExt nc = NamingContextExtHelper.narrow(objRef);

      // Register the local object with the Name Service
      // Use the Interoperable Naming Service interface to simplify matters,
      // and to support URL-formatted names (e.g. "JohnS",
      // "corbaname://orbhost.com#JohnS", etc.)
      NameComponent[] name = nc.to_name(args[0]);
      nc.rebind(name, acctRef);

      System.out.println("Registered account under name " + args[0]);
      System.out.println("New account created and registered.  URLs are: ");
      // Get the IOR using object_to_string()
      System.out.println("\nIOR");
      System.out.println("\t" + myORB.object_to_string(acctRef));
      // Construct the corbaloc URL using local ORB parameters ???
      System.out.println("\ncorbaloc");
      System.out.println(
        "\tcorbaloc:iiop:" + InetAddress.getLocalHost().getHostName());
      System.out.println("\ncorbaname");
      // If the user gave us a corbaname URL directly on the command-line, just
      // emit that
      if (args[0].startsWith("corbaname")) {
        System.out.println("\t" + args[0]);
      }
      // Otherwise, we need to construct the corbaname URL from our ORB and
      // Naming Service parameters.
      else {
        System.out.println(
          "\tcorbaname:iiop:"
            + InetAddress.getLocalHost().getHostName()
            + "#"
            + args[0]);
      }

      // Go into a wait state, waiting for clients to connect
      myORB.run();
    }
    catch (Exception e) {
      System.out.println("An error occurred while initializing server object:");
      e.printStackTrace();
    }
  }
}
