1 package cn.home1.cloud.config.server.util;
2
3 import static lombok.AccessLevel.PRIVATE;
4
5 import lombok.NoArgsConstructor;
6
7 import org.springframework.cloud.config.environment.Environment;
8 import org.springframework.cloud.config.environment.PropertySource;
9
10 import java.util.List;
11 import java.util.Map;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 @NoArgsConstructor(access = PRIVATE)
16 public abstract class EnvironmentUtils {
17
18 private static final Pattern VARIABLE_SUBSTITUTION_PATTERN = Pattern.compile("\\$\\{(\\w+)\\}");
19
20 public static String getEnvProperty(final Environment environment, final String name) {
21 final String result;
22 if (environment == null || name == null) {
23 result = null;
24 } else {
25 final List<PropertySource> propertySources = environment.getPropertySources();
26 result = propertySources != null ?
27 propertySources.stream()
28 .filter(propertySource -> getProperty(propertySource, name) != null)
29 .map(propertySource -> getProperty(propertySource, name))
30 .findFirst()
31 .orElse(null) :
32 null;
33 }
34 return result;
35 }
36
37 private static String getProperty(final PropertySource propertySource, final String key) {
38 final String result;
39
40 final Map<?, ?> source = propertySource.getSource();
41 if (source == null) {
42 result = null;
43 } else {
44 final Object valueObj = source.get(key);
45 result = valueObj == null ? null : "" + valueObj;
46 }
47
48 return result;
49 }
50
51 public static String getApplicationName(final Environment environment) {
52 return getEnvProperty(environment, "spring.application.name");
53 }
54
55 public static String getConfigPassword(final Environment environment) {
56 return getEnvProperty(environment, "spring.cloud.config.password");
57 }
58
59 public static String getParentConfigPassword(final Environment environment) {
60 return getEnvProperty(environment, "spring.cloud.config.parent.password");
61 }
62
63 public static String getParentApplication(final Environment environment) {
64 if (environment == null) {
65 return null;
66 }
67
68 final String evnPropertyValue = getEnvProperty(environment, "spring.cloud.config.parent.application");
69 if (evnPropertyValue == null) {
70 return null;
71 }
72
73 final Matcher matcher = VARIABLE_SUBSTITUTION_PATTERN.matcher(evnPropertyValue);
74
75 String result = evnPropertyValue;
76 while (matcher.find()) {
77 final String name = matcher.group(1);
78 final String value = System.getProperty(name);
79 result = result.replaceAll("\\$\\{" + name + "\\}", value == null ? "" : value);
80 }
81
82 return result;
83 }
84
85 public static String getParentLabel(final Environment environment, final String label) {
86 final String evnPropertyValue = getEnvProperty(environment, "spring.cloud.config.parent.label");
87 return evnPropertyValue != null && label != null ? evnPropertyValue.replace("{label}", label) : label;
88 }
89
90 @Deprecated
91 public static String getParentProfile(final Environment environment, final String profile) {
92 final String evnPropertyValue = getEnvProperty(environment, "spring.cloud.config.parent.profile");
93 return evnPropertyValue != null && profile != null ? evnPropertyValue.replace("{profile}", profile) : profile;
94 }
95 }