All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jmxtrans.agent.util.PropertyPlaceholderResolver Maven / Gradle / Ivy

There is a newer version: 1.2.11
Show newest version
/*
 * Copyright (c) 2010-2013 the original author or authors
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package org.jmxtrans.agent.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


/**
 * 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. *
  3. System.getenv("graphite.port")
  4. *
  5. System.getenv("GRAPHITE_PORT")
  6. *
  7. default to 2003
  8. *
* * @author Cyrille Le Clerc */ public class PropertyPlaceholderResolver implements Serializable { private static final long serialVersionUID = 1L; private Map externalProperties; public PropertyPlaceholderResolver(Map externalProperties) { this.externalProperties = Objects.requireNonNull(externalProperties); } public PropertyPlaceholderResolver() { this(new HashMap()); } /** * Parse the given string resolving property placeholders (${my-property[:default-value]}) * * @param string the string to parse. * @return the parsed string. Non null. * @throws IllegalStateException a property placeholder could not be resolved and no default value has been defined. */ public String resolveString(String string) throws IllegalStateException { StringBuilder result = new StringBuilder(string.length()); int position = 0; while (position < string.length()) { char c = string.charAt(position); if (c == '$' && (position < string.length() - 1) && string.charAt(position + 1) == '{') { int beginningBracketPosition = position + 1; int endingBracketPosition = string.indexOf('}', beginningBracketPosition); int colonPosition = string.indexOf(':', beginningBracketPosition); if (colonPosition > endingBracketPosition) { colonPosition = -1; } String placeholder; String defaultValue; if (colonPosition == -1) { placeholder = string.substring(beginningBracketPosition + 1, endingBracketPosition); defaultValue = null; } else { placeholder = string.substring(beginningBracketPosition + 1, colonPosition); defaultValue = string.substring(colonPosition + 1, endingBracketPosition); } String resolvedPlaceHolder = resolvePlaceholder(placeholder, defaultValue); result.append(resolvedPlaceHolder); position = endingBracketPosition + 1; } else { result.append(c); position++; } } return result.toString(); } /** * Search for the given placeholder in system properties then in environment variables. * * @param property property to resolve * @param defaultValue Default value if the placeholder is not found. null means not default value is * defined and the placeholder must exist * @return 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) */ protected String resolvePlaceholder(String property, String defaultValue) throws IllegalStateException { // "graphite.host" -> "GRAPHITE_HOST" String environmentVariableStyleProperty = property.toUpperCase(); environmentVariableStyleProperty = environmentVariableStyleProperty.replaceAll("\\.", "_"); String result; if (externalProperties.containsKey(property)) { result = externalProperties.get(property); } else if (System.getProperties().containsKey(property)) { result = System.getProperty(property); } else if (System.getenv().containsKey(property)) { result = System.getenv(property); } else if (System.getenv().containsKey(environmentVariableStyleProperty)) { result = System.getenv(environmentVariableStyleProperty); } else if (defaultValue != null) { result = defaultValue; } else { throw new IllegalStateException("Property '" + property + "' not found in System properties nor in Environment variables"); } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy