mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish
This commit is contained in:
parent
ed9468a31a
commit
9c426c63bf
@ -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;
|
||||
}
|
||||
else {
|
||||
try (InputStream is = response.getEntity().getContent()) {
|
||||
Map<String, Object> 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();
|
||||
}
|
||||
}
|
||||
try (InputStream inputStream = response.getEntity().getContent()) {
|
||||
doHealthCheck(builder,
|
||||
StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder(getOrDeduceName(this));
|
||||
@ -273,10 +279,4 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
|
||||
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.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 <T, B extends T> void addAsRegistrationBean(ListableBeanFactory beanFactory,
|
||||
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);
|
||||
for (Entry<String, B> bean : beans) {
|
||||
if (this.seen.add(bean.getValue())) {
|
||||
int order = getOrder(bean.getValue());
|
||||
String beanName = bean.getKey();
|
||||
for (Entry<String, B> 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 <T> List<Entry<String, T>> getOrderedBeansOfType(
|
||||
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);
|
||||
Map<String, T> map = new LinkedHashMap<>();
|
||||
for (String name : names) {
|
||||
@ -243,40 +240,33 @@ public class ServletContextInitializerBeans
|
||||
}
|
||||
List<Entry<String, T>> 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<Class<?>, ServletContextInitializer> initializers) {
|
||||
if (ServletContextInitializerBeans.logger.isDebugEnabled()) {
|
||||
List<ServletContextInitializer> 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<ServletContextInitializer> 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<Class<?>, ServletContextInitializer> initializers,
|
||||
Class<?> type, Class<? extends RegistrationBean> registrationType) {
|
||||
List<ServletContextInitializer> 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<ServletContextInitializer> iterator() {
|
||||
return this.sortedList.iterator();
|
||||
|
@ -210,4 +210,9 @@ public class ServletRegistrationBean<T extends Servlet>
|
||||
return getOrDeduceName(this.servlet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getServletName() + " urls=" + getUrlMappings();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user