public class ConfigMap extends Object implements ConfigStoreListener
Constructor and Description |
---|
ConfigMap(ConfigStore store,
String name)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
ObjectMap |
asMap()
Returns the values in this config map as a map of maps.
|
ConfigMap |
commit()
Persist any changes made to this map and signal all listeners.
|
ConfigEntry |
getEntry(String section,
String key)
Reads an entry from this map.
|
Set<String> |
getKeys(String section)
Returns the keys of the entries in the specified section.
|
Set<ConfigEventListener> |
getListeners()
Returns the listeners currently associated with this config map.
|
List<String> |
getPreLines(String section)
Returns the pre-lines on the specified section.
|
Set<String> |
getSections()
Returns the keys of the entries in the specified section.
|
boolean |
hasSection(String section)
Returns
|
ConfigMap |
load(String contents,
boolean synchronous)
Overwrites the contents of the config file.
|
void |
onChange(String newContents)
Called when the physical contents of a config file have changed.
|
ConfigMap |
register(ConfigEventListener listener)
Registers an event listener on this map.
|
ConfigMap |
removeEntry(String section,
String key)
Removes an entry.
|
ConfigMap |
removeImport(String section,
String importName)
Not implemented.
|
ConfigMap |
removeSection(String section)
Removes a section.
|
ConfigMap |
rollback()
Does a rollback of any changes on this map currently in memory.
|
ConfigMap |
setEntry(String section,
String key,
String value,
String modifiers,
String comment,
List<String> preLines)
Adds or overwrites an existing entry.
|
ConfigMap |
setImport(String section,
String importName,
List<String> preLines)
Not implemented.
|
ConfigMap |
setSection(String section,
List<String> preLines)
Adds a new section or replaces the pre-lines on an existing section.
|
String |
toString() |
ConfigMap |
unregister(ConfigEventListener listener)
Unregisters an event listener from this map.
|
Writer |
writeTo(Writer w)
Serializes this map to the specified writer.
|
public ConfigMap(ConfigStore store, String name) throws IOException
store
- The config store.name
- The config name.IOException
- Thrown by underlying stream.public ConfigEntry getEntry(String section, String key)
section
- The section name.
key
- The entry key.
public List<String> getPreLines(String section)
The pre-lines are all lines such as blank lines and comments that preceed a section.
section
- The section name.
public Set<String> getSections()
public Set<String> getKeys(String section)
section
- The section name.
public boolean hasSection(String section)
section
- The section name.
public ConfigMap setSection(String section, List<String> preLines)
section
- The section name.
preLines
- The pre-lines on the section.
public ConfigMap setEntry(String section, String key, String value, String modifiers, String comment, List<String> preLines)
section
- The section name.
key
- The entry key.
value
- The entry value.
modifiers
- Optional modifiers.
comment
- Optional comment.
preLines
- Optional pre-lines.
public ConfigMap setImport(String section, String importName, List<String> preLines)
section
- The section name where to place the import statement.
importName
- The import name.
preLines
- Optional comment and blank lines to add immediately before the import statement.
public ConfigMap removeSection(String section)
This eliminates all entries in the section as well.
section
- The section name.
public ConfigMap removeEntry(String section, String key)
section
- The section name.
key
- The entry key.
public ConfigMap removeImport(String section, String importName)
section
- The section name where to place the import statement.
importName
- The import name.
public ConfigMap load(String contents, boolean synchronous) throws IOException, InterruptedException
contents
- The new contents of the config file.synchronous
- Wait until the change has been persisted before returning this map.IOException
- Thrown by underlying stream.InterruptedException
- Thread was interrupted.public ConfigMap commit() throws IOException
If the underlying contents of the file have changed, this will reload it and apply the changes on top of the modified file.
Subsequent changes made to the underlying file will also be signaled to all listeners.
We try saving the file up to 10 times.
If the file keeps changing on the file system, we throw an exception.
IOException
- Thrown by underlying stream.public ConfigMap register(ConfigEventListener listener)
listener
- The new listener.public ConfigMap unregister(ConfigEventListener listener)
listener
- The listener to remove.public Set<ConfigEventListener> getListeners()
public void onChange(String newContents)
ConfigStoreListener
onChange
in interface ConfigStoreListener
newContents
- The new config contents;public ObjectMap asMap()
This is considered a snapshot copy of the config map.
The returned map is modifiable, but modifications to the returned map are not reflected in the config map.
public Writer writeTo(Writer w) throws IOException
w
- The writer to serialize to.IOException
- Thrown by underlying stream.Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.