public abstract class ReloadingPropertiesConfiguration extends Object
This configuration does not need you to force an application restart, if you changed the properties file.
Based on this blog post.
Example:
final class MyAppConfig extends ReloadingPropertiesConfiguration {
public MyAppConfig(final File file) {
super(file);
}
public String getServiceUrl() {
return getRequiredProperty("service.url");
}
public boolean getShouldStartSlow() {
return getFlag("start-slow", false);
}
public int getHttpPort(final int defaultPort) {
return getInteger("myapp.http.port", defaultPort);
}
}
This class is thread safe because the method reading the file is synchronized.
| Modifier and Type | Field and Description |
|---|---|
private java.nio.file.Path |
configFile
Where the configuration is loaded from.
|
private long |
lastLoad
Time in milliseconds.
|
private Properties |
properties
Holds the configuration values.
|
| Constructor and Description |
|---|
ReloadingPropertiesConfiguration(File file)
Convenience constructor for file.
|
ReloadingPropertiesConfiguration(java.nio.file.Path file)
Dedicated constructor.
|
ReloadingPropertiesConfiguration(String filename)
Convenience constructor for file name as string.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
ensureConfigurationIsFresh()
Reads the property file from disk if the files modification date is after the last one read.
|
boolean |
getFlag(String propertyName,
boolean defaultValue)
Get a boolean property by name.
|
int |
getInteger(String propertyName,
int defaultValue)
Get a integer property by name.
|
private String |
getProperty(String propertyName)
Get the property from the underlying object and ensures it is fresh loaded.
|
String |
getProperty(String propertyName,
String defaultValue)
Get a property by name.
|
String |
getRequiredProperty(String propertyName)
Get a property by name.
|
private final Properties properties
private final java.nio.file.Path configFile
private long lastLoad
Initial -1 so that file will be read anyway on first property access.
public ReloadingPropertiesConfiguration(String filename)
filename - must not be null or emptypublic ReloadingPropertiesConfiguration(File file)
file - must not be nullpublic ReloadingPropertiesConfiguration(java.nio.file.Path file)
file - must not be nullpublic final String getProperty(String propertyName, String defaultValue)
If the property does not exist then the default value will be returned.
propertyName - must not be null or emptydefaultValue - must not be nullnullpublic final String getRequiredProperty(String propertyName)
Throws a {@link RuntimeException if property does not exists.
propertyName - must not be null or emptynullpublic final boolean getFlag(String propertyName, boolean defaultValue)
This method calls getProperty(java.lang.String, java.lang.String). The result will be
passed through Boolean.parseBoolean(java.lang.String).
propertyName - must not be null or emptydefaultValue - must not be nullnullpublic final int getInteger(String propertyName, int defaultValue)
This method calls getProperty(java.lang.String, java.lang.String). The result will be
passed through Integer.parseInt(java.lang.String, int) with radix of 10.
propertyName - must not be null or emptydefaultValue - must not be nullnullprivate String getProperty(String propertyName)
propertyName - must not be null or emptynullprivate void ensureConfigurationIsFresh()
Copyright © 2012 Sven Strittmatter. All Rights Reserved.