package com.eactive.eai.rms.client.service;

import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.eactive.eai.common.util.ContainerUtil;
import com.eactive.eai.rms.client.context.RmsProperties;
import com.eactive.eai.rms.onl.vo.EAIEngineStatusVo;

/**
 * <pre>
 * 1. 기능 : EAI Engine의 상태를 조회 합니다. 
 * 2. 처리 개요 :
 * -
 * 3. 주의사항 :
 * </pre>
 * 
 * @author  : 박기섭
 * @version : $Revision: 1.9 $
 * @since : rts 1.0
 */
public class EaiEngineStatusService implements ApplicationContextAware {

    /** F/W logger. */
    private static com.eactive.eai.common.util.Logger logger = com.eactive.eai.common.util.Logger.getLogger(com.eactive.eai.common.util.Logger.LOGGER_CLIENT);


    /** The application context. */
    private ApplicationContext applicationContext;

    private RmsProperties rmsProperties;

    private Properties adminClientProperties;

    public void setAdminClientProperties(Properties adminClientProperties) {
        this.adminClientProperties = adminClientProperties;
    }

    public void setProperties(RmsProperties rmsProperties) {
        this.rmsProperties = rmsProperties;
    }

    /**
     * Instantiates a new eai engine status service.
     */
    public EaiEngineStatusService() {
        super();
    }

    /**
     * Gets the eAI engine status.
     * 
     * @return the eAI engine status
     * 
     * @throws Exception the exception
     */
    public EAIEngineStatusVo getEAIEngineStatus() throws Exception {

        EAIEngineStatusVo eaiEngineStatusVo = (EAIEngineStatusVo) applicationContext.getBean("eaiEngineStatusVo");
        try {
            setServerStatus(eaiEngineStatusVo);
            this.setJVMRuntimeStatus(eaiEngineStatusVo);

        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
        return eaiEngineStatusVo;
    }

    /**
     * Sets the server status.
     * 
     * @param status the status
     * @param serverMBean the server m bean
     * @param serverName the server name
     */
    private void setServerStatus(EAIEngineStatusVo status) {

    	String instName     = rmsProperties.getStringProperty(RmsProperties.EAI_SERVER_INSTANCE_NAME);
    	status.setServerName(instName);
    	
		int state = 0;
		try {
			JMXMonitoring monitoring = JMXMonitoringFactory.createServerMonitoring(ContainerUtil.get());
			//JMX가 없을경우 무조건 정상
			if (monitoring == null) {
				status.setServerState(Integer.toString(2));
				return;
			}
			monitoring.init(this.adminClientProperties);
			state = (int)monitoring.getState(instName);
			status.setServerState(Integer.toString(state));
		} catch (Exception e) {
			status.setServerState(Integer.toString(state));
		}
        
    }

    /**
     * Sets the jvm runtime status.
     * 
     * @param status the status
     * @param serverRuntimeMBean the server runtime m bean
     */
    private void setJVMRuntimeStatus(EAIEngineStatusVo status) {
        Runtime runtime = Runtime.getRuntime();

        status.setHeapFreeCurrent(runtime.freeMemory());
        status.setHeapSizeCurrent(runtime.totalMemory());
        status.setHeapSizeMax(runtime.maxMemory());

        long precent = (((status.getHeapSizeMax() - status.getHeapSizeCurrent()) + status.getHeapFreeCurrent()) * 100)
                / status.getHeapSizeMax();

        status.setHeapFreePercent(new Long(precent).intValue());
    }

    /* (non-Javadoc)
     * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

}
