Nova Ordis Utilities Environment Variable Support: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Novaordis-utilities#Subjects|novaordis-utilities]]
* [[Novaordis-utilities#Subjects|novaordis-utilities]]
* [[Nova_Ordis_Generic_Variable_and_Expression_System#Environment_Variable_Access|Nova Ordis Generic Variable and Expression System]]


=Overview=
=Overview=


We install a static EnvironmentVariableProvider per class loading domain (JVM), to allow for environment-independent testing.  
We install a static EnvironmentVariableProvider per class loading domain (JVM), to allow for environment-independent testing.


=Usage Pattern=
The environment variable support can also be integrated with the [[Nova Ordis Generic Variable and Expression System|generic variable and expression system]] via [https://github.com/NovaOrdis/novaordis-utilities/blob/master/src/main/java/io/novaordis/utilities/expressions/env/OSProcessScope.java OSProcessScope].


=Simple Usage Pattern=


<pre>
<syntaxhighlight lang='java'>
...
...
EnvironmentVariableProvider p = EnvironmentVariableProvider.getInstance();
EnvironmentVariableProvider p = EnvironmentVariableProvider.getInstance();
String value = p.getenv("RUNTIME_DIR");
String value = p.getenv("RUNTIME_DIR");
...
...
</pre>
</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:


<pre>
<syntaxhighlight lang='java'>
System.getenv(name);
System.getenv(name);
</pre>
</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:


<pre>
<syntaxhighlight lang='java'>
EnvironmentVariableProvider.reset();
EnvironmentVariableProvider.reset();
</pre>
</syntaxhighlight>


This will cleared the cache instance.
This will cleared the cache instance.
=Variable-based Usage Pattern=
<font color=red>TODO</font>

Latest revision as of 19:14, 18 September 2017

Internal

Overview

We install a static EnvironmentVariableProvider per class loading domain (JVM), to allow for environment-independent testing.

The environment variable support can also be integrated with the generic variable and expression system via OSProcessScope.

Simple 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.

Variable-based Usage Pattern

TODO