From 3a12f98babbc2937ed39d34b02482c148384b8e5 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 28 Jan 2018 23:55:37 -0800 Subject: [PATCH] Migrate callbacks to LambdaSafe util Migrate existing code to the new `LambaSafe` callback handler. Closes gh-11584 --- .../annotation/EndpointDiscoverer.java | 31 ++++------- .../metrics/cache/CacheMetricsRegistrar.java | 49 ++++------------- .../cache/CacheManagerCustomizers.java | 35 ++---------- .../TransactionManagerCustomizers.java | 45 ++++------------ ...verFactoryCustomizerBeanPostProcessor.java | 54 +++---------------- 5 files changed, 43 insertions(+), 171 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java index 9f51497a6c4..8648478e239 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java @@ -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, O extends Operation> implements EndpointsSupplier { - private static final Log logger = LogFactory.getLog(EndpointDiscoverer.class); - private final ApplicationContext applicationContext; private final Collection> filters; @@ -313,23 +310,15 @@ public abstract class EndpointDiscoverer, O exten return isFilterMatch(filter, getFilterEndpoint(endpointBean)); } + @SuppressWarnings("unchecked") private boolean isFilterMatch(EndpointFilter 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 void doIt(Function x) { + } private E getFilterEndpoint(EndpointBean endpointBean) { diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java index f2b71759a22..bf00ec205ec 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java @@ -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); } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java index 489aebd600c..acca87ddb92 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java @@ -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> 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 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); - } - } - } - } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java index 65b54868b10..4108f62a685 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java @@ -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> customizers; public TransactionManagerCustomizers( Collection> 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)); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerFactoryCustomizerBeanPostProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerFactoryCustomizerBeanPostProcessor.java index 5986f91a048..21884502833 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerFactoryCustomizerBeanPostProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerFactoryCustomizerBeanPostProcessor.java @@ -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> 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> getCustomizers() {