[bs-133] Move ServerProperties to spring-bootstrap core
[Fixes #50345533]
This commit is contained in:
@@ -18,7 +18,6 @@ package org.springframework.bootstrap.actuate.autoconfigure;
|
||||
|
||||
import org.springframework.bootstrap.actuate.properties.EndpointsProperties;
|
||||
import org.springframework.bootstrap.actuate.properties.ManagementServerProperties;
|
||||
import org.springframework.bootstrap.actuate.properties.ServerProperties;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.bootstrap.context.annotation.EnableConfigurationProperties;
|
||||
@@ -42,8 +41,7 @@ public class ActuatorAutoConfiguration {
|
||||
* ServerProperties has to be declared in a non-conditional bean, so that it gets
|
||||
* added to the context early enough
|
||||
*/
|
||||
@EnableConfigurationProperties({ ServerProperties.class,
|
||||
ManagementServerProperties.class })
|
||||
@EnableConfigurationProperties(ManagementServerProperties.class)
|
||||
public static class ServerPropertiesConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -19,9 +19,9 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.bootstrap.actuate.properties.ManagementServerProperties;
|
||||
import org.springframework.bootstrap.actuate.properties.ServerProperties;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnExpression;
|
||||
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.bootstrap.properties.ServerProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
@@ -18,41 +18,26 @@ package org.springframework.bootstrap.actuate.autoconfigure;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.apache.catalina.valves.AccessLogValve;
|
||||
import org.apache.catalina.valves.RemoteIpValve;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.bootstrap.actuate.endpoint.error.ErrorEndpoint;
|
||||
import org.springframework.bootstrap.actuate.properties.ServerProperties;
|
||||
import org.springframework.bootstrap.actuate.properties.ServerProperties.Tomcat;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.bootstrap.context.embedded.ErrorPage;
|
||||
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Configuration for injecting externalized properties into the container (e.g. tomcat).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
// Slight hack here (BeanPostProcessor), to force the server properties to be bound in
|
||||
// the right order
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class })
|
||||
@Order(Integer.MIN_VALUE)
|
||||
@Import(InfoConfiguration.class)
|
||||
public class ServerConfiguration implements EmbeddedServletContainerCustomizer {
|
||||
|
||||
@Autowired
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@Value("${endpoints.error.path:/error}")
|
||||
private String errorPath = "/error";
|
||||
|
||||
@@ -63,48 +48,7 @@ public class ServerConfiguration implements EmbeddedServletContainerCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
|
||||
// Need to do a look up here to make it lazy
|
||||
ServerProperties server = this.beanFactory.getBean(ServerProperties.class);
|
||||
|
||||
factory.setPort(server.getPort());
|
||||
factory.setAddress(server.getAddress());
|
||||
factory.setContextPath(server.getContextPath());
|
||||
|
||||
if (factory instanceof TomcatEmbeddedServletContainerFactory) {
|
||||
configureTomcat((TomcatEmbeddedServletContainerFactory) factory, server);
|
||||
}
|
||||
|
||||
factory.addErrorPages(new ErrorPage(this.errorPath));
|
||||
|
||||
}
|
||||
|
||||
private void configureTomcat(TomcatEmbeddedServletContainerFactory tomcatFactory,
|
||||
ServerProperties configuration) {
|
||||
|
||||
Tomcat tomcat = configuration.getTomcat();
|
||||
if (tomcat.getBasedir() != null) {
|
||||
tomcatFactory.setBaseDirectory(tomcat.getBasedir());
|
||||
}
|
||||
|
||||
String remoteIpHeader = tomcat.getRemoteIpHeader();
|
||||
String protocolHeader = tomcat.getProtocolHeader();
|
||||
|
||||
if (StringUtils.hasText(remoteIpHeader) || StringUtils.hasText(protocolHeader)) {
|
||||
RemoteIpValve valve = new RemoteIpValve();
|
||||
valve.setRemoteIpHeader(remoteIpHeader);
|
||||
valve.setProtocolHeader(protocolHeader);
|
||||
tomcatFactory.addContextValves(valve);
|
||||
}
|
||||
|
||||
String pattern = tomcat.getAccessLogPattern();
|
||||
if (pattern != null) {
|
||||
AccessLogValve valve = new AccessLogValve();
|
||||
valve.setPattern(pattern);
|
||||
valve.setSuffix(".log");
|
||||
tomcatFactory.addContextValves(valve);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.actuate.properties;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetAddress;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.bootstrap.context.annotation.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Properties for the web server (e.g. port and path settings).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@ConfigurationProperties(name = "server", ignoreUnknownFields = false)
|
||||
public class ServerProperties {
|
||||
|
||||
private int port = 8080;
|
||||
|
||||
private InetAddress address;
|
||||
|
||||
@NotNull
|
||||
private String contextPath = "";
|
||||
|
||||
private Tomcat tomcat = new Tomcat();
|
||||
|
||||
public Tomcat getTomcat() {
|
||||
return this.tomcat;
|
||||
}
|
||||
|
||||
public String getContextPath() {
|
||||
return this.contextPath;
|
||||
}
|
||||
|
||||
public void setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public InetAddress getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public void setAddress(InetAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public static class Tomcat {
|
||||
|
||||
private String accessLogPattern;
|
||||
|
||||
private String protocolHeader = "x-forwarded-proto";
|
||||
|
||||
private String remoteIpHeader = "x-forwarded-for";
|
||||
|
||||
private File basedir;
|
||||
|
||||
public File getBasedir() {
|
||||
return this.basedir;
|
||||
}
|
||||
|
||||
public void setBasedir(File basedir) {
|
||||
this.basedir = basedir;
|
||||
}
|
||||
|
||||
public String getAccessLogPattern() {
|
||||
return this.accessLogPattern;
|
||||
}
|
||||
|
||||
public void setAccessLogPattern(String accessLogPattern) {
|
||||
this.accessLogPattern = accessLogPattern;
|
||||
}
|
||||
|
||||
public String getProtocolHeader() {
|
||||
return this.protocolHeader;
|
||||
}
|
||||
|
||||
public void setProtocolHeader(String protocolHeader) {
|
||||
this.protocolHeader = protocolHeader;
|
||||
}
|
||||
|
||||
public String getRemoteIpHeader() {
|
||||
return this.remoteIpHeader;
|
||||
}
|
||||
|
||||
public void setRemoteIpHeader(String remoteIpHeader) {
|
||||
this.remoteIpHeader = remoteIpHeader;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.actuate.properties;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.bootstrap.bind.RelaxedDataBinder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* Externalized configuration for server properties
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ServerPropertiesTests {
|
||||
|
||||
private ServerProperties properties = new ServerProperties();
|
||||
|
||||
@Test
|
||||
public void testAddressBinding() throws Exception {
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server");
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap("server.address",
|
||||
"127.0.0.1")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(InetAddress.getByName("127.0.0.1"), this.properties.getAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPortBinding() throws Exception {
|
||||
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
|
||||
Collections.singletonMap("server.port", "9000")));
|
||||
assertEquals(9000, this.properties.getPort());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user