java.lang.Object
net.morimekta.config.ConfigSupplier<ConfigType>
- Type Parameters:
ConfigType- The config type.
- All Implemented Interfaces:
Closeable,AutoCloseable,Supplier<ConfigType>
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-server for
details on the microservice server starter.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionConfigSupplier(ConfigReader<ConfigType> loader) Create a config supplier.protectedConfigSupplier(ConfigReader<ConfigType> loader, Supplier<net.morimekta.file.FileWatcher> fileWatcherSupplier) Create a config supplier.ConfigSupplier(ConfigReaderSupplier<ConfigType> loader) Create a config supplier.protectedConfigSupplier(ConfigReaderSupplier<ConfigType> readerSupplier, Supplier<net.morimekta.file.FileWatcher> fileWatcherSupplier, Clock clock) Create a config supplier. -
Method Summary
Modifier and TypeMethodDescriptionaddChangeListener(ConfigChangeListener<ConfigType> listener) Add a config change listener.addEventListener(ConfigEventListener listener) Add a config event change listener.voidclose()static <ConfigType>
ConfigSupplier<ConfigType>Make a config supplier, and figure out file reader by detecting file type and using default reader for the type.static <ConfigType>
ConfigSupplier<ConfigType>Load a config file, and detect file type and using default reader for the type.get()Get the current config content.getFile()Get the loaded config file.Get the timestamp of last successful update.voidLoad 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.toString()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, Path file, Consumer<com.fasterxml.jackson.databind.ObjectMapper> initMapper) Load config as YAML.static <ConfigType>
ConfigSupplier<ConfigType>yamlConfig(Class<ConfigType> type, Consumer<com.fasterxml.jackson.databind.ObjectMapper> initMapper) Load config as YAML.
-
Constructor Details
-
ConfigSupplier
Create a config supplier.- Parameters:
loader- The config loader to use for the supplier.
-
ConfigSupplier
Create a config supplier.- Parameters:
loader- The config loader to use for the supplier.
-
ConfigSupplier
protected 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. Available for testing.
-
ConfigSupplier
protected ConfigSupplier(ConfigReaderSupplier<ConfigType> readerSupplier, Supplier<net.morimekta.file.FileWatcher> fileWatcherSupplier, Clock clock) Create a config supplier.- Parameters:
readerSupplier- The config loader to use for the supplier.fileWatcherSupplier- Supplier of file watcher. Available for testing.clock- Clock to get update timestamp from.
-
-
Method Details
-
addChangeListener
Add a config change listener.- Parameters:
listener- The config change listener to add.- Returns:
- The config supplier.
-
addEventListener
Add a config event change listener.- Parameters:
listener- The config event listener to add.- Returns:
- The config supplier.
-
load
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
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
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
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.
-
getFile
Get the loaded config file. For monitored config supplier, this is the file that is watched.- Returns:
- The loaded config file.
-
getLastUpdatedAt
Get the timestamp of last successful update.- Returns:
- The last updated timestamp.
-
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
-
toString
-
config
Make a config supplier, and figure out file reader by detecting file type and using default reader for the type.- Type Parameters:
ConfigType- The config entry type.- Parameters:
type- The config entry class.- Returns:
- The config supplier.
-
config
public static <ConfigType> ConfigSupplier<ConfigType> config(Class<ConfigType> type, Path file) throws ConfigException, IOException Load a config file, and detect file type and using default reader for the type.- Type Parameters:
ConfigType- The config entry type.- Parameters:
type- The config entry class.file- The config file to load.- Returns:
- The config supplier.
- Throws:
ConfigException- On config parse failures.IOException- On file read failures.
-
yamlConfig
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.
-
yamlConfig
public static <ConfigType> ConfigSupplier<ConfigType> yamlConfig(Class<ConfigType> type, Path file, Consumer<com.fasterxml.jackson.databind.ObjectMapper> initMapper) throws ConfigException, IOException Load config as YAML.- Type Parameters:
ConfigType- The config entry type.- Parameters:
type- The config entry class.file- The config file to load.initMapper- Initializer for the ObjectMapper to handle the config.- Returns:
- The config supplier.
- Throws:
ConfigException- On config parse failures.IOException- On file read failures.
-