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.compass.core.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 kimchy
029: * @see #PLACEHOLDER_PREFIX
030: * @see #PLACEHOLDER_SUFFIX
031: * @see System#getProperty(String)
032: */
033: public abstract class SystemPropertyUtils {
034:
035: /**
036: * Prefix for system property placeholders: "${"
037: */
038: public static final String PLACEHOLDER_PREFIX = "${";
039:
040: /**
041: * Suffix for system property placeholders: "}"
042: */
043: public static final String PLACEHOLDER_SUFFIX = "}";
044:
045: private static final Log logger = LogFactory
046: .getLog(SystemPropertyUtils.class);
047:
048: /**
049: * Resolve ${...} placeholders in the given text,
050: * replacing them with corresponding system property values.
051: *
052: * @param text the String to resolve
053: * @return the resolved String
054: * @see #PLACEHOLDER_PREFIX
055: * @see #PLACEHOLDER_SUFFIX
056: */
057: public static String resolvePlaceholders(String text) {
058: if (text == null) {
059: return null;
060: }
061:
062: StringBuffer buf = new StringBuffer(text);
063:
064: // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
065: // method to retain JDK 1.3 compatibility. The slight loss in performance
066: // is not really relevant, as this code will typically just run on startup.
067:
068: int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
069: while (startIndex != -1) {
070: int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX,
071: startIndex + PLACEHOLDER_PREFIX.length());
072: if (endIndex != -1) {
073: String placeholder = buf.substring(startIndex
074: + PLACEHOLDER_PREFIX.length(), endIndex);
075: int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
076: try {
077: String propVal = System.getProperty(placeholder);
078: if (propVal == null) {
079: // Fall back to searching the system environment.
080: propVal = System.getenv(placeholder);
081: }
082: if (propVal != null) {
083: buf.replace(startIndex, endIndex
084: + PLACEHOLDER_SUFFIX.length(), propVal);
085: nextIndex = startIndex + propVal.length();
086: } else {
087: if (logger.isWarnEnabled()) {
088: logger
089: .warn("Could not resolve placeholder '"
090: + placeholder
091: + "' in ["
092: + text
093: + "] as system property: neither system property nor environment variable found");
094: }
095: }
096: } catch (Throwable ex) {
097: if (logger.isWarnEnabled()) {
098: logger.warn("Could not resolve placeholder '"
099: + placeholder + "' in [" + text
100: + "] as system property: " + ex);
101: }
102: }
103: startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX,
104: nextIndex);
105: } else {
106: startIndex = -1;
107: }
108: }
109:
110: return buf.toString();
111: }
112:
113: }
|