]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/internal/springutil/SystemPropertyUtils.java
Simplify equinox launching
[gpl/argeo-slc.git] / runtime / org.argeo.slc.osgiboot / src / main / java / org / argeo / slc / osgiboot / 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.slc.osgiboot.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 public static String resolvePlaceholders(String text) {
49 StringBuffer buf = new StringBuffer(text);
50
51 int startIndex = buf.indexOf(PLACEHOLDER_PREFIX);
52 while (startIndex != -1) {
53 int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
54 if (endIndex != -1) {
55 String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
56 int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
57 try {
58 String propVal = System.getProperty(placeholder);
59 if (propVal == null) {
60 // Fall back to searching the system environment.
61 propVal = System.getenv(placeholder);
62 }
63 if (propVal != null) {
64 buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
65 nextIndex = startIndex + propVal.length();
66 }
67 else {
68 System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +
69 "] as system property: neither system property nor environment variable found");
70 }
71 }
72 catch (Throwable ex) {
73 System.err.println("Could not resolve placeholder '" + placeholder + "' in [" + text +
74 "] as system property: " + ex);
75 }
76 startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
77 }
78 else {
79 startIndex = -1;
80 }
81 }
82
83 return buf.toString();
84 }
85
86 }