Skip to content

Commit f847ed2

Browse files
author
Phillip Webb
committed
Remove getEmbeddedServletContainers()
Remove the mutable getEmbeddedServletContainers() Map from EmbeddedWebApplicationContext and instead use the `namespace` to distinguish the management container. The ServerPortInfoApplicationContextInitializer class replaces the previous TestExecutionListener to exposes port properties (by listening for EmbeddedServletContainerInitializedEvents).
1 parent fad5ce4 commit f847ed2

File tree

7 files changed

+93
-91
lines changed

7 files changed

+93
-91
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353
import org.springframework.boot.autoconfigure.web.ServerProperties;
5454
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
5555
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
56-
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
5756
import org.springframework.boot.context.embedded.EmbeddedServletContainerException;
58-
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
5957
import org.springframework.context.ApplicationContext;
6058
import org.springframework.context.ApplicationContextAware;
6159
import org.springframework.context.ApplicationListener;
@@ -173,6 +171,7 @@ private void createChildManagementContext() {
173171

174172
final AnnotationConfigEmbeddedWebApplicationContext childContext = new AnnotationConfigEmbeddedWebApplicationContext();
175173
childContext.setParent(this.applicationContext);
174+
childContext.setNamespace("management");
176175
childContext.setId(this.applicationContext.getId() + ":management");
177176

178177
// Register the ManagementServerChildContextConfiguration first followed
@@ -197,8 +196,6 @@ public void onApplicationEvent(ContextClosedEvent event) {
197196
}
198197
try {
199198
childContext.refresh();
200-
registerContainer(this.applicationContext,
201-
childContext.getEmbeddedServletContainer());
202199
}
203200
catch (RuntimeException ex) {
204201
// No support currently for deploying a war with management.port=<different>,
@@ -213,16 +210,6 @@ public void onApplicationEvent(ContextClosedEvent event) {
213210
}
214211
};
215212

216-
private void registerContainer(ApplicationContext applicationContext,
217-
EmbeddedServletContainer embeddedServletContainer) {
218-
if (applicationContext instanceof EmbeddedWebApplicationContext) {
219-
((EmbeddedWebApplicationContext) applicationContext)
220-
.getEmbeddedServletContainers().put("management",
221-
embeddedServletContainer);
222-
// Maybe unregister it when it shuts down?
223-
}
224-
}
225-
226213
protected static enum ManagementServerPort {
227214

228215
DISABLE, SAME, DIFFERENT;

spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedServletContainerInitializedEvent.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.context.embedded;
1818

19-
import org.springframework.context.ApplicationContext;
2019
import org.springframework.context.ApplicationEvent;
2120

2221
/**
@@ -29,10 +28,11 @@
2928
*/
3029
public class EmbeddedServletContainerInitializedEvent extends ApplicationEvent {
3130

32-
private final ApplicationContext applicationContext;
31+
private final EmbeddedWebApplicationContext applicationContext;
3332

3433
public EmbeddedServletContainerInitializedEvent(
35-
ApplicationContext applicationContext, EmbeddedServletContainer source) {
34+
EmbeddedWebApplicationContext applicationContext,
35+
EmbeddedServletContainer source) {
3636
super(source);
3737
this.applicationContext = applicationContext;
3838
}
@@ -60,7 +60,7 @@ public EmbeddedServletContainer getSource() {
6060
* context) before acting on the server container itself.
6161
* @return the applicationContext that the container was created from
6262
*/
63-
public ApplicationContext getApplicationContext() {
63+
public EmbeddedWebApplicationContext getApplicationContext() {
6464
return this.applicationContext;
6565
}
6666

spring-boot/src/main/java/org/springframework/boot/context/embedded/EmbeddedWebApplicationContext.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Collections;
2222
import java.util.Comparator;
2323
import java.util.EventListener;
24-
import java.util.HashMap;
2524
import java.util.LinkedHashMap;
2625
import java.util.LinkedHashSet;
2726
import java.util.List;
@@ -89,8 +88,6 @@
8988
*/
9089
public class EmbeddedWebApplicationContext extends GenericWebApplicationContext {
9190

92-
private static final String DEFAULT_SERVER_NAME = "server";
93-
9491
/**
9592
* Constant value for the DispatcherServlet bean name. A Servlet bean with this name
9693
* is deemed to be the "main" servlet and is automatically given a mapping of "/" by
@@ -105,8 +102,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
105102

106103
private String namespace;
107104

108-
private Map<String, EmbeddedServletContainer> containers = new HashMap<String, EmbeddedServletContainer>();
109-
110105
/**
111106
* Register ServletContextAwareProcessor.
112107
* @see ServletContextAwareProcessor
@@ -163,7 +158,6 @@ private synchronized void createEmbeddedServletContainer() {
163158
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
164159
this.embeddedServletContainer = containerFactory
165160
.getEmbeddedServletContainer(getSelfInitializer());
166-
this.containers.put(DEFAULT_SERVER_NAME, this.embeddedServletContainer);
167161
}
168162
else if (getServletContext() != null) {
169163
try {
@@ -388,7 +382,6 @@ private synchronized void stopAndReleaseEmbeddedServletContainer() {
388382
try {
389383
this.embeddedServletContainer.stop();
390384
this.embeddedServletContainer = null;
391-
this.containers.remove(DEFAULT_SERVER_NAME);
392385
}
393386
catch (Exception ex) {
394387
throw new IllegalStateException(ex);
@@ -432,14 +425,4 @@ public EmbeddedServletContainer getEmbeddedServletContainer() {
432425
return this.embeddedServletContainer;
433426
}
434427

435-
/**
436-
* A registry of embedded containers by name. The
437-
* {@link #getEmbeddedServletContainer() canonical container} is called "server".
438-
* Anyone else who creates one can register it with whatever name they please.
439-
* @return the containers
440-
*/
441-
public Map<String, EmbeddedServletContainer> getEmbeddedServletContainers() {
442-
return this.containers;
443-
}
444-
445428
}

spring-boot/src/main/java/org/springframework/boot/test/EmbeddedServletContainerTestExecutionListener.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
@Target(ElementType.TYPE)
4242
// Leave out the ServletTestExecutionListener because it only deals with Mock* servlet
4343
// stuff. A real embedded application will not need the mocks.
44-
@TestExecutionListeners(listeners = {
45-
EmbeddedServletContainerTestExecutionListener.class,
46-
DependencyInjectionTestExecutionListener.class,
44+
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
4745
DirtiesContextTestExecutionListener.class,
4846
TransactionalTestExecutionListener.class })
4947
public @interface IntegrationTest {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
21+
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
22+
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.context.ApplicationContextInitializer;
25+
import org.springframework.context.ApplicationListener;
26+
import org.springframework.context.ConfigurableApplicationContext;
27+
import org.springframework.core.env.Environment;
28+
import org.springframework.util.StringUtils;
29+
30+
/**
31+
* {@link ApplicationContextInitializer} that sets {@link Environment} properties for the
32+
* ports that {@link EmbeddedServletContainer} servers are actually listening on. The
33+
* property {@literal "local.server.port"} can be injected directly into tests using
34+
* {@link Value @Value} or obtained via the {@link Environment}.
35+
* <p>
36+
* If the {@link EmbeddedWebApplicationContext} has a
37+
* {@link EmbeddedWebApplicationContext#setNamespace(String) namespace} set, it will be
38+
* used to construct the property name. For example, the "management" actuator context
39+
* will have the property name {@literal "local.management.port"}.
40+
* <p>
41+
* Properties are automatically propagated up to any parent context.
42+
*
43+
* @author Dave Syer
44+
* @author Phillip Webb
45+
*/
46+
public class ServerPortInfoApplicationContextInitializer implements
47+
ApplicationContextInitializer<ConfigurableApplicationContext> {
48+
49+
@Override
50+
public void initialize(ConfigurableApplicationContext applicationContext) {
51+
applicationContext
52+
.addApplicationListener(new ApplicationListener<EmbeddedServletContainerInitializedEvent>() {
53+
@Override
54+
public void onApplicationEvent(
55+
EmbeddedServletContainerInitializedEvent event) {
56+
ServerPortInfoApplicationContextInitializer.this
57+
.onApplicationEvent(event);
58+
}
59+
});
60+
}
61+
62+
protected void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
63+
String propertyName = getPropertyName(event.getApplicationContext());
64+
setPortProperty(event.getApplicationContext(), propertyName, event
65+
.getEmbeddedServletContainer().getPort());
66+
}
67+
68+
protected String getPropertyName(EmbeddedWebApplicationContext context) {
69+
String name = context.getNamespace();
70+
if (StringUtils.isEmpty(name)) {
71+
name = "server";
72+
}
73+
return "local." + name + ".port";
74+
}
75+
76+
private void setPortProperty(ApplicationContext context, String propertyName, int port) {
77+
if (context instanceof ConfigurableApplicationContext) {
78+
EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) context,
79+
propertyName + ":" + port);
80+
}
81+
if (context.getParent() != null) {
82+
setPortProperty(context.getParent(), propertyName, port);
83+
}
84+
}
85+
86+
}

spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ private Map<String, String> extractEnvironmentProperties(String[] values) {
174174
private List<ApplicationContextInitializer<?>> getInitializers(
175175
MergedContextConfiguration mergedConfig, SpringApplication application) {
176176
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>();
177+
initializers.add(new ServerPortInfoApplicationContextInitializer());
177178
initializers.addAll(application.getInitializers());
178179
for (Class<? extends ApplicationContextInitializer<?>> initializerClass : mergedConfig
179180
.getContextInitializerClasses()) {

0 commit comments

Comments
 (0)