|
| 1 | +package org.springframework.cloud.context.refresh; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +import org.springframework.boot.Banner.Mode; |
| 12 | +import org.springframework.boot.builder.SpringApplicationBuilder; |
| 13 | +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; |
| 14 | +import org.springframework.cloud.context.scope.refresh.RefreshScope; |
| 15 | +import org.springframework.context.ApplicationContext; |
| 16 | +import org.springframework.context.ConfigurableApplicationContext; |
| 17 | +import org.springframework.context.annotation.Configuration; |
| 18 | +import org.springframework.core.env.CompositePropertySource; |
| 19 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 20 | +import org.springframework.core.env.EnumerablePropertySource; |
| 21 | +import org.springframework.core.env.MapPropertySource; |
| 22 | +import org.springframework.core.env.MutablePropertySources; |
| 23 | +import org.springframework.core.env.PropertySource; |
| 24 | +import org.springframework.core.env.StandardEnvironment; |
| 25 | +import org.springframework.web.context.support.StandardServletEnvironment; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Dave Syer |
| 29 | + * @author Venil Noronha |
| 30 | + */ |
| 31 | +public class ContextRefresher { |
| 32 | + |
| 33 | + private static final String REFRESH_ARGS_PROPERTY_SOURCE = "refreshArgs"; |
| 34 | + |
| 35 | + private Set<String> standardSources = new HashSet<String>( |
| 36 | + Arrays.asList(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, |
| 37 | + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
| 38 | + StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME, |
| 39 | + StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, |
| 40 | + StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); |
| 41 | + |
| 42 | + private ConfigurableApplicationContext context; |
| 43 | + private RefreshScope scope; |
| 44 | + |
| 45 | + public ContextRefresher(ConfigurableApplicationContext context, RefreshScope scope) { |
| 46 | + this.context = context; |
| 47 | + this.scope = scope; |
| 48 | + } |
| 49 | + |
| 50 | + public synchronized Set<String> refresh() { |
| 51 | + Map<String, Object> before = extract( |
| 52 | + this.context.getEnvironment().getPropertySources()); |
| 53 | + addConfigFilesToEnvironment(); |
| 54 | + Set<String> keys = changes(before, |
| 55 | + extract(this.context.getEnvironment().getPropertySources())).keySet(); |
| 56 | + this.context.publishEvent(new EnvironmentChangeEvent(keys)); |
| 57 | + this.scope.refreshAll(); |
| 58 | + return keys; |
| 59 | + } |
| 60 | + |
| 61 | + private void addConfigFilesToEnvironment() { |
| 62 | + ConfigurableApplicationContext capture = null; |
| 63 | + try { |
| 64 | + StandardEnvironment environment = copyEnvironment( |
| 65 | + this.context.getEnvironment()); |
| 66 | + capture = new SpringApplicationBuilder(Empty.class).bannerMode(Mode.OFF) |
| 67 | + .web(false).environment(environment).run(); |
| 68 | + if (environment.getPropertySources().contains(REFRESH_ARGS_PROPERTY_SOURCE)) { |
| 69 | + environment.getPropertySources().remove(REFRESH_ARGS_PROPERTY_SOURCE); |
| 70 | + } |
| 71 | + MutablePropertySources target = this.context.getEnvironment() |
| 72 | + .getPropertySources(); |
| 73 | + String targetName = null; |
| 74 | + for (PropertySource<?> source : environment.getPropertySources()) { |
| 75 | + String name = source.getName(); |
| 76 | + if (target.contains(name)) { |
| 77 | + targetName = name; |
| 78 | + } |
| 79 | + if (!this.standardSources.contains(name)) { |
| 80 | + if (target.contains(name)) { |
| 81 | + target.replace(name, source); |
| 82 | + } |
| 83 | + else { |
| 84 | + if (targetName != null) { |
| 85 | + target.addAfter(targetName, source); |
| 86 | + } |
| 87 | + else { |
| 88 | + if (target.contains("defaultProperties")) { |
| 89 | + target.addBefore("defaultProperties", source); |
| 90 | + } |
| 91 | + else { |
| 92 | + target.addLast(source); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + finally { |
| 100 | + ConfigurableApplicationContext closeable = capture; |
| 101 | + while (closeable != null) { |
| 102 | + closeable.close(); |
| 103 | + ApplicationContext parent = closeable.getParent(); |
| 104 | + if (parent instanceof ConfigurableApplicationContext) { |
| 105 | + closeable = (ConfigurableApplicationContext) parent; |
| 106 | + } |
| 107 | + else { |
| 108 | + closeable = null; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Don't use ConfigurableEnvironment.merge() in case there are clashes with property |
| 115 | + // source names |
| 116 | + private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) { |
| 117 | + StandardEnvironment environment = new StandardEnvironment(); |
| 118 | + MutablePropertySources capturedPropertySources = environment.getPropertySources(); |
| 119 | + for (PropertySource<?> source : capturedPropertySources) { |
| 120 | + capturedPropertySources.remove(source.getName()); |
| 121 | + } |
| 122 | + for (PropertySource<?> source : input.getPropertySources()) { |
| 123 | + capturedPropertySources.addLast(source); |
| 124 | + } |
| 125 | + environment.setActiveProfiles(input.getActiveProfiles()); |
| 126 | + environment.setDefaultProfiles(input.getDefaultProfiles()); |
| 127 | + Map<String, Object> map = new HashMap<String, Object>(); |
| 128 | + map.put("spring.jmx.enabled", false); |
| 129 | + map.put("spring.main.sources", ""); |
| 130 | + capturedPropertySources |
| 131 | + .addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map)); |
| 132 | + return environment; |
| 133 | + } |
| 134 | + |
| 135 | + private Map<String, Object> changes(Map<String, Object> before, |
| 136 | + Map<String, Object> after) { |
| 137 | + Map<String, Object> result = new HashMap<String, Object>(); |
| 138 | + for (String key : before.keySet()) { |
| 139 | + if (!after.containsKey(key)) { |
| 140 | + result.put(key, null); |
| 141 | + } |
| 142 | + else if (!equal(before.get(key), after.get(key))) { |
| 143 | + result.put(key, after.get(key)); |
| 144 | + } |
| 145 | + } |
| 146 | + for (String key : after.keySet()) { |
| 147 | + if (!before.containsKey(key)) { |
| 148 | + result.put(key, after.get(key)); |
| 149 | + } |
| 150 | + } |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | + private boolean equal(Object one, Object two) { |
| 155 | + if (one == null && two == null) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + if (one == null || two == null) { |
| 159 | + return false; |
| 160 | + } |
| 161 | + return one.equals(two); |
| 162 | + } |
| 163 | + |
| 164 | + private Map<String, Object> extract(MutablePropertySources propertySources) { |
| 165 | + Map<String, Object> result = new HashMap<String, Object>(); |
| 166 | + List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>(); |
| 167 | + for (PropertySource<?> source : propertySources) { |
| 168 | + sources.add(0, source); |
| 169 | + } |
| 170 | + for (PropertySource<?> source : sources) { |
| 171 | + if (!this.standardSources.contains(source.getName())) { |
| 172 | + extract(source, result); |
| 173 | + } |
| 174 | + } |
| 175 | + return result; |
| 176 | + } |
| 177 | + |
| 178 | + private void extract(PropertySource<?> parent, Map<String, Object> result) { |
| 179 | + if (parent instanceof CompositePropertySource) { |
| 180 | + try { |
| 181 | + List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>(); |
| 182 | + for (PropertySource<?> source : ((CompositePropertySource) parent) |
| 183 | + .getPropertySources()) { |
| 184 | + sources.add(0, source); |
| 185 | + } |
| 186 | + for (PropertySource<?> source : sources) { |
| 187 | + extract(source, result); |
| 188 | + } |
| 189 | + } |
| 190 | + catch (Exception e) { |
| 191 | + return; |
| 192 | + } |
| 193 | + } |
| 194 | + else if (parent instanceof EnumerablePropertySource) { |
| 195 | + for (String key : ((EnumerablePropertySource<?>) parent).getPropertyNames()) { |
| 196 | + result.put(key, parent.getProperty(key)); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Configuration |
| 202 | + protected static class Empty { |
| 203 | + |
| 204 | + } |
| 205 | + |
| 206 | +} |
0 commit comments