Class PropertyRetriever

java.lang.Object
com.illumon.util.PropertyRetriever

public class PropertyRetriever extends Object
Class to assist with retrieving properties such as passwords from environment variables, files, and properties.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    getFromEnvVarOrProperty(String envVarName, String propName)
    In cases where a value is needed before having a Configuration object (eg, before and etcd client / gRPC has been used to retrieve properties from etcd) it is still desirable to be able to offer customizations.
    static String
    getProperty(com.fishlib.configuration.Configuration configuration, String propertyMeaning, String environmentVariable, String fileProperty, String propertyName, boolean base64Encoded)
    Return a property value from a set of possible locations, allowing for optional base64 decoding.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PropertyRetriever

      public PropertyRetriever()
  • Method Details

    • getProperty

      public static String getProperty(@NotNull com.fishlib.configuration.Configuration configuration, @NotNull String propertyMeaning, @Nullable String environmentVariable, @Nullable String fileProperty, @Nullable String propertyName, boolean base64Encoded)
      Return a property value from a set of possible locations, allowing for optional base64 decoding. The following order is used.
      • First, if an environment variable is provided, it is checked. if it exists, then it is returned, base64-decoded if requested.
      • Next, the property file is checked if the property is provided (the configuration is checked for the provided property, and the resulting property value defines the filename). If it exists, the contents are read, base64-decoded if requested, then returned.
      • Finally, the property is checked. If it exists it is base64-decoded if requested and returned.
      • At least one of environmentVariable, fileProperty, or propertyName must be specified and exist.
      • If both a property file and property name are provided and exist in the Configuration instance, an exception will be thrown.
      Parameters:
      configuration - the Configuration instance to check
      propertyMeaning - a user-friendly property meaning, included in thrown exceptions
      environmentVariable - an optional environment variable to check for the value
      fileProperty - an optional Configuration property that specifies the file that contains the value
      propertyName - an optional Configuration property that specifies the value
      base64Encoded - if true, the retrieved value is base64 decoded before being returned to the caller
      Returns:
      the found value, base64-decoded if requested
    • getFromEnvVarOrProperty

      public static String getFromEnvVarOrProperty(String envVarName, String propName)
      In cases where a value is needed before having a Configuration object (eg, before and etcd client / gRPC has been used to retrieve properties from etcd) it is still desirable to be able to offer customizations. This method supports reading from an environment variable (higher precedence) or java system properties (lower precedence) via -DProp=value in the JVM invocation.
      Parameters:
      envVarName - Name of environment variable to look up, or null for none.
      propName - Name of property to look up, or null for none.
      Returns:
      a string value found, or null for none.