public class Config extends Object
There is one Config
instance available (lazily created, cached temporarily) for every
directory and every file in a repository. Each Config
inherits the settings from the
parent-directory, if not explicitly overwritten.
The configuration is based on Properties
files. Every property file is optional. If it
does not exist, all settings are inherited. If it does exist, only those properties contained in
the file are overriden. All properties not contained in the file are still inherited. Inheritance
is thus applicable on every individual property.
Modifications, deletions, creations of properties files are detected during runtime (pretty immediately). Note, that this detection is based on the files' timestamps. Since most file systems have a granularity of 1 second (some even 2) for the last-modified-timestamp, multiple modifications in the same second might not be detected.
There is a global properties file in the user's home directory (or wherever ConfigDir
points to): /home/tomcat/.cloudstore/cloudstore.properties
Additionally, every directory can optionally contain the following files:
.cloudstore.properties
cloudstore.properties
.${anyFileName}.cloudstore.properties
${anyFileName}.cloudstore.properties
The files 1. and 2. are applicable to the entire directory and all sub-directories and files in it. Usually, on GNU/Linux people will prefer 1., but when using Windows, files starting with a "." are sometimes a bit hard to deal with. Therefore, we support both. The file 2. overrides the settings of file 1..
The files 3. and 4. are applicable only to the file ${anyFileName}
. Thus, if you want
to set special behaviour for the file example.db
only, you can create the file
.example.db.cloudstore.properties
in the same directory.
Modifier and Type | Field and Description |
---|---|
protected File[] |
propertiesFiles |
static String |
SYSTEM_PROPERTY_PREFIX
Prefix used for system properties overriding configuration entries.
|
Modifier and Type | Method and Description |
---|---|
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 . |
String |
getProperty(String key,
String defaultValue)
Gets the property identified by the given key.
|
boolean |
getPropertyAsBoolean(String key,
boolean defaultValue) |
<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) |
public static final String SYSTEM_PROPERTY_PREFIX
Every property in the configuration (i.e. in its properties files) can be overridden by a corresponding system property. The system property must be prefixed.
For example, to override the configuration property with the key "deferrableExecutor.timeout", you can pass the system property "cloudstore.deferrableExecutor.timeout" to the JVM. If the system property exists, the configuration is not consulted, but the system property value is used as shortcut.
protected final 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 String getProperty(String key, String defaultValue)
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
getPropertyAsNonEmptyTrimmedString(String, String)
instead!
Every property can be overwritten by a system property prefixed with "cloudstore.". 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!
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
.getPropertyAsNonEmptyTrimmedString(String, String)
public String getPropertyAsNonEmptyTrimmedString(String key, String defaultValue)
In contrast to 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
getPropertyAsLong(String, long)
and all other getPropertyAs...(...)
methods.
Every property can be overwritten by a system property prefixed with "cloudstore.". 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!
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)
public long getPropertyAsPositiveOrZeroLong(String key, long defaultValue)
public int getPropertyAsInt(String key, int defaultValue)
public int getPropertyAsPositiveOrZeroInt(String key, int defaultValue)
public <E extends Enum<E>> E getPropertyAsEnum(String key, E defaultValue)
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 getPropertyAsEnum(String, Class, Enum)
instead!null
.getPropertyAsEnum(String, Class, Enum)
,
getPropertyAsNonEmptyTrimmedString(String, String)
public <E extends Enum<E>> E getPropertyAsEnum(String key, Class<E> enumClass, E defaultValue)
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
.getPropertyAsEnum(String, Enum)
,
getPropertyAsNonEmptyTrimmedString(String, String)
public boolean getPropertyAsBoolean(String key, boolean defaultValue)
Copyright © 2013-2014. All Rights Reserved.