2 * Copyright 2002-2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgiboot
.internal
.springutil
;
20 * Helper class for resolving placeholders in texts. Usually applied to file paths.
22 * <p>A text may contain <code>${...}</code> placeholders, to be resolved as
23 * system properties: e.g. <code>${user.dir}</code>.
25 * @author Juergen Hoeller
27 * @see #PLACEHOLDER_PREFIX
28 * @see #PLACEHOLDER_SUFFIX
29 * @see System#getProperty(String)
31 public abstract class SystemPropertyUtils
{
33 /** Prefix for system property placeholders: "${" */
34 public static final String PLACEHOLDER_PREFIX
= "${";
36 /** Suffix for system property placeholders: "}" */
37 public static final String PLACEHOLDER_SUFFIX
= "}";
41 * Resolve ${...} placeholders in the given text,
42 * replacing them with corresponding system property values.
43 * @param text the String to resolve
44 * @return the resolved String
45 * @see #PLACEHOLDER_PREFIX
46 * @see #PLACEHOLDER_SUFFIX
48 public static String
resolvePlaceholders(String text
) {
49 StringBuffer buf
= new StringBuffer(text
);
51 int startIndex
= buf
.indexOf(PLACEHOLDER_PREFIX
);
52 while (startIndex
!= -1) {
53 int endIndex
= buf
.indexOf(PLACEHOLDER_SUFFIX
, startIndex
+ PLACEHOLDER_PREFIX
.length());
55 String placeholder
= buf
.substring(startIndex
+ PLACEHOLDER_PREFIX
.length(), endIndex
);
56 int nextIndex
= endIndex
+ PLACEHOLDER_SUFFIX
.length();
58 String propVal
= System
.getProperty(placeholder
);
59 if (propVal
== null) {
60 // Fall back to searching the system environment.
61 propVal
= System
.getenv(placeholder
);
63 if (propVal
!= null) {
64 buf
.replace(startIndex
, endIndex
+ PLACEHOLDER_SUFFIX
.length(), propVal
);
65 nextIndex
= startIndex
+ propVal
.length();
68 System
.err
.println("Could not resolve placeholder '" + placeholder
+ "' in [" + text
+
69 "] as system property: neither system property nor environment variable found");
72 catch (Throwable ex
) {
73 System
.err
.println("Could not resolve placeholder '" + placeholder
+ "' in [" + text
+
74 "] as system property: " + ex
);
76 startIndex
= buf
.indexOf(PLACEHOLDER_PREFIX
, nextIndex
);
83 return buf
.toString();