Fix property source ordering issue with bootstrap context

The problem is that the bootstrap context gets created first
with the correct wiremock server port, and then the environment
is merged into the main context before the annotation
property source is created, so the latter takes precedence.
Fixed by re-ordering the property sources if detected.

Also switch off bootstrap context for wiremock server

Fixes gh-225
This commit is contained in:
Dave Syer
2017-03-24 09:24:28 +00:00
parent fd04629f8f
commit c1e285e76f
4 changed files with 40 additions and 15 deletions

View File

@@ -4,3 +4,4 @@ target/
.gradle
build/
/.apt_generated/

View File

@@ -21,6 +21,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud-contract.version>1.0.5.BUILD-SNAPSHOT</spring-cloud-contract.version>
<spring-cloud-commons.version>1.1.8.RELEASE</spring-cloud-commons.version>
</properties>
<dependencies>
@@ -32,6 +33,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<!-- tag::stub_runner[] -->
<dependency>
@@ -79,6 +84,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons-dependencies</artifactId>
<version>${spring-cloud-commons.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- end::contract_bom[] -->

View File

@@ -111,7 +111,8 @@ class SpringBootHttpServer
@Override
public void start() {
this.context = new SpringApplicationBuilder(WiremockServerConfiguration.class)
.logStartupInfo(false).bannerMode(Mode.OFF).listeners(this).run();
.logStartupInfo(false).bannerMode(Mode.OFF)
.properties("spring.cloud.bootstrap.enabled=false").listeners(this).run();
this.running = true;
}
@@ -180,7 +181,8 @@ class SpringBootHttpServer
return bean;
}
private void setupHttps(WiremockServerProperties server, HttpsSettings httpsSettings) {
private void setupHttps(WiremockServerProperties server,
HttpsSettings httpsSettings) {
if (httpsSettings.port() < 0 || !httpsSettings.enabled()) {
return;
}
@@ -209,7 +211,7 @@ class SpringBootHttpServer
}
class WiremockServerProperties implements EmbeddedServletContainerCustomizer {
private ServerProperties delegate = new ServerProperties();
public Integer getPort() {
@@ -289,7 +291,8 @@ class WiremockServerConfiguration {
WiremockServerConfiguration.this.adminRequestHandler);
servletContext.setAttribute(StubRequestHandler.class.getName(),
WiremockServerConfiguration.this.stubRequestHandler);
servletContext.setAttribute(Notifier.KEY, WiremockServerConfiguration.this.options.notifier());
servletContext.setAttribute(Notifier.KEY,
WiremockServerConfiguration.this.options.notifier());
}
};
}
@@ -393,7 +396,7 @@ class ContainerConfiguration {
@Autowired
private ContainerProperties container;
private Integer port;
@Bean
@@ -403,8 +406,11 @@ class ContainerConfiguration {
undertow.addBuilderCustomizers(new UndertowBuilderCustomizer() {
@Override
public void customize(Builder builder) {
builder.addHttpListener(UndertowContainerConfiguration.this.options.portNumber(), "localhost");
UndertowContainerConfiguration.this.port = UndertowContainerConfiguration.this.options.portNumber();
builder.addHttpListener(
UndertowContainerConfiguration.this.options.portNumber(),
"localhost");
UndertowContainerConfiguration.this.port = UndertowContainerConfiguration.this.options
.portNumber();
}
});
}

View File

@@ -26,6 +26,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.SocketUtils;
/**
@@ -47,10 +48,7 @@ public class WireMockApplicationListener
private void registerPort(ConfigurableEnvironment environment) {
if (environment.getProperty("wiremock.server.port", Integer.class, 0) == 0) {
MutablePropertySources propertySources = environment.getPropertySources();
if (!propertySources.contains("wiremock")) {
propertySources.addFirst(
new MapPropertySource("wiremock", new HashMap<String, Object>()));
}
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources
.get("wiremock")).getSource();
source.put("wiremock.server.port",
@@ -59,10 +57,7 @@ public class WireMockApplicationListener
if (environment.getProperty("wiremock.server.https-port", Integer.class,
0) == 0) {
MutablePropertySources propertySources = environment.getPropertySources();
if (!propertySources.contains("wiremock")) {
propertySources.addFirst(
new MapPropertySource("wiremock", new HashMap<String, Object>()));
}
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources
.get("wiremock")).getSource();
source.put("wiremock.server.https-port",
@@ -70,4 +65,15 @@ public class WireMockApplicationListener
}
}
private void addPropertySource(MutablePropertySources propertySources) {
if (!propertySources.contains("wiremock")) {
propertySources.addFirst(
new MapPropertySource("wiremock", new HashMap<String, Object>()));
} else {
// Move it up into first place
PropertySource<?> wiremock = propertySources.remove("wiremock");
propertySources.addFirst(wiremock);
}
}
}