Interface ConfigRegistry
-
- All Known Implementing Classes:
CommonsConfigurationRegistry
public interface ConfigRegistry
The configuration registry is a single source of external configuration. Each configuration entry is accessible by a unique key. The keys may be hierarchical so, that you can build subsets of the configuration. A configuration may be combined by multiple sources. How the multiple sources are combined, is part of the implementation classes. You can register listeners that are notified, if the configuration changes. The syntax for filter of notifications is implementation specific.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
addConfigurationFromFile(String name, Path file)
Load configuration from the given file.void
addConfigurationFromFile(String name, Path file, String prefix)
Load configuration from the given file.void
addConfigurationFromResource(String name, String resource)
Load configuration from the given classloader resource.void
addConfigurationFromResource(String name, String resource, String prefix)
Load configuration from the given classloader resource.String
dump()
Dump the entire registry to a string, for debugging purposes.Collection<String>
getBaseKeys()
Get all keys on the base level in this registry.boolean
getBoolean(String key)
Get a boolean value from the registry.boolean
getBoolean(String key, boolean defaultValue)
Get a boolean value from the registry.int
getInt(String key)
Get an integer value from the registry.int
getInt(String key, int defaultValue)
Get an integer value from the registry.Collection<String>
getKeys()
Get all the keys in this registry.Collection<String>
getKeys(String prefix)
Return the keys that match the given prefix.List<String>
getList(String key)
Get a list of strings at the given key in the registry.ConfigRegistry
getPartOfCombined(String name)
Get a configuration source part of the registry, identified by the given name.Map<String,String>
getProperties(String key)
Get the properties at the given key in the registry.String
getString(String key)
Get a string value from the registry.String
getString(String key, String defaultValue)
Get a string value from the registry.ConfigRegistry
getSubset(String key)
Get a subset of the registry, for all keys descended from the given key.List<ConfigRegistry>
getSubsetList(String key)
Get a list of subsets of the registry, for all keys descended from the given key.Object
getValue(String key)
Get the original value stored in the registry.void
initialize()
Initializes the given registry.boolean
isEmpty()
Determine if the registry contains any elements.void
registerChangeListener(RegistryListener listener, String prefix)
Register a change listener for configuration keys that match the given patterns.void
remove(String key)
Remove a keyed element from the registry.void
removeSubset(String key)
Remove a keyed subset of the registry.void
save()
Save any changes to the registry since it was loaded.void
setBoolean(String key, boolean value)
Set a boolean value in the registry.void
setInt(String key, int value)
Set an integer value in the registry.void
setString(String key, String value)
Set a string value in the registry.boolean
unregisterChangeListener(RegistryListener listener)
Unregister the change listener for all events.
-
-
-
Method Detail
-
dump
String dump()
Dump the entire registry to a string, for debugging purposes.- Returns:
- the registry contents
-
getValue
Object getValue(String key)
Get the original value stored in the registry. If not found,null
is returned.- Parameters:
key
- The key in the registry.- Returns:
- The value.
-
getString
String getString(String key)
Get a string value from the registry. If not found,null
is returned.- Parameters:
key
- the key in the registry- Returns:
- the value
-
getString
String getString(String key, String defaultValue)
Get a string value from the registry. If not found, the default value is used.- Parameters:
key
- the key in the registrydefaultValue
- the default value- Returns:
- the value
-
setString
void setString(String key, String value)
Set a string value in the registry.- Parameters:
key
- the key in the registryvalue
- the value to set
-
getInt
int getInt(String key) throws NoSuchElementException
Get an integer value from the registry. If not found, an exception is thrown.- Parameters:
key
- the key in the registry- Returns:
- the value
- Throws:
NoSuchElementException
- if the key is not found
-
getInt
int getInt(String key, int defaultValue)
Get an integer value from the registry. If not found, the default value is used.- Parameters:
key
- the key in the registrydefaultValue
- the default value- Returns:
- the value
-
setInt
void setInt(String key, int value)
Set an integer value in the registry.- Parameters:
key
- the key in the registryvalue
- the value to set
-
getBoolean
boolean getBoolean(String key) throws NoSuchElementException
Get a boolean value from the registry. If not found, an exception is thrown.- Parameters:
key
- the key in the registry- Returns:
- the value
- Throws:
NoSuchElementException
- if the key is not found
-
getBoolean
boolean getBoolean(String key, boolean defaultValue)
Get a boolean value from the registry. If not found, the default value is used.- Parameters:
key
- the key in the registrydefaultValue
- the default value- Returns:
- the value
-
setBoolean
void setBoolean(String key, boolean value)
Set a boolean value in the registry.- Parameters:
key
- the key in the registryvalue
- the value to set
-
addConfigurationFromResource
void addConfigurationFromResource(String name, String resource) throws RegistryException
Load configuration from the given classloader resource.- Parameters:
name
- the unique name that identifies this configuration in the combined oneresource
- the location to load the configuration from- Throws:
RegistryException
- if a problem occurred reading the resource to add to the registry
-
addConfigurationFromResource
void addConfigurationFromResource(String name, String resource, String prefix) throws RegistryException
Load configuration from the given classloader resource.- Parameters:
name
- the unique name that identifies this configuration in the combined oneresource
- the location to load the configuration fromprefix
- the location to add the configuration at in the registry- Throws:
RegistryException
- if a problem occurred reading the resource to add to the registry
-
addConfigurationFromFile
void addConfigurationFromFile(String name, Path file) throws RegistryException
Load configuration from the given file.- Parameters:
name
- the unique name that identifies this configuration in the combined onefile
- the location to load the configuration from- Throws:
RegistryException
- if a problem occurred reading the resource to add to the registry
-
addConfigurationFromFile
void addConfigurationFromFile(String name, Path file, String prefix) throws RegistryException
Load configuration from the given file.- Parameters:
name
- the unique name that identifies this configuration in the combined onefile
- the location to load the configuration fromprefix
- the location to add the configuration at in the registry- Throws:
RegistryException
- if a problem occurred reading the resource to add to the registry
-
isEmpty
boolean isEmpty()
Determine if the registry contains any elements.- Returns:
- whether the registry contains any elements
-
getList
List<String> getList(String key)
Get a list of strings at the given key in the registry. If not found a empty list will be returned.- Parameters:
key
- the key to lookup- Returns:
- the list of strings
-
getProperties
Map<String,String> getProperties(String key)
Get the properties at the given key in the registry.- Parameters:
key
- the key to lookup- Returns:
- the properties
-
getSubset
ConfigRegistry getSubset(String key) throws RegistryException
Get a subset of the registry, for all keys descended from the given key.- Parameters:
key
- the key to take the subset from- Returns:
- the registry subset
- Throws:
RegistryException
-
getSubsetList
List<ConfigRegistry> getSubsetList(String key) throws RegistryException
Get a list of subsets of the registry, for all keys descended from the given key.- Parameters:
key
- the key to take the subsets from- Returns:
- the registry subsets
- Throws:
RegistryException
-
getPartOfCombined
ConfigRegistry getPartOfCombined(String name)
Get a configuration source part of the registry, identified by the given name. If it doesn't exist,null
will be returned. Configurations can be combined from different sources. This returns the configuration of a specific source. The names are the ones given by the addConfiguration* methods or defined during the initialization process.- Parameters:
name
- The source name of the configuration source.- Returns:
- the The config registry object that represents this source part.
-
save
void save() throws RegistryException, UnsupportedOperationException
Save any changes to the registry since it was loaded.- Throws:
RegistryException
- if there was a problem saving the registryUnsupportedOperationException
- if the registry is not writable
-
registerChangeListener
void registerChangeListener(RegistryListener listener, String prefix)
Register a change listener for configuration keys that match the given patterns.- Parameters:
listener
- the listener
-
unregisterChangeListener
boolean unregisterChangeListener(RegistryListener listener)
Unregister the change listener for all events.- Parameters:
listener
-- Returns:
true
if has been removed
-
getBaseKeys
Collection<String> getBaseKeys()
Get all keys on the base level in this registry. Keys are only retrieved at a depth of 1.- Returns:
- the set of keys
-
getKeys
Collection<String> getKeys()
Get all the keys in this registry.- Returns:
- the set of keys
-
getKeys
Collection<String> getKeys(String prefix)
Return the keys that match the given prefix.- Parameters:
prefix
- The prefix- Returns:
- A collection of keys
-
remove
void remove(String key)
Remove a keyed element from the registry.- Parameters:
key
- the key to remove
-
removeSubset
void removeSubset(String key)
Remove a keyed subset of the registry.- Parameters:
key
- the subset to remove
-
initialize
void initialize() throws RegistryException
Initializes the given registry. The definition for the configuration sources is implementation specific. Implementations should provide methods and/or constructors that allows to define the configuration source.- Throws:
RegistryException
- if the initialization was not successful
-
-