1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.argeo.osgi.boot.internal.springutil;
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public abstract class SystemPropertyUtils {
32
33
34 public static final String PLACEHOLDER_PREFIX = "${";
35
36
37 public static final String PLACEHOLDER_SUFFIX = "}";
38
39
40
41
42
43
44
45
46
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
62
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 }