package no.valg.valgnatt.database.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;

public final class ConfigurationLoader {

    private static Properties properties = new Properties();

    private ConfigurationLoader() {
        //Hide default constructor
    }

    public static void load() {
        String configuration = System.getProperty("configuration");
        if (configuration == null) {
            throw new RuntimeException("Det mangler en VM-option ved kjøring av dette programmet som angir en konfigurasjonsfil. Syntaks: -Dconfiguration=classpath:<filnavn>");
        }
        Resource resource = new DefaultResourceLoader().getResource(configuration);
        try {
            InputStream is = resource.getInputStream();
            properties.load(is);
            is.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Properties getProperties() {
        return properties;
    }

    public static String getProperty(String key) {
        return properties.getProperty(key);
    }

}
