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

/**
 * Establishes a connection to the CORBA server PhaseFactory and PhaseManager objects.
 * @version 3.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 phaseFactory server.
   * @param myapplet Applet instance.
   * @return ORB reference.
   */
  public ORB getORB(Applet myapplet) {
    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) {
    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.err.println("ERROR" + e);
		throw e;
	}
  }

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

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

        PhaseFactory pFactory = phases.PhaseFactoryHelper.narrow(x);
        if (pFactory == null)
        {
                System.out.println("Object reference is not a phases::PhaseFactory");
        }
	return pFactory;
	}
	catch (Exception e){
		System.err.println("ERROR" + e);
		throw e;
	}

  }

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

	org.omg.CORBA.Object y = getObjectReference(orb, name5);
        PhaseManager pManager = phases.PhaseManagerHelper.narrow(y);
        if (pManager == null)
        {
                System.out.println("Object reference is not a phases::PhaseManager");
       
	}
	return pManager;
	}
	catch (Exception e) {
		System.err.println("ERROR" + e);
		throw e;
	}
 }

}

