- 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classConfigWatcher.ConfigEntry<ConfigFileType>A config file entry cached in the watcher.
-
Constructor Summary
Constructors Constructor Description ConfigWatcher(Path configDir, ConfigReader<ConfigFileType> configReader, Function<ConfigFileType,String> configIdentifier)Create a config watcher instance.ConfigWatcher(Path configDir, ConfigReader<ConfigFileType> configReader, Function<ConfigFileType,String> configIdentifier, Clock clock)Create a config watcher instance.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ConfigWatcher<ConfigFileType>addChangeListener(ConfigChangeListener<ConfigFileType> listener)Add a config change listener for notification on changes to the loaded config map.ConfigWatcher<ConfigFileType>addEventListener(ConfigEventListener listener)Add a config event listener for events of what is happening on the config watcher.voidclose()Immediately remove all change listeners and stop watching files.PathgetConfigDir()Map<Path,ConfigWatcher.ConfigEntry<ConfigFileType>>getLoadedConfigMap()ConfigWatcher<ConfigFileType>start()Start the NetworkConfigWatcher and throw exception if unable to read and update config.ConfigWatcher<ConfigFileType>start(boolean failOnConfigException)Start the NetworkConfigWatcher and throw exception if unable to read and update config.StringtoString()
-
-
-
Constructor Detail
-
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 Detail
-
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
public ConfigWatcher<ConfigFileType> start() throws ConfigException
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
-