From 4b8f7c3df93b22106ff2333fb90e2032c58ace19 Mon Sep 17 00:00:00 2001 From: Simon Danner Date: Tue, 21 Apr 2020 20:43:32 +0200 Subject: [PATCH] Added base class for file based config classes --- .../rest/config/FileBasedDatabaseConfig.java | 13 ++- .../ojcms/rest/config/RestoreMailConfig.java | 28 ++----- .../utils/config/AbstractFileBasedConfig.java | 83 +++++++++++++++++++ 3 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 ojcms-utils/src/main/java/de/adito/ojcms/utils/config/AbstractFileBasedConfig.java diff --git a/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/FileBasedDatabaseConfig.java b/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/FileBasedDatabaseConfig.java index e72211d..3682789 100644 --- a/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/FileBasedDatabaseConfig.java +++ b/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/FileBasedDatabaseConfig.java @@ -3,10 +3,9 @@ import de.adito.ojcms.sql.datasource.connection.IDatabaseConfig; import de.adito.ojcms.sqlbuilder.platform.*; import de.adito.ojcms.sqlbuilder.platform.connection.*; +import de.adito.ojcms.utils.config.AbstractFileBasedConfig; import javax.enterprise.context.ApplicationScoped; -import java.io.IOException; -import java.nio.file.*; import java.util.*; /** @@ -17,9 +16,9 @@ * @author Simon Danner, 12.01.2020 */ @ApplicationScoped -class FileBasedDatabaseConfig implements IDatabaseConfig +class FileBasedDatabaseConfig extends AbstractFileBasedConfig implements IDatabaseConfig { - private static final String FILE_NAME = "ojcms_database.properties"; + private static final String CONFIG_PATH = "ojcms_database.properties"; private static final String KEY_PLATFORM = "platform"; private static final String KEY_EMBEDDED = "embedded"; private static final String KEY_HOST = "host"; @@ -34,11 +33,9 @@ class FileBasedDatabaseConfig implements IDatabaseConfig * Initializes the filed based config. * Loads all relevant properties from the config file. */ - FileBasedDatabaseConfig() throws IOException + FileBasedDatabaseConfig() { - final Properties properties = new Properties(); - properties.load(Files.newInputStream(Paths.get(FILE_NAME))); - + super(CONFIG_PATH); final String platform = _loadProperty(KEY_PLATFORM, properties, true).toUpperCase(); final boolean embedded = _resolveEmbedded(properties); diff --git a/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/RestoreMailConfig.java b/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/RestoreMailConfig.java index dd1ba3b..22eca8d 100644 --- a/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/RestoreMailConfig.java +++ b/ojcms-rest/ojcms-rest-server/src/main/java/de/adito/ojcms/rest/config/RestoreMailConfig.java @@ -1,8 +1,8 @@ package de.adito.ojcms.rest.config; +import de.adito.ojcms.utils.config.AbstractFileBasedConfig; + import javax.enterprise.context.ApplicationScoped; -import java.io.*; -import java.util.Properties; /** * File based configuration for the mail provider to send user restore codes. @@ -10,7 +10,7 @@ * @author Simon Danner, 30.11.2019 */ @ApplicationScoped -public class RestoreMailConfig +public class RestoreMailConfig extends AbstractFileBasedConfig { private static final String CONFIG_PATH = "ojcms_restore_mail.properties"; private static final String KEY_MAIL_HOST = "MAIL_HOST"; @@ -19,19 +19,9 @@ public class RestoreMailConfig private static final String KEY_MAIL_PASSWORD = "MAIL_PASSWORD"; private static final String KEY_MAIL_SENDER = "MAIL_SENDER"; - private final Properties properties; - public RestoreMailConfig() { - try - { - properties = new Properties(); - properties.load(new FileInputStream(CONFIG_PATH)); - } - catch (IOException pE) - { - throw new RuntimeException("Unable to load properties! Provide config file: " + CONFIG_PATH + "!", pE); - } + super(CONFIG_PATH); } /** @@ -39,7 +29,7 @@ public RestoreMailConfig() */ public String getMailHost() { - return properties.getProperty(KEY_MAIL_HOST); + return readMandatoryProperty(KEY_MAIL_HOST); } /** @@ -47,7 +37,7 @@ public String getMailHost() */ public int getMailPort() { - return Integer.parseInt(properties.getProperty(KEY_MAIL_PORT)); + return readMandatoryIntProperty(KEY_MAIL_PORT); } /** @@ -55,7 +45,7 @@ public int getMailPort() */ public String getMailUser() { - return properties.getProperty(KEY_MAIL_USER); + return readMandatoryProperty(KEY_MAIL_USER); } /** @@ -63,7 +53,7 @@ public String getMailUser() */ public String getMailPassword() { - return properties.getProperty(KEY_MAIL_PASSWORD); + return readMandatoryProperty(KEY_MAIL_PASSWORD); } /** @@ -71,6 +61,6 @@ public String getMailPassword() */ public String getMailSender() { - return properties.getProperty(KEY_MAIL_SENDER); + return readMandatoryProperty(KEY_MAIL_SENDER); } } diff --git a/ojcms-utils/src/main/java/de/adito/ojcms/utils/config/AbstractFileBasedConfig.java b/ojcms-utils/src/main/java/de/adito/ojcms/utils/config/AbstractFileBasedConfig.java new file mode 100644 index 0000000..be1ff56 --- /dev/null +++ b/ojcms-utils/src/main/java/de/adito/ojcms/utils/config/AbstractFileBasedConfig.java @@ -0,0 +1,83 @@ +package de.adito.ojcms.utils.config; + +import de.adito.ojcms.utils.StringUtility; + +import java.io.*; +import java.util.*; + +/** + * Base class for file based config classes. + * + * @author Simon Danner, 21.04.2020 + */ +public abstract class AbstractFileBasedConfig +{ + protected final Properties properties; + + /** + * Initializes the config by loading the properties from the provided path. + */ + protected AbstractFileBasedConfig(String pConfigPath) + { + try + { + StringUtility.requireNotEmpty(pConfigPath, "config path"); + properties = new Properties(); + properties.load(new FileInputStream(pConfigPath)); + } + catch (IOException pE) + { + throw new RuntimeException("Unable to load properties! Provide config file: " + pConfigPath + "!", pE); + } + } + + /** + * Reads a string based property value. Throws an {@link IllegalArgumentException} if the property is not set. + * + * @param pPropertyName the name of the property to read + * @return the value of the property + */ + protected String readMandatoryProperty(String pPropertyName) + { + return readOptionalProperty(pPropertyName) // + .orElseThrow(() -> new IllegalArgumentException("Property " + pPropertyName + "not set!")); + } + + /** + * Tries to read a string based property value. + * + * @param pPropertyName the name of the property to read + * @return the value of the property or empty if not set + */ + protected Optional readOptionalProperty(String pPropertyName) + { + return Optional.ofNullable(properties.getProperty(pPropertyName)); + } + + /** + * Reads an int property value. Throws an {@link IllegalArgumentException} if the property is not set. + * + * @param pPropertyName the name of the property to read + * @return the value of the property + */ + protected int readMandatoryIntProperty(String pPropertyName) + { + return readOptionalIntProperty(pPropertyName) // + .orElseThrow(() -> new IllegalArgumentException("Property " + pPropertyName + "not set!")); + } + + /** + * Tries to read an int property value. + * + * @param pPropertyName the name of the property to read + * @return the value of the property or empty if not set + */ + protected OptionalInt readOptionalIntProperty(String pPropertyName) + { + final String value = properties.getProperty(pPropertyName); + if (value == null) + return OptionalInt.empty(); + + return OptionalInt.of(Integer.parseInt(value)); + } +}