Interface ConfigSource

    • Method Detail

      • create

        static ConfigSource create​(Config metaConfig)
                            throws ConfigMappingException,
                                   MissingValueException
        Initializes a ConfigSource from meta-configuration.

        Meta-config can contain the following top level properties to define one or more ConfigSources:

        • type - specifies the built-in configuration type (environment-variables, system-properties, directory, file, url, prefixed, classpath) or the custom config types of the ConfigSource being defined.
        • class - fully-qualified class name of one of: See the section below on custom source types.
        The meta-config for a source should specify either type or class but not both. If the meta-config specifies both the config system ignores the class information.

        As the config system loads configuration it uses mappers to convert the raw data into Java types. See ConfigMapperProvider for details about mapping.

        The meta-config can modify a type or class source declaration using properties. See AbstractParsableConfigSource.Builder.init(Config) for the available properties for types other than system-properties and environment-variables (which do not support properties settings).

        Predefined Configuration Source Types
        Source Type Further Information Mandatory Properties
        system-properties ConfigSources.systemProperties() none
        environment-variables ConfigSources.environmentVariables() none
        classpath ConfigSources.classpath(String) resource
        file ConfigSources.file(String) path
        directory ConfigSources.directory(String) path
        url ConfigSources.url(URL) url
        prefixed ConfigSources.prefixed(String, Supplier) key
        type or class
        properties
        Example configuration in HOCON format:
         sources = [
             {
                 type = "environment-variables"
             }
             {
                 type = "system-properties"
             }
             {
                 type = "directory"
                 properties {
                     path = "conf/secrets"
                     media-type-mapping {
                         yaml = "application/x-yaml"
                         password = "application/base64"
                     }
                     polling-strategy {
                         type = "regular"
                         properties {
                             interval = "PT15S"
                         }
                     }
                 }
             }
             {
                 type = "url"
                 properties {
                     url = "http://config-service/my-config"
                     media-type = "application/hocon"
                     optional = true
                     retry-policy {
                         type = "repeat"
                         properties {
                             retries = 3
                         }
                     }
                 }
             }
             {
                 type = "file"
                 properties {
                     path = "conf/config.yaml"
                     polling-strategy {
                         type = "watch"
                     }
                 }
             }
             {
                 type = "prefixed"
                 properties {
                     key = "app"
                     type = "classpath"
                     properties {
                         resource = "app.yaml"
                     }
                 }
             }
             {
                 type = "classpath"
                 properties {
                     resource = "default.yaml"
                 }
             }
         ]
         
        The example refers to the built-in polling-strategy types regular and watch. See PollingStrategy for details about all supported properties and custom implementation support. It also shows the built-in retry-policy type repeat. See RetryPolicy for more information.

        Custom Sources and Source Types

        Custom Configuration Sources

        The application can define a custom config source using class instead of type in the meta-configuration. The referenced class must implement either ConfigSource or Config.Builder. If the custom implementation extends AbstractParsableConfigSource.Builder then the config system will invoke its init method passing a Config object representing the information from the meta-configuration for that custom source. The implementation can then use the relevant properties to load and manage the configuration from the origin.

        Custom Configuration Source Types

        The application can also add a custom source type to the list of built-in source types. The config system looks for the resource META-INF/resources/meta-config-sources.properties on the classpath and uses its contents to define custom source types. For each property the name is a new custom source type and the value is the fully-qualified class name of the custom ConfigSource or a builder for a custom ConfigSource.

        For example, the module helidon-config-git includes the resource META-INF/resources/meta-config-sources.properties containing

         git = io.helidon.config.git.GitConfigSourceBuilder
         
        This defines the new source type git which can then be referenced from meta-configuration this way:
         {
             type = "git"
             properties {
                 path = "application.conf"
                 directory = "/app-config/"
             }
         }
         
        Parameters:
        metaConfig - meta-configuration used to initialize the ConfigSource
        Returns:
        ConfigSource described by metaConfig
        Throws:
        MissingValueException - if the configuration tree does not contain all expected sub-nodes required by the mapper implementation to provide an instance of the corresponding Java type.
        ConfigMappingException - if the mapper fails to map the (existing) configuration tree represented by the supplied configuration node to an instance of the given Java type
        See Also:
        ConfigSources.load(Supplier[]), ConfigSources.load(Config)