-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added base class for file based config classes
- Loading branch information
Showing
3 changed files
with
97 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
ojcms-utils/src/main/java/de/adito/ojcms/utils/config/AbstractFileBasedConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |