package org.ccpit.base.utils;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description TODO【用一句话描述该文件做什么】
 * @Author dingwei
 * @Date 2015年4月1日 上午10:00:02
 * @Version V0.1
 */
public class ReadConfigUtil {

    private static final String PLATFORM_CONFIG_PROPERTIES_NAME = "framework.properties";
    protected static final Logger log = LoggerFactory
            .getLogger(ReadConfigUtil.class);

    private static String propertiesName = "";

    private static Properties properties = null;

    public static String getPropertiesName() {
        return propertiesName;
    }

    public static void setPropertiesName(String propertiesName) {
        propertiesName = propertiesName;
    }

    public static Map<String, String> getPlatformConfig() {
        Map resultMap = new HashMap();
        try {
            if (properties == null) {
                properties = loadPlatformConfigProperties();
            }
            if (properties != null)
                for (Enumeration en = properties.propertyNames(); en
                        .hasMoreElements();) {
                    String key = (String) en.nextElement();
                    String value = properties.getProperty(key);

                    resultMap.put(key, value);
                }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("读取配置文件错误！！！");
        }
        return resultMap;
    }

    public static Map<String, String> getPlatformConfig(String[] key) {
        Map result = new HashMap();
        if (key.length == 0)
            return result;
        try {
            if (properties == null) {
                properties = loadPlatformConfigProperties();
            }
            String[] arrayOfString = key;
            int j = key.length;
            for (int i = 0; i < j; i++) {
                String tempkey = arrayOfString[i];
                result.put(tempkey, properties.getProperty(tempkey));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("读取配置文件错误！！！");
        }
        return result;
    }

    public static String getPlatformConfig(String key) {
        String value = null;
        try {
            if (properties == null) {
                properties = loadPlatformConfigProperties();
            }
            value = properties.getProperty(key);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("读取配置文件错误！！！");
        }
        return value;
    }

    public static Properties loadPlatformConfigProperties() {
        try {
            InputStream properInput = null;
            if ((propertiesName == null) || (propertiesName.equals("")))
                properInput = ReadConfigUtil.class.getClassLoader()
                        .getResourceAsStream("framework.properties");
            else {
                properInput = ReadConfigUtil.class.getClassLoader()
                        .getResourceAsStream(propertiesName);
            }
            Properties properties = new Properties();
            properties.load(properInput);
            return properties;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("读取配置文件错误！！！");
        }
        return null;
    }

    public static Properties loadPlatformConfigProperties(
            String propertiesFileName) {
        if (StringUtils.isEmpty(propertiesFileName))
            propertiesFileName = "framework.properties";
        try {
            InputStream properInput = ReadConfigUtil.class.getClassLoader()
                    .getResourceAsStream(propertiesFileName);
            Properties properties = new Properties();
            properties.load(properInput);
            return properties;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("读取配置文件错误！！！");
        }
        return null;
    }
}
