Removed OrderedEnvironmentRepository interface, and applied other review feedback.

This commit is contained in:
Ryan Baxter
2016-12-20 13:46:46 -05:00
parent 3db105c475
commit 5ebecb60b4
12 changed files with 71 additions and 108 deletions

View File

@@ -576,9 +576,9 @@ spring:
svn:
uri: file:///path/to/svn/repo
order: 2
git:
uri: file:///path/to/git/repo
order: 1
git:
uri: file:///path/to/git/repo
order: 1
----
In addition to each repo specifying a URI, you can also specify an `order` property.
@@ -600,13 +600,7 @@ repo does not contain a branch called `master` the entire request will fail.
It is also possible to provide your own `EnvironmentRepository` bean
to be included as part of a composite environment in addition to
using one of the environment repositories from Spring Cloud. To do this your bean
must implement `OrderedEnvironmentRepository`. For convenience sake, there is
an abstract implementation of this interface you can extend called
`AbstractOrderedEnvironmentRepository`. If you extend `AbstractOrderedEnvironmentRepository`
your environment repository will have the lowest possible precedence by default.
If you would like to change this, make sure you call the `setOrder` method
to set the precedence or override the `getOrder` method to return the order
value you would like your environment repository to have.
must implement both the `EnvironmentRepository` and `Ordered` interfaces.
==== Property Overrides

View File

@@ -76,7 +76,7 @@ public class Environment {
this.propertySources.add(propertySource);
}
public void add(List<PropertySource> propertySources) {
public void addAll(List<PropertySource> propertySources) {
this.propertySources.addAll(propertySources);
}

View File

@@ -21,7 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.config.server.environment.CompositeEnvironmentRepository;
import org.springframework.cloud.config.server.environment.OrderedEnvironmentRepository;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.environment.SearchPathCompositeEnvironmentRepository;
import org.springframework.cloud.config.server.environment.SearchPathLocator;
import org.springframework.context.annotation.Bean;
@@ -32,27 +32,26 @@ import org.springframework.context.annotation.Primary;
* @author Ryan Baxter
*/
@Configuration
@ConditionalOnBean(OrderedEnvironmentRepository.class)
public class CompositeConfiguration {
private List<OrderedEnvironmentRepository> environmentRepos = new ArrayList<>();
private List<EnvironmentRepository> environmentRepos = new ArrayList<>();
@Bean
@Primary
@ConditionalOnBean(SearchPathLocator.class)
public SearchPathCompositeEnvironmentRepository compositeEnvironmentRepository() {
public SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository() {
return new SearchPathCompositeEnvironmentRepository(environmentRepos);
}
@Bean
@Primary
@ConditionalOnMissingBean(SearchPathLocator.class)
public CompositeEnvironmentRepository compositeEnvironmentRepository2() {
public CompositeEnvironmentRepository compositeEnvironmentRepository() {
return new CompositeEnvironmentRepository(environmentRepos);
}
@Autowired
private void setEnvironmentRepos(List<OrderedEnvironmentRepository> repos) {
private void setEnvironmentRepos(List<EnvironmentRepository> repos) {
this.environmentRepos = repos;
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2013-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.config.server.environment;
import org.springframework.core.Ordered;
/**
* @author Ryan Baxter
*/
public abstract class AbstractOrderedEnvironmentRepository implements OrderedEnvironmentRepository {
private int order = Ordered.LOWEST_PRECEDENCE;
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}

View File

@@ -26,7 +26,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
*
*/
public abstract class AbstractScmEnvironmentRepository extends AbstractScmAccessor
implements OrderedEnvironmentRepository, SearchPathLocator {
implements EnvironmentRepository, SearchPathLocator, Ordered {
private EnvironmentCleaner cleaner = new EnvironmentCleaner();
private int order = Ordered.LOWEST_PRECEDENCE;

View File

@@ -21,18 +21,18 @@ import org.springframework.cloud.config.environment.Environment;
import org.springframework.core.OrderComparator;
/**
* An {@link EnvironmentRepository} composed of multiple ordered {@link OrderedEnvironmentRepository}s.
* An {@link EnvironmentRepository} composed of multiple ordered {@link EnvironmentRepository}s.
* @author Ryan Baxter
*/
public class CompositeEnvironmentRepository implements EnvironmentRepository {
protected List<OrderedEnvironmentRepository> environmentRepositories;
protected List<EnvironmentRepository> environmentRepositories;
/**
* Creates a new {@link CompositeEnvironmentRepository}.
* @param environmentRepositories The list of {@link OrderedEnvironmentRepository}s to create the composite from.
* @param environmentRepositories The list of {@link EnvironmentRepository}s to create the composite from.
*/
public CompositeEnvironmentRepository(List<OrderedEnvironmentRepository> environmentRepositories) {
public CompositeEnvironmentRepository(List<EnvironmentRepository> environmentRepositories) {
//Sort the environment repositories by the priority
Collections.sort(environmentRepositories, OrderComparator.INSTANCE);
this.environmentRepositories = environmentRepositories;
@@ -42,7 +42,7 @@ public class CompositeEnvironmentRepository implements EnvironmentRepository {
public Environment findOne(String application, String profile, String label) {
Environment env = new Environment(application, new String[]{profile}, label, null, null);
for(EnvironmentRepository repo : environmentRepositories) {
env.add(repo.findOne(application, profile, label).getPropertySources());
env.addAll(repo.findOne(application, profile, label).getPropertySources());
}
return env;
}

View File

@@ -32,6 +32,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
@@ -47,8 +48,7 @@ import org.springframework.util.StringUtils;
* @author Roy Clarkson
*/
@ConfigurationProperties("spring.cloud.config.server.native")
public class NativeEnvironmentRepository extends AbstractOrderedEnvironmentRepository
implements SearchPathLocator {
public class NativeEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, Ordered {
private static Log logger = LogFactory.getLog(NativeEnvironmentRepository.class);
@@ -75,6 +75,8 @@ public class NativeEnvironmentRepository extends AbstractOrderedEnvironmentRepos
private ConfigurableEnvironment environment;
private int order = Ordered.LOWEST_PRECEDENCE;
public NativeEnvironmentRepository(ConfigurableEnvironment environment) {
this.environment = environment;
}
@@ -275,4 +277,12 @@ public class NativeEnvironmentRepository extends AbstractOrderedEnvironmentRepos
&& !location.endsWith(".yml") && !location.endsWith(".yaml");
}
@Override
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
}

View File

@@ -1,25 +0,0 @@
/*
* Copyright 2013-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.config.server.environment;
import org.springframework.core.Ordered;
/**
* A prioritized {@link EnvironmentRepository}.
* @author Ryan Baxter
*/
public interface OrderedEnvironmentRepository extends EnvironmentRepository, Ordered {
}

View File

@@ -27,9 +27,9 @@ public class SearchPathCompositeEnvironmentRepository extends CompositeEnvironme
/**
* Creates a new {@link SearchPathCompositeEnvironmentRepository}.
* @param environmentRepositories The {@link OrderedEnvironmentRepository}s to create this composite from.
* @param environmentRepositories The {@link EnvironmentRepository}s to create this composite from.
*/
public SearchPathCompositeEnvironmentRepository(List<OrderedEnvironmentRepository> environmentRepositories) {
public SearchPathCompositeEnvironmentRepository(List<EnvironmentRepository> environmentRepositories) {
super(environmentRepositories);
}

View File

@@ -34,6 +34,7 @@ import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.core.Ordered;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
@@ -55,8 +56,7 @@ import static org.springframework.cloud.config.client.ConfigClientProperties.TOK
* @author Mark Paluch
*/
@ConfigurationProperties("spring.cloud.config.server.vault")
public class VaultEnvironmentRepository extends AbstractOrderedEnvironmentRepository
implements InitializingBean{
public class VaultEnvironmentRepository implements EnvironmentRepository, InitializingBean, Ordered {
public static final String VAULT_TOKEN = "X-Vault-Token";
@@ -82,6 +82,8 @@ public class VaultEnvironmentRepository extends AbstractOrderedEnvironmentReposi
@NotEmpty
private String profileSeparator = ",";
private int order = Ordered.LOWEST_PRECEDENCE;
private RestTemplate rest;
//TODO: move to watchState:String on findOne?
@@ -230,9 +232,13 @@ public class VaultEnvironmentRepository extends AbstractOrderedEnvironmentReposi
this.profileSeparator = profileSeparator;
}
@Override
public void setOrder(int order) {
super.setOrder(order);
this.order = order;
}
@Override
public int getOrder() {
return order;
}
@JsonIgnoreProperties(ignoreUnknown = true)

View File

@@ -28,11 +28,11 @@ import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.config.server.environment.AbstractOrderedEnvironmentRepository;
import org.springframework.cloud.config.server.environment.OrderedEnvironmentRepository;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@@ -76,25 +76,30 @@ public class CustomCompositeEnvironmentRepositoryTests {
protected static class TestApplication {
@Bean
public OrderedEnvironmentRepository environmentRepository() {
return new AbstractOrderedEnvironmentRepository() {
@Override
public Environment findOne(String application, String profile,
String label) {
Environment e = new Environment("test", new String[0], "label", "version",
"state");
PropertySource p = new PropertySource("p", new HashMap<>());
e.add(p);
return e;
}
};
public EnvironmentRepository environmentRepository() {
return new CustomEnvironmentRepository();
}
public static void main(String[] args) throws Exception {
SpringApplication.run(CustomEnvironmentRepositoryTests.TestApplication.class,
args);
}
}
static class CustomEnvironmentRepository implements EnvironmentRepository, Ordered {
@Override
public Environment findOne(String application, String profile, String label) {
Environment e = new Environment("test", new String[0], "label", "version",
"state");
PropertySource p = new PropertySource("p", new HashMap<>());
e.add(p);
return e;
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
}
}

View File

@@ -20,6 +20,8 @@ import java.util.List;
import org.junit.Test;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;
@@ -30,13 +32,14 @@ import static org.mockito.Mockito.mock;
*/
public class CompositeEnvironmentRepositoryTests {
private class TestOrderedEnvironmentRepository extends AbstractOrderedEnvironmentRepository implements SearchPathLocator {
private class TestOrderedEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, Ordered {
private Environment env;
private Locations locations;
private int order = Ordered.LOWEST_PRECEDENCE;
public TestOrderedEnvironmentRepository(int order, Environment env, Locations locations) {
setOrder(order);
this.order = order;
this.env = env;
this.locations = locations;
}
@@ -50,6 +53,11 @@ public class CompositeEnvironmentRepositoryTests {
public Locations getLocations(String application, String profile, String label) {
return locations;
}
@Override
public int getOrder() {
return order;
}
}
@Test
@@ -80,7 +88,7 @@ public class CompositeEnvironmentRepositoryTests {
SearchPathLocator.Locations loc1 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[]{sLoc1});
SearchPathLocator.Locations loc2 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[]{sLoc5, sLoc4});
SearchPathLocator.Locations loc3 = new SearchPathLocator.Locations("app", "dev", "label", "version", new String[]{sLoc3, sLoc2});
List<OrderedEnvironmentRepository> repos = new ArrayList<OrderedEnvironmentRepository>();
List<EnvironmentRepository> repos = new ArrayList<EnvironmentRepository>();
repos.add(new TestOrderedEnvironmentRepository(3, e1, loc1));
repos.add(new TestOrderedEnvironmentRepository(2, e3, loc2));
repos.add(new TestOrderedEnvironmentRepository(1, e2, loc3));