package com.nokia.sms.sgip;

import java.net.URLDecoder;

import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SgipConfig {
    private static CompositeConfiguration config;
    private static String configFile = "sgipConfig.properties";

    public static void setConfigFile(String configFile) {
        SgipConfig.configFile = configFile;
    }

    public static synchronized CompositeConfiguration getConfig() {
        if (config == null) {
            try {
                config = new CompositeConfiguration();
                if (configFile == null) {
                    configFile = SgipConfig.class.getResource("/sgipConfig.properties").getFile();
                }
                configFile = URLDecoder.decode(configFile, "utf-8");
                PropertiesConfiguration propsConfig = new PropertiesConfiguration();
                propsConfig.setEncoding("UTF-8");
                propsConfig.load(configFile);
                FileChangedReloadingStrategy propsReload = new FileChangedReloadingStrategy();
                propsReload.setRefreshDelay(60000L);
                propsConfig.setReloadingStrategy(propsReload);
                config.addConfiguration(propsConfig);
            } catch (Exception ex) {
                log.error("", ex);
            }
        }
        return config;
    }
}
