Class PropertyPlaceholderResolver

java.lang.Object
org.jmxtrans.agent.util.PropertyPlaceholderResolver
All Implemented Interfaces:
Serializable

public class PropertyPlaceholderResolver
extends Object
implements Serializable
Inspired by Spring Property placeholder mechanism. Placeholders are delimited by '{' and '}' and support default value with ':'. Sample : '{graphite.host}' or '{graphite.port:2003}'. If the placeholder is not found in the system properties, it is searched in the environment variables and then converted to underscore delimited upper case and searched in environment variables. Sample for '{graphite.port:2003}':
  1. System.getProperty("graphite.port")
  2. System.getenv("graphite.port")
  3. System.getenv("GRAPHITE_PORT")
  4. default to 2003
Author:
Cyrille Le Clerc
See Also:
Serialized Form
  • Constructor Details

    • PropertyPlaceholderResolver

      public PropertyPlaceholderResolver​(Map<String,​String> externalProperties)
    • PropertyPlaceholderResolver

      public PropertyPlaceholderResolver()
  • Method Details

    • resolveString

      public String resolveString​(String string) throws IllegalStateException
      Parse the given string resolving property placeholders (${my-property[:default-value]})
      Parameters:
      string - the string to parse.
      Returns:
      the parsed string. Non null.
      Throws:
      IllegalStateException - a property placeholder could not be resolved and no default value has been defined.
    • resolvePlaceholder

      protected String resolvePlaceholder​(String property, String defaultValue) throws IllegalStateException
      Search for the given placeholder in system properties then in environment variables.
      Parameters:
      property - property to resolve
      defaultValue - Default value if the placeholder is not found. null means not default value is defined and the placeholder must exist
      Returns:
      the resolved property or the default value if the placeholder is not found and the default value is defined. Non null.
      Throws:
      IllegalStateException - if the placeholder is not found and the given defaultValue is not defined (null)