import rock.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
import java.util.*;
import java.io.*;
import java.applet.*;

/**
 * Establishes a connection to the CORBA server RockFactory object.
 * @version 1.0 (August 2007)
 * @author Mark S. Ghiorso, OFM-Research Inc.
 */
public class Connection{

  /**
   * Class initializer.
   */
  public Connection() { }

  /**
   * Obtains the Object Request Broker for the Applet. The applet must originate from a 
   * web server that is running a CORBA nameservice and rockFactory server.
   * @param myapplet Applet instance.
   * @return ORB reference.
   */
  public ORB getORB(Applet myapplet) {
    System.out.println("Initializing the Applet ORB.");
    ORB orb = ORB.init(myapplet, null);
    return orb;
  }

  /**
   * Obtains the Object Request Broker for the standalone application.
   * @param args Command line arguments.
   * @param props Properties defined for ORB initialization call. Two properties must be set: 
   * org.omg.CORBA.ORBInitialPort to the CORBA nameserver port number (usually 2809) and
   * org.omg.CORBA.ORBInitialHost to the IP address of the CORBA nameServer.
   * @return ORB reference.
   */
  public ORB getORB(String args[], Properties props) {
    System.out.println("Initializing the Application ORB.");
    ORB orb = ORB.init(args, props);
    return orb;
  }

  /**
   * Obtain a generic object reference from the CORBA nameserver.
   * @param orb ORB reference retuned from getORB.
   * @param name Description of CORBA server object.
   * @return Instance of requested server object.
   * @exception java.lang.Exception CORBA connection error. Description output to System.err.
   */
  public org.omg.CORBA.Object getObjectReference(ORB orb, NameComponent[] name) throws Exception {
    try {
      System.out.println("Initializing the Naming Service...");
      org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
      System.out.println("Object" + objRef);
      
      if (objRef == null) System.out.println("Name Service Object is null");
      NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
      if (ncRef == null) System.out.println("NameService is null");
      
      org.omg.CORBA.Object obj = ncRef.resolve(name);
      return obj;
    } catch (Exception e) {
      System.out.println("ERROR in getObjectReference: " + e);
      throw e;
    }
  }

  /**
   * Obtain a RockFactory object reference from the CORBA nameserver.
   * @param orb ORB reference retuned from getORB.
   * @return Instance of requested RockFactory object.
   * @exception java.lang.Exception CORBA connection error. Description output to System.err.
   */
  public RockFactory getRockFactory(ORB orb) throws Exception {
    try {
      NameComponent name1 = new NameComponent("rock", "context");
      NameComponent name2 = new NameComponent("RockFactory", "object");    
      NameComponent name[] = {name1, name2};

      org.omg.CORBA.Object x = getObjectReference(orb, name);

      RockFactory reference = rock.RockFactoryHelper.narrow(x);
      if (reference == null) System.out.println("Object reference is not a rock::RockFactory");
      return reference;
    } catch (Exception e) {
      System.out.println("ERROR in getRockFactory: " + e);
      throw e;
    }
  }

}
