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

import java.net.InetAddress;
import java.util.Iterator;
import java.util.Properties;

import com.eactive.eai.common.server.Keys;
import com.eactive.eai.rms.client.dao.RmsPropertiesDAO;
import com.eactive.eai.rms.client.util.ContainerUtil;

/**
 * The Class RmsPropertiesImpl.
 * <ul>
 * <li>1. Function :모니터링시 필요한 정보를 로딩 한때 이 클래스를 사용 합니다. </li>
 * <li>2. Summary :Monitoring 프로퍼티 그룹과 Monitoring{IP} 형태의 프로퍼티 그룹의 모든 프로퍼티
 * 그리고 {@link System#getProperties()} 에 존재하는 모든 프로퍼티를 로딩 합니다. </li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @see com.eactive.eai.rms.client.dao.RmsPropertiesDAO#getProperties(String)
 * @author : $Author: 윤창용 $
 * @version : $Revision: 1.10 $
 */
public class RmsPropertiesImpl implements RmsProperties {

    /** 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 rms properties dao. */
    private RmsPropertiesDAO rmsPropertiesDAO;

    /** The properties. */
    private Properties properties;

    /** The server ip. */
    private String serverIp;

    /*
     * (non-Javadoc)
     * 
     * @see com.eactive.eai.rms.client.context.Test#init()
     */
    public void init() throws Exception {
        InetAddress inetAddress = InetAddress.getLocalHost();
        serverIp = inetAddress.getHostAddress();
        this.properties = rmsPropertiesDAO.getProperties();
        this.properties.setProperty(LOCAL_HOST_IP, serverIp);
        if (Boolean.getBoolean("rms.standalone")){
        	this.properties.setProperty(LOCAL_HOST_NAME, System.getProperty("rms.hostname"));
        }else{
        	this.properties.setProperty(LOCAL_HOST_NAME, inetAddress.getHostName());
        }
        this.properties.setProperty("standalone", String.valueOf(Boolean.getBoolean("rms.standalone")));
        this.properties.putAll(System.getProperties());
        this.setLocalMbeanPort(this.properties, System.getProperty(Keys.SERVER_KEY));
        
        if (logger.isInfoEnabled()) {
            printProperties();
        }
    }

    private void setLocalMbeanPort(Properties props, String eaiInstName) {
        if (props.getProperty(LOCAL_MBEAN_SERVER_PORT) == null) {
           
            String port = rmsPropertiesDAO.getLocalMBeanServerPort();
            props.setProperty(LOCAL_MBEAN_SERVER_PORT, port);
        }
    }

    /**
     * Adds the agent infomation for print.
     * 
     * @param sb the sb
     * @param text the text
     */
    private void addAgentInfomationForPrint(StringBuilder sb, String text) {
        sb.append("# ").append(text).append("\n");
    }

    /**
     * Adds the agent infomation for print.
     * 
     * @param sb the sb
     * @param id the id
     * @param value the value
     */
    private void addAgentInfomationForPrint(StringBuilder sb, String id,
            String value) {
        sb.append("# ").append(id).append("\t: ").append(value).append("\n");
    }

    /**
     * Adds the line for print.
     * 
     * @param sb the sb
     */
    private void addLineForPrint(StringBuilder sb) {
        sb.append("###################################").append(
                "############################################").append("\n");
    }

    /**
     * Prints the system properties.
     */
    @SuppressWarnings("unchecked")
    private void printProperties() {

        String javaVersion = System.getProperty("java.version");
        String javaVender = System.getProperty("java.vendor");
        String was = ContainerUtil.getString();

        if (logger.isInfoEnabled()) {
            StringBuilder sb = new StringBuilder(1024);
            addLineForPrint(sb.append("\n"));
            addAgentInfomationForPrint(sb, "MONITORING AGENT_INFOMATION");
            addAgentInfomationForPrint(sb, "VERSION", "");
            addAgentInfomationForPrint(sb, "WAS", was);
            addAgentInfomationForPrint(sb, "JDK VENDER", javaVender);
            addAgentInfomationForPrint(sb, "JDK VERSION", javaVersion);
            addAgentInfomationForPrint(sb, "SERVER IP", serverIp);
            addLineForPrint(sb);
            logger.info(sb.toString());
        }

        for (Iterator it = properties.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            logger.info(key + "=" + properties.getProperty(key));
        }
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getProperties()
     */
    public Properties getProperties() {
        return properties;
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#refresh()
     */
    public void refresh() throws Exception {
        init();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#setProperties(java.util.Properties)
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#setRmsPropertiesDao(com.eactive.eai.rms.client.dao.RmsPropertiesDAO)
     */
    public void setRmsPropertiesDao(RmsPropertiesDAO rmsPropertiesDAO) {
        this.rmsPropertiesDAO = rmsPropertiesDAO;
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getStringProperty(java.lang.String)
     */
    public String getStringProperty(String propertyName) {
        return properties.getProperty(propertyName);
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getStringProperty(java.lang.String, java.lang.String)
     */
    public String getStringProperty(String propertyName, String defaultString) {
        String value = getStringProperty(propertyName);
        return value != null ? value : defaultString;
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getIntProperty(java.lang.String)
     */
    public int getIntProperty(String propertyName) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? 0 : new Integer(prop.trim()).intValue();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getIntProperty(java.lang.String, int)
     */
    public int getIntProperty(String propertyName, int defaultInt) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? defaultInt : new Integer(prop.trim()).intValue();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getLongProperty(java.lang.String)
     */
    public long getLongProperty(String propertyName) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? 0 : new Long(prop.trim()).longValue();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getLongProperty(java.lang.String, long)
     */
    public long getLongProperty(String propertyName, long defaultLong) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? defaultLong : new Long(prop.trim()).longValue();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getBooleanProperty(java.lang.String)
     */
    public boolean getBooleanProperty(String propertyName) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? false : new Boolean(prop.trim()).booleanValue();
    }

    /* (non-Javadoc)
     * @see com.eactive.eai.rms.client.context.RmsProperties#getBooleanProperty(java.lang.String, boolean)
     */
    public boolean getBooleanProperty(String propertyName,
            boolean defaultBoolean) {
        String prop = properties.getProperty(propertyName);
        return prop == null ? defaultBoolean : new Boolean(prop.trim())
                .booleanValue();
    }

}
