From 75d79fb7f582bbf0031e0914afb0e62b38080208 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 20 Oct 2015 09:23:40 -0400 Subject: [PATCH] Be defensive about null and empty search locations --- .../NativeEnvironmentRepository.java | 32 +++++++++++-------- .../NativeEnvironmentRepositoryTests.java | 7 ++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index 7388955c..b8299eb0 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.boot.Banner.Mode; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor; @@ -58,7 +59,7 @@ public class NativeEnvironmentRepository * Locations to search for configuration files. Defaults to the same as a Spring Boot * app so [classpath:/,classpath:/config/,file:./,file:./config/]. */ - private String[] searchLocations; + private String[] searchLocations = new String[0]; /** * Flag to determine how to handle exceptions during decryption (default false). @@ -97,7 +98,7 @@ public class NativeEnvironmentRepository PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment); - builder.web(false).showBanner(false); + builder.web(false).bannerMode(Mode.OFF); String[] args = getArgs(config, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) @@ -118,7 +119,7 @@ public class NativeEnvironmentRepository @Override public Locations getLocations(String application, String profile, String label) { String[] locations = this.searchLocations; - if (this.searchLocations == null) { + if (this.searchLocations == null || this.searchLocations.length == 0) { locations = DEFAULT_LOCATIONS; } List output = new ArrayList(); @@ -130,15 +131,16 @@ public class NativeEnvironmentRepository output.add(location + label.trim() + "/"); } } - return new Locations(application, profile, label, this.version, output.toArray(new String[0])); + return new Locations(application, profile, label, this.version, + output.toArray(new String[0])); } private ConfigurableEnvironment getEnvironment(String profile) { ConfigurableEnvironment environment = new StandardEnvironment(); environment.getPropertySources() .addFirst(new MapPropertySource("profiles", - Collections. singletonMap( - "spring.profiles.active", profile))); + Collections.singletonMap("spring.profiles.active", + profile))); return environment; } @@ -160,7 +162,8 @@ public class NativeEnvironmentRepository .cleanPath(new File(normal.substring("file:".length())) .getAbsolutePath()); } - for (String pattern : getLocations(null, null, result.getLabel()).getLocations()) { + for (String pattern : getLocations(null, null, result.getLabel()) + .getLocations()) { if (!pattern.contains(":")) { pattern = "file:" + pattern; } @@ -202,7 +205,8 @@ public class NativeEnvironmentRepository list.add("--spring.config.name=" + config); list.add("--spring.cloud.bootstrap.enabled=false"); list.add("--encrypt.failOnError=" + this.failOnError); - list.add("--spring.config.location=" + StringUtils.arrayToCommaDelimitedString(getLocations(null, null, label).getLocations())); + list.add("--spring.config.location=" + StringUtils.arrayToCommaDelimitedString( + getLocations(null, null, label).getLocations())); return list.toArray(new String[0]); } @@ -212,12 +216,14 @@ public class NativeEnvironmentRepository public void setSearchLocations(String... locations) { this.searchLocations = locations; - for (int i = 0; i < locations.length; i++) { - String location = locations[i]; - if (isDirectory(location) && !location.endsWith("/")) { - location = location + "/"; + if (locations != null) { + for (int i = 0; i < locations.length; i++) { + String location = locations[i]; + if (isDirectory(location) && !location.endsWith("/")) { + location = location + "/"; + } + locations[i] = location; } - locations[i] = location; } } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index f4afbcb5..8b23cb56 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -41,6 +41,13 @@ public class NativeEnvironmentRepositoryTests { context.close(); } + @Test + public void emptySearchLocations() { + this.repository.setSearchLocations((String[])null); + Environment environment = this.repository.findOne("foo", "development", "master"); + assertEquals(2, environment.getPropertySources().size()); + } + @Test public void vanilla() { Environment environment = this.repository.findOne("foo", "development", "master");