Add WebServerApplicationContext abstraction

Add a new `WebServerApplicationContext` interface that provides a common
abstraction for all application contexts that create and manage the
lifecycle of an embedded `WebServer`.

Allows server namespaces to become a first-class concept (rather
subverting `ConfigurableWebApplicationContext.getNamespace()`) and
allow us to drop `getServerId()` from `WebServerInitializedEvent`.

Also helps to improve `ManagementContextAutoConfiguration` and
`ManagementContextFactory`.

Fixes gh-11881
pull/11812/merge
Phillip Webb 7 years ago
parent c8257b38a2
commit 3ff772957b

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,8 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.web;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Factory for creating a separate management context when the management web server is
@ -35,7 +35,7 @@ public interface ManagementContextFactory {
* @param configurationClasses the configuration classes
* @return a configured application context
*/
ConfigurableApplicationContext createManagementContext(ApplicationContext parent,
Class<?>... configurationClasses);
ConfigurableWebServerApplicationContext createManagementContext(
ApplicationContext parent, Class<?>... configurationClasses);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,10 +25,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ObjectUtils;
/**
@ -39,7 +39,7 @@ import org.springframework.util.ObjectUtils;
class ReactiveManagementContextFactory implements ManagementContextFactory {
@Override
public ConfigurableApplicationContext createManagementContext(
public ConfigurableWebServerApplicationContext createManagementContext(
ApplicationContext parent, Class<?>... configClasses) {
AnnotationConfigReactiveWebServerApplicationContext child = new AnnotationConfigReactiveWebServerApplicationContext();
child.setParent(parent);

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,21 +25,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.util.Assert;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/**
* {@link EnableAutoConfiguration Auto-configuration} for the management context. If the
@ -131,11 +129,11 @@ public class ManagementContextAutoConfiguration {
@Override
public void afterSingletonsInstantiated() {
ConfigurableApplicationContext managementContext = this.managementContextFactory
ConfigurableWebServerApplicationContext managementContext = this.managementContextFactory
.createManagementContext(this.applicationContext,
EnableChildManagementContextConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
setNamespaceIfPossible(managementContext);
managementContext.setServerNamespace("management");
managementContext.setId(this.applicationContext.getId() + ":management");
setClassLoaderIfPossible(managementContext);
CloseManagementContextListener.addIfPossible(this.applicationContext,
@ -145,21 +143,11 @@ public class ManagementContextAutoConfiguration {
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) {
if (child instanceof DefaultResourceLoader) {
((AbstractApplicationContext) child)
((DefaultResourceLoader) child)
.setClassLoader(this.applicationContext.getClassLoader());
}
}
private void setNamespaceIfPossible(ConfigurableApplicationContext child) {
if (child instanceof ConfigurableReactiveWebApplicationContext) {
((ConfigurableReactiveWebApplicationContext) child)
.setNamespace("management");
}
else if (child instanceof ConfigurableWebApplicationContext) {
((ConfigurableWebApplicationContext) child).setNamespace("management");
}
}
}
/**

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,10 +28,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/**
* A {@link ManagementContextFactory} for servlet-based web applications.
@ -41,7 +41,7 @@ import org.springframework.context.ConfigurableApplicationContext;
class ServletManagementContextFactory implements ManagementContextFactory {
@Override
public ConfigurableApplicationContext createManagementContext(
public ConfigurableWebServerApplicationContext createManagementContext(
ApplicationContext parent, Class<?>... configClasses) {
AnnotationConfigServletWebServerApplicationContext child = new AnnotationConfigServletWebServerApplicationContext();
child.setParent(parent);

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.boot.system.SystemProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,14 +21,13 @@ import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/**
* An {@link ApplicationListener} that saves embedded server port and management port into
@ -107,18 +106,18 @@ public class EmbeddedServerPortFileWriter
* @return the file that should be written
*/
protected File getPortFile(ApplicationContext applicationContext) {
String contextName = getContextName(applicationContext);
if (StringUtils.isEmpty(contextName)) {
String namespace = getServerNamespace(applicationContext);
if (StringUtils.isEmpty(namespace)) {
return this.file;
}
String name = this.file.getName();
String extension = StringUtils.getFilenameExtension(this.file.getName());
name = name.substring(0, name.length() - extension.length() - 1);
if (isUpperCase(name)) {
name = name + "-" + contextName.toUpperCase();
name = name + "-" + namespace.toUpperCase();
}
else {
name = name + "-" + contextName.toLowerCase();
name = name + "-" + namespace.toLowerCase();
}
if (StringUtils.hasLength(extension)) {
name = name + "." + extension;
@ -126,13 +125,10 @@ public class EmbeddedServerPortFileWriter
return new File(this.file.getParentFile(), name);
}
private String getContextName(ApplicationContext applicationContext) {
if (applicationContext instanceof ConfigurableWebApplicationContext) {
return ((ConfigurableWebApplicationContext) applicationContext)
.getNamespace();
}
if (applicationContext instanceof ReactiveWebApplicationContext) {
return ((ReactiveWebApplicationContext) applicationContext).getNamespace();
private String getServerNamespace(ApplicationContext applicationContext) {
if (applicationContext instanceof WebServerApplicationContext) {
return ((WebServerApplicationContext) applicationContext)
.getServerNamespace();
}
return null;
}

@ -0,0 +1,39 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.context;
import org.springframework.context.ConfigurableApplicationContext;
/**
* SPI interface to be implemented by most if not all {@link WebServerApplicationContext
* web server application contexts}. Provides facilities to configure the context, in
* addition to the methods in the {WebServerApplicationContext} interface.
*
* @author Phillip Webb
* @since 2.0.0
*/
public interface ConfigurableWebServerApplicationContext
extends ConfigurableApplicationContext, WebServerApplicationContext {
/**
* Set the server namespace of the context.
* @param serverNamespace the server namespance
* @see #getServerNamespace()
*/
void setServerNamespace(String serverNamespace);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/**
* {@link ApplicationContextInitializer} that sets {@link Environment} properties for the
@ -38,9 +39,9 @@ import org.springframework.core.env.PropertySource;
* {@link Value @Value} or obtained via the {@link Environment}.
* <p>
* If the {@link WebServerInitializedEvent} has a
* {@link WebServerInitializedEvent#getServerId() server ID}, it will be used to construct
* the property name. For example, the "management" actuator context will have the
* property name {@literal "local.management.port"}.
* {@link WebServerApplicationContext#getServerNamespace() server namespace} , it will be
* used to construct the property name. For example, the "management" actuator context
* will have the property name {@literal "local.management.port"}.
* <p>
* Properties are automatically propagated up to any parent context.
*
@ -59,11 +60,16 @@ public class ServerPortInfoApplicationContextInitializer
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
String propertyName = "local." + event.getServerId() + ".port";
String propertyName = "local." + getName(event.getApplicationContext()) + ".port";
setPortProperty(event.getApplicationContext(), propertyName,
event.getWebServer().getPort());
}
private String getName(WebServerApplicationContext context) {
String name = context.getServerNamespace();
return (StringUtils.hasText(name) ? name : "server");
}
private void setPortProperty(ApplicationContext context, String propertyName,
int port) {
if (context instanceof ConfigurableApplicationContext) {

@ -0,0 +1,47 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.web.context;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
/**
* Interface to be implemented by {@link ApplicationContext application contexts} that
* create and manage the lifecyle of an embedded {@link WebServer}.
*
* @author Phillip Webb
* @since 2.0.0
*/
public interface WebServerApplicationContext extends ApplicationContext {
/**
* Returns the {@link WebServer} that was created by the context or {@code null} if
* the server has not yet been created.
* @return the web server
*/
WebServer getWebServer();
/**
* Returns the namespace of the web server application context or {@code null} if no
* namepace has been set. Used for disambiguation when multiple web servers are
* running in the same application (for example a management context running on a
* different port).
* @return the server namespace
*/
String getServerNamespace();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.boot.web.context;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
/**
@ -31,8 +30,8 @@ import org.springframework.context.ApplicationEvent;
@SuppressWarnings("serial")
public abstract class WebServerInitializedEvent extends ApplicationEvent {
protected WebServerInitializedEvent(WebServer source) {
super(source);
protected WebServerInitializedEvent(WebServer webServer) {
super(webServer);
}
/**
@ -49,7 +48,7 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent {
* context) before acting on the server itself.
* @return the applicationContext that the server was created from
*/
public abstract ApplicationContext getApplicationContext();
public abstract WebServerApplicationContext getApplicationContext();
/**
* Access the source of the event (an {@link WebServer}).
@ -60,11 +59,4 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent {
return (WebServer) super.getSource();
}
/**
* Access the {@link WebServer} Id used internally to differentiate application /
* management servers.
* @return the server internal Id
*/
public abstract String getServerId();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -65,8 +65,6 @@ public class AnnotationConfigReactiveWebApplicationContext
private final Set<String> basePackages = new LinkedHashSet<>();
private String namespace;
@Override
protected ConfigurableEnvironment createEnvironment() {
return new StandardReactiveWebEnvironment();
@ -326,14 +324,4 @@ public class AnnotationConfigReactiveWebApplicationContext
return new FilteredReactiveWebContextResource(path);
}
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getNamespace() {
return this.namespace;
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,10 +27,4 @@ import org.springframework.context.ConfigurableApplicationContext;
public interface ConfigurableReactiveWebApplicationContext
extends ConfigurableApplicationContext, ReactiveWebApplicationContext {
/**
* Set the namespace for this reactive web application context.
* @param namespace the namespace for the context
*/
void setNamespace(String namespace);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,8 +31,6 @@ import org.springframework.core.io.Resource;
public class GenericReactiveWebApplicationContext extends GenericApplicationContext
implements ConfigurableReactiveWebApplicationContext {
private String namespace;
/**
* Create a new {@link GenericReactiveWebApplicationContext}.
* @see #registerBeanDefinition
@ -57,16 +55,6 @@ public class GenericReactiveWebApplicationContext extends GenericApplicationCont
return new StandardReactiveWebEnvironment();
}
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getNamespace() {
return this.namespace;
}
@Override
protected Resource getResourceByPath(String path) {
// We must be careful not to expose classpath resources

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,10 +26,4 @@ import org.springframework.context.ApplicationContext;
*/
public interface ReactiveWebApplicationContext extends ApplicationContext {
/**
* Return the namespace for this reactive web application context, if any.
* @return the namespace or {@code null}
*/
String getNamespace();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.boot.web.reactive.context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContextException;
@ -32,10 +33,13 @@ import org.springframework.util.StringUtils;
* @since 2.0.0
*/
public class ReactiveWebServerApplicationContext
extends GenericReactiveWebApplicationContext {
extends GenericReactiveWebApplicationContext
implements ConfigurableWebServerApplicationContext {
private volatile WebServer webServer;
private String serverNamespace;
/**
* Create a new {@link ReactiveWebServerApplicationContext}.
*/
@ -102,6 +106,7 @@ public class ReactiveWebServerApplicationContext
* the server has not yet been created.
* @return the web server
*/
@Override
public WebServer getWebServer() {
return this.webServer;
}
@ -170,4 +175,15 @@ public class ReactiveWebServerApplicationContext
}
}
}
@Override
public String getServerNamespace() {
return this.serverNamespace;
}
@Override
public void setServerNamespace(String serverNamespace) {
this.serverNamespace = serverNamespace;
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,9 +32,9 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent
private final ReactiveWebServerApplicationContext applicationContext;
public ReactiveWebServerInitializedEvent(WebServer source,
public ReactiveWebServerInitializedEvent(WebServer webServer,
ReactiveWebServerApplicationContext applicationContext) {
super(source);
super(webServer);
this.applicationContext = applicationContext;
}
@ -43,9 +43,4 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent
return this.applicationContext;
}
@Override
public String getServerId() {
return "server";
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,6 +37,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
@ -87,7 +88,8 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
* @see XmlServletWebServerApplicationContext
* @see ServletWebServerFactory
*/
public class ServletWebServerApplicationContext extends GenericWebApplicationContext {
public class ServletWebServerApplicationContext extends GenericWebApplicationContext
implements ConfigurableWebServerApplicationContext {
private static final Log logger = LogFactory
.getLog(ServletWebServerApplicationContext.class);
@ -104,7 +106,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
private ServletConfig servletConfig;
private String namespace;
private String serverNamespace;
/**
* Create a new {@link ServletWebServerApplicationContext}.
@ -322,13 +324,13 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
}
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
public String getServerNamespace() {
return this.serverNamespace;
}
@Override
public String getNamespace() {
return this.namespace;
public void setServerNamespace(String serverNamespace) {
this.serverNamespace = serverNamespace;
}
@Override
@ -346,6 +348,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
* the server has not yet been created.
* @return the embedded web server
*/
@Override
public WebServer getWebServer() {
return this.webServer;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.boot.web.servlet.context;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.util.StringUtils;
/**
* Event to be published after the {@link ServletWebServerApplicationContext} is refreshed
@ -36,9 +35,9 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent
private final ServletWebServerApplicationContext applicationContext;
public ServletWebServerInitializedEvent(WebServer source,
public ServletWebServerInitializedEvent(WebServer webServer,
ServletWebServerApplicationContext applicationContext) {
super(source);
super(webServer);
this.applicationContext = applicationContext;
}
@ -53,13 +52,4 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent
return this.applicationContext;
}
@Override
public String getServerId() {
String name = this.applicationContext.getNamespace();
if (StringUtils.isEmpty(name)) {
name = "server";
}
return name;
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.ApplicationPid;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,26 +19,17 @@ package org.springframework.boot.system;
import java.io.File;
import java.io.FileReader;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.context.ReactiveWebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
@ -53,52 +44,11 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb
* @author Andy Wilkinson
*/
@RunWith(Parameterized.class)
public class EmbeddedServerPortFileWriterTests {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
@Parameters(name = "{0}")
public static Object[] parameters() {
Map<String, BiFunction<String, Integer, WebServerInitializedEvent>> parameters = new LinkedHashMap<>();
parameters.put("Servlet",
EmbeddedServerPortFileWriterTests::servletEventParameter);
parameters.put("Reactive",
EmbeddedServerPortFileWriterTests::reactiveEventParameter);
return parameters.entrySet().stream()
.map((e) -> new Object[] { e.getKey(), e.getValue() }).toArray();
}
private static WebServerInitializedEvent servletEventParameter(String name,
Integer port) {
ServletWebServerApplicationContext applicationContext = mock(
ServletWebServerApplicationContext.class);
given(applicationContext.getNamespace()).willReturn(name);
WebServer source = mock(WebServer.class);
given(source.getPort()).willReturn(port);
ServletWebServerInitializedEvent event = new ServletWebServerInitializedEvent(
source, applicationContext);
return event;
}
private static WebServerInitializedEvent reactiveEventParameter(String name,
Integer port) {
ReactiveWebServerApplicationContext applicationContext = mock(
ReactiveWebServerApplicationContext.class);
given(applicationContext.getNamespace()).willReturn(name);
WebServer source = mock(WebServer.class);
given(source.getPort()).willReturn(port);
return new ReactiveWebServerInitializedEvent(source, applicationContext);
}
private final BiFunction<String, Integer, ? extends WebServerInitializedEvent> eventFactory;
public EmbeddedServerPortFileWriterTests(String name,
BiFunction<String, Integer, ? extends WebServerInitializedEvent> eventFactory) {
this.eventFactory = eventFactory;
}
@Before
@After
public void reset() {
@ -167,8 +117,17 @@ public class EmbeddedServerPortFileWriterTests {
assertThat(collectFileNames(file.getParentFile())).contains(managementFile);
}
private WebServerInitializedEvent mockEvent(String name, int port) {
return this.eventFactory.apply(name, port);
private WebServerInitializedEvent mockEvent(String namespace, int port) {
WebServer webServer = mock(WebServer.class);
given(webServer.getPort()).willReturn(port);
WebServerApplicationContext applicationContext = mock(
WebServerApplicationContext.class);
given(applicationContext.getServerNamespace()).willReturn(namespace);
given(applicationContext.getWebServer()).willReturn(webServer);
WebServerInitializedEvent event = mock(WebServerInitializedEvent.class);
given(event.getApplicationContext()).willReturn(applicationContext);
given(event.getWebServer()).willReturn(webServer);
return event;
}
private Set<String> collectFileNames(File directory) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,7 @@ package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.system.EmbeddedServerPortFileWriter;
import org.springframework.boot.web.context.EmbeddedServerPortFileWriter;
@SpringBootApplication
public class DevToolsTestApplication {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.system.EmbeddedServerPortFileWriter;
import org.springframework.boot.web.context.EmbeddedServerPortFileWriter;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

Loading…
Cancel
Save