001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.util;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: * Helper class for resolving placeholders in texts. Usually applied to file paths.
024: *
025: * <p>A text may contain <code>${...}</code> placeholders, to be resolved as
026: * system properties: e.g. <code>${user.dir}</code>.
027: *
028: * @author Juergen Hoeller
029: * @since 1.2.5
030: * @see #PLACEHOLDER_PREFIX
031: * @see #PLACEHOLDER_SUFFIX
032: * @see System#getProperty(String)
033: */
034: public abstract class SystemPropertyUtils {
035:
036: /** Prefix for system property placeholders: "${" */
037: public static final String PLACEHOLDER_PREFIX = "${";
038:
039: /** Suffix for system property placeholders: "}" */
040: public static final String PLACEHOLDER_SUFFIX = "}";
041:
042: private static final Log logger = LogFactory
043: .getLog(SystemPropertyUtils.class);
044:
045: /**
046: * Resolve ${...} placeholders in the given text,
047: * replacing them with corresponding system property values.
048: * @param text the String to resolve
049: * @return the resolved String
050: * @see #PLACEHOLDER_PREFIX
051: * @see #PLACEHOLDER_SUFFIX
052: */
053: public static String resolvePlaceholders(String text) {
054: StringBuffer buf = new StringBuffer(text);
055:
056: int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
057: while (startIndex != -1) {
058: int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex
059: + PLACEHOLDER_PREFIX.length());
060: if (endIndex != -1) {
061: String placeholder = buf.substring(startIndex
062: + PLACEHOLDER_PREFIX.length(), endIndex);
063: int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
064: try {
065: String propVal = System.getProperty(placeholder);
066: if (propVal == null) {
067: // Fall back to searching the system environment.
068: propVal = System.getenv(placeholder);
069: }
070: if (propVal != null) {
071: buf.replace(startIndex, endIndex
072: + PLACEHOLDER_SUFFIX.length(), propVal);
073: nextIndex = startIndex + propVal.length();
074: } else {
075: if (logger.isWarnEnabled()) {
076: logger
077: .warn("Could not resolve placeholder '"
078: + placeholder
079: + "' in ["
080: + text
081: + "] as system property: neither system property nor environment variable found");
082: }
083: }
084: } catch (Throwable ex) {
085: if (logger.isWarnEnabled()) {
086: logger.warn("Could not resolve placeholder '"
087: + placeholder + "' in [" + text
088: + "] as system property: " + ex);
089: }
090: }
091: startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
092: } else {
093: startIndex = -1;
094: }
095: }
096:
097: return buf.toString();
098: }
099:
100: }
|