Convert Actuator sample to dynamic ports

This commit is contained in:
Dave Syer
2014-04-17 17:36:09 -07:00
parent 559009b8cf
commit f134e96053
19 changed files with 120 additions and 71 deletions

View File

@@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventListener;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -88,6 +89,11 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
*/
public class EmbeddedWebApplicationContext extends GenericWebApplicationContext {
/**
*
*/
private static final String SERVER = "server";
/**
* Constant value for the DispatcherServlet bean name. A Servlet bean with this name
* is deemed to be the "main" servlet and is automatically given a mapping of "/" by
@@ -102,6 +108,8 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
private String namespace;
private Map<String, EmbeddedServletContainer> containers = new HashMap<String, EmbeddedServletContainer>();
/**
* Register ServletContextAwareProcessor.
* @see ServletContextAwareProcessor
@@ -158,6 +166,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
this.embeddedServletContainer = containerFactory
.getEmbeddedServletContainer(getSelfInitializer());
this.containers.put(SERVER, this.embeddedServletContainer);
}
else if (getServletContext() != null) {
try {
@@ -382,6 +391,7 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
try {
this.embeddedServletContainer.stop();
this.embeddedServletContainer = null;
this.containers.remove(SERVER);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
@@ -425,4 +435,15 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
return this.embeddedServletContainer;
}
/**
* A registry of embedded containers by name. The
* {@link #getEmbeddedServletContainer() canonical container} is called "server".
* Anyone else who creates one can register it with whatever name they please.
*
* @return the containers
*/
public Map<String, EmbeddedServletContainer> getEmbeddedServletContainers() {
return this.containers;
}
}

View File

@@ -16,7 +16,10 @@
package org.springframework.boot.test;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;
@@ -37,7 +40,12 @@ public class EmbeddedServletContainerListener extends AbstractTestExecutionListe
return;
}
EmbeddedWebApplicationContext embedded = (EmbeddedWebApplicationContext) context;
final int port = embedded.getEmbeddedServletContainer().getPort();
EnvironmentTestUtils.addEnvironment(embedded, "local.server.port:" + port);
Map<String, EmbeddedServletContainer> containers = embedded
.getEmbeddedServletContainers();
for (String name : containers.keySet()) {
int port = containers.get(name).getPort();
EnvironmentTestUtils.addEnvironment(embedded, "local." + name + ".port:"
+ port);
}
}
}

View File

@@ -143,6 +143,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
if (annotation == null) {
// Not running an embedded server, just setting up web context
args.put("server.port", "-1");
args.put("management.port", "-1");
}
else {
args.putAll(extractProperties(annotation.value()));