All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.valfirst.slf4jtest.OverridableProperties Maven / Gradle / Ivy

There is a newer version: 3.0.1
Show newest version
package com.github.valfirst.slf4jtest;

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

class OverridableProperties {
    private static final Properties EMPTY_PROPERTIES = new Properties();
    private final String propertySourceName;
    private final Properties properties;

    OverridableProperties(final String propertySourceName) throws IOException {
        this.propertySourceName = propertySourceName;
        this.properties = getProperties();
    }

    private Properties getProperties() throws IOException {
        InputStream resourceAsStream =
                Thread.currentThread()
                        .getContextClassLoader()
                        .getResourceAsStream(propertySourceName + ".properties");
        if (resourceAsStream != null) {
            return loadProperties(resourceAsStream);
        }
        return EMPTY_PROPERTIES;
    }

    private static Properties loadProperties(InputStream propertyResource) throws IOException {
        try (InputStream closablePropertyResource = propertyResource) {
            final Properties loadedProperties = new Properties();
            loadedProperties.load(closablePropertyResource);
            return loadedProperties;
        }
    }

    String getProperty(final String propertyKey, final String defaultValue) {
        final String propertyFileProperty = properties.getProperty(propertyKey, defaultValue);
        return System.getProperty(propertySourceName + "." + propertyKey, propertyFileProperty);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy