Class Settings
- java.lang.Object
-
- dk.netarkivet.common.utils.Settings
-
public class Settings extends Object
Provides access to general application settings. The settings are retrieved from xml files. XML files may be specified one of two places: 1) Default settings in XML files, specified by class path. These are intended to be packaged in the jar files, to provide a fallback for settings. 2) Overriding settings in XML files in file systems. These are intended to override the necessary values with minimal XML files. The location of these files are either specified by the system propertySETTINGS_FILE_PROPERTY
, multiple files can be separated byFile.pathSeparator
, that is ':' on linux and ';' on windows; or if that property is not set, the default location isDEFAULT_SETTINGS_FILEPATH
.
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SETTINGS_FILEPATH
The file path to look for settings in, if the system propertySETTINGS_FILE_PROPERTY
is not set.static String
SETTINGS_FILE_PROPERTY
This system property specifies alternative position(s) to look for settings files.
-
Constructor Summary
Constructors Constructor Description Settings()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addDefaultClasspathSettings(String defaultClasspathSettingsPath)
Add the settings file represented by this path to the list of default classpath settings.static String
get(String key)
Gets a setting.static String[]
getAll(String key)
Gets a list of settings.static boolean
getBoolean(String key)
Gets a setting as a boolean.static double
getDouble(String key)
Gets a setting as a double.static File
getFile(String key)
Gets a setting as a file.static int
getInt(String key)
Gets a setting as an int.static long
getLong(String key)
Gets a setting as a long.static List<File>
getSettingsFiles()
Return the file these settings are read from.static StringTree<String>
getTree(String path)
Get a tree view of a part of the settings.static void
reload()
Reloads the settings.static void
set(String key, String... values)
Sets the key to one or more values.static boolean
verifyClass(String classname)
Verify, if the given class exists in the classpath.
-
-
-
Field Detail
-
SETTINGS_FILE_PROPERTY
public static final String SETTINGS_FILE_PROPERTY
This system property specifies alternative position(s) to look for settings files. If more files are specified, they should be separated byFile.pathSeparatorChar
- See Also:
- Constant Field Values
-
DEFAULT_SETTINGS_FILEPATH
public static final String DEFAULT_SETTINGS_FILEPATH
The file path to look for settings in, if the system propertySETTINGS_FILE_PROPERTY
is not set.- See Also:
- Constant Field Values
-
-
Method Detail
-
getSettingsFiles
public static List<File> getSettingsFiles()
Return the file these settings are read from. If the property given in the constructor is set, that will be used to determine the file. If it is not set, the default settings file path given in the constructor will be used.- Returns:
- The settings file.
-
get
public static String get(String key) throws UnknownID, IOFailure, ArgumentNotValid
Gets a setting. The search order for a given setting is as follows:First it is checked, if the argument key is set as a System property. If yes, return this value. If no, we continue the search.
Secondly, we check, if the setting is in one of the loaded settings xml files. If the value is there, it is returned. If no, we continue the search.
Finally, we check if the setting is in one of default settings files from classpath. If the value is there, it is returned. Otherwise an UnknownId exception is thrown.
Note: The retrieved value can be the empty string
- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved value
- Throws:
ArgumentNotValid
- if key is null or the empty stringUnknownID
- if no setting loaded matches keyIOFailure
- if IO Failure
-
getInt
public static int getInt(String key) throws UnknownID, ArgumentNotValid
Gets a setting as an int. This method calls get(key) and then parses the value as integer.- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved int
- Throws:
ArgumentNotValid
- if key is null, the empty string or key is not parseable as an integerUnknownID
- if no setting loaded matches key
-
getLong
public static long getLong(String key) throws UnknownID, ArgumentNotValid
Gets a setting as a long. This method calls get(key) and then parses the value as a long.- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved long
- Throws:
ArgumentNotValid
- if key is null, the empty string or key is not parseable as a longUnknownID
- if no setting loaded matches key
-
getDouble
public static double getDouble(String key) throws UnknownID, ArgumentNotValid
Gets a setting as a double. This method calls get(key) and then parses the value as a double.- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved double
- Throws:
ArgumentNotValid
- if key is null, the empty string or key is not parseable as a doubleUnknownID
- if no setting loaded matches key
-
getFile
public static File getFile(String key)
Gets a setting as a file. This method calls get(key) and then returns the value as a file.- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved file
- Throws:
ArgumentNotValid
- if key is null, the empty stringUnknownID
- if no setting loaded matches ke
-
getBoolean
public static boolean getBoolean(String key) throws UnknownID, ArgumentNotValid
Gets a setting as a boolean. This method calls get(key) and then parses the value as a boolean.- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved boolean
- Throws:
ArgumentNotValid
- if key is null or the empty stringUnknownID
- if no setting loaded matches key
-
getAll
public static String[] getAll(String key) throws UnknownID, ArgumentNotValid
Gets a list of settings. First it is checked, if the key is registered as a System property. If yes, registered value is returned in a list of length 1. If no, the data loaded from the settings xml files are examined. If value is there, it is returned in a list. If not, the default settings from classpath are examined. If values for this setting are found here, they are returned. Otherwise, an UnknownId exception is thrown.Note that the values will not be concatenated, the first place with a match will define the entire list. Furthemore the list cannot be empty.
- Parameters:
key
- name of the setting to retrieve- Returns:
- the retrieved values (as a non-empty String array)
- Throws:
ArgumentNotValid
- if key is null or the empty stringUnknownID
- if no setting loaded matches key
-
set
public static void set(String key, String... values)
Sets the key to one or more values. Calls to this method are forgotten whenever thereload()
is executed.TODO write these values to its own simpleXml structure, that are not reset during reload.
- Parameters:
key
- The settings key to add this under, legal keys are fields in this class.values
- The (ordered) list of values to put under this key.- Throws:
ArgumentNotValid
- if key or values are nullUnknownID
- if the key does not already exist
-
reload
public static void reload()
Reloads the settings. This will reload the settings from disk, and forget all settings that were set withset(java.lang.String, java.lang.String...)
The field
lastModified
is updated to timestamp of the settings file that has been changed most recently.- Throws:
IOFailure
- if settings cannot be loaded
-
addDefaultClasspathSettings
public static void addDefaultClasspathSettings(String defaultClasspathSettingsPath)
Add the settings file represented by this path to the list of default classpath settings.- Parameters:
defaultClasspathSettingsPath
- the given default classpath setting.
-
getTree
public static StringTree<String> getTree(String path)
Get a tree view of a part of the settings. Note: settings read with this mechanism do not support overriding with system properties!- Parameters:
path
- Dotted path to a unique element in the tree.- Returns:
- The part of the setting structure below the element given.
-
verifyClass
public static boolean verifyClass(String classname)
Verify, if the given class exists in the classpath.- Parameters:
classname
- A given class to check for- Returns:
- true, if the given class exists in the classpath, else false
-
-