Nova Ordis Utilities Environment Variable Support: Difference between revisions
No edit summary |
|||
Line 10: | Line 10: | ||
=Usage Pattern= | =Usage Pattern= | ||
<syntaxhighlight lang='java'> | |||
< | |||
... | ... | ||
EnvironmentVariableProvider p = EnvironmentVariableProvider.getInstance(); | EnvironmentVariableProvider p = EnvironmentVariableProvider.getInstance(); | ||
String value = p.getenv("RUNTIME_DIR"); | String value = p.getenv("RUNTIME_DIR"); | ||
... | ... | ||
</ | </syntaxhighlight> | ||
getInstance() invocation creates and caches a provider. The default implementation, in absence of any special configuration, is SystemEnvironmentVariableProvider, which delegates to: | getInstance() invocation creates and caches a provider. The default implementation, in absence of any special configuration, is SystemEnvironmentVariableProvider, which delegates to: | ||
< | <syntaxhighlight lang='java'> | ||
System.getenv(name); | System.getenv(name); | ||
</ | </syntaxhighlight> | ||
If "env.variable.provider.class.name" system property (the exact string is defined by EnvironmentVariableProvider.ENVIRONMENT_VARIABLE_PROVIDER_CLASS_NAME_SYSTEM_PROPERTY) is defined and contains a fully qualified valid class name that implements EnvironmentVariableProvider, it will be instantiated, cached and used. getInstance() will throw anIllegalStateException if failure to instantiate the custom class is encountered. | If "env.variable.provider.class.name" system property (the exact string is defined by EnvironmentVariableProvider.ENVIRONMENT_VARIABLE_PROVIDER_CLASS_NAME_SYSTEM_PROPERTY) is defined and contains a fully qualified valid class name that implements EnvironmentVariableProvider, it will be instantiated, cached and used. getInstance() will throw anIllegalStateException if failure to instantiate the custom class is encountered. | ||
To reset the mechanism, use: | To reset the mechanism, use: | ||
< | <syntaxhighlight lang='java'> | ||
EnvironmentVariableProvider.reset(); | EnvironmentVariableProvider.reset(); | ||
</ | </syntaxhighlight> | ||
This will cleared the cache instance. | This will cleared the cache instance. |
Revision as of 19:05, 18 September 2017
Internal
Overview
We install a static EnvironmentVariableProvider per class loading domain (JVM), to allow for environment-independent testing.
Usage Pattern
...
EnvironmentVariableProvider p = EnvironmentVariableProvider.getInstance();
String value = p.getenv("RUNTIME_DIR");
...
getInstance() invocation creates and caches a provider. The default implementation, in absence of any special configuration, is SystemEnvironmentVariableProvider, which delegates to:
System.getenv(name);
If "env.variable.provider.class.name" system property (the exact string is defined by EnvironmentVariableProvider.ENVIRONMENT_VARIABLE_PROVIDER_CLASS_NAME_SYSTEM_PROPERTY) is defined and contains a fully qualified valid class name that implements EnvironmentVariableProvider, it will be instantiated, cached and used. getInstance() will throw anIllegalStateException if failure to instantiate the custom class is encountered.
To reset the mechanism, use:
EnvironmentVariableProvider.reset();
This will cleared the cache instance.