KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > SystemPropertyUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  * Helper class for resolving placeholders in texts. Usually applied to file paths.
24  *
25  * <p>A text may contain <code>${...}</code> placeholders, to be resolved as
26  * system properties: e.g. <code>${user.dir}</code>.
27  *
28  * @author Juergen Hoeller
29  * @since 1.2.5
30  * @see #PLACEHOLDER_PREFIX
31  * @see #PLACEHOLDER_SUFFIX
32  * @see System#getProperty(String)
33  */

34 public abstract class SystemPropertyUtils {
35
36     /** Prefix for system property placeholders: "${" */
37     public static final String JavaDoc PLACEHOLDER_PREFIX = "${";
38
39     /** Suffix for system property placeholders: "}" */
40     public static final String JavaDoc PLACEHOLDER_SUFFIX = "}";
41
42     private static final Log logger = LogFactory.getLog(SystemPropertyUtils.class);
43
44
45     /**
46      * Resolve ${...} placeholders in the given text,
47      * replacing them with corresponding system property values.
48      * @param text the String to resolve
49      * @return the resolved String
50      * @see #PLACEHOLDER_PREFIX
51      * @see #PLACEHOLDER_SUFFIX
52      */

53     public static String JavaDoc resolvePlaceholders(String JavaDoc text) {
54         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(text);
55
56         // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
57
// method to retain JDK 1.3 compatibility. The slight loss in performance
58
// is not really relevant, as this code will typically just run on startup.
59

60         int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
61         while (startIndex != -1) {
62             int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
63             if (endIndex != -1) {
64                 String JavaDoc placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
65                 int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
66                 try {
67                     String JavaDoc propVal = System.getProperty(placeholder);
68                     if (propVal == null) {
69                         // Fall back to searching the system environment.
70
propVal = System.getenv(placeholder);
71                     }
72                     if (propVal != null) {
73                         buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
74                         nextIndex = startIndex + propVal.length();
75                     }
76                     else {
77                         if (logger.isWarnEnabled()) {
78                             logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text +
79                                     "] as system property: neither system property nor environment variable found");
80                         }
81                     }
82                 }
83                 catch (Throwable JavaDoc ex) {
84                     if (logger.isWarnEnabled()) {
85                         logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text +
86                                 "] as system property: " + ex);
87                     }
88                 }
89                 startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, nextIndex);
90             }
91             else {
92                 startIndex = -1;
93             }
94         }
95
96         return buf.toString();
97     }
98
99 }
100
Popular Tags