]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/internal/springutil/SystemPropertyUtils.java
Increase release number for TP.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / internal / springutil / SystemPropertyUtils.java
1 /*
2 * Copyright 2002-2008 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.argeo.osgi.boot.internal.springutil;
18
19 /**
20 * Helper class for resolving placeholders in texts. Usually applied to file paths.
21 *
22 * <p>A text may contain <code>${...}</code> placeholders, to be resolved as
23 * system properties: e.g. <code>${user.dir}</code>.
24 *
25 * @author Juergen Hoeller
26 * @since 1.2.5
27 * @see #PLACEHOLDER_PREFIX
28 * @see #PLACEHOLDER_SUFFIX
29 * @see System#getProperty(String)
30 */
31 public abstract class SystemPropertyUtils {
32
33 /** Prefix for system property placeholders: "${" */
34 public static final String PLACEHOLDER_PREFIX = "${";
35
36 /** Suffix for system property placeholders: "}" */
37 public static final String PLACEHOLDER_SUFFIX = "}";
38
39
40 /**
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
47 */
48 @SuppressWarnings("unused")
49 public static String resolvePlaceholders(String text) {
50 StringBuffer buf = new StringBuffer(text);
51
52 int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
53 while (startIndex != -1) {
54 int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
55 if (endIndex != -1) {
56 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
57 int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
58 try {
59 String propVal = System.getProperty(placeholder);
60 if (propVal == null) {
61 // Fall back to searching the system environment.
62 //propVal = System.getenv(placeholder);// mbaudier - 2009-07-26
63 throw new Error("getenv no longer supported, use properties and -D instead: " + placeholder);
64 }
65 if (propVal != null) {
66 buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
67 nextIndex = startIndex + propVal.length();
68 }
69 else {
70 System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +
71 "] as system property: neither system property nor environment variable found");
72 }
73 }
74 catch (Throwable ex) {
75 System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +
76 "] as system property: " + ex);
77 }
78 startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
79 }
80 else {
81 startIndex = -1;
82 }
83 }
84
85 return buf.toString();
86 }
87
88 }