diff --git a/docs/src/main/asciidoc/spring-cloud-config.adoc b/docs/src/main/asciidoc/spring-cloud-config.adoc index d2bd9dd7..57673154 100644 --- a/docs/src/main/asciidoc/spring-cloud-config.adoc +++ b/docs/src/main/asciidoc/spring-cloud-config.adoc @@ -479,8 +479,9 @@ on the search path, so properties files are loaded from each search location *and* a subdirectory with the same name as the label (the labelled properties take precedence in the Spring Environment). Thus the default behaviour with no placeholders is the same as adding a -search location ending with `/{label}/. For example `file:/tmp/config` -is the same as `file:/tmp/config,file:/tmp/config/{label}` +search location ending with `/{label}/`. For example `file:/tmp/config` +is the same as `file:/tmp/config,file:/tmp/config/{label}`. This behavior can be +disabled by setting `spring.cloud.config.server.native.addLabelLocations=false`. ==== Vault Backend 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 cf435669..c316b636 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 @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,9 @@ package org.springframework.cloud.config.server.environment; import java.io.File; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import org.apache.commons.logging.Log; @@ -46,6 +48,7 @@ import org.springframework.util.StringUtils; * * @author Dave Syer * @author Roy Clarkson + * @author Venil Noronha */ @ConfigurationProperties("spring.cloud.config.server.native") public class NativeEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, Ordered { @@ -65,6 +68,11 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc */ private boolean failOnError = false; + /** + * Flag to determine whether label locations should be added. + */ + private boolean addLabelLocations = true; + /** * Version string to be reported for native repository */ @@ -89,6 +97,14 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc return this.failOnError; } + public void setAddLabelLocations(boolean addLabelLocations) { + this.addLabelLocations = addLabelLocations; + } + + public boolean isAddLabelLocations() { + return this.addLabelLocations; + } + public String getDefaultLabel() { return DEFAULT_LABEL; } @@ -126,7 +142,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc if (this.searchLocations == null || this.searchLocations.length == 0) { locations = DEFAULT_LOCATIONS; } - List output = new ArrayList(); + Collection output = new LinkedHashSet(); for (String location : locations) { String[] profiles = new String[] { profile }; if (profile != null) { @@ -157,11 +173,13 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc } } } - for (String location : locations) { - if (StringUtils.hasText(label)) { - String labelled = location + label.trim() + "/"; - if (isDirectory(labelled)) { - output.add(labelled); + if (this.addLabelLocations) { + for (String location : locations) { + if (StringUtils.hasText(label)) { + String labelled = location + label.trim() + "/"; + if (isDirectory(labelled)) { + output.add(labelled); + } } } } 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 f43a5eec..354ccfb0 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 @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.cloud.config.server.environment; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.junit.Before; import org.junit.Test; @@ -27,7 +28,7 @@ import org.springframework.context.ConfigurableApplicationContext; /** * @author Dave Syer * @author Spencer Gibb - * + * @author Venil Noronha */ public class NativeEnvironmentRepositoryTests { @@ -163,4 +164,28 @@ public class NativeEnvironmentRepositoryTests { environment.getPropertySources().get(0).getSource().get("foo")); } + @Test + public void locationAddLabelLocations() { + this.repository.setSearchLocations("classpath:/test/dev/"); + Environment environment = this.repository.findOne("foo", "development", "ignore"); + assertEquals(2, environment.getPropertySources().size()); + assertNotEquals("dev_bar", environment.getPropertySources().get(0).getSource().get("foo")); + } + + @Test + public void locationDontAddLabelLocations() { + this.repository.setSearchLocations("classpath:/test/dev/"); + this.repository.setAddLabelLocations(false); + Environment environment = this.repository.findOne("foo", "development", "ignore"); + assertEquals(1, environment.getPropertySources().size()); + assertEquals("dev_bar", environment.getPropertySources().get(0).getSource().get("foo")); + } + + @Test + public void locationNoDuplicates() { + this.repository.setSearchLocations("classpath:/test/{profile}", "classpath:/test/dev"); + Locations locations = this.repository.getLocations("foo", "dev", null); + assertEquals(1, locations.getLocations().length); + } + } diff --git a/spring-cloud-config-server/src/test/resources/test/dev/ignore/application.properties b/spring-cloud-config-server/src/test/resources/test/dev/ignore/application.properties new file mode 100644 index 00000000..e9e0aaf5 --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/test/dev/ignore/application.properties @@ -0,0 +1 @@ +foo: app \ No newline at end of file