Class NBEnvironment

java.lang.Object
io.nosqlbench.nb.api.system.NBEnvironment

public class NBEnvironment extends Object
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 Details

  • Constructor Details

    • NBEnvironment

      public NBEnvironment()
  • Method Details

    • resetRefs

      public NBEnvironment resetRefs()
    • put

      public void put(String propname, String value)
    • getOr

      public String getOr(String name, String defaultValue, Map<String,String> supplemental)
      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 name
      defaultValue - The value to return if the name is not found
      Returns:
      the system property or environment variable's value, or the default value
    • getOr

      public String getOr(String name, String defaultValue)
    • get

      public String get(String name)
      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

      public boolean containsKey(String name)
    • containsKey

      public boolean containsKey(String name, Map<String,String> supplemental)
    • hasPropertyLayer

      public boolean hasPropertyLayer()
    • setPropertyLayer

      public void setPropertyLayer()
    • interpolate

      public Optional<String> 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. 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

      public Optional<String> interpolate(String word)
    • interpolateEach

      public List<String> interpolateEach(CharSequence delim, String toBeRecombined)
    • 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-SCENARIO are 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 %f which is supported by the time fields in Formatter
      • are honored and used with the timestamp provided.
      • System Properties: Any token in the form $word.word will 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 forms
      millis - The timestamp to use for any temporal tokens
      map - 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

      public final Optional<String> interpolateWithTimestamp(String rawText, long millis)
    • toString

      public String toString()
      Overrides:
      toString in class Object