Class PropertyPlaceholderResolver

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

public class PropertyPlaceholderResolver
extends java.lang.Object
implements java.io.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 Summary

    Constructors
    Constructor Description
    PropertyPlaceholderResolver()  
    PropertyPlaceholderResolver​(java.util.Map<java.lang.String,​java.lang.String> externalProperties)  
  • Method Summary

    Modifier and Type Method Description
    protected java.lang.String resolvePlaceholder​(java.lang.String property, java.lang.String defaultValue)
    Search for the given placeholder in system properties then in environment variables.
    java.lang.String resolveString​(java.lang.String string)
    Parse the given string resolving property placeholders (${my-property[:default-value]})

    Methods inherited from class java.lang.Object

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

    • PropertyPlaceholderResolver

      public PropertyPlaceholderResolver​(java.util.Map<java.lang.String,​java.lang.String> externalProperties)
    • PropertyPlaceholderResolver

      public PropertyPlaceholderResolver()
  • Method Details

    • resolveString

      public java.lang.String resolveString​(java.lang.String string) throws java.lang.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:
      java.lang.IllegalStateException - a property placeholder could not be resolved and no default value has been defined.
    • resolvePlaceholder

      protected java.lang.String resolvePlaceholder​(java.lang.String property, java.lang.String defaultValue) throws java.lang.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:
      java.lang.IllegalStateException - if the placeholder is not found and the given defaultValue is not defined (null)