[bs-133] Move ServerProperties to spring-bootstrap core
[Fixes #50345533]
This commit is contained in:
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.bootstrap.autoconfigure;
|
||||
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -28,9 +27,9 @@ import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
* {@link PropertySourcesPlaceholderConfigurer}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(PropertySourcesPlaceholderConfigurer.class)
|
||||
public class PropertyPlaceholderAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.bootstrap.autoconfigure.web;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.util.Loader;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
||||
@@ -25,23 +25,43 @@ import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
|
||||
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;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for
|
||||
* {@link JettyEmbeddedServletContainerFactory}.
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for an embedded servlet container.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
||||
@ConditionalOnMissingBean(EmbeddedServletContainerFactory.class)
|
||||
public class EmbeddedJettyAutoConfiguration {
|
||||
@Import(ServerPropertiesConfiguration.class)
|
||||
public class EmbeddedContainerConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Server.class, Loader.class })
|
||||
@ConditionalOnMissingBean(EmbeddedServletContainerFactory.class)
|
||||
protected static class EmbeddedJettyAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
|
||||
return new JettyEmbeddedServletContainerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
||||
@ConditionalOnMissingBean(EmbeddedServletContainerFactory.class)
|
||||
protected static class EmbeddedTomcatAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
|
||||
return new TomcatEmbeddedServletContainerFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
|
||||
return new JettyEmbeddedServletContainerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,7 +43,7 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class })
|
||||
@ConditionalOnClass({ Servlet.class, EmbeddedServletContainerCustomizer.class })
|
||||
public class EmbeddedContainerCustomizerConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
|
||||
@@ -1,46 +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.autoconfigure.web;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for
|
||||
* {@link TomcatEmbeddedServletContainerFactory}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, Tomcat.class })
|
||||
@ConditionalOnMissingBean(EmbeddedServletContainerFactory.class)
|
||||
public class EmbeddedTomcatAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
|
||||
return new TomcatEmbeddedServletContainerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.autoconfigure.web;
|
||||
|
||||
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.bootstrap.context.annotation.EnableConfigurationProperties;
|
||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.bootstrap.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.properties.ServerProperties;
|
||||
import org.springframework.bootstrap.properties.ServerProperties.Tomcat;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ServerProperties.class)
|
||||
public class ServerPropertiesConfiguration implements EmbeddedServletContainerCustomizer {
|
||||
|
||||
@Autowired
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* 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.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,9 +7,8 @@ org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguratio
|
||||
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerCustomizerConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedJettyAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedTomcatAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.WebMvcAutoConfiguration
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
org.springframework.bootstrap.context.initializer.ConfigFileApplicationContextInitializer,\
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.autoconfigure.web;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.properties.ServerProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ServerPropertiesConfigurationTests {
|
||||
|
||||
private static ConfigurableEmbeddedServletContainerFactory containerFactory;
|
||||
|
||||
private AnnotationConfigEmbeddedWebApplicationContext context;
|
||||
|
||||
private Map<String, Object> environment = new HashMap<String, Object>();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
containerFactory = Mockito
|
||||
.mock(ConfigurableEmbeddedServletContainerFactory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createFromConfigClass() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(EmbeddedContainerConfiguration.class,
|
||||
ServerPropertiesConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.environment.put("server.port", "9000");
|
||||
this.context.getEnvironment().getPropertySources()
|
||||
.addFirst(new MapPropertySource("test", this.environment));
|
||||
this.context.refresh();
|
||||
ServerProperties server = this.context.getBean(ServerProperties.class);
|
||||
assertNotNull(server);
|
||||
assertEquals(9000, server.getPort());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class EmbeddedContainerConfiguration {
|
||||
|
||||
@Bean
|
||||
public EmbeddedServletContainerFactory containerFactory() {
|
||||
return ServerPropertiesConfigurationTests.containerFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.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