[bs-133] Move ServerProperties to spring-bootstrap core

[Fixes #50345533]
pull/1/merge
Dave Syer 12 years ago
parent 6e52d7dd39
commit a700ed4479

@ -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);
}
}
}

@ -141,12 +141,6 @@
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>

@ -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 {
@Bean
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
return new JettyEmbeddedServletContainerFactory();
@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();
}
}
}

@ -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);
}
}
}

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.properties;
package org.springframework.bootstrap.properties;
import java.io.File;
import java.net.InetAddress;

@ -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;
}
}
}

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.properties;
package org.springframework.bootstrap.properties;
import java.net.InetAddress;
import java.util.Collections;
@ -27,7 +27,7 @@ import static org.junit.Assert.assertFalse;
/**
* Externalized configuration for server properties
*
*
* @author Dave Syer
*/
public class ServerPropertiesTests {
Loading…
Cancel
Save