package com.eactive.eai.rms.onl.server.repository;

import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.keyvalue.MultiKey;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eactive.eai.rms.onl.vo.MonitorDataVo;

/**
 * The Class Repository.
 * <ul>
 * <li>1. Function : EAI Framework 으로 부터 수신된 모든 객체는 이 클래스에 저장 됩니다. </li>
 * <li>2. Summary : 저장된 객체는 timeToLive 설정에 의해 Expire 됩니다. </li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @author  : $Author: 윤창용 $
 * @version : $Revision: 1.8.14.4 $
 */
public class Repository {

	/** The Sms breaker.  header time */
	private String smsBreaker ;
	
    private static final Log logger = LogFactory.getLog(Repository.class);
            

	
    /** The repository. */

    private ExpiringMap<MultiKey, Object> repository;

    
    /** Expire 되지 않는 맵 */
    private ConcurrentHashMap<MultiKey, Object> repositoryNoExpire;
    
    
    /**
     * Instantiates a new repository.
     * 
     * @param timeToLive the time to live
     * @param expirationInterval the expiration interval
     */
    public Repository(int timeToLive, int expirationInterval) {
        repository = new ExpiringMap<MultiKey, Object>(timeToLive,
                expirationInterval);
        
        repositoryNoExpire = new ConcurrentHashMap<MultiKey, Object>();
    }
    
    public String getSmsBreaker( ) {
    	if( smsBreaker == null ) {
    		smsBreaker = "" ;
    	}
    	return smsBreaker;
    }
    
    public void setSmsBreaker( String b ) {
    	smsBreaker = b ;
    }

    /**
     * Adds the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * @param o the o
     * @param timeToLive the time to live
     */
    public void addMonitorData(String hostName, String instanceName,
            String category, MonitorDataVo o, int timeToLive) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        repository.put(key, o, timeToLive);
    }
    
    /**
     * Adds the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param category
     * @param o
     */
    public void addMonitorDataNoExpire(String hostName, String instanceName,
            String category, MonitorDataVo o) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        repositoryNoExpire.put(key, o);
    }

    /**
     * Adds the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param o the o
     * @param timeToLive the time to live
     */
    public void addMonitorData(String hostName, String instanceName, Object o,
            int timeToLive) {
        MultiKey key = new MultiKey(hostName, instanceName, o.getClass()
                .getName());
        repository.put(key, o, timeToLive);
    }

    /**
     * Adds the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param o
     */
    public void addMonitorDataNoExpire(String hostName, String instanceName, Object o) {
        MultiKey key = new MultiKey(hostName, instanceName, o.getClass()
                .getName());
        repositoryNoExpire.put(key, o);
    }

    /**
     * Gets the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the monitor data
     */
    public Object getMonitorData(String hostName, String instanceName,
            String category) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        return repository.get(key);
    }
    
    /**
     * Gets the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object getMonitorDataNoExpire(String hostName, String instanceName,
            String category) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        return repositoryNoExpire.get(key);
    }
    

    /**
     * Gets the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the monitor data
     */
    public Object getMonitorData(String hostName, String instanceName,
            Class<?> category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getName());
        return repository.get(key);
    }

    /**
     * Gets the monitor data
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object getMonitorDataNoExpire(String hostName, String instanceName,
            Class<?> category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getName());
        return repositoryNoExpire.get(key);
    }

    
    /**
     * Gets the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the monitor data
     */
    public Object getMonitorData(String hostName, String instanceName,
            Object category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getClass()
                .getName());
        return repository.get(key);
    }

    /**
     * Gets the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object getMonitorDataNoExpire(String hostName, String instanceName,
            Object category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getClass()
                .getName());
        return repositoryNoExpire.get(key);
    }

    
    /**
     * Removes the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the object
     */
    public Object removeMonitorData(String hostName, String instanceName,
            String category) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        return repository.remove(key);
    }
    
    /**
     * Removes the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object removeMonitorDataNoExpire(String hostName, String instanceName,
            String category) {
        MultiKey key = new MultiKey(hostName, instanceName, category);
        return repositoryNoExpire.remove(key);
    }
    

    /**
     * Removes the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the object
     */
    public Object removeMonitorData(String hostName, String instanceName,
            Class<?> category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getName());
        return repository.remove(key);
    }

    /**
     * Removes the monitor data no expire
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object removeMonitorDataNoExpire(String hostName, String instanceName,
            Class<?> category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getName());
        return repositoryNoExpire.remove(key);
    }

    /**
     * Removes the monitor data.
     * 
     * @param hostName the host name
     * @param instanceName the instance name
     * @param category the category
     * 
     * @return the object
     */
    public Object removeMonitorData(String hostName, String instanceName,
            Object category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getClass()
                .getName());
        return repository.remove(key);
    }

    /**
     * Removes the monitor data no expire.
     * @param hostName
     * @param instanceName
     * @param category
     * @return
     */
    public Object removeMonitorDataNoExpire(String hostName, String instanceName,
            Object category) {
        MultiKey key = new MultiKey(hostName, instanceName, category.getClass()
                .getName());
        return repositoryNoExpire.remove(key);
    }

    /**
     * Adds the monitor data.
     * 
     * @param message the message
     * @param timeToLive the time to live
     */
    public void addMonitorData(MonitorDataVo message, int timeToLive) {
        if (message != null) {
            addMonitorData(message.getHostName(), message.getInstanceName(),
                    message.getCategory(), message, timeToLive);
        }
    }
    
    /**
     * Adds the monitor data no expire.
     * @param message
     */
    public void addMonitorDataNoExpire(MonitorDataVo message) {
        if (message != null) {
            addMonitorDataNoExpire(message.getHostName(), message.getInstanceName(),
                    message.getCategory(), message);
        }
    }
    
    public void cleanupNoExpire() {
    	repositoryNoExpire.clear();
    	repositoryNoExpire = new ConcurrentHashMap<MultiKey, Object>();
    	
//    	Iterator it = repository.keySet().iterator();
//    	while( it.hasNext() ) {
//    		MultiKey key = (MultiKey)it.next();
//    		Object obj1 = key.getKey(2);
//    		Object obj2 = repository.get(key);
//    		
//    	}
    }
    
    public void checkNoExpire() {
    	logger.debug("check repositoryNoExpire size=" + repositoryNoExpire.size() );
    	logger.debug("check repository size=" + repository.size() );
    }
    
}
