Add Environment abstraction

This commit is contained in:
Dave Syer
2014-06-13 17:58:21 +01:00
parent c6cee8a8cc
commit 5d8abad123
9 changed files with 282 additions and 81 deletions

14
pom.xml
View File

@@ -82,4 +82,18 @@
<id>full</id>
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.platform</groupId>
<artifactId>spring-platform-config-client</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.platform</groupId>
<artifactId>spring-platform-config-client</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

View File

@@ -16,21 +16,15 @@
package org.springframework.platform.bootstrap.config;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Dave Syer
*
@@ -49,14 +43,12 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
private RestTemplate restTemplate = new RestTemplate();
@Override
public PropertySource<?> locate() {
public org.springframework.core.env.PropertySource<?> locate() {
CompositePropertySource composite = new CompositePropertySource("configService");
List<SerializableMapPropertySource> result = restTemplate.exchange(
url + "/{name}/{env}/{label}", HttpMethod.GET,
new HttpEntity<Void>((Void) null),
new ParameterizedTypeReference<List<SerializableMapPropertySource>>() {
}, name, env, label).getBody();
for (SerializableMapPropertySource source : result) {
Environment result = restTemplate.exchange(url + "/{name}/{env}/{label}",
HttpMethod.GET, new HttpEntity<Void>((Void) null), Environment.class,
name, env, label).getBody();
for (PropertySource source : result.getPropertySources()) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) source.getSource();
composite.addPropertySource(new MapPropertySource(source.getName(), map));
@@ -96,26 +88,4 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
this.label = label;
}
protected static class SerializableMapPropertySource {
private String name;
private Map<?, ?> source;
@JsonCreator
public SerializableMapPropertySource(@JsonProperty("name") String name,
@JsonProperty("source") Map<?, ?> source) {
this.name = name;
this.source = source;
}
public String getName() {
return name;
}
public Map<?, ?> getSource() {
return source;
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2013-2014 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.platform.bootstrap.config;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Dave Syer
*
*/
public class Environment {
private String name;
private String label;
private List<PropertySource> propertySources = new ArrayList<PropertySource>();
@JsonCreator
public Environment(@JsonProperty("name") String name,
@JsonProperty("label") String label) {
super();
this.name = name;
this.label = label;
}
public void add(PropertySource propertySource) {
this.propertySources.add(propertySource);
}
public List<PropertySource> getPropertySources() {
return propertySources;
}
public String getName() {
return name;
}
public String getLabel() {
return label;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2013-2014 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.platform.bootstrap.config;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Dave Syer
*
*/
public class PropertySource {
private String name;
private Map<?, ?> source;
@JsonCreator
public PropertySource(@JsonProperty("name") String name,
@JsonProperty("source") Map<?, ?> source) {
this.name = name;
this.source = source;
}
public String getName() {
return name;
}
public Map<?, ?> getSource() {
return source;
}
}

View File

@@ -17,6 +17,10 @@
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.platform</groupId>
<artifactId>spring-platform-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -25,6 +29,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>2.3.1.201302201838-r</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -1,26 +1,17 @@
package org.springframework.platform.config.server;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.platform.bootstrap.config.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.StandardServletEnvironment;
@Configuration
@ComponentScan
@@ -29,53 +20,30 @@ import org.springframework.web.context.support.StandardServletEnvironment;
public class Application {
@Autowired
private ConfigurableEnvironment environment;
private Set<String> standardSources = new HashSet<String>(Arrays.asList(
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
private EnvironmentRepository repository;
@RequestMapping("/{name}/{env}")
public List<SerializableMapPropertySource> master(@PathVariable String env) {
return properties(env, "master");
public Environment master(@PathVariable String name, @PathVariable String env) {
return properties(name, env, "master");
}
@RequestMapping("/{name}/{env}/{label}")
public List<SerializableMapPropertySource> properties(@PathVariable String env, @PathVariable String label) {
List<SerializableMapPropertySource> result = new ArrayList<Application.SerializableMapPropertySource>();
for (PropertySource<?> source : environment.getPropertySources()) {
String name = source.getName();
if (!standardSources .contains(name) && source instanceof MapPropertySource) {
result.add(new SerializableMapPropertySource(name, (Map<?,?>)source.getSource()));
}
}
return result;
public Environment properties(@PathVariable String name, @PathVariable String env, @PathVariable String label) {
return repository.findOne(name, env, label);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
protected static class SerializableMapPropertySource {
private String name;
private Map<?, ?> source;
public SerializableMapPropertySource(String name, Map<?,?> source) {
this.name = name;
this.source = source;
}
@Configuration
protected static class NativeRepositoryConfiguration {
@Autowired
private ConfigurableEnvironment environment;
public String getName() {
return name;
@Bean
public NativeEnvironmentRepository repository() {
return new NativeEnvironmentRepository(environment);
}
public Map<?, ?> getSource() {
return source;
}
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2013-2014 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.platform.config.server;
import org.springframework.platform.bootstrap.config.Environment;
/**
* @author Dave Syer
*
*/
public interface EnvironmentRepository {
Environment findOne(String application, String name, String label);
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2013-2014 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.platform.config.server;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.platform.bootstrap.config.Environment;
import org.springframework.platform.bootstrap.config.PropertySource;
import org.springframework.web.context.support.StandardServletEnvironment;
/**
* @author Dave Syer
*
*/
public class NativeEnvironmentRepository implements EnvironmentRepository {
private Set<String> standardSources = new HashSet<String>(Arrays.asList(
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
private ConfigurableEnvironment environment;
public NativeEnvironmentRepository(ConfigurableEnvironment environment) {
this.environment = environment;
}
@Override
public Environment findOne(String application, String env, String label) {
Environment result = new Environment(env, label);
for (org.springframework.core.env.PropertySource<?> source : environment.getPropertySources()) {
String name = source.getName();
if (!standardSources .contains(name) && source instanceof MapPropertySource) {
result.add(new PropertySource(name, (Map<?,?>)source.getSource()));
}
}
return result;
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2013-2014 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.platform.config.server;
import java.util.Map;
class SerializableMapPropertySource {
private String name;
private Map<?, ?> source;
public SerializableMapPropertySource(String name, Map<?,?> source) {
this.name = name;
this.source = source;
}
public String getName() {
return name;
}
public Map<?, ?> getSource() {
return source;
}
}