diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java index 267e0fd8ff..963faa73bd 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/elasticsearch/ElasticsearchRestHealthIndicator.java @@ -18,7 +18,6 @@ package org.springframework.boot.actuate.elasticsearch; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.Map; import org.apache.http.HttpStatus; import org.elasticsearch.client.Request; @@ -57,23 +56,23 @@ public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator { protected void doHealthCheck(Health.Builder builder) throws Exception { Response response = this.client .performRequest(new Request("GET", "/_cluster/health/")); - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { builder.down(); + return; + } + try (InputStream inputStream = response.getEntity().getContent()) { + doHealthCheck(builder, + StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8)); } - else { - try (InputStream is = response.getEntity().getContent()) { - Map root = this.jsonParser - .parseMap(StreamUtils.copyToString(is, StandardCharsets.UTF_8)); - String status = (String) root.get("status"); - if (status.equals(RED_STATUS)) { - builder.outOfService(); - } - else { - builder.up(); - } - } + } + + private void doHealthCheck(Health.Builder builder, String json) { + String status = (String) this.jsonParser.parseMap(json).get("status"); + if (RED_STATUS.equals(status)) { + builder.outOfService(); + return; } + builder.up(); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java index def751c124..df762a5904 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBean.java @@ -256,6 +256,12 @@ public abstract class AbstractFilterRegistrationBean } } + /** + * Return the {@link Filter} to be registered. + * @return the filter + */ + public abstract T getFilter(); + @Override public String toString() { StringBuilder builder = new StringBuilder(getOrDeduceName(this)); @@ -273,10 +279,4 @@ public abstract class AbstractFilterRegistrationBean return builder.toString(); } - /** - * Return the {@link Filter} to be registered. - * @return the filter - */ - public abstract T getFilter(); - } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java index fd070da5ed..7dc67e29b4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java @@ -20,7 +20,6 @@ import java.util.AbstractCollection; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.EventListener; import java.util.HashSet; import java.util.Iterator; @@ -94,7 +93,7 @@ public class ServletContextInitializerBeans .sorted(AnnotationAwareOrderComparator.INSTANCE)) .collect(Collectors.toList()); this.sortedList = Collections.unmodifiableList(sortedInitializers); - logBeansInformation(this.initializers); + logMappings(this.initializers); } private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) { @@ -145,12 +144,12 @@ public class ServletContextInitializerBeans // Mark the underlying source as seen in case it wraps an existing bean this.seen.add(source); } - if (ServletContextInitializerBeans.logger.isTraceEnabled()) { + if (logger.isTraceEnabled()) { String resourceDescription = getResourceDescription(beanName, beanFactory); int order = getOrder(initializer); - ServletContextInitializerBeans.logger.trace("Added existing " - + type.getSimpleName() + " initializer bean '" + beanName - + "'; order=" + order + ", resource=" + resourceDescription); + logger.trace("Added existing " + type.getSimpleName() + " initializer bean '" + + beanName + "'; order=" + order + ", resource=" + + resourceDescription); } } @@ -191,19 +190,20 @@ public class ServletContextInitializerBeans private void addAsRegistrationBean(ListableBeanFactory beanFactory, Class type, Class beanType, RegistrationBeanAdapter adapter) { - List> beans = getOrderedBeansOfType(beanFactory, beanType, + List> entries = getOrderedBeansOfType(beanFactory, beanType, this.seen); - for (Entry bean : beans) { - if (this.seen.add(bean.getValue())) { - int order = getOrder(bean.getValue()); - String beanName = bean.getKey(); + for (Entry entry : entries) { + String beanName = entry.getKey(); + B bean = entry.getValue(); + if (this.seen.add(bean)) { // One that we haven't already seen RegistrationBean registration = adapter.createRegistrationBean(beanName, - bean.getValue(), beans.size()); + bean, entries.size()); + int order = getOrder(bean); registration.setOrder(order); this.initializers.add(type, registration); - if (ServletContextInitializerBeans.logger.isTraceEnabled()) { - ServletContextInitializerBeans.logger.trace( + if (logger.isTraceEnabled()) { + logger.trace( "Created " + type.getSimpleName() + " initializer for bean '" + beanName + "'; order=" + order + ", resource=" + getResourceDescription(beanName, beanFactory)); @@ -228,9 +228,6 @@ public class ServletContextInitializerBeans private List> getOrderedBeansOfType( ListableBeanFactory beanFactory, Class type, Set excludes) { - Comparator> comparator = (o1, - o2) -> AnnotationAwareOrderComparator.INSTANCE.compare(o1.getValue(), - o2.getValue()); String[] names = beanFactory.getBeanNamesForType(type, true, false); Map map = new LinkedHashMap<>(); for (String name : names) { @@ -243,40 +240,33 @@ public class ServletContextInitializerBeans } List> beans = new ArrayList<>(); beans.addAll(map.entrySet()); - beans.sort(comparator); + beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE + .compare(o1.getValue(), o2.getValue())); return beans; } - private void logBeansInformation( + private void logMappings( MultiValueMap, ServletContextInitializer> initializers) { - if (ServletContextInitializerBeans.logger.isDebugEnabled()) { - List filterRegistrations = new ArrayList<>(); - filterRegistrations.addAll(initializers - .getOrDefault(FilterRegistrationBean.class, Collections.emptyList())); - filterRegistrations.addAll( - initializers.getOrDefault(Filter.class, Collections.emptyList())); - String filtersInfo = filterRegistrations.stream() - .map(AbstractFilterRegistrationBean.class::cast) - .map(AbstractFilterRegistrationBean::toString) - .collect(Collectors.joining(", ")); - ServletContextInitializerBeans.logger - .debug("Mapping filters: " + filtersInfo); - - List servletRegistrations = new ArrayList<>(); - servletRegistrations.addAll(initializers.getOrDefault( - ServletRegistrationBean.class, Collections.emptyList())); - servletRegistrations.addAll( - initializers.getOrDefault(Servlet.class, Collections.emptyList())); - String servletsInfo = servletRegistrations.stream() - .map(ServletRegistrationBean.class::cast) - .map((servlet) -> servlet.getServletName() + " " - + servlet.getUrlMappings()) - .collect(Collectors.joining(", ")); - ServletContextInitializerBeans.logger - .debug("Mapping servlets: " + servletsInfo); + if (logger.isDebugEnabled()) { + logMappings("filters", initializers, Filter.class, + FilterRegistrationBean.class); + logMappings("servlets", initializers, Servlet.class, + ServletRegistrationBean.class); } } + private void logMappings(String name, + MultiValueMap, ServletContextInitializer> initializers, + Class type, Class registrationType) { + List registrations = new ArrayList<>(); + registrations.addAll( + initializers.getOrDefault(registrationType, Collections.emptyList())); + registrations.addAll(initializers.getOrDefault(type, Collections.emptyList())); + String info = registrations.stream().map(Object::toString) + .collect(Collectors.joining(", ")); + logger.debug("Mapping " + name + ": " + info); + } + @Override public Iterator iterator() { return this.sortedList.iterator(); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistrationBean.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistrationBean.java index 908856f296..9a9b23f867 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistrationBean.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistrationBean.java @@ -210,4 +210,9 @@ public class ServletRegistrationBean return getOrDeduceName(this.servlet); } + @Override + public String toString() { + return getServletName() + " urls=" + getUrlMappings(); + } + }