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);
}
/**

View File

@ -20,11 +20,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.util.LambdaSafe;
import org.springframework.cache.CacheManager;
import org.springframework.core.ResolvableType;
/**
* Invokes the available {@link CacheManagerCustomizer} instances in the context for a
@ -35,8 +32,6 @@ import org.springframework.core.ResolvableType;
*/
public class CacheManagerCustomizers {
private static final Log logger = LogFactory.getLog(CacheManagerCustomizers.class);
private final List<CacheManagerCustomizer<?>> customizers;
public CacheManagerCustomizers(
@ -53,32 +48,12 @@ public class CacheManagerCustomizers {
* @param cacheManager the cache manager to customize
* @return the cache manager
*/
@SuppressWarnings("unchecked")
public <T extends CacheManager> T customize(T cacheManager) {
for (CacheManagerCustomizer<?> customizer : this.customizers) {
Class<?> generic = ResolvableType
.forClass(CacheManagerCustomizer.class, customizer.getClass())
.resolveGeneric();
if (generic.isInstance(cacheManager)) {
customize(cacheManager, customizer);
}
}
LambdaSafe.callbacks(CacheManagerCustomizer.class, this.customizers, cacheManager)
.withLogger(CacheManagerCustomizers.class)
.invoke((customizer) -> customizer.customize(cacheManager));
return cacheManager;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void customize(CacheManager cacheManager, CacheManagerCustomizer customizer) {
try {
customizer.customize(cacheManager);
}
catch (ClassCastException ex) {
// Possibly a lambda-defined customizer which we could not resolve the generic
// cache manager type for
if (logger.isDebugEnabled()) {
logger.debug(
"Non-matching cache manager type for customizer: " + customizer,
ex);
}
}
}
}

View File

@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.transaction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.ResolvableType;
import org.springframework.boot.util.LambdaSafe;
import org.springframework.transaction.PlatformTransactionManager;
/**
@ -34,44 +32,21 @@ import org.springframework.transaction.PlatformTransactionManager;
*/
public class TransactionManagerCustomizers {
private static final Log logger = LogFactory
.getLog(TransactionManagerCustomizers.class);
private final List<PlatformTransactionManagerCustomizer<?>> customizers;
public TransactionManagerCustomizers(
Collection<? extends PlatformTransactionManagerCustomizer<?>> customizers) {
this.customizers = (customizers == null ? null : new ArrayList<>(customizers));
this.customizers = (customizers == null ? Collections.emptyList()
: new ArrayList<>(customizers));
}
@SuppressWarnings("unchecked")
public void customize(PlatformTransactionManager transactionManager) {
if (this.customizers != null) {
for (PlatformTransactionManagerCustomizer<?> customizer : this.customizers) {
Class<?> generic = ResolvableType
.forClass(PlatformTransactionManagerCustomizer.class,
customizer.getClass())
.resolveGeneric();
if (generic.isInstance(transactionManager)) {
customize(transactionManager, customizer);
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void customize(PlatformTransactionManager transactionManager,
PlatformTransactionManagerCustomizer customizer) {
try {
customizer.customize(transactionManager);
}
catch (ClassCastException ex) {
// Possibly a lambda-defined customizer which we could not resolve the generic
// transaction manager type for
if (logger.isDebugEnabled()) {
logger.debug("Non-matching transaction manager type for customizer: "
+ customizer, ex);
}
}
LambdaSafe
.callbacks(PlatformTransactionManagerCustomizer.class, this.customizers,
transactionManager)
.withLogger(TransactionManagerCustomizers.class)
.invoke((customizer) -> customizer.customize(transactionManager));
}
}

View File

@ -21,15 +21,12 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.ResolvableType;
import org.springframework.boot.util.LambdaSafe;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.Assert;
@ -45,9 +42,6 @@ import org.springframework.util.Assert;
public class WebServerFactoryCustomizerBeanPostProcessor
implements BeanPostProcessor, BeanFactoryAware {
private static final Log logger = LogFactory
.getLog(WebServerFactoryCustomizerBeanPostProcessor.class);
private ListableBeanFactory beanFactory;
private List<WebServerFactoryCustomizer<?>> customizers;
@ -75,45 +69,13 @@ public class WebServerFactoryCustomizerBeanPostProcessor
return bean;
}
private void postProcessBeforeInitialization(WebServerFactory bean) {
for (WebServerFactoryCustomizer<?> customizer : getCustomizers()) {
Class<?> type = ResolvableType
.forClass(WebServerFactoryCustomizer.class, customizer.getClass())
.getGeneric().resolve(WebServerFactory.class);
if (type.isInstance(bean)) {
invokeCustomizer(customizer, bean);
}
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void invokeCustomizer(WebServerFactoryCustomizer customizer,
WebServerFactory webServerFactory) {
try {
customizer.customize(webServerFactory);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || msg.startsWith(webServerFactory.getClass().getName())) {
// Possibly a lambda-defined WebServerFactoryCustomizer which we could not
// resolve the
// generic WebServerFactory type for
logLambdaDebug(customizer, ex);
}
else {
throw ex;
}
}
}
private void logLambdaDebug(WebServerFactoryCustomizer<?> customizer,
ClassCastException ex) {
if (logger.isDebugEnabled()) {
logger.debug(
"Non-matching WebServerFactory type for WebServerFactoryCustomizer: "
+ customizer,
ex);
}
@SuppressWarnings("unchecked")
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
LambdaSafe
.callbacks(WebServerFactoryCustomizer.class, getCustomizers(),
webServerFactory)
.withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
.invoke((customizer) -> customizer.customize(webServerFactory));
}
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {