Class ConfigWatcher<ConfigFileType>

java.lang.Object
net.morimekta.config.ConfigWatcher<ConfigFileType>
All Implemented Interfaces:
Closeable, AutoCloseable

public class ConfigWatcher<ConfigFileType> extends Object implements Closeable
The config watcher watches the whole directory of config files of the same type, and notifies listeners of any changes. The purpose of this watcher is to manage config files serving different entities of the same type, where the entity config for each can change live, be created and removed.
  • Constructor Details

    • ConfigWatcher

      public ConfigWatcher(Path configDir, ConfigReader<ConfigFileType> configReader, Function<ConfigFileType,String> configIdentifier)
      Create a config watcher instance.
      Parameters:
      configDir - The directory where the config files should be.
      configReader - The config file reader used for parsing.
      configIdentifier - Get identifiers from the config file for metrics and logging.
    • ConfigWatcher

      public ConfigWatcher(Path configDir, ConfigReader<ConfigFileType> configReader, Function<ConfigFileType,String> configIdentifier, Clock clock)
      Create a config watcher instance.
      Parameters:
      configDir - The directory where the config files should be.
      configReader - The config file reader used for parsing.
      configIdentifier - Get identifiers from the config file for metrics and logging.
      clock - Clock for knowing config update times.
    • ConfigWatcher

      public ConfigWatcher(Path configDir, ConfigReader<ConfigFileType> configReader, Function<ConfigFileType,String> configIdentifier, Clock clock, net.morimekta.file.DirWatcher dirWatcher, net.morimekta.file.FileWatcher fileWatcher)
      Create a config watcher instance.
      Parameters:
      configDir - The directory where the config files should be.
      configReader - The config file reader used for parsing.
      configIdentifier - Get identifiers from the config file for metrics and logging.
      clock - Clock for knowing config update times.
      dirWatcher - A directory watcher.
      fileWatcher - A file watcher.
  • Method Details

    • getConfigDir

      public Path getConfigDir()
      Returns:
      The path to the config directory.
    • getLoadedConfigMap

      public Map<Path,ConfigWatcher.ConfigEntry<ConfigFileType>> getLoadedConfigMap()
      Returns:
      The currently loaded config map as map of absolute file path to loaded config entry with metadata.
    • addEventListener

      public ConfigWatcher<ConfigFileType> addEventListener(ConfigEventListener listener)
      Add a config event listener for events of what is happening on the config watcher.
      Parameters:
      listener - Config Event Listener
      Returns:
      The config watcher.
    • addChangeListener

      public ConfigWatcher<ConfigFileType> addChangeListener(ConfigChangeListener<ConfigFileType> listener)
      Add a config change listener for notification on changes to the loaded config map.
      Parameters:
      listener - Config Event Listener
      Returns:
      The config watcher.
    • start

      Start the NetworkConfigWatcher and throw exception if unable to read and update config.
      Returns:
      The config watcher.
      Throws:
      ConfigException - On any config read, parse or update issues.
    • start

      public ConfigWatcher<ConfigFileType> start(boolean failOnConfigException) throws ConfigException
      Start the NetworkConfigWatcher and throw exception if unable to read and update config.
      Parameters:
      failOnConfigException - If the start sequence should fail on config exception. If this is set to false, config exceptions will be logged, but otherwise ignored.
      Returns:
      The config watcher.
      Throws:
      ConfigException - On any config read, parse or update issues.
    • close

      public void close()
      Immediately remove all change listeners and stop watching files.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

      public String toString()
      Overrides:
      toString in class Object