Add display-name option

Allow the display-name of the application to be customized when deployed
in an embedded container via the `server.display-name` property.

Closes gh-2600
This commit is contained in:
Stephane Nicoll 2015-04-21 11:27:39 +02:00
parent 0a9b8cbb41
commit 62a2dd659b
8 changed files with 66 additions and 0 deletions

View File

@ -80,6 +80,11 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
*/
private String contextPath;
/**
* Display name of the application.
*/
private String displayName = "application";
@NestedConfigurationProperty
private Ssl ssl;
@ -122,6 +127,14 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
this.contextPath = contextPath;
}
public String getDisplayName() {
return this.displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getServletPath() {
return this.servletPath;
}
@ -213,6 +226,9 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
if (getContextPath() != null) {
container.setContextPath(getContextPath());
}
if (getDisplayName() != null) {
container.setDisplayName(getDisplayName());
}
if (getSessionTimeout() != null) {
container.setSessionTimeout(getSessionTimeout());
}

View File

@ -112,6 +112,21 @@ public class ServerPropertiesTests {
verify(factory, never()).setContextPath("");
}
@Test
public void testDefaultDisplayName() throws Exception {
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
this.properties.customize(factory);
verify(factory).setDisplayName("application");
}
@Test
public void testCustomizeDisplayName() throws Exception {
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
this.properties.setDisplayName("TestName");
this.properties.customize(factory);
verify(factory).setDisplayName("TestName");
}
@Test
public void testCustomizeTomcatPort() throws Exception {
ConfigurableEmbeddedServletContainer factory = mock(ConfigurableEmbeddedServletContainer.class);
@ -136,6 +151,18 @@ public class ServerPropertiesTests {
assertEquals(9999, this.properties.getTomcat().getMaxHttpHeaderSize());
}
@Test
public void customizeTomcatDisplayName() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.display-name", "MyBootApp");
bindProperties(map);
TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
this.properties.customize(container);
assertEquals("MyBootApp", container.getDisplayName());
}
@Test
public void disableTomcatRemoteIpValve() throws Exception {
Map<String, String> map = new HashMap<String, String>();

View File

@ -61,6 +61,7 @@ content into your application; rather pick only the properties that you need.
server.jsp-servlet.init-parameters.*= # Init parameters used to configure the JSP servlet
server.jsp-servlet.registered=true # Whether or not the JSP servlet is registered
server.servlet-path= # the servlet path, defaults to '/'
server.display-name= # the display name of the application
server.ssl.enabled=true # if SSL support is enabled
server.ssl.client-auth= # want or need
server.ssl.key-alias=

View File

@ -34,6 +34,7 @@ import org.springframework.util.ClassUtils;
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
* @see AbstractEmbeddedServletContainerFactory
*/
public abstract class AbstractConfigurableEmbeddedServletContainer implements
@ -44,6 +45,8 @@ public abstract class AbstractConfigurableEmbeddedServletContainer implements
private String contextPath = "";
private String displayName;
private boolean registerDefaultServlet = true;
private int port = 8080;
@ -120,6 +123,15 @@ public abstract class AbstractConfigurableEmbeddedServletContainer implements
return this.contextPath;
}
@Override
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return this.displayName;
}
@Override
public void setPort(int port) {
this.port = port;

View File

@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit;
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
* @see EmbeddedServletContainerFactory
* @see EmbeddedServletContainerCustomizer
*/
@ -41,6 +42,12 @@ public interface ConfigurableEmbeddedServletContainer {
*/
void setContextPath(String contextPath);
/**
* Sets the display name of the application deployed in the embedded servlet container.
* @param displayName the displayName to set
*/
void setDisplayName(String displayName);
/**
* Sets the port that the embedded servlet container should listen on. If not
* specified port '8080' will be used. Use port -1 to disable auto-start (i.e start

View File

@ -231,6 +231,7 @@ public class JettyEmbeddedServletContainerFactory extends
}
String contextPath = getContextPath();
context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
context.setDisplayName(getDisplayName());
configureDocumentRoot(context);
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);

View File

@ -161,6 +161,7 @@ public class TomcatEmbeddedServletContainerFactory extends
docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
TomcatEmbeddedContext context = new TomcatEmbeddedContext();
context.setName(getContextPath());
context.setDisplayName(getDisplayName());
context.setPath(getContextPath());
context.setDocBase(docBase.getAbsolutePath());
context.addLifecycleListener(new FixContextListener());

View File

@ -325,6 +325,7 @@ public class UndertowEmbeddedServletContainerFactory extends
initializers);
deployment.setClassLoader(getServletClassLoader());
deployment.setContextPath(getContextPath());
deployment.setDisplayName(getDisplayName());
deployment.setDeploymentName("spring-boot");
if (isRegisterDefaultServlet()) {
deployment.addServlet(Servlets.servlet("default", DefaultServlet.class));