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​(ConfigReaderSupplier<ConfigType> loader)
        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 Detail

      • addEventListener

        public ConfigSupplier<ConfigType> addEventListener​(ConfigEventListener listener)
        Add a config event change listener.
        Parameters:
        listener - The config event listener to add.
        Returns:
        The config supplier.
      • 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.
      • getFile

        public Path getFile()
        Get the loaded config file. For monitored config supplier, this is the file that is watched.
        Returns:
        The loaded config file.
      • getLastUpdatedAt

        public Instant getLastUpdatedAt()
        Get the timestamp of last successful update.
        Returns:
        The last updated timestamp.
      • config

        public static <ConfigType> ConfigSupplier<ConfigType> config​(Class<ConfigType> type)
        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

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