package com.eactive.eai.rms.common.util;

import java.util.HashMap;
import java.util.Map;

/**
 * The Class ContainerUtil.
 * <ul>
 * <li>1. Function :현재 기동되는 컨테이너를 식별 합니다. </li>
 * <li>2. Summary :</li>
 * <li>3. Notification :</li>
 * </ul>
 * 
 * @author  : $Author: 박기섭 $
 * @version : $Revision: 1.2 $
 */
public class ContainerUtil {

    /** The Constant UNKNOWN. */
    public static final int UNKNOWN = 0;

    /** The Constant TOMCAT. */
    public static final int TOMCAT = 1;

    /** The Constant RESIN. */
    public static final int RESIN = 2;

    /** The Constant ORION. */
    public static final int ORION = 3;

    /** The Constant OC4J. */
    public static final int OC4J = 3;

    /** The Constant ORACLE_AS. */
    public static final int ORACLE_AS = 3;

    /** The Constant WEBLOGIC. */
    public static final int WEBLOGIC = 4;

    /** The Constant HPAS. */
    public static final int HPAS = 5;

    /** The Constant JRUN. */
    public static final int JRUN = 6;

    /** The Constant WEBSPHERE. */
    public static final int WEBSPHERE = 7;

    /** The Constant JEUS. */
    public static final int JEUS = 8;

    /** The Constant JBOSS. */
    public static final int JBOSS = 9;

    /** The result. */
    private static int result;
    
    /** The Constant containerString. */
    private static final Map<String, String> containerString = new HashMap<String, String>();

    static {

        initServerString();

        String weblogic  = System.getProperty("weblogic.Name");
        String tomcat    = System.getProperty("catalina.home");
        String jeus      = System.getProperty("jeus.home");
        String jboss     = System.getProperty("program.name");
        String websphere = System.getProperty("ibm.websphere.internalClassAccessMode");

        if (tomcat != null && !tomcat.trim().equals("")) {
            result = TOMCAT;
        } else if (weblogic != null && !weblogic.trim().equals("")) {
            result = WEBLOGIC;
        } else if (jeus != null && !jeus.trim().equals("")) {
            result = JEUS;
        } else if (jboss != null && jboss.startsWith("JBossTools")) {
        	result = JBOSS;        	
        } else if (websphere != null && !websphere.trim().equals("")) {
        	result = WEBSPHERE;
        } else {
            result = 0;
        }

    }

    /**
     * Inits the server string.
     */
    private static void initServerString() {
        containerString.put(String.valueOf(TOMCAT), "TOMCAT");
        containerString.put(String.valueOf(WEBLOGIC), "WEBLOGIC");
        containerString.put(String.valueOf(JEUS), "JEUS");
        containerString.put(String.valueOf(ORACLE_AS), "ORACLE_AS");
        containerString.put(String.valueOf(WEBSPHERE), "WEBSPHERE");
        containerString.put(String.valueOf(JBOSS), "JBOSS");
        containerString.put(String.valueOf(UNKNOWN), "UNKNOWN");
    }

    /**
     * Gets the string.
     * 
     * @param container the container
     * 
     * @return the string
     */
    public static String getString(int container) {
        return (String) containerString.get(String.valueOf(container));
    }

    /**
     * Gets the string.
     * 
     * @return the string
     */
    public static String getString() {
        return getString(get());
    }

    /**
     * Gets the Container.
     * 
     * 다음과 같은 형태로 사용 합니다. {@code 
     * if (ContainerUtil.get() == ContainerUtil.WEBLOGIC) {
     *   ...
     * }
     * }
     * 
     * @return int Container
     */
    public static int get() {
        return result;
    }
    public static String getInstanceName(){
    	switch(result) {
    	case TOMCAT : 
    	case WEBLOGIC : 
    	case JEUS : 
    	case WEBSPHERE : 
    	case JBOSS : 
    	case UNKNOWN : 
    		return System.getProperty("inst.Name");
    	default : 
    		return "";
    	}
  	
    	
    }
}
