Class EnvironmentConfiguration

  • All Implemented Interfaces:
    io.confound.config.Configuration

    public class EnvironmentConfiguration
    extends io.confound.config.StringMapConfiguration
    Configuration implementation backed by environment variables.

    This implementation provides a facility for converting queried configuration keys to match environment variable conventions. Best practices for Confound configuration variables use lowercase or camelCase names with optional dot separators. Environment variables however are traditionally in uppercase with underscore separators.

    By default this implementation considers a query for a key foo.bar to be a request for the environment variable named FOO_BAR. This allows a consistent pattern for key requests while allowing environment variables to follow traditional naming conventions.

    Author:
    Garret Wilson
    See Also:
    System.getenv()
    • Field Summary

      • Fields inherited from interface io.confound.config.Configuration

        KEY_SEGMENT_SEPARATOR
    • Constructor Summary

      Constructors 
      Constructor Description
      EnvironmentConfiguration()
      System environment constructor.
      EnvironmentConfiguration​(boolean normalNames)
      System environment constructor with normal names option.
      EnvironmentConfiguration​(java.util.Map<java.lang.String,​java.lang.String> env)
      Environment map constructor.
      EnvironmentConfiguration​(java.util.Map<java.lang.String,​java.lang.String> env, boolean normalNames)
      Environment map constructor with normal names option.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isNormalNames()
      Indicates whether environment variables use uppercase/underscore (e.g.
      protected java.lang.String normalizeKey​(java.lang.String key)
      This version normalizes requested keys by replacing '.' with '_' and converting the key to uppercase, but only if isNormalNames() is enabled.
      • Methods inherited from class io.confound.config.StringMapConfiguration

        findConfigurationValueImpl, hasConfigurationValueImpl
      • Methods inherited from class io.confound.config.AbstractStringConfiguration

        dereferenceString, findBoolean, findConfigurationValue, findDouble, findInt, findLong, findObject, findPath, findString, findUri
      • Methods inherited from class io.confound.config.BaseConfiguration

        convertValue, hasConfigurationValue
      • Methods inherited from class io.confound.config.AbstractConfiguration

        or
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface io.confound.config.Configuration

        createMissingConfigurationKeyException, findObject, getBoolean, getDouble, getInt, getLong, getObject, getObject, getPath, getString, getUri, requireConfiguration, resolvePath, subConfiguration, superConfiguration, withFallback
    • Constructor Detail

      • EnvironmentConfiguration

        public EnvironmentConfiguration()
        System environment constructor.

        Requested keys will be normalized to match environment variable naming conventions.

        Throws:
        java.lang.NullPointerException - if the given environment map is null.
        java.lang.SecurityException - If a security manager exists and it doesn't allow access to environment variables.
        See Also:
        System.getenv()
      • EnvironmentConfiguration

        public EnvironmentConfiguration​(boolean normalNames)
        System environment constructor with normal names option.
        Parameters:
        normalNames - true if requested keys such as foo.bar should be normalized to match configuration keys using environment variable naming convention such as FOO_BAR.
        Throws:
        java.lang.NullPointerException - if the given environment map is null.
        java.lang.SecurityException - If a security manager exists and it doesn't allow access to environment variables.
        See Also:
        System.getenv()
      • EnvironmentConfiguration

        public EnvironmentConfiguration​(@Nonnull
                                        java.util.Map<java.lang.String,​java.lang.String> env)
        Environment map constructor.

        Requested keys will be normalized to match environment variable naming conventions.

        Parameters:
        env - The map representing the environment.
        Throws:
        java.lang.NullPointerException - if the given environment map is null.
      • EnvironmentConfiguration

        public EnvironmentConfiguration​(@Nonnull
                                        java.util.Map<java.lang.String,​java.lang.String> env,
                                        boolean normalNames)
        Environment map constructor with normal names option.
        Parameters:
        env - The map representing the environment.
        normalNames - true if requested keys such as foo.bar should be normalized to match configuration keys using environment variable naming convention such as FOO_BAR.
        Throws:
        java.lang.NullPointerException - if the given environment map is null.
    • Method Detail

      • isNormalNames

        public boolean isNormalNames()
        Indicates whether environment variables use uppercase/underscore (e.g. FOO_BAR) conventions, normalizing requested keys (e.g. foo.bar). If this is set to false, environment variables will only be retrieved if their names exactly match the requested keys.
        Returns:
        true if requested configuration keys will be normalized to match environment variable naming conventions.
      • normalizeKey

        protected java.lang.String normalizeKey​(java.lang.String key)
        This version normalizes requested keys by replacing '.' with '_' and converting the key to uppercase, but only if isNormalNames() is enabled.
        Overrides:
        normalizeKey in class io.confound.config.BaseConfiguration<java.lang.String>
        See Also:
        isNormalNames(), Configuration.KEY_SEGMENT_SEPARATOR