From c6c391158f15514288914e047c80df5ddc9baa7e Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 28 Apr 2017 15:29:49 -0600 Subject: [PATCH] Removes RelaxedPropertyResolver --- .../EnableCircuitBreakerImportSelector.java | 3 +- .../EnableDiscoveryClientImportSelector.java | 3 +- .../cloud/commons/util/IdUtils.java | 12 +++-- .../AbstractAutoServiceRegistrationTests.java | 2 +- .../PropertySourceBootstrapConfiguration.java | 6 +-- .../EncryptionBootstrapConfiguration.java | 12 +++-- .../cloud/env/EnvironmentUtils.java | 44 +++++++++++++++++++ .../cloud/logging/LoggingRebinder.java | 6 +-- 8 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 spring-cloud-context/src/main/java/org/springframework/cloud/env/EnvironmentUtils.java diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java index bba53d58..e56b592b 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/circuitbreaker/EnableCircuitBreakerImportSelector.java @@ -16,7 +16,6 @@ package org.springframework.cloud.client.circuitbreaker; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.cloud.commons.util.SpringFactoryImportSelector; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -31,7 +30,7 @@ public class EnableCircuitBreakerImportSelector extends @Override protected boolean isEnabled() { - return new RelaxedPropertyResolver(getEnvironment()).getProperty( + return getEnvironment().getProperty( "spring.cloud.circuit.breaker.enabled", Boolean.class, Boolean.TRUE); } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java index 2d4bf09b..41b19f98 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java @@ -16,7 +16,6 @@ package org.springframework.cloud.client.discovery; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.cloud.commons.util.SpringFactoryImportSelector; import org.springframework.core.Ordered; import org.springframework.core.annotation.AnnotationAttributes; @@ -54,7 +53,7 @@ public class EnableDiscoveryClientImportSelector @Override protected boolean isEnabled() { - return new RelaxedPropertyResolver(getEnvironment()).getProperty( + return getEnvironment().getProperty( "spring.cloud.discovery.enabled", Boolean.class, Boolean.TRUE); } diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java index 21a77faf..e9e70645 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/IdUtils.java @@ -1,6 +1,5 @@ package org.springframework.cloud.commons.util; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.PropertyResolver; import org.springframework.util.StringUtils; @@ -12,19 +11,18 @@ public class IdUtils { private static final String SEPARATOR = ":"; public static String getDefaultInstanceId(PropertyResolver resolver) { - RelaxedPropertyResolver relaxed = new RelaxedPropertyResolver(resolver); - String vcapInstanceId = relaxed.getProperty("vcap.application.instance_id"); + String vcapInstanceId = resolver.getProperty("vcap.application.instance_id"); if (StringUtils.hasText(vcapInstanceId)) { return vcapInstanceId; } - String hostname = relaxed.getProperty("spring.cloud.client.hostname"); - String appName = relaxed.getProperty("spring.application.name"); + String hostname = resolver.getProperty("spring.cloud.client.hostname"); + String appName = resolver.getProperty("spring.application.name"); String namePart = combineParts(hostname, SEPARATOR, appName); - String indexPart = relaxed.getProperty("spring.application.instance_id", - relaxed.getProperty("server.port")); + String indexPart = resolver.getProperty("spring.application.instance_id", + resolver.getProperty("server.port")); return combineParts(namePart, SEPARATOR, indexPart); } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index cd50473c..683edfb5 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -7,8 +7,8 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.LocalManagementPort; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit4.SpringRunner; diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index fbbc9ed3..0d117af4 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -29,7 +29,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.config.ConfigFileApplicationListener; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.logging.LogFile; @@ -51,6 +50,8 @@ import org.springframework.core.env.StandardEnvironment; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; +import static org.springframework.cloud.env.EnvironmentUtils.getSubProperties; + /** * @author Dave Syer * @@ -114,8 +115,7 @@ public class PropertySourceBootstrapConfiguration implements private void reinitializeLoggingSystem(ConfigurableEnvironment environment, String oldLogConfig, LogFile oldLogFile) { - Map props = new RelaxedPropertyResolver(environment) - .getSubProperties("logging."); + Map props = getSubProperties(environment, "logging."); if (!props.isEmpty()) { String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java index 17b53742..5996f263 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java @@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.bootstrap.encrypt.KeyProperties.KeyStore; import org.springframework.cloud.context.encrypt.EncryptorFactory; @@ -110,22 +109,21 @@ public class EncryptionBootstrapConfiguration { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); - RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment); - if (hasProperty(propertyResolver, environment, "encrypt.keyStore.location")) { - if (hasProperty(propertyResolver, environment, "encrypt.keyStore.password")) { + if (hasProperty(environment, "encrypt.keyStore.location")) { + if (hasProperty(environment, "encrypt.keyStore.password")) { return ConditionOutcome.match("Keystore found in Environment"); } return ConditionOutcome .noMatch("Keystore found but no password in Environment"); } - else if (hasProperty(propertyResolver, environment, "encrypt.key")) { + else if (hasProperty(environment, "encrypt.key")) { return ConditionOutcome.match("Key found in Environment"); } return ConditionOutcome.noMatch("Keystore nor key found in Environment"); } - private boolean hasProperty(RelaxedPropertyResolver propertyResolver, Environment environment, String key) { - String value = propertyResolver.getProperty(key); + private boolean hasProperty(Environment environment, String key) { + String value = environment.getProperty(key); if (value == null) { return false; } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/env/EnvironmentUtils.java b/spring-cloud-context/src/main/java/org/springframework/cloud/env/EnvironmentUtils.java new file mode 100644 index 00000000..8e8321a6 --- /dev/null +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/env/EnvironmentUtils.java @@ -0,0 +1,44 @@ +package org.springframework.cloud.env; + +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.PropertySources; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * @author Spencer Gibb + */ +public class EnvironmentUtils { + public static Map getSubProperties(Environment environment, String keyPrefix) { + if (environment instanceof ConfigurableEnvironment) { + ConfigurableEnvironment env = (ConfigurableEnvironment) environment; + Map subProperties = new LinkedHashMap<>(); + PropertySources propertySources = env.getPropertySources(); + for (PropertySource source : propertySources) { + if (source instanceof EnumerablePropertySource) { + for (String name : ((EnumerablePropertySource) source) + .getPropertyNames()) { + String key = getSubKey(name, keyPrefix); + if (key != null && !subProperties.containsKey(key)) { + subProperties.put(key, source.getProperty(name)); + } + } + } + } + return Collections.unmodifiableMap(subProperties); + } + return Collections.emptyMap(); + } + + private static String getSubKey(String name, String keyPrefix) { + if (name.startsWith(keyPrefix)) { + return name.substring(keyPrefix.length()); + } + return null; + } +} diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java b/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java index f8098386..13ed7378 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java @@ -20,7 +20,6 @@ import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggingSystem; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; @@ -28,6 +27,8 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; +import static org.springframework.cloud.env.EnvironmentUtils.getSubProperties; + /** * Listener that looks for {@link EnvironmentChangeEvent} and rebinds logger levels if any * changed. @@ -57,8 +58,7 @@ public class LoggingRebinder } protected void setLogLevels(LoggingSystem system, Environment environment) { - Map levels = new RelaxedPropertyResolver(environment) - .getSubProperties("logging.level."); + Map levels = getSubProperties(environment, "logging.level."); for (Entry entry : levels.entrySet()) { setLogLevel(system, environment, entry.getKey(), entry.getValue().toString()); }