Skip to content

Commit ec97577

Browse files
authored
Polishing (#1639)
1 parent 9f0e1d4 commit ec97577

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

spring-cloud-context/src/main/java/org/springframework/cloud/context/environment/EnvironmentChangeEvent.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525
* Event published to signal a change in the {@link Environment}.
2626
*
2727
* @author Dave Syer
28-
*
28+
* @author Mikhail Polivakha
2929
*/
3030
@SuppressWarnings("serial")
3131
public class EnvironmentChangeEvent extends ApplicationEvent {
3232

33-
private Set<String> keys;
33+
private final Set<String> keys;
3434

35+
/**
36+
* @param keys Key set that represents the difference between old and new versions of
37+
* the {@link Environment}.
38+
*/
3539
public EnvironmentChangeEvent(Set<String> keys) {
3640
// Backwards compatible constructor with less utility (practically no use at all)
3741
this(keys, keys);
@@ -43,7 +47,8 @@ public EnvironmentChangeEvent(Object context, Set<String> keys) {
4347
}
4448

4549
/**
46-
* @return The keys.
50+
* @return Key set that represents the difference between old and new versions of the
51+
* {@link Environment}.
4752
*/
4853
public Set<String> getKeys() {
4954
return this.keys;

spring-cloud-context/src/main/java/org/springframework/cloud/context/environment/EnvironmentManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public Map<String, Object> reset() {
7777

7878
@ManagedOperation
7979
public void setProperty(String name, String value) {
80-
8180
if (!this.environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {
8281
synchronized (this.map) {
8382
if (!this.environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {

spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ConfigDataContextRefresher.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,13 @@ protected void updateEnvironment() {
106106
else {
107107
if (targetName != null) {
108108
target.addAfter(targetName, source);
109-
// update targetName to preserve ordering
110-
targetName = name;
111109
}
112110
else {
113111
// targetName was null so we are at the start of the list
114112
target.addFirst(source);
115-
targetName = name;
116113
}
114+
// update targetName to preserve ordering
115+
targetName = name;
117116
}
118117
}
119118
}

spring-cloud-context/src/main/java/org/springframework/cloud/context/refresh/ContextRefresher.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
/**
4747
* @author Dave Syer
4848
* @author Venil Noronha
49+
* @author Mikhail Polivakha
4950
*/
5051
public abstract class ContextRefresher {
5152

@@ -64,9 +65,9 @@ public abstract class ContextRefresher {
6465

6566
protected final List<String> additionalPropertySourcesToRetain;
6667

67-
private ConfigurableApplicationContext context;
68+
private final ConfigurableApplicationContext context;
6869

69-
private RefreshScope scope;
70+
private final RefreshScope scope;
7071

7172
@Deprecated
7273
protected ContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) {
@@ -96,18 +97,22 @@ public synchronized Set<String> refresh() {
9697
}
9798

9899
public synchronized Set<String> refreshEnvironment() {
99-
Map<String, Object> before = extract(this.context.getEnvironment().getPropertySources());
100+
Map<String, Object> before = getCurrentEnvironmentProperties();
100101
updateEnvironment();
101-
Set<String> keys = changes(before, extract(this.context.getEnvironment().getPropertySources())).keySet();
102+
Set<String> keys = changes(before, getCurrentEnvironmentProperties()).keySet();
102103
this.context.publishEvent(new EnvironmentChangeEvent(this.context, keys));
103104
return keys;
104105
}
105106

107+
private Map<String, Object> getCurrentEnvironmentProperties() {
108+
return extract(this.context.getEnvironment().getPropertySources());
109+
}
110+
106111
protected abstract void updateEnvironment();
107112

108113
// Don't use ConfigurableEnvironment.merge() in case there are clashes with property
109114
// source names
110-
protected StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
115+
protected StandardEnvironment copyEnvironment(ConfigurableEnvironment sourceEnv) {
111116
StandardEnvironment environment = new StandardEnvironment();
112117
MutablePropertySources capturedPropertySources = environment.getPropertySources();
113118
// Only copy the default property source(s) and the profiles over from the main
@@ -117,18 +122,17 @@ protected StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
117122
propertySourcesToRetain.addAll(additionalPropertySourcesToRetain);
118123
}
119124

120-
for (String name : propertySourcesToRetain) {
121-
if (input.getPropertySources().contains(name)) {
122-
if (capturedPropertySources.contains(name)) {
123-
capturedPropertySources.replace(name, input.getPropertySources().get(name));
124-
}
125-
else {
126-
capturedPropertySources.addLast(input.getPropertySources().get(name));
127-
}
125+
propertySourcesToRetain.stream().filter(s -> sourceEnv.getPropertySources().contains(s)).forEach(s -> {
126+
if (capturedPropertySources.contains(s)) {
127+
capturedPropertySources.replace(s, sourceEnv.getPropertySources().get(s));
128128
}
129-
}
130-
environment.setActiveProfiles(input.getActiveProfiles());
131-
environment.setDefaultProfiles(input.getDefaultProfiles());
129+
else {
130+
capturedPropertySources.addLast(sourceEnv.getPropertySources().get(s));
131+
}
132+
});
133+
134+
environment.setActiveProfiles(sourceEnv.getActiveProfiles());
135+
environment.setDefaultProfiles(sourceEnv.getDefaultProfiles());
132136
return environment;
133137
}
134138

@@ -164,13 +168,15 @@ private Map<String, Object> extract(MutablePropertySources propertySources) {
164168
return result;
165169
}
166170

167-
private void extract(PropertySource<?> parent, Map<String, Object> result) {
168-
if (parent instanceof CompositePropertySource) {
171+
private void extract(PropertySource<?> propertySource, Map<String, Object> result) {
172+
if (propertySource instanceof CompositePropertySource cps) {
169173
try {
170174
List<PropertySource<?>> sources = new ArrayList<>();
171-
for (PropertySource<?> source : ((CompositePropertySource) parent).getPropertySources()) {
175+
176+
for (PropertySource<?> source : cps.getPropertySources()) {
172177
sources.add(0, source);
173178
}
179+
174180
for (PropertySource<?> source : sources) {
175181
extract(source, result);
176182
}
@@ -179,9 +185,9 @@ private void extract(PropertySource<?> parent, Map<String, Object> result) {
179185
return;
180186
}
181187
}
182-
else if (parent instanceof EnumerablePropertySource) {
183-
for (String key : ((EnumerablePropertySource<?>) parent).getPropertyNames()) {
184-
result.put(key, parent.getProperty(key));
188+
else if (propertySource instanceof EnumerablePropertySource<?> eps) {
189+
for (String key : eps.getPropertyNames()) {
190+
result.put(key, propertySource.getProperty(key));
185191
}
186192
}
187193
}

0 commit comments

Comments
 (0)