17
17
package org .keycloak .common .util ;
18
18
19
19
import java .io .File ;
20
- import java .util .Properties ;
20
+ import java .util .Optional ;
21
21
22
22
/**
23
- * A utility class for replacing properties in strings.
23
+ * A utility class for replacing properties in strings.
24
24
*
25
25
* @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
26
26
* @author <a href="Scott.Stark@jboss.org">Scott Stark</a>
27
27
* @author <a href="claudio.vesco@previnet.it">Claudio Vesco</a>
28
28
* @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
29
29
* @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
30
- * @version <tt>$Revision: 2898 $</tt>
30
+ * @version <tt>$Revision: 2898 $</tt>
31
31
*/
32
32
public final class StringPropertyReplacer
33
33
{
34
- /** New line string constant */
35
- public static final String NEWLINE = System .getProperty ("line.separator" , "\n " );
36
34
37
35
/** File separator value */
38
36
private static final String FILE_SEPARATOR = File .separator ;
@@ -51,7 +49,12 @@ public final class StringPropertyReplacer
51
49
private static final int SEEN_DOLLAR = 1 ;
52
50
private static final int IN_BRACKET = 2 ;
53
51
54
- private static final Properties systemEnvProperties = new SystemEnvProperties ();
52
+ private static final PropertyResolver NULL_RESOLVER = property -> null ;
53
+ private static PropertyResolver DEFAULT_PROPERTY_RESOLVER ;
54
+
55
+ public static void setDefaultPropertyResolver (PropertyResolver systemVariables ) {
56
+ DEFAULT_PROPERTY_RESOLVER = systemVariables ;
57
+ }
55
58
56
59
/**
57
60
* Go through the input string and replace any occurrence of ${p} with
@@ -72,14 +75,13 @@ public final class StringPropertyReplacer
72
75
* @return the input string with all property references replaced if any.
73
76
* If there are no valid references the input string will be returned.
74
77
*/
75
- public static String replaceProperties (final String string )
76
- {
77
- return replaceProperties (string , (Properties ) null );
78
+ public static String replaceProperties (final String string ) {
79
+ return replaceProperties (string , getDefaultPropertyResolver ());
78
80
}
79
81
80
82
/**
81
83
* Go through the input string and replace any occurrence of ${p} with
82
- * the props.getProperty(p) value. If there is no such property p defined,
84
+ * the value resolves from {@code resolver} . If there is no such property p defined,
83
85
* then the ${p} reference will remain unchanged.
84
86
*
85
87
* If the property reference is of the form ${p:v} and there is no such property p,
@@ -93,17 +95,10 @@ public static String replaceProperties(final String string)
93
95
* value and the property ${:} is replaced with System.getProperty("path.separator").
94
96
*
95
97
* @param string - the string with possible ${} references
96
- * @param props - the source for ${x} property ref values, null means use System.getProperty()
98
+ * @param resolver - the property resolver
97
99
* @return the input string with all property references replaced if any.
98
100
* If there are no valid references the input string will be returned.
99
101
*/
100
- public static String replaceProperties (final String string , final Properties props ) {
101
- if (props == null ) {
102
- return replaceProperties (string , (PropertyResolver ) null );
103
- }
104
- return replaceProperties (string , props ::getProperty );
105
- }
106
-
107
102
public static String replaceProperties (final String string , PropertyResolver resolver )
108
103
{
109
104
if (string == null ) {
@@ -171,10 +166,7 @@ else if (PATH_SEPARATOR_ALIAS.equals(key))
171
166
else
172
167
{
173
168
// check from the properties
174
- if (resolver != null )
175
- value = resolver .resolve (key );
176
- else
177
- value = systemEnvProperties .getProperty (key );
169
+ value = resolveValue (resolver , key );
178
170
179
171
if (value == null )
180
172
{
@@ -183,10 +175,7 @@ else if (PATH_SEPARATOR_ALIAS.equals(key))
183
175
if (colon > 0 )
184
176
{
185
177
String realKey = key .substring (0 , colon );
186
- if (resolver != null )
187
- value = resolver .resolve (realKey );
188
- else
189
- value = systemEnvProperties .getProperty (realKey );
178
+ value = resolveValue (resolver , realKey );
190
179
191
180
if (value == null )
192
181
{
@@ -239,7 +228,7 @@ else if (PATH_SEPARATOR_ALIAS.equals(key))
239
228
throw new IllegalStateException ("Infinite recursion happening when replacing properties on '" + buffer + "'" );
240
229
}
241
230
}
242
-
231
+
243
232
// Done
244
233
return buffer .toString ();
245
234
}
@@ -257,26 +246,32 @@ private static String resolveCompositeKey(String key, PropertyResolver resolver)
257
246
{
258
247
// Check the first part
259
248
String key1 = key .substring (0 , comma );
260
- if (resolver != null )
261
- value = resolver .resolve (key1 );
262
- else
263
- value = systemEnvProperties .getProperty (key1 );
249
+ value = resolveValue (resolver , key1 );
264
250
}
265
251
// Check the second part, if there is one and first lookup failed
266
252
if (value == null && comma < key .length () - 1 )
267
253
{
268
254
String key2 = key .substring (comma + 1 );
269
- if (resolver != null )
270
- value = resolver .resolve (key2 );
271
- else
272
- value = systemEnvProperties .getProperty (key2 );
255
+ value = resolveValue (resolver , key2 );
273
256
}
274
257
}
275
258
// Return whatever we've found or null
276
259
return value ;
277
260
}
278
-
261
+
279
262
public interface PropertyResolver {
280
263
String resolve (String property );
281
264
}
265
+
266
+ private static String resolveValue (PropertyResolver resolver , String key ) {
267
+ if (resolver == null ) {
268
+ return getDefaultPropertyResolver ().resolve (key );
269
+ }
270
+
271
+ return resolver .resolve (key );
272
+ }
273
+
274
+ private static PropertyResolver getDefaultPropertyResolver () {
275
+ return Optional .ofNullable (DEFAULT_PROPERTY_RESOLVER ).orElse (NULL_RESOLVER );
276
+ }
282
277
}
0 commit comments