Migrate callbacks to LambdaSafe util

Migrate existing code to the new `LambaSafe` callback handler.

Closes gh-11584
This commit is contained in:
Phillip Webb
2018-01-28 23:55:37 -08:00
parent b0cb728944
commit 3a12f98bab
5 changed files with 43 additions and 171 deletions

View File

@@ -26,12 +26,10 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.actuate.endpoint.EndpointFilter;
@@ -41,6 +39,7 @@ import org.springframework.boot.actuate.endpoint.Operation;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
import org.springframework.boot.actuate.endpoint.invoke.OperationInvokerAdvisor;
import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
import org.springframework.boot.util.LambdaSafe;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -66,8 +65,6 @@ import org.springframework.util.StringUtils;
public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O extends Operation>
implements EndpointsSupplier<E> {
private static final Log logger = LogFactory.getLog(EndpointDiscoverer.class);
private final ApplicationContext applicationContext;
private final Collection<EndpointFilter<E>> filters;
@@ -313,23 +310,15 @@ public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O exten
return isFilterMatch(filter, getFilterEndpoint(endpointBean));
}
@SuppressWarnings("unchecked")
private boolean isFilterMatch(EndpointFilter<E> filter, E endpoint) {
try {
return filter.match(endpoint);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || msg.startsWith(endpoint.getClass().getName())) {
// Possibly a lambda-defined EndpointFilter which we could not resolve the
// generic EndpointInfo type for
if (logger.isDebugEnabled()) {
logger.debug("Non-matching Endpoint for EndpointFilter: " + filter,
ex);
}
return false;
}
throw ex;
}
return LambdaSafe.callback(EndpointFilter.class, filter, endpoint)
.withLogger(EndpointDiscoverer.class).invokeAnd((f) -> f.match(endpoint))
.get();
}
public <A, B> void doIt(Function<A, B> x) {
}
private E getFilterEndpoint(EndpointBean endpointBean) {

View File

@@ -17,16 +17,15 @@
package org.springframework.boot.actuate.metrics.cache;
import java.util.Collection;
import java.util.Objects;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.util.LambdaSafe;
import org.springframework.cache.Cache;
import org.springframework.core.ResolvableType;
/**
* Register supported {@link Cache} to a {@link MeterRegistry}.
@@ -36,8 +35,6 @@ import org.springframework.core.ResolvableType;
*/
public class CacheMetricsRegistrar {
private static final Log logger = LogFactory.getLog(CacheMetricsRegistrar.class);
private final MeterRegistry registry;
private final String metricName;
@@ -74,41 +71,15 @@ public class CacheMetricsRegistrar {
return false;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked" })
private MeterBinder getMeterBinder(Cache cache, Tags tags) {
tags = tags.and(getAdditionalTags(cache));
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
Class<?> cacheType = ResolvableType
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
.resolveGeneric();
if (cacheType.isInstance(cache)) {
try {
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
.getMeterBinder(cache, this.metricName, tags);
if (meterBinder != null) {
return meterBinder;
}
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || msg.startsWith(cache.getClass().getName())) {
// Possibly a lambda-defined CacheMeterBinderProvider which we
// could not resolve the generic Cache type for
if (logger.isDebugEnabled()) {
logger.debug(
"Non-matching Cache type for CacheMeterBinderProvider: "
+ binderProvider,
ex);
}
}
else {
throw ex;
}
}
}
}
return null;
Tags cacheTags = tags.and(getAdditionalTags(cache));
return LambdaSafe
.callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache)
.withLogger(CacheMetricsRegistrar.class)
.invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache,
this.metricName, cacheTags))
.filter(Objects::nonNull).findFirst().orElse(null);
}
/**