Class FileStore

All Implemented Interfaces:
Closeable, AutoCloseable, AnnotationProvider

public class FileStore extends ConfigStore
Filesystem-based storage location for configuration files.

Points to a file system directory containing configuration files.

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

    • DEFAULT

      public static final FileStore DEFAULT
      Default file store, all default values.
  • Constructor Details

  • Method Details

    • create

      public static FileStore.Builder create()
      Creates a new builder for this object.
      Returns:
      A new builder.
    • copy

      Description copied from class: Context
      Creates a builder from this context object.

      Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.

      Overrides:
      copy in class Context
      Returns:
      A new Builder object.
    • read

      public String read(String name) throws IOException
      Description copied from class: ConfigStore
      Returns the contents of the configuration file.
      Specified by:
      read in class ConfigStore
      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.
    • write

      public String write(String name, String expectedContents, String newContents) throws IOException
      Description copied from class: ConfigStore
      Saves the contents of the configuration file if the underlying storage hasn't been modified.
      Specified by:
      write in class ConfigStore
      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.
    • exists

      public boolean exists(String name)
      Description copied from class: ConfigStore
      Checks whether the configuration with the specified name exists in this store.
      Specified by:
      exists in class ConfigStore
      Parameters:
      name - The config name.
      Returns:
      true if the configuration with the specified name exists in this store.
    • resolveName

      protected String resolveName(String name)
      Description copied from class: ConfigStore
      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".

      Overrides:
      resolveName in class ConfigStore
      Parameters:
      name - The name to resolve.
      Returns:
      The resolved name.
    • update

      public FileStore update(String name, String newContents)
      Description copied from class: ConfigStore
      Called when the physical contents of a config file have changed.

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

      Overrides:
      update in class ConfigStore
      Parameters:
      name - The config name (e.g. the filename without the extension).
      newContents - The new contents.
      Returns:
      This object.
    • close

      public void close()
    • onFileEvent

      protected void onFileEvent(WatchEvent<Path> e) throws IOException
      Gets called when the watcher service on this store is triggered with a file system change.
      Parameters:
      e - The file system event.
      Throws:
      IOException - Thrown by underlying stream.
    • properties

      protected JsonMap properties()
      Description copied from class: Context
      Returns the properties on this bean as a map for debugging.
      Overrides:
      properties in class Context
      Returns:
      The properties on this bean as a map for debugging.