Skip to content

Commit

Permalink
Add display-name option
Browse files Browse the repository at this point in the history
Allow the display-name of the application to be customized when deployed
in an embedded container via the `server.display-name` property.

Closes spring-projectsgh-2600
  • Loading branch information
snicoll committed May 15, 2015
1 parent 0a9b8cb commit 62a2dd6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -122,6 +127,14 @@ public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}

public String getDisplayName() {
return this.displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getServletPath() {
return this.servletPath;
}
Expand Down Expand Up @@ -213,6 +226,9 @@ public void customize(ConfigurableEmbeddedServletContainer container) {
if (getContextPath() != null) {
container.setContextPath(getContextPath());
}
if (getDisplayName() != null) {
container.setDisplayName(getDisplayName());
}
if (getSessionTimeout() != null) {
container.setSessionTimeout(getSessionTimeout());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ public void testCustomizeTomcat() throws Exception {
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);
Expand All @@ -136,6 +151,18 @@ public void testCustomizeTomcatHeaderSize() throws Exception {
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>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
* @see AbstractEmbeddedServletContainerFactory
*/
public abstract class AbstractConfigurableEmbeddedServletContainer implements
Expand All @@ -44,6 +45,8 @@ public abstract class AbstractConfigurableEmbeddedServletContainer implements

private String contextPath = "";

private String displayName;

private boolean registerDefaultServlet = true;

private int port = 8080;
Expand Down Expand Up @@ -120,6 +123,15 @@ public String getContextPath() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Dave Syer
* @author Andy Wilkinson
* @author Stephane Nicoll
* @see EmbeddedServletContainerFactory
* @see EmbeddedServletContainerCustomizer
*/
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ protected final void configureWebAppContext(WebAppContext context,
}
String contextPath = getContextPath();
context.setContextPath(StringUtils.hasLength(contextPath) ? contextPath : "/");
context.setDisplayName(getDisplayName());
configureDocumentRoot(context);
if (isRegisterDefaultServlet()) {
addDefaultServlet(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private DeploymentManager createDeploymentManager(
initializers);
deployment.setClassLoader(getServletClassLoader());
deployment.setContextPath(getContextPath());
deployment.setDisplayName(getDisplayName());
deployment.setDeploymentName("spring-boot");
if (isRegisterDefaultServlet()) {
deployment.addServlet(Servlets.servlet("default", DefaultServlet.class));
Expand Down

0 comments on commit 62a2dd6

Please sign in to comment.