From 5d8abad123efdb137c93e1203c64ba5f7fc72dbb Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 13 Jun 2014 17:58:21 +0100 Subject: [PATCH] Add Environment abstraction --- pom.xml | 14 +++++ .../ConfigServicePropertySourceLocator.java | 40 ++---------- .../bootstrap/config/Environment.java | 61 ++++++++++++++++++ .../bootstrap/config/PropertySource.java | 49 +++++++++++++++ spring-platform-config-server/pom.xml | 9 +++ .../platform/config/server/Application.java | 60 +++++------------- .../config/server/EnvironmentRepository.java | 29 +++++++++ .../server/NativeEnvironmentRepository.java | 63 +++++++++++++++++++ .../server/SerializableMapPropertySource.java | 38 +++++++++++ 9 files changed, 282 insertions(+), 81 deletions(-) create mode 100644 spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/Environment.java create mode 100644 spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/PropertySource.java create mode 100644 spring-platform-config-server/src/main/java/org/springframework/platform/config/server/EnvironmentRepository.java create mode 100644 spring-platform-config-server/src/main/java/org/springframework/platform/config/server/NativeEnvironmentRepository.java create mode 100644 spring-platform-config-server/src/main/java/org/springframework/platform/config/server/SerializableMapPropertySource.java diff --git a/pom.xml b/pom.xml index 697a0e1b..ec5b4148 100644 --- a/pom.xml +++ b/pom.xml @@ -82,4 +82,18 @@ full + + + + org.springframework.platform + spring-platform-config-client + 1.0.0.BUILD-SNAPSHOT + + + org.springframework.platform + spring-platform-config-client + 1.0.0.BUILD-SNAPSHOT + + + diff --git a/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/ConfigServicePropertySourceLocator.java b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/ConfigServicePropertySourceLocator.java index cd1861e9..d330074c 100644 --- a/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/ConfigServicePropertySourceLocator.java +++ b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/ConfigServicePropertySourceLocator.java @@ -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 result = restTemplate.exchange( - url + "/{name}/{env}/{label}", HttpMethod.GET, - new HttpEntity((Void) null), - new ParameterizedTypeReference>() { - }, name, env, label).getBody(); - for (SerializableMapPropertySource source : result) { + Environment result = restTemplate.exchange(url + "/{name}/{env}/{label}", + HttpMethod.GET, new HttpEntity((Void) null), Environment.class, + name, env, label).getBody(); + for (PropertySource source : result.getPropertySources()) { @SuppressWarnings("unchecked") Map map = (Map) 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; - } - - } } diff --git a/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/Environment.java b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/Environment.java new file mode 100644 index 00000000..587a3d88 --- /dev/null +++ b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/Environment.java @@ -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 propertySources = new ArrayList(); + + @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 getPropertySources() { + return propertySources; + } + + public String getName() { + return name; + } + + public String getLabel() { + return label; + } + +} diff --git a/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/PropertySource.java b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/PropertySource.java new file mode 100644 index 00000000..a98b62b1 --- /dev/null +++ b/spring-platform-config-client/src/main/java/org/springframework/platform/bootstrap/config/PropertySource.java @@ -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; + } + +} diff --git a/spring-platform-config-server/pom.xml b/spring-platform-config-server/pom.xml index 152ae86c..112d04be 100644 --- a/spring-platform-config-server/pom.xml +++ b/spring-platform-config-server/pom.xml @@ -17,6 +17,10 @@ + + org.springframework.platform + spring-platform-config-client + org.springframework.boot spring-boot-starter-actuator @@ -25,6 +29,11 @@ org.springframework.boot spring-boot-starter-web + + org.eclipse.jgit + org.eclipse.jgit + 2.3.1.201302201838-r + org.springframework.boot spring-boot-starter-test diff --git a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/Application.java b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/Application.java index fdd13cf4..ff15eaa4 100644 --- a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/Application.java +++ b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/Application.java @@ -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 standardSources = new HashSet(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 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 properties(@PathVariable String env, @PathVariable String label) { - List result = new ArrayList(); - 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; - } - } } diff --git a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/EnvironmentRepository.java b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/EnvironmentRepository.java new file mode 100644 index 00000000..e0ac1ab7 --- /dev/null +++ b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/EnvironmentRepository.java @@ -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); + +} diff --git a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/NativeEnvironmentRepository.java b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/NativeEnvironmentRepository.java new file mode 100644 index 00000000..085482db --- /dev/null +++ b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/NativeEnvironmentRepository.java @@ -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 standardSources = new HashSet(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; + + } + +} diff --git a/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/SerializableMapPropertySource.java b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/SerializableMapPropertySource.java new file mode 100644 index 00000000..2ae40dfd --- /dev/null +++ b/spring-platform-config-server/src/main/java/org/springframework/platform/config/server/SerializableMapPropertySource.java @@ -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; + } + +} \ No newline at end of file