-
Notifications
You must be signed in to change notification settings - Fork 29
/
PlaceholderConfigSourceHandler.java
53 lines (41 loc) 路 1.63 KB
/
PlaceholderConfigSourceHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.foldright.examples.config.handler;
import com.foldright.examples.config.pipeline.ConfigSourceHandler;
import com.foldright.examples.config.pipeline.ConfigSourceHandlerContext;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.lang3.StringUtils.removeEnd;
import static org.apache.commons.lang3.StringUtils.removeStart;
/**
* Support value with placeholder with format {@code ${value}}.
* <p>
* Not support nested placeholder yet, e.g. {@code ${${value}}}.
*/
public class PlaceholderConfigSourceHandler implements ConfigSourceHandler {
private static final Pattern PATTERN = Pattern.compile("(\\$\\{.+?})");
@Override
public String get(String key, ConfigSourceHandlerContext context) {
String value = context.get(key);
if (StringUtils.isBlank(value)) {
return null;
}
Matcher matcher = PATTERN.matcher(value);
if (!matcher.find()) {
return value;
}
int groupCount = matcher.groupCount();
for (int i = 0; i < groupCount; i++) {
// like ${xxx}
String matched = matcher.group(i + 1);
// like xxx
String placeholderKey = removeEnd(removeStart(matched, "${"), "}");
// get xxx's value and replace ${xxx} with placeholderValue
String placeholderValue = context.pipeline().get(placeholderKey);
value = StringUtils.replace(value, matched, placeholderValue);
}
if (StringUtils.isBlank(value)) {
return null;
}
return value;
}
}