Commit 371edbf8 by Dave Syer

Prevent possible memory leak with archaius

parent f8df3378
...@@ -16,21 +16,12 @@ ...@@ -16,21 +16,12 @@
package org.springframework.cloud.netflix.archaius; package org.springframework.cloud.netflix.archaius;
import static com.netflix.config.ConfigurationManager.APPLICATION_PROPERTIES;
import static com.netflix.config.ConfigurationManager.DISABLE_DEFAULT_ENV_CONFIG;
import static com.netflix.config.ConfigurationManager.DISABLE_DEFAULT_SYS_CONFIG;
import static com.netflix.config.ConfigurationManager.ENV_CONFIG_NAME;
import static com.netflix.config.ConfigurationManager.SYS_CONFIG_NAME;
import static com.netflix.config.ConfigurationManager.URL_CONFIG_NAME;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.configuration.AbstractConfiguration; import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.ConfigurationBuilder; import org.apache.commons.configuration.ConfigurationBuilder;
import org.apache.commons.configuration.EnvironmentConfiguration; import org.apache.commons.configuration.EnvironmentConfiguration;
...@@ -53,14 +44,25 @@ import com.netflix.config.AggregatedConfiguration; ...@@ -53,14 +44,25 @@ import com.netflix.config.AggregatedConfiguration;
import com.netflix.config.ConcurrentCompositeConfiguration; import com.netflix.config.ConcurrentCompositeConfiguration;
import com.netflix.config.ConfigurationManager; import com.netflix.config.ConfigurationManager;
import com.netflix.config.DeploymentContext; import com.netflix.config.DeploymentContext;
import com.netflix.config.DynamicProperty;
import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicURLConfiguration; import com.netflix.config.DynamicURLConfiguration;
import static com.netflix.config.ConfigurationManager.APPLICATION_PROPERTIES;
import static com.netflix.config.ConfigurationManager.DISABLE_DEFAULT_ENV_CONFIG;
import static com.netflix.config.ConfigurationManager.DISABLE_DEFAULT_SYS_CONFIG;
import static com.netflix.config.ConfigurationManager.ENV_CONFIG_NAME;
import static com.netflix.config.ConfigurationManager.SYS_CONFIG_NAME;
import static com.netflix.config.ConfigurationManager.URL_CONFIG_NAME;
import lombok.extern.apachecommons.CommonsLog;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Configuration @Configuration
@ConditionalOnClass({ ConcurrentCompositeConfiguration.class, ConfigurationBuilder.class }) @ConditionalOnClass({ ConcurrentCompositeConfiguration.class,
ConfigurationBuilder.class })
@CommonsLog @CommonsLog
public class ArchaiusAutoConfiguration { public class ArchaiusAutoConfiguration {
...@@ -78,6 +80,7 @@ public class ArchaiusAutoConfiguration { ...@@ -78,6 +80,7 @@ public class ArchaiusAutoConfiguration {
setStatic(ConfigurationManager.class, "customConfigurationInstalled", false); setStatic(ConfigurationManager.class, "customConfigurationInstalled", false);
setStatic(DynamicPropertyFactory.class, "config", null); setStatic(DynamicPropertyFactory.class, "config", null);
setStatic(DynamicPropertyFactory.class, "initializedWithDefaultConfig", false); setStatic(DynamicPropertyFactory.class, "initializedWithDefaultConfig", false);
setStatic(DynamicProperty.class, "dynamicPropertySupportImpl", null);
initialized.compareAndSet(true, false); initialized.compareAndSet(true, false);
} }
...@@ -101,8 +104,8 @@ public class ArchaiusAutoConfiguration { ...@@ -101,8 +104,8 @@ public class ArchaiusAutoConfiguration {
@Configuration @Configuration
@ConditionalOnProperty(value = "archaius.propagate.environmentChangedEvent", matchIfMissing = true) @ConditionalOnProperty(value = "archaius.propagate.environmentChangedEvent", matchIfMissing = true)
@ConditionalOnClass(EnvironmentChangeEvent.class) @ConditionalOnClass(EnvironmentChangeEvent.class)
protected static class PropagateEventsConfiguration implements protected static class PropagateEventsConfiguration
ApplicationListener<EnvironmentChangeEvent> { implements ApplicationListener<EnvironmentChangeEvent> {
@Autowired @Autowired
private Environment env; private Environment env;
...@@ -110,11 +113,12 @@ public class ArchaiusAutoConfiguration { ...@@ -110,11 +113,12 @@ public class ArchaiusAutoConfiguration {
public void onApplicationEvent(EnvironmentChangeEvent event) { public void onApplicationEvent(EnvironmentChangeEvent event) {
AbstractConfiguration manager = ConfigurationManager.getConfigInstance(); AbstractConfiguration manager = ConfigurationManager.getConfigInstance();
for (String key : event.getKeys()) { for (String key : event.getKeys()) {
for (ConfigurationListener listener : manager.getConfigurationListeners()) { for (ConfigurationListener listener : manager
.getConfigurationListeners()) {
Object source = event.getSource(); Object source = event.getSource();
// TODO: Handle add vs set vs delete? // TODO: Handle add vs set vs delete?
int type = AbstractConfiguration.EVENT_SET_PROPERTY; int type = AbstractConfiguration.EVENT_SET_PROPERTY;
String value = env.getProperty(key); String value = this.env.getProperty(key);
boolean beforeUpdate = false; boolean beforeUpdate = false;
listener.configurationChanged(new ConfigurationEvent(source, type, listener.configurationChanged(new ConfigurationEvent(source, type,
key, value, beforeUpdate)); key, value, beforeUpdate));
...@@ -136,8 +140,8 @@ public class ArchaiusAutoConfiguration { ...@@ -136,8 +140,8 @@ public class ArchaiusAutoConfiguration {
// support to add other Configurations (Jdbc, DynamoDb, Zookeeper, jclouds, // support to add other Configurations (Jdbc, DynamoDb, Zookeeper, jclouds,
// etc...) // etc...)
if (externalConfigurations != null) { if (this.externalConfigurations != null) {
for (AbstractConfiguration externalConfig : externalConfigurations) { for (AbstractConfiguration externalConfig : this.externalConfigurations) {
config.addConfiguration(externalConfig); config.addConfiguration(externalConfig);
} }
} }
...@@ -165,14 +169,15 @@ public class ArchaiusAutoConfiguration { ...@@ -165,14 +169,15 @@ public class ArchaiusAutoConfiguration {
ConcurrentCompositeConfiguration appOverrideConfig = new ConcurrentCompositeConfiguration(); ConcurrentCompositeConfiguration appOverrideConfig = new ConcurrentCompositeConfiguration();
config.addConfiguration(appOverrideConfig, APPLICATION_PROPERTIES); config.addConfiguration(appOverrideConfig, APPLICATION_PROPERTIES);
config.setContainerConfigurationIndex(config config.setContainerConfigurationIndex(
.getIndexOfConfiguration(appOverrideConfig)); config.getIndexOfConfiguration(appOverrideConfig));
addArchaiusConfiguration(config); addArchaiusConfiguration(config);
} }
else { else {
// TODO: reinstall ConfigurationManager // TODO: reinstall ConfigurationManager
log.warn("Netflix ConfigurationManager has already been installed, unable to re-install"); log.warn(
"Netflix ConfigurationManager has already been installed, unable to re-install");
} }
} }
...@@ -187,7 +192,8 @@ public class ArchaiusAutoConfiguration { ...@@ -187,7 +192,8 @@ public class ArchaiusAutoConfiguration {
else { else {
installedConfiguration.append(config); installedConfiguration.append(config);
if (!(installedConfiguration instanceof AggregatedConfiguration)) { if (!(installedConfiguration instanceof AggregatedConfiguration)) {
log.warn("Appending a configuration to an existing non-aggregated installed configuration will have no effect"); log.warn(
"Appending a configuration to an existing non-aggregated installed configuration will have no effect");
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment