26
26
* and value.
27
27
*
28
28
* @author Juergen Hoeller
29
+ * @author Rob Harrop
29
30
* @author Dave Syer
31
+ * @since 1.2.5
30
32
* @see #PLACEHOLDER_PREFIX
31
33
* @see #PLACEHOLDER_SUFFIX
32
34
* @see System#getProperty(String)
33
- * @since 1.2.5
34
35
*/
35
36
public abstract class SystemPropertyUtils {
36
37
@@ -43,19 +44,20 @@ public abstract class SystemPropertyUtils {
43
44
/** Value separator for system property placeholders: ":" */
44
45
public static final String VALUE_SEPARATOR = ":" ;
45
46
46
- private static final PropertyPlaceholderHelper strictHelper = new PropertyPlaceholderHelper (PLACEHOLDER_PREFIX ,
47
- PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false );
48
47
49
- private static final PropertyPlaceholderHelper nonStrictHelper = new PropertyPlaceholderHelper (PLACEHOLDER_PREFIX ,
50
- PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , true );
48
+ private static final PropertyPlaceholderHelper strictHelper =
49
+ new PropertyPlaceholderHelper (PLACEHOLDER_PREFIX , PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , false );
50
+
51
+ private static final PropertyPlaceholderHelper nonStrictHelper =
52
+ new PropertyPlaceholderHelper (PLACEHOLDER_PREFIX , PLACEHOLDER_SUFFIX , VALUE_SEPARATOR , true );
53
+
51
54
52
55
/**
53
56
* Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
54
57
* @param text the String to resolve
55
58
* @return the resolved String
56
59
* @see #PLACEHOLDER_PREFIX
57
60
* @see #PLACEHOLDER_SUFFIX
58
- *
59
61
* @throws IllegalArgumentException if there is an unresolvable placeholder
60
62
*/
61
63
public static String resolvePlaceholders (final String text ) {
@@ -66,49 +68,41 @@ public static String resolvePlaceholders(final String text) {
66
68
* Resolve ${...} placeholders in the given text, replacing them with corresponding system property values.
67
69
* Unresolvable placeholders with no default value are ignored and passed through unchanged if the
68
70
* flag is set to true.
69
- *
70
71
* @param text the String to resolve
71
72
* @param ignoreUnresolvablePlaceholders flag to determine is unresolved placeholders are ignored
72
73
* @return the resolved String
73
74
* @see #PLACEHOLDER_PREFIX
74
75
* @see #PLACEHOLDER_SUFFIX
75
- *
76
76
* @throws IllegalArgumentException if there is an unresolvable placeholder and the flag is false
77
- *
78
77
*/
79
78
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 ));
79
+ PropertyPlaceholderHelper helper = (ignoreUnresolvablePlaceholders ? nonStrictHelper : strictHelper );
80
+ return helper .replacePlaceholders (text , new SystemPropertyPlaceholderResolver (text ));
84
81
}
85
82
86
- private static final class PlaceholderResolverImplementation implements PlaceholderResolver {
83
+
84
+ private static class SystemPropertyPlaceholderResolver implements PlaceholderResolver {
85
+
87
86
private final String text ;
88
87
89
- private PlaceholderResolverImplementation (String text ) {
88
+ public SystemPropertyPlaceholderResolver (String text ) {
90
89
this .text = text ;
91
90
}
92
91
93
92
public String resolvePlaceholder (String placeholderName ) {
94
- String propVal = null ;
95
93
try {
96
- propVal = System .getProperty (placeholderName );
94
+ String propVal = System .getProperty (placeholderName );
97
95
if (propVal == null ) {
98
96
// Fall back to searching the system environment.
99
97
propVal = System .getenv (placeholderName );
100
98
}
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" );
105
- }
106
- } catch (Throwable ex ) {
107
- System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" + text
108
- + "] as system property: " + ex );
109
-
99
+ return propVal ;
100
+ }
101
+ catch (Throwable ex ) {
102
+ System .err .println ("Could not resolve placeholder '" + placeholderName + "' in [" +
103
+ this .text + "] as system property: " + ex );
104
+ return null ;
110
105
}
111
- return propVal ;
112
106
}
113
107
}
114
108
0 commit comments