- java.lang.Object
-
- net.morimekta.config.ConfigSupplier<ConfigType>
-
- Type Parameters:
ConfigType- The config type.
- All Implemented Interfaces:
Closeable,AutoCloseable,Supplier<ConfigType>
public class ConfigSupplier<ConfigType> extends Object implements Supplier<ConfigType>, Closeable
A wrapper around a config file to handle loading and parsing during application setup.class MyApplication extends TinyApplication { private var config = ConfigSupplier.yamlConfig(MyConfig.class) {@literal@}Override public void initialize(ArgParser argParser, TinyApplicationContext.Builder context) { argParser.add(Option .optionLong("--config", "A config file", ValueParser.path(config::loadFromFile)) .required()) } public void onStart(TinyApplicationContext context) { var myConfig = config.get(); // you have a config! } }See
net.morimekta.tiny.server:tiny-serverfor details on the microservice server starter.
-
-
Constructor Summary
Constructors Constructor Description ConfigSupplier(ConfigReader<ConfigType> loader)Create a config supplier.ConfigSupplier(ConfigReader<ConfigType> loader, Supplier<net.morimekta.file.FileWatcher> fileWatcherSupplier)Create a config supplier.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ConfigSupplier<ConfigType>addChangeListener(ConfigChangeListener<ConfigType> listener)ConfigSupplier<ConfigType>addEventListener(ConfigEventListener listener)voidclose()ConfigTypeget()Get the current config content.voidload(Path filePath)Load config from file and store the result as the supplied config.voidloadAndMonitor(Path filePath)Load config from file, store the result as the supplied config and start monitoring the actual file for updates.voidloadAndMonitorUnchecked(Path filePath)Load config from file, store the result as the supplied config and start monitoring the actual file for updates.voidloadUnchecked(Path filePath)Load config from file and store the result as the supplied config.StringtoString()static <ConfigType>
ConfigSupplier<ConfigType>yamlConfig(Class<ConfigType> type)Load config as YAML, just using available jackson modules.static <ConfigType>
ConfigSupplier<ConfigType>yamlConfig(Class<ConfigType> type, Consumer<com.fasterxml.jackson.databind.ObjectMapper> initMapper)Load config as YAML.
-
-
-
Constructor Detail
-
ConfigSupplier
public ConfigSupplier(ConfigReader<ConfigType> loader)
Create a config supplier.- Parameters:
loader- The config loader to use for the supplier.
-
ConfigSupplier
public ConfigSupplier(ConfigReader<ConfigType> loader, Supplier<net.morimekta.file.FileWatcher> fileWatcherSupplier)
Create a config supplier.- Parameters:
loader- The config loader to use for the supplier.fileWatcherSupplier- Supplier of file watcher. Mainly for testing.
-
-
Method Detail
-
addChangeListener
public ConfigSupplier<ConfigType> addChangeListener(ConfigChangeListener<ConfigType> listener)
-
addEventListener
public ConfigSupplier<ConfigType> addEventListener(ConfigEventListener listener)
-
load
public void load(Path filePath) throws IOException, ConfigException
Load config from file and store the result as the supplied config.- Parameters:
filePath- The file to load.- Throws:
IOException- If unable to read the file.ConfigException- If unable to parse the file.
-
loadAndMonitor
public void loadAndMonitor(Path filePath) throws ConfigException, IOException
Load config from file, store the result as the supplied config and start monitoring the actual file for updates. Updates will then cause config listeners to be triggered.- Parameters:
filePath- The file to load.- Throws:
IOException- If unable to read the file.ConfigException- If unable to parse the file.
-
loadUnchecked
public void loadUnchecked(Path filePath)
Load config from file and store the result as the supplied config. Protect the caller from checked exceptions by wrapping them as matching unchecked variants.- Parameters:
filePath- The file to load.
-
loadAndMonitorUnchecked
public void loadAndMonitorUnchecked(Path filePath)
Load config from file, store the result as the supplied config and start monitoring the actual file for updates. Updates will then cause config listeners to be triggered. Protect the caller from checked exceptions by wrapping them as matching unchecked variants.- Parameters:
filePath- The file to load.
-
get
public ConfigType get()
Get the current config content.- Specified by:
getin interfaceSupplier<ConfigType>- Returns:
- The last loaded config.
- Throws:
NullPointerException- If no config has been loaded.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
yamlConfig
public static <ConfigType> ConfigSupplier<ConfigType> yamlConfig(Class<ConfigType> type)
Load config as YAML, just using available jackson modules.- Type Parameters:
ConfigType- The config entry type.- Parameters:
type- The config entry class.- Returns:
- The config supplier.
-
yamlConfig
public static <ConfigType> ConfigSupplier<ConfigType> yamlConfig(Class<ConfigType> type, Consumer<com.fasterxml.jackson.databind.ObjectMapper> initMapper)
Load config as YAML.- Type Parameters:
ConfigType- The config entry type.- Parameters:
type- The config entry class.initMapper- Initializer for the ObjectMapper to handle the config.- Returns:
- The config supplier.
-
-