Commit 3ff77295 authored by Phillip Webb's avatar Phillip Webb

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
parent c8257b38
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.web; package org.springframework.boot.actuate.autoconfigure.web;
import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* Factory for creating a separate management context when the management web server is * Factory for creating a separate management context when the management web server is
...@@ -35,7 +35,7 @@ public interface ManagementContextFactory { ...@@ -35,7 +35,7 @@ public interface ManagementContextFactory {
* @param configurationClasses the configuration classes * @param configurationClasses the configuration classes
* @return a configured application context * @return a configured application context
*/ */
ConfigurableApplicationContext createManagementContext(ApplicationContext parent, ConfigurableWebServerApplicationContext createManagementContext(
Class<?>... configurationClasses); ApplicationContext parent, Class<?>... configurationClasses);
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -25,10 +25,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerAutoConfiguration; 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.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
...@@ -39,7 +39,7 @@ import org.springframework.util.ObjectUtils; ...@@ -39,7 +39,7 @@ import org.springframework.util.ObjectUtils;
class ReactiveManagementContextFactory implements ManagementContextFactory { class ReactiveManagementContextFactory implements ManagementContextFactory {
@Override @Override
public ConfigurableApplicationContext createManagementContext( public ConfigurableWebServerApplicationContext createManagementContext(
ApplicationContext parent, Class<?>... configClasses) { ApplicationContext parent, Class<?>... configClasses) {
AnnotationConfigReactiveWebServerApplicationContext child = new AnnotationConfigReactiveWebServerApplicationContext(); AnnotationConfigReactiveWebServerApplicationContext child = new AnnotationConfigReactiveWebServerApplicationContext();
child.setParent(parent); child.setParent(parent);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -25,21 +25,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; ...@@ -25,21 +25,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties; 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.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for the management context. If the * {@link EnableAutoConfiguration Auto-configuration} for the management context. If the
...@@ -131,11 +129,11 @@ public class ManagementContextAutoConfiguration { ...@@ -131,11 +129,11 @@ public class ManagementContextAutoConfiguration {
@Override @Override
public void afterSingletonsInstantiated() { public void afterSingletonsInstantiated() {
ConfigurableApplicationContext managementContext = this.managementContextFactory ConfigurableWebServerApplicationContext managementContext = this.managementContextFactory
.createManagementContext(this.applicationContext, .createManagementContext(this.applicationContext,
EnableChildManagementContextConfiguration.class, EnableChildManagementContextConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
setNamespaceIfPossible(managementContext); managementContext.setServerNamespace("management");
managementContext.setId(this.applicationContext.getId() + ":management"); managementContext.setId(this.applicationContext.getId() + ":management");
setClassLoaderIfPossible(managementContext); setClassLoaderIfPossible(managementContext);
CloseManagementContextListener.addIfPossible(this.applicationContext, CloseManagementContextListener.addIfPossible(this.applicationContext,
...@@ -145,21 +143,11 @@ public class ManagementContextAutoConfiguration { ...@@ -145,21 +143,11 @@ public class ManagementContextAutoConfiguration {
private void setClassLoaderIfPossible(ConfigurableApplicationContext child) { private void setClassLoaderIfPossible(ConfigurableApplicationContext child) {
if (child instanceof DefaultResourceLoader) { if (child instanceof DefaultResourceLoader) {
((AbstractApplicationContext) child) ((DefaultResourceLoader) child)
.setClassLoader(this.applicationContext.getClassLoader()); .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");
}
}
} }
/** /**
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -28,10 +28,10 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextFactory;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; 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.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/** /**
* A {@link ManagementContextFactory} for servlet-based web applications. * A {@link ManagementContextFactory} for servlet-based web applications.
...@@ -41,7 +41,7 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -41,7 +41,7 @@ import org.springframework.context.ConfigurableApplicationContext;
class ServletManagementContextFactory implements ManagementContextFactory { class ServletManagementContextFactory implements ManagementContextFactory {
@Override @Override
public ConfigurableApplicationContext createManagementContext( public ConfigurableWebServerApplicationContext createManagementContext(
ApplicationContext parent, Class<?>... configClasses) { ApplicationContext parent, Class<?>... configClasses) {
AnnotationConfigServletWebServerApplicationContext child = new AnnotationConfigServletWebServerApplicationContext(); AnnotationConfigServletWebServerApplicationContext child = new AnnotationConfigServletWebServerApplicationContext();
child.setParent(parent); child.setParent(parent);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 ...@@ -31,6 +31,7 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven
import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.SpringApplicationEvent; import org.springframework.boot.context.event.SpringApplicationEvent;
import org.springframework.boot.system.SystemProperties;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,14 +21,13 @@ import java.io.File; ...@@ -21,14 +21,13 @@ import java.io.File;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.context.WebServerInitializedEvent;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
/** /**
* An {@link ApplicationListener} that saves embedded server port and management port into * An {@link ApplicationListener} that saves embedded server port and management port into
...@@ -107,18 +106,18 @@ public class EmbeddedServerPortFileWriter ...@@ -107,18 +106,18 @@ public class EmbeddedServerPortFileWriter
* @return the file that should be written * @return the file that should be written
*/ */
protected File getPortFile(ApplicationContext applicationContext) { protected File getPortFile(ApplicationContext applicationContext) {
String contextName = getContextName(applicationContext); String namespace = getServerNamespace(applicationContext);
if (StringUtils.isEmpty(contextName)) { if (StringUtils.isEmpty(namespace)) {
return this.file; return this.file;
} }
String name = this.file.getName(); String name = this.file.getName();
String extension = StringUtils.getFilenameExtension(this.file.getName()); String extension = StringUtils.getFilenameExtension(this.file.getName());
name = name.substring(0, name.length() - extension.length() - 1); name = name.substring(0, name.length() - extension.length() - 1);
if (isUpperCase(name)) { if (isUpperCase(name)) {
name = name + "-" + contextName.toUpperCase(); name = name + "-" + namespace.toUpperCase();
} }
else { else {
name = name + "-" + contextName.toLowerCase(); name = name + "-" + namespace.toLowerCase();
} }
if (StringUtils.hasLength(extension)) { if (StringUtils.hasLength(extension)) {
name = name + "." + extension; name = name + "." + extension;
...@@ -126,13 +125,10 @@ public class EmbeddedServerPortFileWriter ...@@ -126,13 +125,10 @@ public class EmbeddedServerPortFileWriter
return new File(this.file.getParentFile(), name); return new File(this.file.getParentFile(), name);
} }
private String getContextName(ApplicationContext applicationContext) { private String getServerNamespace(ApplicationContext applicationContext) {
if (applicationContext instanceof ConfigurableWebApplicationContext) { if (applicationContext instanceof WebServerApplicationContext) {
return ((ConfigurableWebApplicationContext) applicationContext) return ((WebServerApplicationContext) applicationContext)
.getNamespace(); .getServerNamespace();
}
if (applicationContext instanceof ReactiveWebApplicationContext) {
return ((ReactiveWebApplicationContext) applicationContext).getNamespace();
} }
return null; return null;
} }
......
/*
* 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);
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -30,6 +30,7 @@ import org.springframework.core.env.Environment; ...@@ -30,6 +30,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
/** /**
* {@link ApplicationContextInitializer} that sets {@link Environment} properties for the * {@link ApplicationContextInitializer} that sets {@link Environment} properties for the
...@@ -38,9 +39,9 @@ import org.springframework.core.env.PropertySource; ...@@ -38,9 +39,9 @@ import org.springframework.core.env.PropertySource;
* {@link Value @Value} or obtained via the {@link Environment}. * {@link Value @Value} or obtained via the {@link Environment}.
* <p> * <p>
* If the {@link WebServerInitializedEvent} has a * If the {@link WebServerInitializedEvent} has a
* {@link WebServerInitializedEvent#getServerId() server ID}, it will be used to construct * {@link WebServerApplicationContext#getServerNamespace() server namespace} , it will be
* the property name. For example, the "management" actuator context will have the * used to construct the property name. For example, the "management" actuator context
* property name {@literal "local.management.port"}. * will have the property name {@literal "local.management.port"}.
* <p> * <p>
* Properties are automatically propagated up to any parent context. * Properties are automatically propagated up to any parent context.
* *
...@@ -59,11 +60,16 @@ public class ServerPortInfoApplicationContextInitializer ...@@ -59,11 +60,16 @@ public class ServerPortInfoApplicationContextInitializer
@Override @Override
public void onApplicationEvent(WebServerInitializedEvent event) { public void onApplicationEvent(WebServerInitializedEvent event) {
String propertyName = "local." + event.getServerId() + ".port"; String propertyName = "local." + getName(event.getApplicationContext()) + ".port";
setPortProperty(event.getApplicationContext(), propertyName, setPortProperty(event.getApplicationContext(), propertyName,
event.getWebServer().getPort()); 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, private void setPortProperty(ApplicationContext context, String propertyName,
int port) { int port) {
if (context instanceof ConfigurableApplicationContext) { if (context instanceof ConfigurableApplicationContext) {
......
/*
* 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();
}
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.springframework.boot.web.context; package org.springframework.boot.web.context;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
/** /**
...@@ -31,8 +30,8 @@ import org.springframework.context.ApplicationEvent; ...@@ -31,8 +30,8 @@ import org.springframework.context.ApplicationEvent;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public abstract class WebServerInitializedEvent extends ApplicationEvent { public abstract class WebServerInitializedEvent extends ApplicationEvent {
protected WebServerInitializedEvent(WebServer source) { protected WebServerInitializedEvent(WebServer webServer) {
super(source); super(webServer);
} }
/** /**
...@@ -49,7 +48,7 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent { ...@@ -49,7 +48,7 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent {
* context) before acting on the server itself. * context) before acting on the server itself.
* @return the applicationContext that the server was created from * @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}). * Access the source of the event (an {@link WebServer}).
...@@ -60,11 +59,4 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent { ...@@ -60,11 +59,4 @@ public abstract class WebServerInitializedEvent extends ApplicationEvent {
return (WebServer) super.getSource(); 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();
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -65,8 +65,6 @@ public class AnnotationConfigReactiveWebApplicationContext ...@@ -65,8 +65,6 @@ public class AnnotationConfigReactiveWebApplicationContext
private final Set<String> basePackages = new LinkedHashSet<>(); private final Set<String> basePackages = new LinkedHashSet<>();
private String namespace;
@Override @Override
protected ConfigurableEnvironment createEnvironment() { protected ConfigurableEnvironment createEnvironment() {
return new StandardReactiveWebEnvironment(); return new StandardReactiveWebEnvironment();
...@@ -326,14 +324,4 @@ public class AnnotationConfigReactiveWebApplicationContext ...@@ -326,14 +324,4 @@ public class AnnotationConfigReactiveWebApplicationContext
return new FilteredReactiveWebContextResource(path); return new FilteredReactiveWebContextResource(path);
} }
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getNamespace() {
return this.namespace;
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,10 +27,4 @@ import org.springframework.context.ConfigurableApplicationContext; ...@@ -27,10 +27,4 @@ import org.springframework.context.ConfigurableApplicationContext;
public interface ConfigurableReactiveWebApplicationContext public interface ConfigurableReactiveWebApplicationContext
extends ConfigurableApplicationContext, ReactiveWebApplicationContext { extends ConfigurableApplicationContext, ReactiveWebApplicationContext {
/**
* Set the namespace for this reactive web application context.
* @param namespace the namespace for the context
*/
void setNamespace(String namespace);
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -31,8 +31,6 @@ import org.springframework.core.io.Resource; ...@@ -31,8 +31,6 @@ import org.springframework.core.io.Resource;
public class GenericReactiveWebApplicationContext extends GenericApplicationContext public class GenericReactiveWebApplicationContext extends GenericApplicationContext
implements ConfigurableReactiveWebApplicationContext { implements ConfigurableReactiveWebApplicationContext {
private String namespace;
/** /**
* Create a new {@link GenericReactiveWebApplicationContext}. * Create a new {@link GenericReactiveWebApplicationContext}.
* @see #registerBeanDefinition * @see #registerBeanDefinition
...@@ -57,16 +55,6 @@ public class GenericReactiveWebApplicationContext extends GenericApplicationCont ...@@ -57,16 +55,6 @@ public class GenericReactiveWebApplicationContext extends GenericApplicationCont
return new StandardReactiveWebEnvironment(); return new StandardReactiveWebEnvironment();
} }
@Override
public void setNamespace(String namespace) {
this.namespace = namespace;
}
@Override
public String getNamespace() {
return this.namespace;
}
@Override @Override
protected Resource getResourceByPath(String path) { protected Resource getResourceByPath(String path) {
// We must be careful not to expose classpath resources // We must be careful not to expose classpath resources
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,10 +26,4 @@ import org.springframework.context.ApplicationContext; ...@@ -26,10 +26,4 @@ import org.springframework.context.ApplicationContext;
*/ */
public interface ReactiveWebApplicationContext extends 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();
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -18,6 +18,7 @@ package org.springframework.boot.web.reactive.context;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; 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.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
...@@ -32,10 +33,13 @@ import org.springframework.util.StringUtils; ...@@ -32,10 +33,13 @@ import org.springframework.util.StringUtils;
* @since 2.0.0 * @since 2.0.0
*/ */
public class ReactiveWebServerApplicationContext public class ReactiveWebServerApplicationContext
extends GenericReactiveWebApplicationContext { extends GenericReactiveWebApplicationContext
implements ConfigurableWebServerApplicationContext {
private volatile WebServer webServer; private volatile WebServer webServer;
private String serverNamespace;
/** /**
* Create a new {@link ReactiveWebServerApplicationContext}. * Create a new {@link ReactiveWebServerApplicationContext}.
*/ */
...@@ -102,6 +106,7 @@ public class ReactiveWebServerApplicationContext ...@@ -102,6 +106,7 @@ public class ReactiveWebServerApplicationContext
* the server has not yet been created. * the server has not yet been created.
* @return the web server * @return the web server
*/ */
@Override
public WebServer getWebServer() { public WebServer getWebServer() {
return this.webServer; return this.webServer;
} }
...@@ -170,4 +175,15 @@ public class ReactiveWebServerApplicationContext ...@@ -170,4 +175,15 @@ public class ReactiveWebServerApplicationContext
} }
} }
} }
@Override
public String getServerNamespace() {
return this.serverNamespace;
}
@Override
public void setServerNamespace(String serverNamespace) {
this.serverNamespace = serverNamespace;
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -32,9 +32,9 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent ...@@ -32,9 +32,9 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent
private final ReactiveWebServerApplicationContext applicationContext; private final ReactiveWebServerApplicationContext applicationContext;
public ReactiveWebServerInitializedEvent(WebServer source, public ReactiveWebServerInitializedEvent(WebServer webServer,
ReactiveWebServerApplicationContext applicationContext) { ReactiveWebServerApplicationContext applicationContext) {
super(source); super(webServer);
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }
...@@ -43,9 +43,4 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent ...@@ -43,9 +43,4 @@ public class ReactiveWebServerInitializedEvent extends WebServerInitializedEvent
return this.applicationContext; return this.applicationContext;
} }
@Override
public String getServerId() {
return "server";
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -37,6 +37,7 @@ import org.springframework.beans.BeansException; ...@@ -37,6 +37,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.Scope; import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; 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.server.WebServer;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.boot.web.servlet.ServletContextInitializer;
...@@ -87,7 +88,8 @@ import org.springframework.web.context.support.WebApplicationContextUtils; ...@@ -87,7 +88,8 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
* @see XmlServletWebServerApplicationContext * @see XmlServletWebServerApplicationContext
* @see ServletWebServerFactory * @see ServletWebServerFactory
*/ */
public class ServletWebServerApplicationContext extends GenericWebApplicationContext { public class ServletWebServerApplicationContext extends GenericWebApplicationContext
implements ConfigurableWebServerApplicationContext {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(ServletWebServerApplicationContext.class); .getLog(ServletWebServerApplicationContext.class);
...@@ -104,7 +106,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon ...@@ -104,7 +106,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
private ServletConfig servletConfig; private ServletConfig servletConfig;
private String namespace; private String serverNamespace;
/** /**
* Create a new {@link ServletWebServerApplicationContext}. * Create a new {@link ServletWebServerApplicationContext}.
...@@ -322,13 +324,13 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon ...@@ -322,13 +324,13 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
} }
@Override @Override
public void setNamespace(String namespace) { public String getServerNamespace() {
this.namespace = namespace; return this.serverNamespace;
} }
@Override @Override
public String getNamespace() { public void setServerNamespace(String serverNamespace) {
return this.namespace; this.serverNamespace = serverNamespace;
} }
@Override @Override
...@@ -346,6 +348,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon ...@@ -346,6 +348,7 @@ public class ServletWebServerApplicationContext extends GenericWebApplicationCon
* the server has not yet been created. * the server has not yet been created.
* @return the embedded web server * @return the embedded web server
*/ */
@Override
public WebServer getWebServer() { public WebServer getWebServer() {
return this.webServer; return this.webServer;
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; ...@@ -18,7 +18,6 @@ package org.springframework.boot.web.servlet.context;
import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServer;
import org.springframework.util.StringUtils;
/** /**
* Event to be published after the {@link ServletWebServerApplicationContext} is refreshed * Event to be published after the {@link ServletWebServerApplicationContext} is refreshed
...@@ -36,9 +35,9 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent ...@@ -36,9 +35,9 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent
private final ServletWebServerApplicationContext applicationContext; private final ServletWebServerApplicationContext applicationContext;
public ServletWebServerInitializedEvent(WebServer source, public ServletWebServerInitializedEvent(WebServer webServer,
ServletWebServerApplicationContext applicationContext) { ServletWebServerApplicationContext applicationContext) {
super(source); super(webServer);
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
} }
...@@ -53,13 +52,4 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent ...@@ -53,13 +52,4 @@ public class ServletWebServerInitializedEvent extends WebServerInitializedEvent
return this.applicationContext; return this.applicationContext;
} }
@Override
public String getServerId() {
String name = this.applicationContext.getNamespace();
if (StringUtils.isEmpty(name)) {
name = "server";
}
return name;
}
} }
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -24,6 +24,7 @@ import org.junit.Test; ...@@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.springframework.boot.ApplicationPid;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,26 +19,17 @@ package org.springframework.boot.system; ...@@ -19,26 +19,17 @@ package org.springframework.boot.system;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.function.BiFunction;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; 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.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.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.FileCopyUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -53,52 +44,11 @@ import static org.mockito.Mockito.mock; ...@@ -53,52 +44,11 @@ import static org.mockito.Mockito.mock;
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(Parameterized.class)
public class EmbeddedServerPortFileWriterTests { public class EmbeddedServerPortFileWriterTests {
@Rule @Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder(); 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 @Before
@After @After
public void reset() { public void reset() {
...@@ -167,8 +117,17 @@ public class EmbeddedServerPortFileWriterTests { ...@@ -167,8 +117,17 @@ public class EmbeddedServerPortFileWriterTests {
assertThat(collectFileNames(file.getParentFile())).contains(managementFile); assertThat(collectFileNames(file.getParentFile())).contains(managementFile);
} }
private WebServerInitializedEvent mockEvent(String name, int port) { private WebServerInitializedEvent mockEvent(String namespace, int port) {
return this.eventFactory.apply(name, 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) { private Set<String> collectFileNames(File directory) {
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -18,7 +18,7 @@ package com.example; ...@@ -18,7 +18,7 @@ package com.example;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.system.EmbeddedServerPortFileWriter; import org.springframework.boot.web.context.EmbeddedServerPortFileWriter;
@SpringBootApplication @SpringBootApplication
public class DevToolsTestApplication { public class DevToolsTestApplication {
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -26,7 +26,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; 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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment