Prevent System properties in server from affecting profiles

This commit is contained in:
Dave Syer
2014-07-20 16:29:07 +01:00
parent 8d17a9bed1
commit 667d96b276
6 changed files with 103 additions and 9 deletions

64
pom.xml
View File

@@ -72,6 +72,70 @@
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>milestone</id>
<distributionManagement>
<repository>
<id>repo.spring.io</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/libs-milestone-local</url>
</repository>
</distributionManagement>
</profile>
<profile>
<id>central</id>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<name>Nexus Release Repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<distributionManagement>
<!-- see 'staging' profile for dry-run deployment settings -->
<downloadUrl>http://www.springsource.com/download/community
</downloadUrl>
<site>
<id>spring-docs</id>
<url>scp://static.springframework.org/var/www/domains/springframework.org/static/htdocs/spring-retry/docs/${project.version}
</url>
</site>
<repository>
<id>repo.spring.io</id>
<name>Spring Release Repository</name>
<url>https://repo.spring.io/libs-release-local</url>
</repository>
<snapshotRepository>
<id>repo.spring.io</id>
<name>Spring Snapshot Repository</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
</snapshotRepository>
</distributionManagement>
<dependencyManagement>
<dependencies>
<dependency>

View File

@@ -59,6 +59,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
</dependency>
</dependencies>
<properties>

View File

@@ -17,11 +17,15 @@
package org.springframework.platform.config.server;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.platform.config.Environment;
import org.springframework.util.StringUtils;
@@ -42,21 +46,35 @@ public class SpringApplicationEnvironmentRepository implements EnvironmentReposi
public Environment findOne(String config, String profile, String label) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class);
builder.profiles(profile.split(",")).web(false).showBanner(false);
ConfigurableEnvironment environment = getEnvironment(profile);
builder.environment(environment);
builder.web(false).showBanner(false);
String[] args = getArgs(config);
ConfigurableApplicationContext context = builder.run(args);
environment.getPropertySources().remove("profiles");
try {
return new NativeEnvironmentRepository(context.getEnvironment()).findOne(
return new NativeEnvironmentRepository(environment).findOne(
config, profile, label);
} finally {
}
finally {
context.close();
}
}
private ConfigurableEnvironment getEnvironment(String profile) {
ConfigurableEnvironment environment = new StandardEnvironment();
environment.getPropertySources()
.addFirst(
new MapPropertySource("profiles", Collections
.<String, Object> singletonMap("spring.profiles.active",
profile)));
return environment;
}
private String[] getArgs(String config) {
List<String> list = new ArrayList<String>();
if (!config.startsWith("application")) {
config = "application," + config;
config = "application," + config;
}
list.add("--spring.config.name=" + config);
list.add("--spring.platform.bootstrap.enabled=false");

View File

@@ -48,7 +48,7 @@ public class JGitEnvironmentRepositoryTests {
public void vanilla() {
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(1, environment.getPropertySources().size());
assertEquals(2, environment.getPropertySources().size());
assertEquals(JGitEnvironmentRepository.DEFAULT_URI + "/bar.properties",
environment.getPropertySources().get(0).getName());
}
@@ -58,7 +58,7 @@ public class JGitEnvironmentRepositoryTests {
repository.setBasedir(basedir);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(1, environment.getPropertySources().size());
assertEquals(2, environment.getPropertySources().size());
assertEquals(JGitEnvironmentRepository.DEFAULT_URI + "/bar.properties",
environment.getPropertySources().get(0).getName());
}
@@ -70,7 +70,7 @@ public class JGitEnvironmentRepositoryTests {
repository.setBasedir(basedir);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(1, environment.getPropertySources().size());
assertEquals(2, environment.getPropertySources().size());
assertEquals(JGitEnvironmentRepository.DEFAULT_URI + "/bar.properties",
environment.getPropertySources().get(0).getName());
}

View File

@@ -32,6 +32,13 @@ public class SpringApplicationEnvironmentRepositoryTests {
@Test
public void vanilla() {
Environment environment = repository.findOne("foo", "development", "master");
assertEquals(3, environment.getPropertySources().size());
}
@Test
public void ignoresExistingProfile() {
System.setProperty("spring.profiles.active", "cloud");
Environment environment = repository.findOne("foo", "main", "master");
assertEquals(2, environment.getPropertySources().size());
}
@@ -39,14 +46,14 @@ public class SpringApplicationEnvironmentRepositoryTests {
public void prefixed() {
repository.setSearchLocations("classpath:/test");
Environment environment = repository.findOne("foo", "development", "master");
assertEquals(3, environment.getPropertySources().size());
assertEquals(4, environment.getPropertySources().size());
}
@Test
public void prefixedWithFile() {
repository.setSearchLocations("file:./src/test/resources/test");
Environment environment = repository.findOne("foo", "development", "master");
assertEquals(3, environment.getPropertySources().size());
assertEquals(4, environment.getPropertySources().size());
}
}

View File

@@ -0,0 +1 @@
bar: cloud