Class ConfigMap

java.lang.Object
org.apache.juneau.config.internal.ConfigMap
All Implemented Interfaces:
ConfigStoreListener

public class ConfigMap extends Object implements ConfigStoreListener
Represents the parsed contents of a configuration.
  • Constructor Details

  • Method Details

    • getEntry

      public ConfigMapEntry getEntry(String section, String key)
      Reads an entry from this map.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      key - The entry key.
      Must not be null.
      Returns:
      The entry, or null if the entry doesn't exist.
    • getPreLines

      public List<String> getPreLines(String section)
      Returns the pre-lines on the specified section.

      The pre-lines are all lines such as blank lines and comments that preceed a section.

      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      Returns:
      An unmodifiable list of lines, or null if the section doesn't exist.
    • getSections

      public Set<String> getSections()
      Returns the keys of the entries in the specified section.
      Returns:
      An unmodifiable set of keys.
    • getKeys

      public Set<String> getKeys(String section)
      Returns the keys of the entries in the specified section.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      Returns:
      An unmodifiable set of keys, or an empty set if the section doesn't exist.
    • hasSection

      public boolean hasSection(String section)
      Returns true if this config has the specified section.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      Returns:
      true if this config has the specified section.
    • setSection

      public ConfigMap setSection(String section, List<String> preLines)
      Adds a new section or replaces the pre-lines on an existing section.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      preLines - The pre-lines on the section.
      If null, the previous value will not be overwritten.
      Returns:
      This object.
    • setEntry

      public ConfigMap setEntry(String section, String key, String value, String modifiers, String comment, List<String> preLines)
      Adds or overwrites an existing entry.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      key - The entry key.
      Must not be null.
      value - The entry value.
      If null, the previous value will not be overwritten.
      modifiers - Optional modifiers.
      If null, the previous value will not be overwritten.
      comment - Optional comment.
      If null, the previous value will not be overwritten.
      preLines - Optional pre-lines.
      If null, the previous value will not be overwritten.
      Returns:
      This object.
    • setImport

      public ConfigMap setImport(String section, String importName, List<String> preLines)
      Not implemented.
      Parameters:
      section - The section name where to place the import statement.
      Must not be null.
      Use blank for the default section.
      importName - The import name.
      Must not be null.
      preLines - Optional comment and blank lines to add immediately before the import statement.
      If null, previous pre-lines will not be replaced.
      Returns:
      This object.
    • removeSection

      public ConfigMap removeSection(String section)
      Removes a section.

      This eliminates all entries in the section as well.

      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      Returns:
      This object.
    • removeEntry

      public ConfigMap removeEntry(String section, String key)
      Removes an entry.
      Parameters:
      section - The section name.
      Must not be null.
      Use blank to refer to the default section.
      key - The entry key.
      Must not be null.
      Returns:
      This object.
    • removeImport

      public ConfigMap removeImport(String section, String importName)
      Not implemented.
      Parameters:
      section - The section name where to place the import statement.
      Must not be null.
      Use blank for the default section.
      importName - The import name.
      Must not be null.
      Returns:
      This object.
    • load

      public ConfigMap load(String contents, boolean synchronous) throws IOException, InterruptedException
      Overwrites the contents of the config file.
      Parameters:
      contents - The new contents of the config file.
      synchronous - Wait until the change has been persisted before returning this map.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
      InterruptedException - Thread was interrupted.
    • commit

      public ConfigMap commit() throws IOException
      Persist any changes made to this map and signal all listeners.

      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.

      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • register

      Registers an event listener on this map.
      Parameters:
      listener - The new listener.
      Returns:
      This object.
    • unregister

      Unregisters an event listener from this map.
      Parameters:
      listener - The listener to remove.
      Returns:
      This object.
    • getListeners

      Returns the listeners currently associated with this config map.
      Returns:
      The listeners currently associated with this config map.
    • onChange

      public void onChange(String newContents)
      Description copied from interface: ConfigStoreListener
      Called when the physical contents of a config file have changed.
      Specified by:
      onChange in interface ConfigStoreListener
      Parameters:
      newContents - The new config contents;
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • asMap

      public JsonMap asMap()
      Returns the values in this config map as a map of maps.

      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.

      Returns:
      A copy of this config as a map of maps.
    • writeTo

      public Writer writeTo(Writer w) throws IOException
      Serializes this map to the specified writer.
      Parameters:
      w - The writer to serialize to.
      Returns:
      The same writer passed in.
      Throws:
      IOException - Thrown by underlying stream.
    • rollback

      public ConfigMap rollback()
      Does a rollback of any changes on this map currently in memory.
      Returns:
      This object.