1 package com.github.valfirst.slf4jtest; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.UncheckedIOException; 6 import java.util.Properties; 7 8 class OverridableProperties { 9 private static final Properties EMPTY_PROPERTIES = new Properties(); 10 private final String propertySourceName; 11 private final Properties properties; 12 13 OverridableProperties(final String propertySourceName) throws IOException { 14 this.propertySourceName = propertySourceName; 15 this.properties = getProperties(); 16 } 17 18 private Properties getProperties() throws IOException { 19 InputStream resourceAsStream = 20 Thread.currentThread() 21 .getContextClassLoader() 22 .getResourceAsStream(propertySourceName + ".properties"); 23 if (resourceAsStream != null) { 24 return loadProperties(resourceAsStream); 25 } 26 return EMPTY_PROPERTIES; 27 } 28 29 private static Properties loadProperties(InputStream propertyResource) throws IOException { 30 try (InputStream closablePropertyResource = propertyResource) { 31 final Properties loadedProperties = new Properties(); 32 loadedProperties.load(closablePropertyResource); 33 return loadedProperties; 34 } 35 } 36 37 String getProperty(final String propertyKey, final String defaultValue) { 38 final String propertyFileProperty = properties.getProperty(propertyKey, defaultValue); 39 return System.getProperty(propertySourceName + "." + propertyKey, propertyFileProperty); 40 } 41 42 public static OverridableProperties createUnchecked(final String propertySourceName) { 43 try { 44 return new OverridableProperties(propertySourceName); 45 } catch (IOException e) { 46 throw new UncheckedIOException(e); 47 } 48 } 49 }