Remove reflection hack

This commit is contained in:
Dave Syer
2014-12-03 15:06:17 +00:00
parent 0d2cb07384
commit 3911d91e57
3 changed files with 9 additions and 46 deletions

View File

@@ -15,10 +15,8 @@
*/
package org.springframework.cloud.bootstrap.encrypt;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -32,7 +30,6 @@ import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.util.ReflectionUtils;
/**
* @author Dave Syer
@@ -48,20 +45,8 @@ public class EnvironmentDecryptApplicationListener implements
private TextEncryptor encryptor;
private Field propertySourcesField;
private boolean failOnError = true;
{
initField();
}
private void initField() {
propertySourcesField = ReflectionUtils.findField(CompositePropertySource.class,
"propertySources");
propertySourcesField.setAccessible(true);
}
public EnvironmentDecryptApplicationListener(TextEncryptor encryptor) {
this.encryptor = encryptor;
}
@@ -131,17 +116,11 @@ public class EnvironmentDecryptApplicationListener implements
}
else if (source instanceof CompositePropertySource) {
try {
@SuppressWarnings("unchecked")
Set<PropertySource<?>> sources = (Set<PropertySource<?>>) propertySourcesField
.get(source);
for (PropertySource<?> nested : sources) {
decrypt(nested, overrides);
}
}
catch (IllegalAccessException e) {
return;
for (PropertySource<?> nested : ((CompositePropertySource) source)
.getPropertySources()) {
decrypt(nested, overrides);
}
}
}

View File

@@ -1,16 +1,13 @@
package org.springframework.cloud.config.client;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* @author Spencer Gibb
@@ -31,14 +28,8 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator {
PropertySource<?> propertySource = locator.locate(env);
builder.up();
if (propertySource instanceof CompositePropertySource) {
CompositePropertySource composite = CompositePropertySource.class.cast(propertySource);
Field field = ReflectionUtils.findField(CompositePropertySource.class,
"propertySources");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Set<PropertySource<?>> propertySources = (Set<PropertySource<?>>) field.get(composite);
List<String> sources = new ArrayList<>();
for (PropertySource<?> ps : propertySources) {
for (PropertySource<?> ps : ((CompositePropertySource) propertySource).getPropertySources()) {
sources.add(ps.getName());
}
builder.withDetail("propertySources", sources);

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.config.client;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
@@ -38,7 +37,6 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.support.StandardServletEnvironment;
/**
@@ -146,12 +144,7 @@ public class RefreshEndpoint extends AbstractEndpoint<Collection<String>> {
private void extract(PropertySource<?> parent, Map<String, Object> result) {
if (parent instanceof CompositePropertySource) {
try {
Field field = ReflectionUtils.findField(CompositePropertySource.class,
"propertySources");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Set<PropertySource<?>> sources = (Set<PropertySource<?>>) field.get(parent);
for (PropertySource<?> source : sources) {
for (PropertySource<?> source : ((CompositePropertySource) parent).getPropertySources()) {
extract(source, result);
}
} catch (Exception e) {