package com.lightinit.hsdataplatform.common;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by WangYao on 2018/6/13.
 */
public class ConfigUtils {

    private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);

    private Configuration configuration=null;
    private static final Map<String, Configuration> configurationMap = new HashMap<>();

    public static Configuration getConfiguration(String propertiesFile){
        Configuration configuration = null;
        if (!configurationMap.containsKey(propertiesFile)){
            try {
                Configurations configurations = new Configurations();
                configuration = configurations.properties(new File(propertiesFile));
                configurationMap.put(propertiesFile, configuration);
            } catch (ConfigurationException e) {
                logger.error(e.getMessage());
                return null;
            }
        }
        return configurationMap.get(propertiesFile);
    }
}
