Class Example

package rest.api.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration {

    protected static Configuration instance = new Configuration();

    private static final String CONFIG_FILE_NAME = "src/test/resources/config.properties";

    private static Properties properties = new Properties();

    static {
        loadProperties();

        // override the set properties with command line and system properties
        addProperties(System.getProperties());
    }

    /**
     * Get the String value of the property.
     * 
     * @param key
     * @param defaultValue
     * @return The String value of the property that is set or the defaultValue
     *         if it is not set.
     */
    public static String getValue(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

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

    /**
     * Get the int value of a property.
     *
     * @param key
     * @param defaultValue
     * @return The int value of the property that is set or the defaultValue if
     *         it is not set.
     * @throws ConfigurationException
     *             - if the string does not contain a parsable integer.
     */
    public static int getInt(String key, int defaultValue) {
        // not the most efficient but effective
        String defaultInt = Integer.toString(defaultValue);
        return Integer.parseInt(properties.getProperty(key, defaultInt));

    }

    protected static void loadProperties() {
        InputStream input = null;

        File config = new File(System.getProperty("user.dir") + "/config.properties");

        try {
            if (config.exists()) {
                // read a config.properties which is at same dir with JAR
                System.out.println("Load Out Constant Config ...");
                properties.load(new FileInputStream(config));
            } else {
                properties.load(new FileInputStream(CONFIG_FILE_NAME));
                System.out.println("Load Default Constant Config ...");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // We do this for testability
    protected static void addProperties(Properties props) {
        properties.putAll(props);
    }

}