diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/RefreshEndpoint.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/RefreshEndpoint.java index 8a137b4f..55760409 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/RefreshEndpoint.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/RefreshEndpoint.java @@ -28,6 +28,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; +import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.CompositePropertySource; @@ -66,7 +67,8 @@ public class RefreshEndpoint extends AbstractEndpoint> { @ManagedOperation public synchronized String[] refresh() { - Map before = extract(context.getEnvironment().getPropertySources()); + Map before = extract(context.getEnvironment() + .getPropertySources()); addConfigFilesToEnvironment(); Set keys = changes(before, extract(context.getEnvironment().getPropertySources())).keySet(); @@ -79,20 +81,36 @@ public class RefreshEndpoint extends AbstractEndpoint> { } private void addConfigFilesToEnvironment() { - ConfigurableApplicationContext capture = new SpringApplicationBuilder(Empty.class).showBanner( - false).web(false).environment(context.getEnvironment()).run(); - MutablePropertySources target = context.getEnvironment().getPropertySources(); - for (PropertySource source : capture.getEnvironment().getPropertySources()) { - String name = source.getName(); - if (!standardSources.contains(name)) { - if (target.contains(name)) { - target.replace(name, source); - } else { - if (target.contains("defaultProperties")) { - target.addBefore("defaultProperties", source); - } else { - target.addLast(source); + ConfigurableApplicationContext capture = null; + try { + capture = new SpringApplicationBuilder(Empty.class).showBanner(false) + .web(false).environment(context.getEnvironment()).run(); + MutablePropertySources target = context.getEnvironment().getPropertySources(); + for (PropertySource source : capture.getEnvironment().getPropertySources()) { + String name = source.getName(); + if (!standardSources.contains(name)) { + if (target.contains(name)) { + target.replace(name, source); } + else { + if (target.contains("defaultProperties")) { + target.addBefore("defaultProperties", source); + } + else { + target.addLast(source); + } + } + } + } + } + finally { + while (capture != null) { + capture.close(); + ApplicationContext parent = capture.getParent(); + if (parent instanceof ConfigurableApplicationContext) { + capture = (ConfigurableApplicationContext) parent; + } else { + capture = null; } } } @@ -109,7 +127,8 @@ public class RefreshEndpoint extends AbstractEndpoint> { for (String key : before.keySet()) { if (!after.containsKey(key)) { result.put(key, null); - } else if (!equal(before.get(key), after.get(key))) { + } + else if (!equal(before.get(key), after.get(key))) { result.put(key, after.get(key)); } } @@ -144,13 +163,16 @@ public class RefreshEndpoint extends AbstractEndpoint> { private void extract(PropertySource parent, Map result) { if (parent instanceof CompositePropertySource) { try { - for (PropertySource source : ((CompositePropertySource) parent).getPropertySources()) { + for (PropertySource source : ((CompositePropertySource) parent) + .getPropertySources()) { extract(source, result); } - } catch (Exception e) { + } + catch (Exception e) { return; } - } else if (parent instanceof EnumerablePropertySource) { + } + else if (parent instanceof EnumerablePropertySource) { for (String key : ((EnumerablePropertySource) parent).getPropertyNames()) { result.put(key, parent.getProperty(key)); } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/RefreshEndpointTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/RefreshEndpointTests.java new file mode 100644 index 00000000..7cf765f5 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/RefreshEndpointTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.bootstrap.config; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.junit.Test; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.context.scope.refresh.RefreshScope; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; + +/** + * @author Dave Syer + * + */ +public class RefreshEndpointTests { + + @Test + public void shutdownHooksCleaned() { + ConfigurableApplicationContext context = new SpringApplicationBuilder(Empty.class) + .web(false).showBanner(false).run(); + RefreshScope scope = new RefreshScope(); + scope.setApplicationContext(context); + RefreshEndpoint endpoint = new RefreshEndpoint(context, scope); + int count = countShutdownHooks(); + endpoint.invoke(); + int after = countShutdownHooks(); + assertEquals("Shutdown hooks not cleaned on refresh", count, after); + } + + private int countShutdownHooks() { + Class type = ClassUtils.resolveClassName("java.lang.ApplicationShutdownHooks", + null); + Field field = ReflectionUtils.findField(type, "hooks"); + ReflectionUtils.makeAccessible(field); + @SuppressWarnings("rawtypes") + Map map = (Map) ReflectionUtils.getField(field, null); + return map.size(); + } + + @Configuration + protected static class Empty { + + } +}