diff --git a/README.md b/README.md index 74b8cb2..3b05980 100644 --- a/README.md +++ b/README.md @@ -69,3 +69,7 @@ Regarding `language.languageSetting` please check [languages](https://github.com/gaborbata/jpass/tree/master/src/main/resources/resources/languages) resources folder for possible configuration values. +Each configuration property can be overridden by system properties, with the `jpass.` key prefix, e.g. + + java -Djpass.entry.details=TITLE -jar jpass-1.0.6-SNAPSHOT.jar + diff --git a/src/main/java/jpass/util/Configuration.java b/src/main/java/jpass/util/Configuration.java index d1f0010..7f86f94 100644 --- a/src/main/java/jpass/util/Configuration.java +++ b/src/main/java/jpass/util/Configuration.java @@ -31,12 +31,18 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.util.Optional; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; /** - * Class for loading configurations from {@code jpass.properties}. + * Class for loading configurations from {@code jpass.properties} or system properties. + * + *

+ * Each configuration property can be overridden by system properties, + * with the {@code jpass.} prefix. + *

* * @author Gabor_Bata */ @@ -75,7 +81,7 @@ private File getConfigurationFolderPath() { private T getValue(String key, T defaultValue, Class type) { T value = defaultValue; - String prop = properties.getProperty(key); + String prop = getProperty(key); if (prop != null) { try { value = type.getConstructor(String.class).newInstance(prop); @@ -86,6 +92,11 @@ private T getValue(String key, T defaultValue, Class type) { return value; } + private String getProperty(String key) { + return Optional.ofNullable(System.getProperty(String.format("jpass.%s", key))) + .orElseGet(() -> properties.getProperty(key)); + } + public Boolean is(String key, Boolean defaultValue) { return getValue(key, defaultValue, Boolean.class); } @@ -95,11 +106,12 @@ public Integer getInteger(String key, Integer defaultValue) { } public String get(String key, String defaultValue) { - return properties.getProperty(key, defaultValue); + String prop = getProperty(key); + return prop != null ? prop : defaultValue; } public String[] getArray(String key, String[] defaultValue) { - String prop = properties.getProperty(key); + String prop = getProperty(key); if (prop != null) { return prop.split(","); }