Class EnvironmentVariables
- java.lang.Object
-
- io.helidon.config.EnvironmentVariables
-
public 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
ConfigSource
s instead; therefore this implementation produces additional KV pairs with aliases for any variable that can meaningfully be mapped. SeeshouldAlias(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'-'
. Seeexpand()
for the aliases produced.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Map<String,String>
expand()
Returns the environment variables and their aliases.static Map<String,String>
expand(Map<String,String> env)
Returns the environment variables and their aliases.static boolean
shouldAlias(String name)
Tests whether aliases should be created for the given environment variable name.
-
-
-
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:
- does not begin or end with a
'_'
character - does not contain
"__"
- contains one or more
'_'
characters
- Parameters:
name
- The environment variable name.- Returns:
true
if aliases should be created.
- does not begin or end with a
-
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)
returnstrue
:- Replace
"_dash_"
by'-'
, e.g."SERVER_EXECUTOR_dash_SERVICE_MAX_dash_POOL_dash_SIZE"
becomes"SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE"
. - 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"
. - 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.
- Replace
-
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)
returnstrue
:- Replace
"_dash_"
by'-'
, e.g."SERVER_EXECUTOR_dash_SERVICE_MAX_dash_POOL_dash_SIZE"
becomes"SERVER_EXECUTOR-SERVICE_MAX-POOL-SIZE"
. - 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"
. - 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.
- Replace
-
-