Class ConfigStore

java.lang.Object
org.apache.juneau.marshall.Context
org.apache.juneau.config.store.ConfigStore
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
ClasspathStore, FileStore, MemoryStore, ProfileConfigStore, SqlStore

public abstract class ConfigStore extends Context implements Closeable
Represents a storage location for configuration files.

Content stores require two methods to be implemented:

Notes:
  • This class is thread safe and reusable.
  • Constructor Details

  • Method Details

    • exists

      public abstract boolean exists(String name)
      Checks whether the configuration with the specified name exists in this store.
      Parameters:
      name - The config name.
      Returns:
      true if the configuration with the specified name exists in this store.
    • getMap

      public ConfigMap getMap(String name) throws IOException
      Returns a map file containing the parsed contents of a configuration.
      Parameters:
      name - The configuration name.
      Returns:
      The parsed configuration.
      Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • getMap

      public ConfigMap getMap(String name, ConfigFormat format) throws IOException
      Returns a map file containing the parsed contents of a configuration.
      Parameters:
      name - The configuration name.
      format - The configuration format.
      Can be null (defaults to IniConfigFormat).
      Returns:
      The parsed configuration.
      Throws:
      IOException - Thrown by underlying stream.
    • read

      public abstract String read(String name) throws IOException
      Returns the contents of the configuration file.
      Parameters:
      name - The config file name.
      Returns:
      The contents of the configuration file.
      A blank string if the config does not exist.
      Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • register

      Registers a new listener on this store.
      Parameters:
      name - The configuration name to listen for.
      Cannot be null.
      l - The new listener.
      Returns:
      This object.
    • unregister

      Unregisters a listener from this store.
      Parameters:
      name - The configuration name to listen for.
      Cannot be null.
      l - The listener to unregister.
      Returns:
      This object.
    • update

      public ConfigStore update(String name, String contents)
      Called when the physical contents of a config file have changed.

      Triggers calls to ConfigStoreListener.onChange(String) on all registered listeners.

      Parameters:
      name - The config name (e.g. the filename without the extension).
      Cannot be null.
      contents - The new contents.
      Returns:
      This object.
    • update

      public ConfigStore update(String name, String... contentLines)
      Convenience method for updating the contents of a file with lines.
      Parameters:
      name - The config name (e.g. the filename without the extension).
      Cannot be null.
      contentLines - The new contents.
      Returns:
      This object.
    • write

      public abstract String write(String name, String expectedContents, String newContents) throws IOException
      Saves the contents of the configuration file if the underlying storage hasn't been modified.
      Parameters:
      name - The config file name.
      expectedContents - The expected contents of the file.
      newContents - The new contents.
      Returns:
      If null, then we successfully stored the contents of the file.
      Otherwise the contents of the file have changed and we return the new contents of the file.
      Throws:
      IOException - Thrown by underlying stream.
    • getNotifiedContents

      public String getNotifiedContents(String name, String written) throws IOException
      Returns the contents that listeners will be notified with after the specified contents are written.

      Stores that notify listeners with text other than what was written (e.g. after merging in overlays) should override this method.

      This is called before the write, so any processing that fails (e.g. merging unparseable written text) fails before anything is persisted.

      Parameters:
      name - The config name.
      written - The contents being written.
      Returns:
      The contents that listeners will receive. The default implementation returns written.
      Throws:
      IOException - Thrown by underlying stream.
    • resolveName

      protected String resolveName(String name)
      Subclasses can override this method to convert config names to internal forms.

      For example, the FileStore class can take in both "MyConfig" and "MyConfig.cfg" as names that both resolve to "MyConfig.cfg".

      Parameters:
      name - The name to resolve.
      Returns:
      The resolved name.