001: /*
002: * Copyright 2002-2006 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: // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
057: // method to retain JDK 1.3 compatibility. The slight loss in performance
058: // is not really relevant, as this code will typically just run on startup.
059:
060: int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
061: while (startIndex != -1) {
062: int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX,
063: startIndex + PLACEHOLDER_PREFIX.length());
064: if (endIndex != -1) {
065: String placeholder = buf.substring(startIndex
066: + PLACEHOLDER_PREFIX.length(), endIndex);
067: int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
068: try {
069: String propVal = System.getProperty(placeholder);
070: if (propVal == null) {
071: // Fall back to searching the system environment.
072: propVal = System.getenv(placeholder);
073: }
074: if (propVal != null) {
075: buf.replace(startIndex, endIndex
076: + PLACEHOLDER_SUFFIX.length(), propVal);
077: nextIndex = startIndex + propVal.length();
078: } else {
079: if (logger.isWarnEnabled()) {
080: logger
081: .warn("Could not resolve placeholder '"
082: + placeholder
083: + "' in ["
084: + text
085: + "] as system property: neither system property nor environment variable found");
086: }
087: }
088: } catch (Throwable ex) {
089: if (logger.isWarnEnabled()) {
090: logger.warn("Could not resolve placeholder '"
091: + placeholder + "' in [" + text
092: + "] as system property: " + ex);
093: }
094: }
095: startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX,
096: nextIndex);
097: } else {
098: startIndex = -1;
099: }
100: }
101:
102: return buf.toString();
103: }
104:
105: }
|