Adds addLabelLocations option to ConfigServerProperties (#681)

Adds addLabelLocations option to server properties.
Also eliminates any duplicate locations.

Fixes gh-676
This commit is contained in:
Venil Noronha
2017-05-19 15:13:02 -07:00
committed by Spencer Gibb
parent 3135d97475
commit d9113a417d
4 changed files with 56 additions and 11 deletions

View File

@@ -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

View File

@@ -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<String> output = new ArrayList<String>();
Collection<String> output = new LinkedHashSet<String>();
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);
}
}
}
}

View File

@@ -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);
}
}