package com.twc.isa.ServerModule;

import java.net.InetAddress;

import org.apache.log4j.Logger;
import org.omg.CORBA.Any;
import org.omg.CORBA.StringHolder;
import org.omg.CosEventChannelAdmin.ProxyPushConsumer;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

public class ServantBase_ implements ServantBaseOperations {
	
	private final Logger log = Logger.getLogger(ServantBase_.class);
    
    private static POA m_poa     = null;
    public String m_strName      = null;
    public AdministrativeState   m_AdministrativeState   = null;
    public OperationalState      m_OperationalState      = null;
    
    public long m_lastModifiedTm = 0;
    public long m_createdTm      = 0;
    
    public static org.omg.CORBA.ORB  m_orb         = null;
    private static ProxyPushConsumer m_PushConsumr = null;
    
    private String [] m_strFullPath;
    private static int m_iCnt = 0;
    private int m_iIdx  = 0;
    
    public ServantBase_() {        
        init();       
        m_iIdx  = ++m_iCnt ;       
        log.debug("Servant Base Created : " + m_iIdx + " times");
    }
    
    public ServantBase_(org.omg.CORBA.ORB orb,String strName) {
       
        init();        
        m_iIdx  = ++m_iCnt ;
        
        log.debug("Servant Base Created : " + m_iIdx + " times");
        
        if (strName != null && !strName.trim().equals("") ) {
            m_strName = strName;
        
        } else {
            
            try {            
                m_strName  = "Object_" + m_iIdx + "_on_" + InetAddress.getLocalHost().getHostAddress()+"_for_"; // +System.getProperties().getProperty("ApplicationName");           
            } catch (Exception ex) {            
                log.error(null, ex);           
            }
        }        
        
        if (m_orb == null) {
        	m_orb = orb;
        }
        
        if (m_poa == null) {
            
            try {                
                m_poa = POAHelper.narrow(m_orb.resolve_initial_references("RootPOA"));               
            } catch (Exception ex){              
                log.error(null, ex);               
            }
        }
    }
   
    private void init() {        
        m_AdministrativeState = AdministrativeState.as_Unprovisioned;        
        m_OperationalState    = OperationalState.os_Created;      
        m_strName = "Un_Named_Object";        
    }
  
    public static void 
    destroy(org.omg.PortableServer.Servant servant){
        
        try {
            
            byte[] oid = m_poa.servant_to_id(servant);           
            m_poa.deactivate_object(oid);
            
        } catch (org.omg.PortableServer.POAPackage.WrongPolicy ex) {           
            throw new RuntimeException();           
        } catch (org.omg.PortableServer.POAPackage.ServantNotActive ex) {           
            throw new RuntimeException();            
        } catch (org.omg.PortableServer.POAPackage.ObjectNotActive ex) {            
            throw new RuntimeException();           
        }
        
    }
      
    public String 
    name() {        
        return m_strName;       
    }
    
    public void 
    provisioningGui(StringHolder strHolder) throws NoGuiProvisioned {       
        throw new NoGuiProvisioned();       
    }
     
    
    public void 
    destroy(){        
        throw new RuntimeException("Not Implemetnted");        
    }   
    
    public void 
    provision() {       
        throw new RuntimeException("Not Implemetnted");       
    }  
    
    public void 
    statusGui(org.omg.CORBA.StringHolder statusGui) throws NoGuiProvisioned {       
        throw new NoGuiProvisioned();       
    }
    
    public void 
    setPushConsumer(ProxyPushConsumer pc){        
        m_PushConsumr  = pc;       
    }
    
    public ProxyPushConsumer 
    getPushConsumer(){        
        return m_PushConsumr;       
    }
    
    public synchronized int 
    send(String strEventName){
        
        Any any = m_orb.create_any();
        
        any.insert_string(strEventName);
        
        try {
            
            m_PushConsumr.push(any);
            
            return 0;
            
        } catch(org.omg.CosEventComm.Disconnected ex) {
            log.info(null, ex);
        } catch(org.omg.CORBA.SystemException ex) {           
            log.info(null, ex);           
        }
        
        return 1;
        
    }
    
    public void 
    setFullPath(String [] strFullPath){        
        m_strFullPath  = strFullPath;       
    }
    
    public String [] 
    getFullPath(){      
        return m_strFullPath;       
    }
    
    public void 
    setAdminState(com.twc.isa.ServerModule.AdministrativeState st) throws com.twc.isa.ServerModule.UnspecifiedException {
        m_AdministrativeState = st;        
    }
    
    public com.twc.isa.ServerModule.AdministrativeState 
    getAdminState() throws com.twc.isa.ServerModule.UnspecifiedException {
        return m_AdministrativeState;        
    }
    
    public void 
    getCreateTime(org.omg.CORBA.LongHolder longHolder) throws com.twc.isa.ServerModule.UnspecifiedException, com.twc.isa.ServerModule.Unimplemented {
        longHolder.value = m_createdTm;
    }
    
    public void 
    getLastModifiedTime(org.omg.CORBA.LongHolder longHolder) throws com.twc.isa.ServerModule.UnspecifiedException, com.twc.isa.ServerModule.Unimplemented {
        longHolder.value = m_lastModifiedTm;
    }
    
    public com.twc.isa.ServerModule.OperationalState 
    getOpState() throws com.twc.isa.ServerModule.UnspecifiedException {
        return m_OperationalState;       
    }
    
}
