Example usage for org.apache.commons.configuration EnvironmentConfiguration getString

List of usage examples for org.apache.commons.configuration EnvironmentConfiguration getString

Introduction

In this page you can find the example usage for org.apache.commons.configuration EnvironmentConfiguration getString.

Prototype

public String getString(String key) 

Source Link

Usage

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

/**
 * Resolves variable in path given as string
 * /*from  w w w . ja  v  a2s.c  om*/
 * @param input
 *            String input url Code inspired by
 *            :http://stackoverflow.com/questions/2263929/
 *            regarding-application-properties-file-and-environment-variable
 */
public static String resolvePath(String input) {
    if (null == input) {
        return input;
    }

    // matching for 2 groups match ${VAR_NAME} or $VAR_NAME
    Pattern pathPattern = Pattern.compile("\\$\\{(.+?)\\}");
    Matcher matcherPattern = pathPattern.matcher(input); // get a matcher
                                                         // object
    StringBuffer sb = new StringBuffer();
    EnvironmentConfiguration config = new EnvironmentConfiguration();
    SystemConfiguration sysConfig = new SystemConfiguration();

    while (matcherPattern.find()) {

        String confVarName = matcherPattern.group(1) != null ? matcherPattern.group(1)
                : matcherPattern.group(2);
        String envConfVarValue = config.getString(confVarName);
        String sysVarValue = sysConfig.getString(confVarName);

        if (envConfVarValue != null) {

            matcherPattern.appendReplacement(sb, envConfVarValue);

        } else if (sysVarValue != null) {

            matcherPattern.appendReplacement(sb, sysVarValue);

        } else {
            matcherPattern.appendReplacement(sb, "");
        }
    }
    matcherPattern.appendTail(sb);
    return sb.toString();
}