/*
 *  OrbGeneric.java v0.10 20-DEC-1999
 *  Copyright (c) TKK/TLM/Calypso
 *  Author: Alexey Mednonogov
 */

package codec.orb;

import codec.*;
import codec.adapt.*;
import codec.convert.*;
import codec.debug.*;
import codec.dyntree.*;
import codec.export.*;
import codec.orb.*;
import codec.pco.*;
import codec.server.*;
import codec.client.*;
import codec.visit.*;
import codec.build.*;

/** Class implementing generic ORB operations. */
final public class OrbGeneric {

    private org.omg.CORBA.ORB orb = null;
    private org.omg.CORBA.BOA boa = null;
    private org.omg.CORBA.Repository rep = null;
    private org.omg.CosNaming.NamingContext nc = null;

   	public static final int INIT_ORB_ONLY     = 0;
   	public static final int INIT_ALL_ENTITIES = 1;

    /** Initialize ORB, BOA and Naming Service. Shall be called only once. */
    public OrbGeneric(String args[], int options) {

		if ((options != INIT_ORB_ONLY) && (options != INIT_ALL_ENTITIES)) {

			 System.err.println("OrbGeneric::OrbGeneric(): " +
                "Invalid options.");
            System.exit(0);
		}

        java.util.Properties props = System.getProperties();
        props.put("org.omg.CORBA.ORBClass", "com.ooc.CORBA.ORB");
        props.put("org.omg.CORBA.ORBSingletonClass",
                "com.ooc.CORBA.ORBSingleton");
        System.setProperties(props);

        // Connecting to ORB:
        try {
            orb = org.omg.CORBA.ORB.init(args, props);
        } 
        catch (org.omg.CORBA.SystemException ex) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "ORB initialization failed.");
            System.exit(0);
        }
		if (options == INIT_ORB_ONLY) return;

        // Connecting to Naming Service:
        org.omg.CORBA.Object namingObject = null;
        try {
            namingObject = orb.resolve_initial_references("NameService");
        } 
        catch (org.omg.CORBA.ORBPackage.InvalidName ex) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "No Naming Service available.");
            System.exit(0);
        }
        if (namingObject == null) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "Invalid Naming Service IOR.");
            System.exit(0);
        }
		try {
			System.gc(); // Magic fix to JDK bugs.
			nc = org.omg.CosNaming.NamingContextHelper.narrow(namingObject);
		}
		catch (org.omg.CORBA.SystemException se) {
			System.err.println("OrbGeneric::OrbGeneric(): " +
                "Advertised IOR does not point to Naming Service.");
            System.exit(0);
		}
        if (nc == null) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "Advertised IOR does not point to Naming Service.");
            System.exit(0);
        }

        // Connecting to Interface Repository:
        org.omg.CORBA.Object repObject = null;
        try {
            repObject = orb.resolve_initial_references("InterfaceRepository");
        } 
        catch (org.omg.CORBA.ORBPackage.InvalidName ex) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "No Interface Repository available.");
            System.exit(0);
        }
        if (repObject == null) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "Invalid Interface Repository IOR.");
            System.exit(0);
        }
        rep = org.omg.CORBA.RepositoryHelper.narrow(repObject);
        if (rep == null) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "Advertised IOR does not point to Interface Repository.");
            System.exit(0);
        }
        // Test whether Interface Repository is actually active:
        try {
            rep.lookup("");
        }
        catch (org.omg.CORBA.SystemException ex) {

            System.err.println("OrbGeneric::OrbGeneric(): " +
                "Advertised IOR does not point to Interface Repository.");
            System.exit(0);
        }

        // Connecting to BOA:
        boa = ((com.ooc.CORBA.ORB)orb).BOA_init(args, props);
    }

    /** Obtain pointer to ORB. */
    public org.omg.CORBA.ORB getORB() {
        return orb;
    }

    /** Obtain pointer to BOA. */
    public org.omg.CORBA.BOA getBOA() {
        return boa;
    }

    /** Obtain pointer to Naming Context. */
    public org.omg.CosNaming.NamingContext getNamingContext() {
        return nc;
    }

    /** Obtain pointer to Interface Repository. */
    public org.omg.CORBA.Repository getRepository() {
        return rep;
    }
}
