parent
6e52d7dd39
commit
a700ed4479
@ -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,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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue