Class 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-server for details on the microservice server starter.

    • 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

      • 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.
      • 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.