Class NBEnvironment
java.lang.Object
io.nosqlbench.nb.api.system.NBEnvironment
Safer, Easier lookup of property and environment variables, so
that commonly used env vars as seen on *n*x systems map to stable
system properties where known, but attempt to fall through to
the env variables if not.
As long as all accesses and mutations for System properties and/or
environment variables are marshaled through a singleton instance
of this class, then users will not easily make a modify-after-reference
bug without a warning or error.
Referencing a variable here means that a value was asked for under a
name, and a value was returned, even if this was the default value.
Properties which contains a dot are presumed to be System properties,
and so in that case System properties take precedence for those.
Environment variables which are known to map to a stable system property
are redirected to the system property.
Otherwise, the environment variable is checked.
Finally, System properties are checked for names not containing a dot.
The first location to return a non-null value is used. Null values are considered
invalid in this API, except when provided as a default value.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final NBEnvironmentstatic final Stringstatic final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsKey(String name) booleancontainsKey(String name, Map<String, String> supplemental) Attempt to read the variable in System properties or the shell environment.Return the value in the first place it is found to be non-null, or the default value otherwise.booleaninterpolate(String word) interpolate(String word, Map<String, String> supplemental) For the given word, if it contains a pattern with '$' followed by alpha, followed by alphanumeric and underscores, replace this pattern with the system property or environment variable of the same name.interpolateEach(CharSequence delim, String toBeRecombined) interpolateWithTimestamp(String rawText, long millis) interpolateWithTimestamp(String rawtext, long millis, Map<String, String> map) Interpolate system properties, environment variables, time fields, and arbitrary replacement strings into a single result.voidvoidtoString()
-
Field Details
-
NBSTATEDIR
- See Also:
-
NBLIBS
- See Also:
-
INSTANCE
-
-
Constructor Details
-
NBEnvironment
public NBEnvironment()
-
-
Method Details
-
resetRefs
-
put
-
getOr
Return the value in the first place it is found to be non-null, or the default value otherwise.- Parameters:
name- The system property or environment variable namedefaultValue- The value to return if the name is not found- Returns:
- the system property or environment variable's value, or the default value
-
getOr
-
get
Attempt to read the variable in System properties or the shell environment. If it is not found, throw an exception.- Parameters:
name- The System property or environment variable- Returns:
- the variable
- Throws:
BasicError- if no value was found which was non-null
-
containsKey
-
containsKey
-
hasPropertyLayer
public boolean hasPropertyLayer() -
setPropertyLayer
public void setPropertyLayer() -
interpolate
For the given word, if it contains a pattern with '$' followed by alpha, followed by alphanumeric and underscores, replace this pattern with the system property or environment variable of the same name. Do this for all occurrences of this pattern. For patterns which start with '${' and end with '}', replace the contents with the same rules as above, but allow any character in between. Nesting is not supported.- Parameters:
word- The word to interpolate the environment values into- Returns:
- The interpolated value, after substitutions, or null if any lookup failed
-
interpolate
-
interpolateEach
-
interpolateWithTimestamp
public final Optional<String> interpolateWithTimestamp(String rawtext, long millis, Map<String, String> map) Interpolate system properties, environment variables, time fields, and arbitrary replacement strings into a single result. Templates such as/tmp/%d-${testrun}-$System.index-SCENARIOare supported.
The tokens found in the raw template are interpolated in the following order.- Any token which exactly matches one of the keys in the provided map is substituted directly as is. No token sigil like '$' is used here, so if you want to support that as is, you need to provide the keys in your substitution map as such.
- Any token in the form
%fwhich is supported by the time fields inFormatter are honored and used with the timestamp provided.
- System Properties: Any token in the form
$word.wordwill be taken as the name of a system property to be substituted. - Environment Variables: Any token in the form
$name will be taken as
an environment variable to be substituted.
- Parameters:
rawtext- The template, including any of the supported token formsmillis- The timestamp to use for any temporal tokensmap- Any additional parameters to interpolate into the template first- Returns:
- Optionally, the interpolated string, as long as all references were qualified. Error handling is contextual to the caller -- If not getting a valid result would cause a downstream error, an error should likely be thrown.
-
interpolateWithTimestamp
-
toString
-