package com.kingdee.eas.custom.utils;

import com.kingdee.bos.BOSException;
import org.apache.log4j.Logger;

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

/**
 * @Description 配置工具类
 * @Date 2024/5/21 8:59
 * @Created by Heyuan
 */
public class PropertiesUtil {
    private static Logger logger = Logger.getLogger(PropertiesUtil.class);

    private static Map maps = new HashMap();
    private static String propertiesUrl = System.getProperty("EAS_HOME") + "/server/properties/notifyTodo/propertie.properties";

    static {
        loadConfig();
    }

    /**
     * 加载配置
     */
    public static void loadConfig() {
        Properties props = new Properties();
        try {
            logger.debug("propertiesUrl:" + propertiesUrl);
            InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(propertiesUrl)));
            props.load(InputStream);
        } catch (IOException var4) {
            var4.printStackTrace();
            logger.error("获取自定义配置文件报错: " + var4.getMessage());
        }
        Enumeration en = props.propertyNames();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = props.getProperty(key);
            maps.put(key, value);
            logger.debug(key + " : " + value);
        }
    }

    /**
     * 获取配置属性
     *
     * @param key
     * @return
     */
    public static String getConfigValue(String key) throws BOSException {
        String value = (String) maps.get(key);
        if (com.kingdee.util.StringUtils.isEmpty(value)) {
            throw new BOSException(key + "为空，请检查配置文件（" + propertiesUrl + "）中是否维护" + key + "元素");
        }
        return value;
    }
}
