pull/15366/head
Phillip Webb 6 years ago
parent ed9468a31a
commit 9c426c63bf

@ -18,7 +18,6 @@ package org.springframework.boot.actuate.elasticsearch;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.elasticsearch.client.Request; import org.elasticsearch.client.Request;
@ -57,23 +56,23 @@ public class ElasticsearchRestHealthIndicator extends AbstractHealthIndicator {
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
Response response = this.client Response response = this.client
.performRequest(new Request("GET", "/_cluster/health/")); .performRequest(new Request("GET", "/_cluster/health/"));
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
builder.down(); builder.down();
return;
} }
else { try (InputStream inputStream = response.getEntity().getContent()) {
try (InputStream is = response.getEntity().getContent()) { doHealthCheck(builder,
Map<String, Object> root = this.jsonParser StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
.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();
} }
} }

@ -256,6 +256,12 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
} }
} }
/**
* Return the {@link Filter} to be registered.
* @return the filter
*/
public abstract T getFilter();
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(getOrDeduceName(this)); StringBuilder builder = new StringBuilder(getOrDeduceName(this));
@ -273,10 +279,4 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
return builder.toString(); return builder.toString();
} }
/**
* Return the {@link Filter} to be registered.
* @return the filter
*/
public abstract T getFilter();
} }

@ -20,7 +20,6 @@ import java.util.AbstractCollection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.EventListener; import java.util.EventListener;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -94,7 +93,7 @@ public class ServletContextInitializerBeans
.sorted(AnnotationAwareOrderComparator.INSTANCE)) .sorted(AnnotationAwareOrderComparator.INSTANCE))
.collect(Collectors.toList()); .collect(Collectors.toList());
this.sortedList = Collections.unmodifiableList(sortedInitializers); this.sortedList = Collections.unmodifiableList(sortedInitializers);
logBeansInformation(this.initializers); logMappings(this.initializers);
} }
private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) { 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 // Mark the underlying source as seen in case it wraps an existing bean
this.seen.add(source); this.seen.add(source);
} }
if (ServletContextInitializerBeans.logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
String resourceDescription = getResourceDescription(beanName, beanFactory); String resourceDescription = getResourceDescription(beanName, beanFactory);
int order = getOrder(initializer); int order = getOrder(initializer);
ServletContextInitializerBeans.logger.trace("Added existing " logger.trace("Added existing " + type.getSimpleName() + " initializer bean '"
+ type.getSimpleName() + " initializer bean '" + beanName + beanName + "'; order=" + order + ", resource="
+ "'; order=" + order + ", resource=" + resourceDescription); + resourceDescription);
} }
} }
@ -191,19 +190,20 @@ public class ServletContextInitializerBeans
private <T, B extends T> void addAsRegistrationBean(ListableBeanFactory beanFactory, private <T, B extends T> void addAsRegistrationBean(ListableBeanFactory beanFactory,
Class<T> type, Class<B> beanType, RegistrationBeanAdapter<T> adapter) { Class<T> type, Class<B> beanType, RegistrationBeanAdapter<T> adapter) {
List<Map.Entry<String, B>> beans = getOrderedBeansOfType(beanFactory, beanType, List<Map.Entry<String, B>> entries = getOrderedBeansOfType(beanFactory, beanType,
this.seen); this.seen);
for (Entry<String, B> bean : beans) { for (Entry<String, B> entry : entries) {
if (this.seen.add(bean.getValue())) { String beanName = entry.getKey();
int order = getOrder(bean.getValue()); B bean = entry.getValue();
String beanName = bean.getKey(); if (this.seen.add(bean)) {
// One that we haven't already seen // One that we haven't already seen
RegistrationBean registration = adapter.createRegistrationBean(beanName, RegistrationBean registration = adapter.createRegistrationBean(beanName,
bean.getValue(), beans.size()); bean, entries.size());
int order = getOrder(bean);
registration.setOrder(order); registration.setOrder(order);
this.initializers.add(type, registration); this.initializers.add(type, registration);
if (ServletContextInitializerBeans.logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
ServletContextInitializerBeans.logger.trace( logger.trace(
"Created " + type.getSimpleName() + " initializer for bean '" "Created " + type.getSimpleName() + " initializer for bean '"
+ beanName + "'; order=" + order + ", resource=" + beanName + "'; order=" + order + ", resource="
+ getResourceDescription(beanName, beanFactory)); + getResourceDescription(beanName, beanFactory));
@ -228,9 +228,6 @@ public class ServletContextInitializerBeans
private <T> List<Entry<String, T>> getOrderedBeansOfType( private <T> List<Entry<String, T>> getOrderedBeansOfType(
ListableBeanFactory beanFactory, Class<T> type, Set<?> excludes) { ListableBeanFactory beanFactory, Class<T> type, Set<?> excludes) {
Comparator<Entry<String, T>> comparator = (o1,
o2) -> AnnotationAwareOrderComparator.INSTANCE.compare(o1.getValue(),
o2.getValue());
String[] names = beanFactory.getBeanNamesForType(type, true, false); String[] names = beanFactory.getBeanNamesForType(type, true, false);
Map<String, T> map = new LinkedHashMap<>(); Map<String, T> map = new LinkedHashMap<>();
for (String name : names) { for (String name : names) {
@ -243,38 +240,31 @@ public class ServletContextInitializerBeans
} }
List<Entry<String, T>> beans = new ArrayList<>(); List<Entry<String, T>> beans = new ArrayList<>();
beans.addAll(map.entrySet()); beans.addAll(map.entrySet());
beans.sort(comparator); beans.sort((o1, o2) -> AnnotationAwareOrderComparator.INSTANCE
.compare(o1.getValue(), o2.getValue()));
return beans; return beans;
} }
private void logBeansInformation( private void logMappings(
MultiValueMap<Class<?>, ServletContextInitializer> initializers) { MultiValueMap<Class<?>, ServletContextInitializer> initializers) {
if (ServletContextInitializerBeans.logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
List<ServletContextInitializer> filterRegistrations = new ArrayList<>(); logMappings("filters", initializers, Filter.class,
filterRegistrations.addAll(initializers FilterRegistrationBean.class);
.getOrDefault(FilterRegistrationBean.class, Collections.emptyList())); logMappings("servlets", initializers, Servlet.class,
filterRegistrations.addAll( ServletRegistrationBean.class);
initializers.getOrDefault(Filter.class, Collections.emptyList())); }
String filtersInfo = filterRegistrations.stream() }
.map(AbstractFilterRegistrationBean.class::cast)
.map(AbstractFilterRegistrationBean::toString) private void logMappings(String name,
.collect(Collectors.joining(", ")); MultiValueMap<Class<?>, ServletContextInitializer> initializers,
ServletContextInitializerBeans.logger Class<?> type, Class<? extends RegistrationBean> registrationType) {
.debug("Mapping filters: " + filtersInfo); List<ServletContextInitializer> registrations = new ArrayList<>();
registrations.addAll(
List<ServletContextInitializer> servletRegistrations = new ArrayList<>(); initializers.getOrDefault(registrationType, Collections.emptyList()));
servletRegistrations.addAll(initializers.getOrDefault( registrations.addAll(initializers.getOrDefault(type, Collections.emptyList()));
ServletRegistrationBean.class, Collections.emptyList())); String info = registrations.stream().map(Object::toString)
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(", ")); .collect(Collectors.joining(", "));
ServletContextInitializerBeans.logger logger.debug("Mapping " + name + ": " + info);
.debug("Mapping servlets: " + servletsInfo);
}
} }
@Override @Override

@ -210,4 +210,9 @@ public class ServletRegistrationBean<T extends Servlet>
return getOrDeduceName(this.servlet); return getOrDeduceName(this.servlet);
} }
@Override
public String toString() {
return getServletName() + " urls=" + getUrlMappings();
}
} }

Loading…
Cancel
Save