
package com.twc.isa.ServerModule;

import org.apache.log4j.Logger;
import org.omg.CORBA.StringHolder;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;

public abstract class ServantBase_impl extends ServantBasePOA {
    
    private static final Logger log = Logger.getLogger(ServantBase_impl.class);
    
    protected String                m_strName               = null;
    protected AdministrativeState   m_AdministrativeState   = null;
    protected OperationalState      m_OperationalState      = null;
    private static int m_iCnt = 0;
    private int m_iIdx  =0;
    
    public ServantBase_impl() {
        m_iIdx  = ++m_iCnt ;
        log.debug("Servant Base Created : " + m_iIdx + " times");
    }
    
    public ServantBase_impl(org.omg.CORBA.ORB orb) {
        m_iIdx  = ++m_iCnt ;
        log.debug("Servant Base Created : " + m_iIdx + " times");
    }
    
    public String 
    name() { 
        return m_strName; 
    }
    
    public void 
    name(String value) {  
        log.debug("Name is : " + value);
        m_strName   = value;
    }
    
    public void 
    setAdminState(AdministrativeState value) {
        m_AdministrativeState	= value; 
    }
    
    public void 
    theAdministrativeState(AdministrativeState value) {
        m_AdministrativeState	= value; 
    }
    
    public AdministrativeState 
    theAdministrativeState() {
        return m_AdministrativeState;
    }
    
    public void theOperationalState(OperationalState value) {  
        m_OperationalState	= value;
    }
    
    public OperationalState 
    theOperationalState() {
        return m_OperationalState;
    }
    
    public void 
    provisioningGui(StringHolder strHolder) throws NoGuiProvisioned {
        throw new NoGuiProvisioned(); 
    }
    
    public void 
    destroy() {;}
    
    public void 
    statusGui(org.omg.CORBA.StringHolder statusGui) throws NoGuiProvisioned{  
        throw new NoGuiProvisioned();
    }
    
    public static org.omg.CosNaming.NamingContextExt 
    getNamingContext(org.omg.CORBA.ORB orb) {
        
        org.omg.CORBA.Object Naming_obj = null;
        
        org.omg.CosNaming.NamingContextExt ctx = null;
        
        // Try getting the Naming service refrence
        
        log.debug("Trying to get the Naming context");
        
        try {
            Naming_obj = orb.resolve_initial_references("NameService"); 
        } catch (org.omg.CORBA.ORBPackage.InvalidName ex){  
            log.error("Can not find the naming service.", ex);  
        } catch (Exception e) {
            log.error("Can not find the naming service.", e);
        }
        
        if (Naming_obj == null) {  
            log.error("The Naming Service Object is null");           
        }
        
        try {
            ctx = org.omg.CosNaming.NamingContextExtHelper.narrow(Naming_obj);
        
            if(ctx == null) {
                log.error("The Naming Context Object is null . This object does not implement a NamingContext");
            } else {
                log.debug("Naming Context Acquired....");
            }

        } catch (Exception e) {           
            log.error("Attempt to narrow naming object failed.", e);
        }
        
        return ctx;
    }
    
    /**
     *
     *  This method retrievs the the factory specified by fullpath.
     *
     */
    public static org.omg.CORBA.Object 
    getFactoryObject(org.omg.CORBA.ORB orb,String [] strFullPath){
        
        org.omg.CosNaming.NamingContextExt ctx = createNamingContext(orb,strFullPath , strFullPath.length -1);
        
        // Compose a name array for Package Factory
        
        try { 
            NameComponent[] PackageFactory_NameComp = new NameComponent[1];
            PackageFactory_NameComp[0] = new NameComponent(strFullPath[strFullPath.length -1],"Factory");
            
            // Resolve the name
            
            org.omg.CORBA.Object obj = ctx.resolve(PackageFactory_NameComp) ;
            
            return obj;
            
        } catch (Exception ex) {  
            log.error(null, ex);  
        }
        
        return null; 
    }
    
    /**
     *
     *  This method registers the the factory specified by fullpath.
     *
     */
    public static void 
    setFactoryObject(org.omg.CORBA.ORB orb,String [] strFullPath, org.omg.CORBA.Object obj){
        
        org.omg.CosNaming.NamingContextExt ctx = createNamingContext(orb, strFullPath, strFullPath.length - 1);
        
        try {
            NameComponent[] PackageFactory_NameComp = new NameComponent[1]; 
            PackageFactory_NameComp[0] = new NameComponent(strFullPath[strFullPath.length -1], "Factory");
            
            // Register the Package Factory object with naming service
            
            ctx.rebind(PackageFactory_NameComp, obj);
        } catch (Exception ex){ 
            log.error(null, ex); 
        }
    }
    
    
    /**
     *
     *  This method creates the naming context if it does not exit else returns the
     *  reference of an existing one.
     */
    
    public static synchronized NamingContextExt 
    createNamingContext(org.omg.CORBA.ORB orb, String[] strContextFullPath, int iLen) {
    	log.debug("Processing NameComponent - " + strContextFullPath[1]);
        for(int iCnt = 1; iCnt < iLen; iCnt++) {
            createNamingContext(orb, strContextFullPath, iCnt);
        }
        
        org.omg.CosNaming.NamingContextExt ctx = ServantBase_impl.getNamingContext(orb);
        org.omg.CORBA.Object Naming_ctx = null;
        
        // Compose a name array for Package Factory
        
        try{
            
            NameComponent[] NameComp = new NameComponent[iLen];
            
            for ( int iCnt = 0; iCnt < iLen; iCnt++){  
                NameComp[iCnt] = new NameComponent(strContextFullPath[iCnt], "Context");
            }
            
            // Create the naming context if it is not already present
            
            try { 
                Naming_ctx  = ctx.resolve(NameComp);
            } catch (org.omg.CosNaming.NamingContextPackage.NotFound ex){
                
                try {
                	log.debug("NameComponent not found " + strContextFullPath[1]);
                    ctx.bind_new_context(NameComp);
                    Naming_ctx = ctx.resolve(NameComp);
                    
                } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound already) {
                    log.debug(null, already);
                    
                    // oops, someone bound in between....
                    
                    Naming_ctx  = ctx.resolve(NameComp);
                    
                } catch (Exception exp){
                    log.error(null, exp); 
                }
                
            } catch (Exception ex){ 
                log.error(null, ex);
            }
            
            return org.omg.CosNaming.NamingContextExtHelper.narrow(Naming_ctx);
            
        } catch (Exception ex){
            log.error(null, ex);
        }
        
        return null;
        
    }
    
    public static org.omg.CORBA.ORB orb_main(String args[]){
        return orb_main(args,System.getProperties());
    }
    
    public static org.omg.CORBA.ORB 
    orb_main(String args[], java.util.Properties props){
             
        log.debug("Inside orb_main ");
        
        //props.put("org.omg.CORBA.ORBClass", "org.openorb.CORBA.ORB");       
        //props.put("org.omg.CORBA.ORBSingletonClass", "org.openorb.CORBA.ORBSingleton");        
        //props.put("ooc.config","/ORB/Orbacus/ORBACUS.config");
        
        org.omg.CORBA.ORB orb = null;
        
        try {
            orb = org.omg.CORBA.ORB.init(args, props);
            
            if (orb == null) {   
                log.error("orb is null");
            }
            
        } catch(Exception ex) {    
            log.error(null, ex);  
        }
        
        return orb;
    }
}
