package com.twc.isa.ServerModule;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.omg.CORBA.StringHolder;
import org.omg.CosEventChannelAdmin.AlreadyConnected;
import org.omg.CosEventChannelAdmin.EventChannel;

public class ServantFactory_ extends ServantBase_ implements ServantFactoryOperations {
    
	private static final Logger log = Logger.getLogger(ServantBase_.class);
	
	public Hashtable m_sbaseHashTable = null;    
    
	public ServantFactory_(org.omg.CORBA.ORB orb,String strName) {
        
		super(orb,strName);
        
		m_sbaseHashTable = new Hashtable();
		log.debug("strName in ServantFactory_=" + strName);
        
		log.debug("Servant Factory Created");
        
	}
    
	public org.omg.CORBA.ORB 
	getORB(){
		return super.m_orb;
	}
    
	// Implentation for ServantFactoryOperations
    
	public ServantBase 
	createServant(String strName) throws ServantCreateFailed {
		throw new ServantCreateFailed();
	}
    
    
	public ServantBase 
	find(java.lang.String name) throws NameNotFound {
        
        
		// look for the given name in the Servant Base hash table
        
		ServantBase s_base = (ServantBase)m_sbaseHashTable.get(name);
        
		// check for the result
        
		if (s_base == null) {
            
			// throw the exception if name does not exists
            
			throw new NameNotFound();
            
		} else {
            
			log.debug("Servant base "+ name + " found");
            
			// return the object reference
            
			return s_base;
		}
        
	}
    
	public void 
	removeServant(ServantBase s) {
		
		if(m_sbaseHashTable.containsKey(s.name())) {           
			m_sbaseHashTable.remove(s.name());           
		}
	}
    
	// Implementation for ServantBaseOperations
    
	public String 
	name() {
		return super.m_strName;
	}
    
	public void 
	name(String value) {
		super.m_strName   = value;
	}
    
	public void 
	setAdminState(AdministrativeState value) {
		super.m_AdministrativeState	= value;
	}
    
	public void 
	theAdministrativeState(AdministrativeState value) {
		super.m_AdministrativeState	= value;
	}
    
	public AdministrativeState 
	theAdministrativeState() {
		return super.m_AdministrativeState;
	}
    
	public void 
	theOperationalState(OperationalState value) {
		super.m_OperationalState	= value;
	}
    
	public OperationalState 
	theOperationalState() {
		return super.m_OperationalState;
	}
    
	public void 
	provisioningGui(StringHolder strHolder) throws NoGuiProvisioned {
		super.provisioningGui(strHolder);
	}
    
	public void destroy(){;}
    
	public void 
	statusGui(org.omg.CORBA.StringHolder statusGui) throws NoGuiProvisioned {
		super.provisioningGui(statusGui);
	}
    
	public void
	add(java.lang.String name, ServantBase sb) {
		m_sbaseHashTable.put(name,sb);
	}
    
	public void 
	list(int iCount, com.twc.isa.ServerModule.ServantBaseListHolder sblh, com.twc.isa.ServerModule.ServantBaseIteratorHolder sbih){
        
		// get the size of the hashTable
        
		int iObjCount = m_sbaseHashTable.size();
        
		ServantBase [] sbList = new ServantBase [iObjCount < iCount? iObjCount:iCount];
        
		System.arraycopy( m_sbaseHashTable.values().toArray(),0,sbList,0,iObjCount < iCount? iObjCount:iCount);
        
		if(iObjCount > iCount) {
            
			// if there are tones of components then we will do more brainstroming
            
			ServantBaseIterator_impl sbii = new ServantBaseIterator_impl(iCount,m_sbaseHashTable);
            
			sbih.value = sbii._this(super.m_orb);  
		}
        
		sblh.value  = sbList;
	}
    
	public static String 
	getProvisionGUI_URL_Name(String strTxtFileName) {
        
		try {
            
			BufferedReader br = new BufferedReader(new FileReader(""+System.getProperties().get("user.dir") + "/ProvisioningURLs/" + strTxtFileName ));
            
			String strLine = br.readLine();
            
			br.close();
            
			return strLine;
            
		} catch( Exception ex){
            
			log.error(null, ex);
		}
        
		return null;
	}
    
	static private EventChannel 
	GetEventChannel(org.omg.CORBA.ORB orb, String name){
        
		EventChannel e = null;
        
		log.debug("Inside the EventChannel GetEventChannel");

		return e;
	}
    
    
	/**
	 *
	 *  This method connects the Proxy Push Consumer to the event Service
	 *
	 */
    
	public int connectPushConsumer() {
        
		try {
			super.getPushConsumer().connect_push_supplier(null);
            
		} catch(AlreadyConnected ex){
			log.info(null, ex);
			return 1;
		}
        
		return 0;
	}
    
	/**
	 *  This method writes any given string into the event channel.
	 */
    
	public int 
	send(String strEventName){
		return super.send(strEventName);
	}
}