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.
Concurrency:

The parsed contents are held in a single immutable ConfigMap.State snapshot published through a volatile reference. Reads (getEntry(String,String), getKeys(String), getSections(), asMap(), toString(), etc.) are lock-free: they take a single snapshot of the current state and operate entirely on immutable collections, so there is no live-view leak and no ConcurrentModificationException hazard. Writes (setEntry(String,String,String,String,String,List), commit(), onChange(String), etc.) build a fresh immutable state and swap it under a write lock, which is still required to make read-modify-write sequences atomic.

  • Constructor Details

  • Method Details

    • 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.
    • 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.
    • 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.
    • 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.
    • getListeners

      Returns the listeners currently associated with this config map.
      Returns:
      The listeners currently associated with this config map.
    • 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.
    • 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.
    • 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.
    • 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;
    • register

      Registers an event listener on this map.
      Parameters:
      listener - The new listener.
      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)
      Removes the import statement with the specified name.

      The imported config is detached from this map: its entries are no longer visible through this map and its change-propagation listeners are unregistered. No-op if no matching import exists.

      Parameters:
      section - The section name where the import statement was placed.
      Must not be null.
      Use blank for the default section.
      importName - The import name.
      Must not be null.
      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.
    • rollback

      public ConfigMap rollback()
      Does a rollback of any changes on this map currently in memory.
      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)
      Creates the specified import statement if it doesn't already exist.

      The named config is resolved through this map's store and attached to this map so that its entries become visible through this map and subsequent changes to it are propagated to this map's listeners. No-op if an import with the same name already exists.

      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.
    • 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.
    • toString

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

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

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

      public String asIniString()
      Serializes the current entries snapshot to INI form.
      Returns:
      The INI-formatted string.