Class EnvironmentConfiguration

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

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

    This implementation provides a facility for converting queried parameter 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()
    • Constructor Summary

      Constructors 
      Constructor Description
      EnvironmentConfiguration()
      System environment constructor.
      EnvironmentConfiguration​(boolean normalNames)
      System environment constructor with normal names option.
      EnvironmentConfiguration​(boolean normalNames, io.confound.config.Configuration parentConfiguration)
      Parent configuration with system environment constructor and normal names option.
      EnvironmentConfiguration​(io.confound.config.Configuration parentConfiguration)
      Parent configuration with system environment constructor.
      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.
      EnvironmentConfiguration​(java.util.Map<java.lang.String,​java.lang.String> env, boolean normalNames, io.confound.config.Configuration parentConfiguration)
      Parent configuration and environment map constructor and normal names option.
      EnvironmentConfiguration​(java.util.Map<java.lang.String,​java.lang.String> env, io.confound.config.Configuration parentConfiguration)
      Parent configuration and environment map constructor.
    • 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

        findParameterImpl, hasParameterImpl
      • Methods inherited from class io.confound.config.AbstractStringConfiguration

        dereferenceString, findParameter, getOptionalBoolean, getOptionalDouble, getOptionalInt, getOptionalLong, getOptionalPath, getOptionalString, getOptionalUri
      • Methods inherited from class io.confound.config.BaseConfiguration

        getOptionalParameter, hasParameter
      • Methods inherited from class io.confound.config.AbstractConfiguration

        getParentConfiguration, 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.Parameters

        getBoolean, getDouble, getInt, getLong, getParameter, getPath, getString, getUri, requireParameter, resolvePath
    • 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​(@Nullable
                                        io.confound.config.Configuration parentConfiguration)
        Parent configuration with system environment constructor.

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

        Parameters:
        parentConfiguration - The parent configuration to use for fallback lookup, or null if there is no parent configuration.
        Throws:
        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 parameters 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​(boolean normalNames,
                                        @Nullable
                                        io.confound.config.Configuration parentConfiguration)
        Parent configuration with system environment constructor and normal names option.
        Parameters:
        normalNames - true if requested keys such as foo.bar should be normalized to match parameters using environment variable naming convention such as FOO_BAR.
        parentConfiguration - The parent configuration to use for fallback lookup, or null if there is no parent configuration.
        Throws:
        java.lang.NullPointerException - if the given parent configuration and/or 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,
                                        @Nullable
                                        io.confound.config.Configuration parentConfiguration)
        Parent configuration and environment map constructor.

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

        Parameters:
        env - The map representing the environment.
        parentConfiguration - The parent configuration to use for fallback lookup, or null if there is no parent configuration.
        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 parameters using environment variable naming convention such as FOO_BAR.
        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,
                                        @Nullable
                                        io.confound.config.Configuration parentConfiguration)
        Parent configuration and environment map constructor and normal names option.
        Parameters:
        env - The map representing the environment.
        normalNames - true if requested keys such as foo.bar should be normalized to match parameters using environment variable naming convention such as FOO_BAR.
        parentConfiguration - The parent configuration to use for fallback lookup, or null if there is no parent configuration.
        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 parameter 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()