public class ConfigImpl extends Object implements Config
See Config
.
Modifier and Type | Field and Description |
---|---|
protected Properties |
properties |
protected File[] |
propertiesFiles |
APP_ID_SIMPLE_ID, PROPERTIES_FILE_NAME_FOR_DIRECTORY, PROPERTIES_FILE_NAME_FOR_DIRECTORY_LOCAL, PROPERTIES_FILE_NAME_PARENT, PROPERTIES_FILE_NAME_PARENT_PREFIX, PROPERTIES_FILE_NAME_SUFFIX, SYSTEM_PROPERTY_PREFIX
Modifier | Constructor and Description |
---|---|
protected |
ConfigImpl(ConfigImpl parentConfig,
File file,
File[] propertiesFiles) |
Modifier and Type | Method and Description |
---|---|
String |
getDirectProperty(String key)
Gets the property identified by the given key; not taking inheritance into account.
|
protected File |
getFile()
Get the directory or file for which this Config instance is responsible.
|
static Config |
getInstance()
Gets the global
Config for the current user. |
static Config |
getInstanceForDirectory(File directory)
Gets the
Config for the given directory . |
static Config |
getInstanceForFile(File file)
Gets the
Config for the given file . |
Map<String,List<String>> |
getKey2GroupsMatching(Pattern regex)
Gets all config-property-keys matching the given regular expression.
|
String |
getProperty(String key,
String defaultValue)
Gets the property identified by the given key.
|
boolean |
getPropertyAsBoolean(String key,
boolean defaultValue) |
Date |
getPropertyAsDate(String key,
Date defaultValue)
Gets the property identified by the given key.
|
<E extends Enum<E>> |
getPropertyAsEnum(String key,
Class<E> enumClass,
E defaultValue)
Gets the property identified by the given key.
|
<E extends Enum<E>> |
getPropertyAsEnum(String key,
E defaultValue)
Gets the property identified by the given key.
|
int |
getPropertyAsInt(String key,
int defaultValue) |
long |
getPropertyAsLong(String key,
long defaultValue) |
String |
getPropertyAsNonEmptyTrimmedString(String key,
String defaultValue)
Gets the property identified by the given key; trimmed.
|
int |
getPropertyAsPositiveOrZeroInt(String key,
int defaultValue) |
long |
getPropertyAsPositiveOrZeroLong(String key,
long defaultValue) |
long |
getVersion()
Gets a version number that is guaranteed to be changed whenever the underlying files change.
|
protected void |
populateKeysMatching(Map<String,List<String>> key2Groups,
Pattern regex) |
void |
setDirectProperty(String key,
String value)
Sets the property identified by the given key; not taking inheritance into account.
|
protected final File[] propertiesFiles
protected final Properties properties
protected ConfigImpl(ConfigImpl parentConfig, File file, File[] propertiesFiles)
protected File getFile()
null
, if already
garbage-collected or if this is the root-parent-Config. We try to make garbage-collection extremely unlikely
as long as the Config is held in memory.public static Config getInstance()
Config
for the current user.Config
for the current user. Never null
.public static Config getInstanceForDirectory(File directory)
Config
for the given directory
.directory
- a directory inside a repository. Must not be null
.
The directory does not need to exist (it may be created later).Config
for the given directory
. Never null
.public static Config getInstanceForFile(File file)
Config
for the given file
.file
- a file inside a repository. Must not be null
.
The file does not need to exist (it may be created later).Config
for the given file
. Never null
.public long getVersion()
Config
It is not guaranteed to be incremented! Depending on the underlying change, a newer version number might be less than a previous version number! In most cases, however, the version number actually grows with each change. Code must not rely on this, but it is a helpful assumption for debugging.
getVersion
in interface Config
public String getProperty(String key, String defaultValue)
Config
This method directly delegates to Properties.getProperty(String, String)
.
Thus, an empty String in the internal Properties
is returned instead of the
given defaultValue
. The defaultValue
is only returned, if neither
the internal Properties
of this Config
nor any of its parents contains
the entry.
Important: This is often not the desired behaviour. You might want to use
Config.getPropertyAsNonEmptyTrimmedString(String, String)
instead!
Every property can be overwritten by a system property prefixed with . If - for example - the key "updater.force" is to be read and a system property named "cloudstore.updater.force" is set, this system property is returned instead!
getProperty
in interface Config
key
- the key identifying the property. Must not be null
.defaultValue
- the default value to fall back to, if neither this Config
's
internal Properties
nor any of its parents contains a matching entry.
May be null
.null
unless defaultValue
is null
.Config.getPropertyAsNonEmptyTrimmedString(String, String)
public String getDirectProperty(String key)
Config
This method corresponds to Config.getProperty(String, String)
, but it does not fall back
to any inherited value.
Important: This method should never be used in order to control the behaviour of the application! It is intended for use of administrative tools / UIs which need to read/write directly.
getDirectProperty
in interface Config
key
- the key identifying the property. Must not be null
.null
, if the property is not set.Config.setDirectProperty(String, String)
public void setDirectProperty(String key, String value)
Config
setDirectProperty
in interface Config
key
- the key identifying the property. Must not be null
.value
- the property's value. null
removes the property from this concrete
configuration instance.Config.getDirectProperty(String)
public String getPropertyAsNonEmptyTrimmedString(String key, String defaultValue)
Config
In contrast to Config.getProperty(String, String)
, this method falls back to the given
defaultValue
, if the internal Properties
contains an empty String
(after trimming) as value for the given key
.
It therefore means that a value set to an empty String
in the properties file means
to use the program's default instead. It is therefore consistent with
Config.getPropertyAsLong(String, long)
and all other getPropertyAs...(...)
methods.
The same rules apply to the fall-back-strategy from system property to environment variable and finally config files.
Every property can be overwritten by a system property prefixed with . If - for example - the key "updater.force" is to be read and a system property named "cloudstore.updater.force" is set, this system property is returned instead!
getPropertyAsNonEmptyTrimmedString
in interface Config
key
- the key identifying the property. Must not be null
.defaultValue
- the default value to fall back to, if neither this Config
's
internal Properties
nor any of its parents contains a matching entry or
if this entry's value is an empty String
.
May be null
.null
unless defaultValue
is null
.public long getPropertyAsLong(String key, long defaultValue)
getPropertyAsLong
in interface Config
public long getPropertyAsPositiveOrZeroLong(String key, long defaultValue)
getPropertyAsPositiveOrZeroLong
in interface Config
public int getPropertyAsInt(String key, int defaultValue)
getPropertyAsInt
in interface Config
public int getPropertyAsPositiveOrZeroInt(String key, int defaultValue)
getPropertyAsPositiveOrZeroInt
in interface Config
public <E extends Enum<E>> E getPropertyAsEnum(String key, E defaultValue)
Config
getPropertyAsEnum
in interface Config
key
- the key identifying the property. Must not be null
.defaultValue
- the default value to fall back to, if neither this Config
's
internal Properties
nor any of its parents contains a matching entry or
if this entry's value does not match any possible enum value. Must not be null
.
If a null
default value is required, use Config.getPropertyAsEnum(String, Class, Enum)
instead!null
.Config.getPropertyAsEnum(String, Class, Enum)
,
Config.getPropertyAsNonEmptyTrimmedString(String, String)
public <E extends Enum<E>> E getPropertyAsEnum(String key, Class<E> enumClass, E defaultValue)
Config
getPropertyAsEnum
in interface Config
key
- the key identifying the property. Must not be null
.enumClass
- the enum's type. Must not be null
.defaultValue
- the default value to fall back to, if neither this Config
's
internal Properties
nor any of its parents contains a matching entry or
if this entry's value does not match any possible enum value. May be null
.null
unless defaultValue
is null
.Config.getPropertyAsEnum(String, Enum)
,
Config.getPropertyAsNonEmptyTrimmedString(String, String)
public boolean getPropertyAsBoolean(String key, boolean defaultValue)
getPropertyAsBoolean
in interface Config
public Date getPropertyAsDate(String key, Date defaultValue)
Config
The date must be ISO8601
-parseable. If it is not parseable, a warning is logged and the
default value is returned.
getPropertyAsDate
in interface Config
key
- the key identifying the property. Must not be null
.defaultValue
- the default value to fall back to, if neither this Config
's
internal Properties
nor any of its parents contains a matching entry or
if this entry's value cannot be parsed as an ISO8601
-encoded date+time value.
May be null
.null
unless defaultValue
is null
.public Map<String,List<String>> getKey2GroupsMatching(Pattern regex)
Config
Just like Config.getProperty(String, String)
, this method takes inheritance into account:
It collects keys from the current Config
instance and all its parents, recursively.
Note, that Matcher.matches()
is used, i.e. the regex
must match the entire
config-key.
The given regex
may contain capturing groups, whose results are returned in the Map
's
values. Note, that the entire match (which is by convention the group 0) is ignored in the values,
because it is the Map
's key, already. Hence, the first entry in the Map
's values
(with index 0) corresponds to the first capturing group in the regular expression.
For example, let's say the regex is "ignore\[([^]]*)\]\.(.*)" and the following matching properties exist:
This leads to a resulting map with the following entries:
getKey2GroupsMatching
in interface Config
regex
- the regular expression to look for. Must not be null
.null
.Copyright © 2013–2019. All rights reserved.