From ade073aae96b361fee0b2dbc241374f765a64f44 Mon Sep 17 00:00:00 2001 From: erabii Date: Tue, 6 Jul 2021 12:16:42 -0400 Subject: [PATCH] Minor refactor in ConfigMapPropertySourceLocator (#813) * minor refactor * minor refactor map * removed un-used import * more changes --- ...ventBasedConfigMapChangeDetectorTests.java | 6 ++-- .../ConfigMapPropertySourceLocator.java | 30 +++++++++++-------- .../commons/config/ConfigUtils.java | 3 +- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java index 7d1b4608..f2589042 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java @@ -88,7 +88,7 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { } @Test - void watch() throws Exception { + void watch() { Map data = new HashMap<>(); data.put("application.properties", "spring.cloud.kubernetes.configuration.watcher.refreshDelay=0\n" + "logging.level.org.springframework.cloud.kubernetes=TRACE"); @@ -142,7 +142,9 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { mock(KubernetesClientConfigMapPropertySource.class)).withProperty("debug", "true"); KubernetesClientConfigMapPropertySourceLocator locator = mock( KubernetesClientConfigMapPropertySourceLocator.class); - when(locator.locate(environment)).thenReturn(new MockPropertySource().withProperty("debug", "false")); + when(locator.locate(environment)).thenAnswer( + x -> new MockPropertySource().withProperty("debug", "false") + ); KubernetesNamespaceProvider kubernetesNamespaceProvider = mock(KubernetesNamespaceProvider.class); when(kubernetesNamespaceProvider.getNamespace()).thenReturn("default"); KubernetesClientEventBasedConfigMapChangeDetector changeDetector = new KubernetesClientEventBasedConfigMapChangeDetector( diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java index a450f63a..bf2f4729 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceLocator.java @@ -23,6 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,8 +57,11 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo this.properties = properties; } + protected abstract MapPropertySource getMapPropertySource(String name, NormalizedSource normalizedSource, + String configurationTarget, ConfigurableEnvironment environment); + @Override - public PropertySource locate(Environment environment) { + public PropertySource locate(Environment environment) { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment env = (ConfigurableEnvironment) environment; @@ -78,24 +82,25 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo NormalizedSource normalizedSource) { String configurationTarget = this.properties.getConfigurationTarget(); - return getMapPropertySource(getApplicationName(environment, normalizedSource.getName(), configurationTarget), - normalizedSource, configurationTarget, environment); + String applicationName = getApplicationName(environment, normalizedSource.getName(), configurationTarget); + return getMapPropertySource(applicationName, normalizedSource, configurationTarget, environment); } - protected abstract MapPropertySource getMapPropertySource(String name, NormalizedSource normalizedSource, - String configurationTarget, ConfigurableEnvironment environment); - private void addPropertySourcesFromPaths(Environment environment, CompositePropertySource composite) { - this.properties.getPaths().stream().map(Paths::get).peek(p -> { - if (!Files.exists(p)) { + properties.getPaths().stream().map(Paths::get).filter(p -> { + boolean exists = Files.exists(p); + if (!exists) { LOG.warn("Configured input path: " + p + " will be ignored because it does not exist on the file system"); } - }).filter(Files::exists).peek(p -> { - if (!Files.isRegularFile(p)) { + return exists; + }).filter(p -> { + boolean regular = Files.isRegularFile(p); + if (!regular) { LOG.warn("Configured input path: " + p + " will be ignored because it is not a regular file"); } - }).filter(Files::isRegularFile).forEach(p -> { + return regular; + }).collect(Collectors.toList()).forEach(p -> { try { String content = new String(Files.readAllBytes(p)).trim(); String filename = p.toAbsolutePath().toString().toLowerCase(); @@ -117,8 +122,7 @@ public abstract class ConfigMapPropertySourceLocator implements PropertySourceLo private void addPropertySourceIfNeeded(Function> contentToMapFunction, String content, String name, CompositePropertySource composite) { - Map map = new HashMap<>(); - map.putAll(contentToMapFunction.apply(content)); + Map map = new HashMap<>(contentToMapFunction.apply(content)); if (map.isEmpty()) { LOG.warn("Property source: " + name + "will be ignored because no properties could be found"); } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java index 6ebf5c61..86d2059b 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java @@ -38,8 +38,7 @@ public final class ConfigUtils { } public static String getApplicationName(Environment env, String configName, String configurationTarget) { - if (StringUtils.isEmpty(configName)) { - // TODO: use relaxed binding + if (!StringUtils.hasLength(configName)) { LOG.debug(configurationTarget + " name has not been set, taking it from property/env " + SPRING_APPLICATION_NAME + " (default=" + FALLBACK_APPLICATION_NAME + ")"); configName = env.getProperty(SPRING_APPLICATION_NAME, FALLBACK_APPLICATION_NAME);