diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextFactory.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextFactory.java index ec17a98724e..416530709c9 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextFactory.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextFactory.java @@ -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); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java index 2fa7986003d..97258809924 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/reactive/ReactiveManagementContextFactory.java @@ -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); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java index b08af79f2f6..9599c42c644 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java @@ -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"); - } - } - } /** diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementContextFactory.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementContextFactory.java index ba79dfa41bd..ef237ea21ec 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementContextFactory.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementContextFactory.java @@ -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); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java index 726a32a06e5..667fa154704 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationPidFileWriter.java @@ -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; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java index 386e88776ed..bd3468d1c1c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/EmbeddedServerPortFileWriter.java @@ -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; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ConfigurableWebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ConfigurableWebServerApplicationContext.java new file mode 100644 index 00000000000..3e368505024 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ConfigurableWebServerApplicationContext.java @@ -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); + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServerPortInfoApplicationContextInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServerPortInfoApplicationContextInitializer.java index 017af4138ba..d139f9feb62 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServerPortInfoApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/ServerPortInfoApplicationContextInitializer.java @@ -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}. *
* 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"}. *
* 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) {
diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java
new file mode 100644
index 00000000000..f72067cbf32
--- /dev/null
+++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerApplicationContext.java
@@ -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();
+
+}
diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerInitializedEvent.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerInitializedEvent.java
index 45fcfd6b5d5..8b7fde79a4e 100644
--- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerInitializedEvent.java
+++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerInitializedEvent.java
@@ -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();
-
}
diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebApplicationContext.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebApplicationContext.java
index 09f37b8aa60..e0c2927ebf7 100644
--- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebApplicationContext.java
+++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/context/AnnotationConfigReactiveWebApplicationContext.java
@@ -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