Class HelixEnvironment

java.lang.Object
com.helixframework.env.HelixEnvironment
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware

@Component public class HelixEnvironment extends Object implements org.springframework.context.ApplicationContextAware
Holds context about the currently executing helix environment.
  • Field Details

  • Constructor Details

    • HelixEnvironment

      @Autowired public HelixEnvironment(org.springframework.core.env.Environment environment)
      Creates an instance of the HelixEnvironment based on the supplied Spring Environment.
      Parameters:
      environment - spring environment
  • Method Details

    • getEnv

      public String getEnv(String name)
      Gets the value of the environment variable.
      Parameters:
      name - environment variable name
      Returns:
      the string value of the variable, or null if the variable is not defined in the system environment
    • getEnv

      public String getEnv(String name, String defaultValue)
      Gets the value of the environment variable.
      Parameters:
      name - environment variable name
      defaultValue - string value to return if environment variable does not exist
      Returns:
      the string value of the variable, or the default value if the variable is not defined in the system environment
    • containsProperty

      public boolean containsProperty(String key)
      Return whether the given property key is available for resolution, i.e. if the value for the given key is not null.
      Parameters:
      key - property key
      Returns:
      true if the property key exists; otherwise false
    • getProperty

      public String getProperty(String key)
      Gets the string value of the system property.
      Parameters:
      key - property key
      Returns:
      the string value of the property, or null if the property does not exist
    • getProperty

      public String getProperty(String key, String defaultValue)
      Gets the string value of the system property.
      Parameters:
      key - property key
      defaultValue - string value to return if the property does not exist
      Returns:
      the string value of the property, or the default value if the property is not defined
    • getProperty

      public <T> T getProperty(String key, Class<T> targetType)
      Gets the value of the system property.
      Type Parameters:
      T - property value type
      Parameters:
      key - property key
      targetType - the expected type of the property value
      Returns:
      the value of the property, or null if the property does not exist
    • getProperty

      public <T> T getProperty(String key, Class<T> targetType, T defaultValue)
      Gets the value of the system property.
      Type Parameters:
      T - property value type
      Parameters:
      key - property key
      targetType - the expected type of the property value
      defaultValue - value to return if the property does not exist
      Returns:
      the value of the property
    • getDelimitedProperty

      public Optional<List<String>> getDelimitedProperty(String key, String delimiter)
      Gets the value of a delimited property.

      For example, a property with value `foo,bar,baz` would return a list containing [`foo`, `bar`, `baz`].

      Parameters:
      key - property key
      delimiter - property delimiter
      Returns:
      the value of the property if it exists
    • getDelimitedProperty

      public List<String> getDelimitedProperty(String key, String delimiter, List<String> defaults)
      Gets the value of a delimited property. If property does not exist, returns the provided defaults.
      Parameters:
      key - property key
      delimiter - property delimiter
      defaults - default list of values to return if property does not exist
      Returns:
      the value of the property if it exists, or the default list of values
    • getRequiredProperty

      public String getRequiredProperty(String key)
      Gets the string value of the system property; throwing a RuntimeException if the property is not defined.
      Parameters:
      key - property key
      Returns:
      the string value of the property
      Throws:
      RuntimeException - if the specified property is not defined
    • getRequiredProperty

      public <T> T getRequiredProperty(String key, Class<T> targetType)
      Gets the value of the system property; throwing a RuntimeException if the property is not defined.
      Type Parameters:
      T - property value type
      Parameters:
      key - property key
      targetType - the expected type of the property value
      Returns:
      the value of the property
    • getProperties

      public Properties getProperties()
      Gets all system properties.
      Returns:
      system properties
    • getProperties

      public Properties getProperties(String baseName)
      Gets all system properties whose keys begin with the supplied string (basename).

      For example getProperties("app.database") would return all properties whose keys begin with the "app.database" string.

      Parameters:
      baseName - base property key name
      Returns:
      all system properties whose keys begin with the basename
    • getProperties

      public Properties getProperties(Class<?> baseClass)
      Gets all properties whose keys begin with the package name of the supplied class.
      Parameters:
      baseClass - base class from which to get the package name to use for basename
      Returns:
      all system properties whose keys begin with the package name
    • getProperties

      public Properties getProperties(Pattern pattern)
      Gets all properties whose keys match the regular expression.
      Parameters:
      pattern - regex
      Returns:
      all system properties whose keys match the regular expression
    • resolveValue

      public String resolveValue(String propertyKey, String varName)
      Gets the string value of a property or environment variable with the supplied names.

      System properties are resolved first, followed by environment variables.

      Parameters:
      propertyKey - property key
      varName - environment variable
      Returns:
      the value of the property or environment variable, or null if neither exist
    • resolveValue

      public String resolveValue(String propertyKey, String varName, String defaultValue)
      Gets the string value of a property or environment variable with the supplied names.
      Parameters:
      propertyKey - property key
      varName - environment variable
      defaultValue - string value to return if both system property and environment variable do not exist
      Returns:
      the value of the property or environment variable, or the default value if neither the system property nor the environment variable exist
    • isDebug

      public boolean isDebug()
      Gets the current debug state of the application.
      Returns:
      true if in debug mode; otherwise false
    • isLocal

      public boolean isLocal()
      Checks to see if the application is running in the "local" environment.
      Returns:
      true if application is running in local environment; otherwise false
    • getEnvironmentName

      public String getEnvironmentName()
      Gets the Helix environment name.
      Returns:
      environment
    • getAccount

      public String getAccount()
      Gets the Helix account.
      Returns:
      account
    • getOrganization

      public String getOrganization()
      Gets the Helix organization name.
      Returns:
      organization
    • getRegion

      public String getRegion()
      Gets the current region.
      Returns:
      region
    • getTeam

      public String getTeam()
      Gets the Helix team name.
      Returns:
      team
    • getAppId

      public String getAppId()
      Gets the Helix application identifier.
      Returns:
      application identifier
    • getInstanceId

      public String getInstanceId()
      Gets the instance identifier.
      Returns:
      instance identifier
    • getHelixVersion

      public String getHelixVersion()
      Gets the Helix version.
      Returns:
      helix version
    • getAppVersion

      public String getAppVersion()
      Gets the version of the application if available.
      Returns:
      application version
    • getMetadata

      public Map<String,Object> getMetadata()
      Gets metadata.
      Returns:
      metadata in environment variables and system properties
    • addMetadata

      public void addMetadata(String key, Object value)
      Adds a new metadata entry.
      Parameters:
      key - entry key
      value - entry value
    • addAllMetadata

      public void addAllMetadata(Map<String,Object> values)
      Adds a collection of metadata entries.
      Parameters:
      values - metadata to add to the metadata collection
    • getApplicationContext

      public org.springframework.context.ApplicationContext getApplicationContext()
      Gets the spring application context.
      Returns:
      the spring application context
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException