Class EnvironmentVariables


  • public final class EnvironmentVariables
    extends Object
    Provides environment variables that include aliases enabling setting or overriding configuration with keys that are unlikely to be legal as environment variables.

    The MP config specification describes the environment variables ConfigSource as follows:

     Some operating systems allow only alphabetic characters or an underscore, _, in environment variables. Other
     characters such as ., /, etc may be disallowed. In order to set a value for a config property that has a name
     containing such disallowed characters from an environment variable, the following rules are used.
    
     This ConfigSource searches 3 environment variables for a given property name (e.g. com.ACME.size):
    
      1. Exact match (i.e. com.ACME.size)
      2. Replace the character that is neither alphanumeric nor _ with _ (i.e. com_ACME_size)
      3. Replace the character that is neither alphanumeric nor _ with _ and convert to upper case (i.e. COM_ACME_SIZE)
    
     The first environment variable that is found is returned by this ConfigSource.
     

    The spec assumes the mapping takes place during search, where the desired key is known, but Helidon merges ConfigSources instead; therefore this implementation produces additional KV pairs with aliases for any variable that can meaningfully be mapped. See shouldAlias(String) for the mapping criteria.

    Since Helidon supports many configuration keys that contain '-' (e.g. "server.executor-service.max-pool-size"), an additional mapping is required to produce a matching alias. Given that it must map from legal environment variable names and reduce the chances of inadvertent mappings, a verbose mapping is used: "_dash_" substrings (upper and lower case) are first replaced by '-'. See expand() for the aliases produced.

    • Method Detail

      • shouldAlias

        public static boolean shouldAlias​(String name)
        Tests whether aliases should be created for the given environment variable name.

        To provide a meaningful alias, the name must meet all of the following criteria:

        1. does not begin or end with a '_' character
        2. does not contain "__"
        3. contains one or more '_' characters
        Parameters:
        name - The environment variable name.
        Returns:
        true if aliases should be created.
      • expand

        public static Map<String,​String> expand()
        Returns the environment variables and their aliases.

        The following mappings are applied to any environment variable name for which shouldAlias(String) returns true:

        1. Replace "_dash_" by '-', e.g. "SERVER_EXECUTOR_dash_SERVICE_MAX_dash_POOL_dash_SIZE" becomes "SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE".
        2. Replace '_' by '.' and add as a alias, e.g. "com_ACME_size" becomes "com.ACME.size" and "SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE" becomes "SERVER.EXECUTOR-SERVICE.MAX-POOL-SIZE". This mapping is added primarily to support mixed case config keys such as "app.someCamelCaseKey".
        3. Convert the result of step 2 to lowercase and add as a alias, e.g. "com.ACME.size" becomes "com.acme.size" and "SERVER.EXECUTOR-SERVICE.MAX-POOL-SIZE" becomes "server.executor-service.max-pool-size".
        Returns:
        An unmodifiable copy of System.getenv() including aliases.
      • expand

        public static Map<String,​String> expand​(Map<String,​String> env)
        Returns the environment variables and their aliases.

        The following mappings are applied to any environment variable name for which shouldAlias(String) returns true:

        1. Replace "_dash_" by '-', e.g. "SERVER_EXECUTOR_dash_SERVICE_MAX_dash_POOL_dash_SIZE" becomes "SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE".
        2. Replace '_' by '.' and add as an alias, e.g. "com_ACME_size" becomes "com.ACME.size" and "SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE" becomes "SERVER.EXECUTOR-SERVICE.MAX-POOL-SIZE". This mapping is added primarily to support mixed case config keys such as "app.someCamelCaseKey".
        3. Convert the result of step 2 to lowercase and add as an alias, e.g. "com.ACME.size" becomes "com.acme.size" and "SERVER.EXECUTOR-SERVICE.MAX-POOL-SIZE" becomes "server.executor-service.max-pool-size".
        Parameters:
        env - The environment variables.
        Returns:
        An unmodifiable copy of env with aliases added.