Skip to content

Commit

Permalink
Added base class for file based config classes
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDan committed Apr 21, 2020
1 parent d516336 commit 4b8f7c3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

/**
Expand All @@ -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";
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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.
*
* @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";
Expand All @@ -19,58 +19,48 @@ 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);
}

/**
* The host of the mail provider to use.
*/
public String getMailHost()
{
return properties.getProperty(KEY_MAIL_HOST);
return readMandatoryProperty(KEY_MAIL_HOST);
}

/**
* The port of the mail provider to use.
*/
public int getMailPort()
{
return Integer.parseInt(properties.getProperty(KEY_MAIL_PORT));
return readMandatoryIntProperty(KEY_MAIL_PORT);
}

/**
* The name of the user from the mail provider to use.
*/
public String getMailUser()
{
return properties.getProperty(KEY_MAIL_USER);
return readMandatoryProperty(KEY_MAIL_USER);
}

/**
* The password of the user from the mail provider to use.
*/
public String getMailPassword()
{
return properties.getProperty(KEY_MAIL_PASSWORD);
return readMandatoryProperty(KEY_MAIL_PASSWORD);
}

/**
* The name of the sender of the restore code.
*/
public String getMailSender()
{
return properties.getProperty(KEY_MAIL_SENDER);
return readMandatoryProperty(KEY_MAIL_SENDER);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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));
}
}

0 comments on commit 4b8f7c3

Please sign in to comment.