001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)PropertyFilter.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.util;
030:
031: /**
032: * This is a helper class to get Application Server configuration information.
033: *
034: * @author Sun Microsystems, Inc.
035: */
036: public class PropertyFilter {
037: public static final String SYSTEM_PROPERTY_PREFIX = "${";
038: public static final String SYSTEM_PROPERTY_SUFFIX = "}";
039:
040: /**
041: * This operation parses the path string and replaces any occurences of
042: * a system property with the value of the system property.
043: *
044: * A system property is one which is enclosed in curly braces with a '$' prefix.
045: * Example: ${i.am.a.property}
046: */
047:
048: public static String filterProperties(String string) {
049: String resStr = string;
050:
051: if (string != null) {
052: int x = string.indexOf(SYSTEM_PROPERTY_PREFIX);
053: int y = string.indexOf(SYSTEM_PROPERTY_SUFFIX);
054:
055: // -- there aren't any embedded properties
056: if (x == -1 || y == -1) {
057: return resStr;
058: }
059:
060: String a = string.substring(0, x);
061: String property = string.substring(x + 2, y);
062:
063: StringBuffer strBuf = new StringBuffer(a);
064: strBuf.append(System.getProperty(property));
065: if (y < string.length()) {
066: String remainder = string.substring(y + 1);
067: strBuf.append(filterProperties(remainder));
068: }
069: resStr = strBuf.toString();
070: }
071: return resStr;
072:
073: }
074:
075: /**
076: * Replace any occurence of a system property value with the system property key
077: *
078: * @param value - the value to be replaced with the key
079: * @param key - the property key whose value is to be searched and replaced.
080: */
081: public static String replacePropertyValues(String string, String key) {
082: String value = System.getProperty(key);
083:
084: if (value == null) {
085: return string;
086: }
087:
088: String resStr = string;
089: if (string != null) {
090: string = string.trim();
091: String normalizedString = normalizeString(string);
092:
093: if (key != null) {
094: String normalizedValue = normalizeString(value);
095:
096: int x = normalizedString.indexOf(normalizedValue);
097: int y = x + normalizedValue.length() - 1;
098:
099: if (x == -1 || y == -1) {
100: return resStr;
101: }
102:
103: String a = string.substring(0, x);
104:
105: StringBuffer strBuf = new StringBuffer(a);
106: strBuf.append(SYSTEM_PROPERTY_PREFIX);
107: strBuf.append(key);
108: strBuf.append(SYSTEM_PROPERTY_SUFFIX);
109:
110: if (y < normalizedString.length()) {
111: String remainder = string.substring(y + 1);
112: strBuf
113: .append(replacePropertyValues(remainder,
114: key));
115: }
116: resStr = strBuf.toString();
117: }
118: }
119: return resStr;
120:
121: }
122:
123: /**
124: * Normalize the string for comparison. Replace all '\' with '/'
125: *
126: * @param srcStr - source string to nromalize
127: */
128: private static String normalizeString(String srcStr) {
129: return srcStr.replace('\\', '/');
130: }
131: }
|