From e5740ee0412062332a2c689603f0bb1f0191d0da Mon Sep 17 00:00:00 2001 From: Gabor Bata Date: Mon, 25 Mar 2024 10:31:33 +0100 Subject: [PATCH] add possibility to override config via system properties --- README.md | 4 ++++ src/main/java/jpass/util/Configuration.java | 20 ++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) 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(","); }