Skip to content

Commit

Permalink
add possibility to override config via system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbata committed Mar 25, 2024
1 parent d55bfd0 commit e5740ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

20 changes: 16 additions & 4 deletions src/main/java/jpass/util/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* Each configuration property can be overridden by system properties,
* with the {@code jpass.} prefix.
* </p>
*
* @author Gabor_Bata
*/
Expand Down Expand Up @@ -75,7 +81,7 @@ private File getConfigurationFolderPath() {

private <T> T getValue(String key, T defaultValue, Class<T> type) {
T value = defaultValue;
String prop = properties.getProperty(key);
String prop = getProperty(key);
if (prop != null) {
try {
value = type.getConstructor(String.class).newInstance(prop);
Expand All @@ -86,6 +92,11 @@ private <T> T getValue(String key, T defaultValue, Class<T> 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);
}
Expand All @@ -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(",");
}
Expand Down

0 comments on commit e5740ee

Please sign in to comment.