4646/**
4747 * @author Dave Syer
4848 * @author Venil Noronha
49+ * @author Mikhail Polivakha
4950 */
5051public 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