This commit is contained in:
Phillip Webb
2014-07-28 13:31:22 -07:00
parent 5e1552b718
commit f8bf0e2031
14 changed files with 114 additions and 93 deletions

View File

@@ -72,65 +72,67 @@ public class PropertySourcesPropertyValues implements PropertyValues {
this.propertySources = propertySources;
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(
propertySources);
String[] includes = patterns == null ? new String[0] : patterns
.toArray(new String[0]);
String[] exacts = names == null ? new String[0] : names.toArray(new String[0]);
String[] includes = toArray(patterns);
String[] exacts = toArray(names);
for (PropertySource<?> source : propertySources) {
processPropertySource(source, resolver, includes, exacts);
}
}
private String[] toArray(Collection<String> strings) {
if (strings == null) {
return new String[0];
}
return strings.toArray(new String[strings.size()]);
}
private void processPropertySource(PropertySource<?> source,
PropertySourcesPropertyResolver resolver, String[] includes, String[] exacts) {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
if (enumerable.getPropertyNames().length > 0) {
for (String propertyName : enumerable.getPropertyNames()) {
if (this.NON_ENUMERABLE_ENUMERABLES.contains(source.getName())
&& !PatternMatchUtils.simpleMatch(includes, propertyName)) {
continue;
}
Object value = source.getProperty(propertyName);
try {
value = resolver.getProperty(propertyName);
}
catch (RuntimeException ex) {
// Probably could not resolve placeholders, ignore it here
}
if (!this.propertyValues.containsKey(propertyName)) {
this.propertyValues.put(propertyName, new PropertyValue(
propertyName, value));
}
}
}
processEnumerablePropertySource((EnumerablePropertySource<?>) source,
resolver, includes, exacts);
}
else if (source instanceof CompositePropertySource) {
CompositePropertySource composite = (CompositePropertySource) source;
for (PropertySource<?> nested : extractSources(composite)) {
processPropertySource(nested, resolver, includes, exacts);
}
processCompositePropertySource((CompositePropertySource) source, resolver,
includes, exacts);
}
else {
// We can only do exact matches for non-enumerable property names, but
// that's better than nothing...
for (String propertyName : exacts) {
Object value;
value = resolver.getProperty(propertyName);
if (value != null && !this.propertyValues.containsKey(propertyName)) {
this.propertyValues.put(propertyName, new PropertyValue(propertyName,
value));
processDefaultPropertySource(source, resolver, includes, exacts);
}
}
private void processEnumerablePropertySource(EnumerablePropertySource<?> source,
PropertySourcesPropertyResolver resolver, String[] includes, String[] exacts) {
if (source.getPropertyNames().length > 0) {
for (String propertyName : source.getPropertyNames()) {
if (this.NON_ENUMERABLE_ENUMERABLES.contains(source.getName())
&& !PatternMatchUtils.simpleMatch(includes, propertyName)) {
continue;
}
value = source.getProperty(propertyName.toUpperCase());
if (value != null && !this.propertyValues.containsKey(propertyName)) {
Object value = source.getProperty(propertyName);
try {
value = resolver.getProperty(propertyName);
}
catch (RuntimeException ex) {
// Probably could not resolve placeholders, ignore it here
}
if (!this.propertyValues.containsKey(propertyName)) {
this.propertyValues.put(propertyName, new PropertyValue(propertyName,
value));
continue;
}
}
}
}
private void processCompositePropertySource(CompositePropertySource source,
PropertySourcesPropertyResolver resolver, String[] includes, String[] exacts) {
for (PropertySource<?> nested : extractSources(source)) {
processPropertySource(nested, resolver, includes, exacts);
}
}
private Collection<PropertySource<?>> extractSources(CompositePropertySource composite) {
Field field = ReflectionUtils.findField(CompositePropertySource.class,
"propertySources");
@@ -141,9 +143,24 @@ public class PropertySourcesPropertyValues implements PropertyValues {
.get(composite);
return collection;
}
catch (Exception e) {
catch (Exception ex) {
throw new IllegalStateException(
"Cannot extract property sources from composite", e);
"Cannot extract property sources from composite", ex);
}
}
private void processDefaultPropertySource(PropertySource<?> source,
PropertySourcesPropertyResolver resolver, String[] includes, String[] exacts) {
for (String propertyName : exacts) {
Object value = resolver.getProperty(propertyName);
if (value == null) {
value = source.getProperty(propertyName.toUpperCase());
}
if (value != null && !this.propertyValues.containsKey(propertyName)) {
this.propertyValues.put(propertyName, new PropertyValue(propertyName,
value));
continue;
}
}
}

View File

@@ -76,17 +76,21 @@ public class JettyEmbeddedServletContainer implements EmbeddedServletContainer {
this.server.start();
}
catch (Exception ex) {
try {
// Ensure process isn't left running
this.server.stop();
}
catch (Exception e) {
}
// Ensure process isn't left running
stopSilently();
throw new EmbeddedServletContainerException(
"Unable to start embedded Jetty servlet container", ex);
}
}
private void stopSilently() {
try {
this.server.stop();
}
catch (Exception ex) {
}
}
@Override
public void start() throws EmbeddedServletContainerException {
this.server.setConnectors(this.connectors);

View File

@@ -95,7 +95,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
this.tomcat.stop();
throw new IllegalStateException("Tomcat connector in failed state");
}
}
catch (Exception ex) {
throw new EmbeddedServletContainerException(
@@ -154,15 +153,19 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer
}
// Ensure process isn't left running if it actually failed to start
if (LifecycleState.FAILED.equals(this.tomcat.getConnector().getState())) {
try {
this.tomcat.stop();
}
catch (LifecycleException e) {
}
stopSilently();
throw new IllegalStateException("Tomcat connector in failed state");
}
}
private void stopSilently() {
try {
this.tomcat.stop();
}
catch (LifecycleException ex) {
}
}
private void addPreviouslyRemovedConnectors() {
Service[] services = this.tomcat.getServer().findServices();
for (Service service : services) {

View File

@@ -77,14 +77,13 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple
private final Map<Class<?>, String> exceptions = new HashMap<Class<?>, String>();
private final Map<Class<?>, Class<?>> subtypes = new HashMap<Class<?>, Class<?>>();
private final OncePerRequestFilter delegate = new OncePerRequestFilter(
) {
private final OncePerRequestFilter delegate = new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
HttpServletResponse response, FilterChain chain) throws ServletException,
IOException {
ErrorPageFilter.this.doFilter(request, response, chain);
}
@@ -92,13 +91,13 @@ class ErrorPageFilter extends AbstractConfigurableEmbeddedServletContainer imple
@Override
public void init(FilterConfig filterConfig) throws ServletException {
delegate.init(filterConfig);
this.delegate.init(filterConfig);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
delegate.doFilter(request, response, chain);
this.delegate.doFilter(request, response, chain);
}
private void doFilter(HttpServletRequest request, HttpServletResponse response,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 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.

View File

@@ -16,11 +16,6 @@
package org.springframework.boot.context.web;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
@@ -38,6 +33,11 @@ import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link ErrorPageFilter}.
*
@@ -110,7 +110,7 @@ public class ErrorPageFilterTests {
super.doFilter(request, response);
}
};
filter.init(new MockFilterConfig("FILTER"));
this.filter.init(new MockFilterConfig("FILTER"));
this.filter.doFilter(this.request, this.response, this.chain);
}