22
22
* Helper class for resolving placeholders in texts. Usually applied to file paths.
23
23
*
24
24
* <p>A text may contain <code>${...}</code> placeholders, to be resolved as system properties: e.g.
25
- * <code>${user.dir}</code>.
25
+ * <code>${user.dir}</code>. Default values can be supplied using the ":" separator between key
26
+ * and value.
26
27
*
27
28
* @author Juergen Hoeller
29
+ * @author Dave Syer
28
30
* @see #PLACEHOLDER_PREFIX
29
31
* @see #PLACEHOLDER_SUFFIX
30
32
* @see System#getProperty(String)
@@ -38,44 +40,76 @@ public abstract class SystemPropertyUtils {
38
40
/** Suffix for system property placeholders: "}" */
39
41
public static final String PLACEHOLDER_SUFFIX = "}" ;
40
42
41
- /** Value separator for system property placeholders: "} " */
43
+ /** Value separator for system property placeholders: ": " */
42
44
public static final String VALUE_SEPARATOR = ":" ;
43
45
44
- private static final PropertyPlaceholderHelper helper =
45
- new PropertyPlaceholderHelper ( PLACEHOLDER_PREFIX , PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false );
46
+ private static final PropertyPlaceholderHelper strictHelper = new PropertyPlaceholderHelper ( PLACEHOLDER_PREFIX ,
47
+ PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false );
46
48
49
+ private static final PropertyPlaceholderHelper nonStrictHelper = new PropertyPlaceholderHelper (PLACEHOLDER_PREFIX ,
50
+ PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , true );
47
51
48
52
/**
49
53
* Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
50
54
* @param text the String to resolve
51
55
* @return the resolved String
52
56
* @see #PLACEHOLDER_PREFIX
53
57
* @see #PLACEHOLDER_SUFFIX
58
+ *
59
+ * @throws IllegalArgumentException if there is an unresolvable placeholder
54
60
*/
55
61
public static String resolvePlaceholders (final String text ) {
56
- return helper .replacePlaceholders (text , new PlaceholderResolver () {
57
- public String resolvePlaceholder (String placeholderName ) {
58
- String propVal = null ;
59
- try {
60
- propVal = System .getProperty (placeholderName );
61
- if (propVal == null ) {
62
- // Fall back to searching the system environment.
63
- propVal = System .getenv (placeholderName );
64
- }
65
-
66
- if (propVal == null ) {
67
- System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" + text +
68
- "] as system property: neither system property nor environment variable found" );
69
- }
62
+ return resolvePlaceholders (text , false );
63
+ }
64
+
65
+ /**
66
+ * Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
67
+ * Unresolvable placeholders with no default value are ignored and passed through unchanged if the
68
+ * flag is set to true.
69
+ *
70
+ * @param text the String to resolve
71
+ * @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
72
+ * @return the resolved String
73
+ * @see #PLACEHOLDER_PREFIX
74
+ * @see #PLACEHOLDER_SUFFIX
75
+ *
76
+ * @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
77
+ *
78
+ */
79
+ public static String resolvePlaceholders (final String text , boolean ignoreUnresolvablePlaceholders ) {
80
+ if (ignoreUnresolvablePlaceholders ) {
81
+ return nonStrictHelper .replacePlaceholders (text , new PlaceholderResolverImplementation (text ));
82
+ }
83
+ return strictHelper .replacePlaceholders (text , new PlaceholderResolverImplementation (text ));
84
+ }
85
+
86
+ private static final class PlaceholderResolverImplementation implements PlaceholderResolver {
87
+ private final String text ;
88
+
89
+ private PlaceholderResolverImplementation (String text ) {
90
+ this .text = text ;
91
+ }
92
+
93
+ public String resolvePlaceholder (String placeholderName ) {
94
+ String propVal = null ;
95
+ try {
96
+ propVal = System .getProperty (placeholderName );
97
+ if (propVal == null ) {
98
+ // Fall back to searching the system environment.
99
+ propVal = System .getenv (placeholderName );
70
100
}
71
- catch (Throwable ex ) {
72
- System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" + text +
73
- "] as system property: " + ex );
74
101
102
+ if (propVal == null ) {
103
+ System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" + text
104
+ + "] as system property: neither system property nor environment variable found" );
75
105
}
76
- return propVal ;
106
+ } catch (Throwable ex ) {
107
+ System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" + text
108
+ + "] as system property: " + ex );
109
+
77
110
}
78
- });
111
+ return propVal ;
112
+ }
79
113
}
80
114
81
115
}
0 commit comments