Skip to content

Commit 3a12f98

Browse files
committed
Migrate callbacks to LambdaSafe util
Migrate existing code to the new `LambaSafe` callback handler. Closes gh-11584
1 parent b0cb728 commit 3a12f98

File tree

5 files changed

+43
-171
lines changed

5 files changed

+43
-171
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java

+10-21
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
import java.util.Objects;
2727
import java.util.Set;
2828
import java.util.concurrent.ConcurrentHashMap;
29+
import java.util.function.Function;
2930
import java.util.function.Supplier;
3031
import java.util.stream.Collectors;
3132

32-
import org.apache.commons.logging.Log;
33-
import org.apache.commons.logging.LogFactory;
34-
3533
import org.springframework.beans.BeanUtils;
3634
import org.springframework.beans.factory.BeanFactoryUtils;
3735
import org.springframework.boot.actuate.endpoint.EndpointFilter;
@@ -41,6 +39,7 @@
4139
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
4240
import org.springframework.boot.actuate.endpoint.invoke.OperationInvokerAdvisor;
4341
import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
42+
import org.springframework.boot.util.LambdaSafe;
4443
import org.springframework.context.ApplicationContext;
4544
import org.springframework.core.ResolvableType;
4645
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -66,8 +65,6 @@
6665
public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O extends Operation>
6766
implements EndpointsSupplier<E> {
6867

69-
private static final Log logger = LogFactory.getLog(EndpointDiscoverer.class);
70-
7168
private final ApplicationContext applicationContext;
7269

7370
private final Collection<EndpointFilter<E>> filters;
@@ -313,23 +310,15 @@ private boolean isFilterMatch(EndpointFilter<E> filter, EndpointBean endpointBea
313310
return isFilterMatch(filter, getFilterEndpoint(endpointBean));
314311
}
315312

313+
@SuppressWarnings("unchecked")
316314
private boolean isFilterMatch(EndpointFilter<E> filter, E endpoint) {
317-
try {
318-
return filter.match(endpoint);
319-
}
320-
catch (ClassCastException ex) {
321-
String msg = ex.getMessage();
322-
if (msg == null || msg.startsWith(endpoint.getClass().getName())) {
323-
// Possibly a lambda-defined EndpointFilter which we could not resolve the
324-
// generic EndpointInfo type for
325-
if (logger.isDebugEnabled()) {
326-
logger.debug("Non-matching Endpoint for EndpointFilter: " + filter,
327-
ex);
328-
}
329-
return false;
330-
}
331-
throw ex;
332-
}
315+
return LambdaSafe.callback(EndpointFilter.class, filter, endpoint)
316+
.withLogger(EndpointDiscoverer.class).invokeAnd((f) -> f.match(endpoint))
317+
.get();
318+
}
319+
320+
public <A, B> void doIt(Function<A, B> x) {
321+
333322
}
334323

335324
private E getFilterEndpoint(EndpointBean endpointBean) {

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/cache/CacheMetricsRegistrar.java

+10-39
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
package org.springframework.boot.actuate.metrics.cache;
1818

1919
import java.util.Collection;
20+
import java.util.Objects;
2021

2122
import io.micrometer.core.instrument.MeterRegistry;
2223
import io.micrometer.core.instrument.Tag;
2324
import io.micrometer.core.instrument.Tags;
2425
import io.micrometer.core.instrument.binder.MeterBinder;
25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
2726

27+
import org.springframework.boot.util.LambdaSafe;
2828
import org.springframework.cache.Cache;
29-
import org.springframework.core.ResolvableType;
3029

3130
/**
3231
* Register supported {@link Cache} to a {@link MeterRegistry}.
@@ -36,8 +35,6 @@
3635
*/
3736
public class CacheMetricsRegistrar {
3837

39-
private static final Log logger = LogFactory.getLog(CacheMetricsRegistrar.class);
40-
4138
private final MeterRegistry registry;
4239

4340
private final String metricName;
@@ -74,41 +71,15 @@ public boolean bindCacheToRegistry(Cache cache, Tag... tags) {
7471
return false;
7572
}
7673

77-
@SuppressWarnings({ "unchecked", "rawtypes" })
74+
@SuppressWarnings({ "unchecked" })
7875
private MeterBinder getMeterBinder(Cache cache, Tags tags) {
79-
tags = tags.and(getAdditionalTags(cache));
80-
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
81-
Class<?> cacheType = ResolvableType
82-
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
83-
.resolveGeneric();
84-
if (cacheType.isInstance(cache)) {
85-
try {
86-
MeterBinder meterBinder = ((CacheMeterBinderProvider) binderProvider)
87-
.getMeterBinder(cache, this.metricName, tags);
88-
if (meterBinder != null) {
89-
return meterBinder;
90-
}
91-
}
92-
catch (ClassCastException ex) {
93-
String msg = ex.getMessage();
94-
if (msg == null || msg.startsWith(cache.getClass().getName())) {
95-
// Possibly a lambda-defined CacheMeterBinderProvider which we
96-
// could not resolve the generic Cache type for
97-
if (logger.isDebugEnabled()) {
98-
logger.debug(
99-
"Non-matching Cache type for CacheMeterBinderProvider: "
100-
+ binderProvider,
101-
ex);
102-
}
103-
}
104-
else {
105-
throw ex;
106-
}
107-
}
108-
}
109-
110-
}
111-
return null;
76+
Tags cacheTags = tags.and(getAdditionalTags(cache));
77+
return LambdaSafe
78+
.callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache)
79+
.withLogger(CacheMetricsRegistrar.class)
80+
.invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache,
81+
this.metricName, cacheTags))
82+
.filter(Objects::nonNull).findFirst().orElse(null);
11283
}
11384

11485
/**

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java

+5-30
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
25-
23+
import org.springframework.boot.util.LambdaSafe;
2624
import org.springframework.cache.CacheManager;
27-
import org.springframework.core.ResolvableType;
2825

2926
/**
3027
* Invokes the available {@link CacheManagerCustomizer} instances in the context for a
@@ -35,8 +32,6 @@
3532
*/
3633
public class CacheManagerCustomizers {
3734

38-
private static final Log logger = LogFactory.getLog(CacheManagerCustomizers.class);
39-
4035
private final List<CacheManagerCustomizer<?>> customizers;
4136

4237
public CacheManagerCustomizers(
@@ -53,32 +48,12 @@ public CacheManagerCustomizers(
5348
* @param cacheManager the cache manager to customize
5449
* @return the cache manager
5550
*/
51+
@SuppressWarnings("unchecked")
5652
public <T extends CacheManager> T customize(T cacheManager) {
57-
for (CacheManagerCustomizer<?> customizer : this.customizers) {
58-
Class<?> generic = ResolvableType
59-
.forClass(CacheManagerCustomizer.class, customizer.getClass())
60-
.resolveGeneric();
61-
if (generic.isInstance(cacheManager)) {
62-
customize(cacheManager, customizer);
63-
}
64-
}
53+
LambdaSafe.callbacks(CacheManagerCustomizer.class, this.customizers, cacheManager)
54+
.withLogger(CacheManagerCustomizers.class)
55+
.invoke((customizer) -> customizer.customize(cacheManager));
6556
return cacheManager;
6657
}
6758

68-
@SuppressWarnings({ "unchecked", "rawtypes" })
69-
private void customize(CacheManager cacheManager, CacheManagerCustomizer customizer) {
70-
try {
71-
customizer.customize(cacheManager);
72-
}
73-
catch (ClassCastException ex) {
74-
// Possibly a lambda-defined customizer which we could not resolve the generic
75-
// cache manager type for
76-
if (logger.isDebugEnabled()) {
77-
logger.debug(
78-
"Non-matching cache manager type for customizer: " + customizer,
79-
ex);
80-
}
81-
}
82-
}
83-
8459
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/transaction/TransactionManagerCustomizers.java

+10-35
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collection;
21+
import java.util.Collections;
2122
import java.util.List;
2223

23-
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
25-
26-
import org.springframework.core.ResolvableType;
24+
import org.springframework.boot.util.LambdaSafe;
2725
import org.springframework.transaction.PlatformTransactionManager;
2826

2927
/**
@@ -34,44 +32,21 @@
3432
*/
3533
public class TransactionManagerCustomizers {
3634

37-
private static final Log logger = LogFactory
38-
.getLog(TransactionManagerCustomizers.class);
39-
4035
private final List<PlatformTransactionManagerCustomizer<?>> customizers;
4136

4237
public TransactionManagerCustomizers(
4338
Collection<? extends PlatformTransactionManagerCustomizer<?>> customizers) {
44-
this.customizers = (customizers == null ? null : new ArrayList<>(customizers));
39+
this.customizers = (customizers == null ? Collections.emptyList()
40+
: new ArrayList<>(customizers));
4541
}
4642

43+
@SuppressWarnings("unchecked")
4744
public void customize(PlatformTransactionManager transactionManager) {
48-
if (this.customizers != null) {
49-
for (PlatformTransactionManagerCustomizer<?> customizer : this.customizers) {
50-
Class<?> generic = ResolvableType
51-
.forClass(PlatformTransactionManagerCustomizer.class,
52-
customizer.getClass())
53-
.resolveGeneric();
54-
if (generic.isInstance(transactionManager)) {
55-
customize(transactionManager, customizer);
56-
}
57-
}
58-
}
59-
}
60-
61-
@SuppressWarnings({ "unchecked", "rawtypes" })
62-
private void customize(PlatformTransactionManager transactionManager,
63-
PlatformTransactionManagerCustomizer customizer) {
64-
try {
65-
customizer.customize(transactionManager);
66-
}
67-
catch (ClassCastException ex) {
68-
// Possibly a lambda-defined customizer which we could not resolve the generic
69-
// transaction manager type for
70-
if (logger.isDebugEnabled()) {
71-
logger.debug("Non-matching transaction manager type for customizer: "
72-
+ customizer, ex);
73-
}
74-
}
45+
LambdaSafe
46+
.callbacks(PlatformTransactionManagerCustomizer.class, this.customizers,
47+
transactionManager)
48+
.withLogger(TransactionManagerCustomizers.class)
49+
.invoke((customizer) -> customizer.customize(transactionManager));
7550
}
7651

7752
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/WebServerFactoryCustomizerBeanPostProcessor.java

+8-46
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import java.util.Collections;
2222
import java.util.List;
2323

24-
import org.apache.commons.logging.Log;
25-
import org.apache.commons.logging.LogFactory;
26-
2724
import org.springframework.beans.BeansException;
2825
import org.springframework.beans.factory.BeanFactory;
2926
import org.springframework.beans.factory.BeanFactoryAware;
3027
import org.springframework.beans.factory.ListableBeanFactory;
3128
import org.springframework.beans.factory.config.BeanPostProcessor;
32-
import org.springframework.core.ResolvableType;
29+
import org.springframework.boot.util.LambdaSafe;
3330
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3431
import org.springframework.util.Assert;
3532

@@ -45,9 +42,6 @@
4542
public class WebServerFactoryCustomizerBeanPostProcessor
4643
implements BeanPostProcessor, BeanFactoryAware {
4744

48-
private static final Log logger = LogFactory
49-
.getLog(WebServerFactoryCustomizerBeanPostProcessor.class);
50-
5145
private ListableBeanFactory beanFactory;
5246

5347
private List<WebServerFactoryCustomizer<?>> customizers;
@@ -75,45 +69,13 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
7569
return bean;
7670
}
7771

78-
private void postProcessBeforeInitialization(WebServerFactory bean) {
79-
for (WebServerFactoryCustomizer<?> customizer : getCustomizers()) {
80-
Class<?> type = ResolvableType
81-
.forClass(WebServerFactoryCustomizer.class, customizer.getClass())
82-
.getGeneric().resolve(WebServerFactory.class);
83-
if (type.isInstance(bean)) {
84-
invokeCustomizer(customizer, bean);
85-
}
86-
}
87-
}
88-
89-
@SuppressWarnings({ "rawtypes", "unchecked" })
90-
private void invokeCustomizer(WebServerFactoryCustomizer customizer,
91-
WebServerFactory webServerFactory) {
92-
try {
93-
customizer.customize(webServerFactory);
94-
}
95-
catch (ClassCastException ex) {
96-
String msg = ex.getMessage();
97-
if (msg == null || msg.startsWith(webServerFactory.getClass().getName())) {
98-
// Possibly a lambda-defined WebServerFactoryCustomizer which we could not
99-
// resolve the
100-
// generic WebServerFactory type for
101-
logLambdaDebug(customizer, ex);
102-
}
103-
else {
104-
throw ex;
105-
}
106-
}
107-
}
108-
109-
private void logLambdaDebug(WebServerFactoryCustomizer<?> customizer,
110-
ClassCastException ex) {
111-
if (logger.isDebugEnabled()) {
112-
logger.debug(
113-
"Non-matching WebServerFactory type for WebServerFactoryCustomizer: "
114-
+ customizer,
115-
ex);
116-
}
72+
@SuppressWarnings("unchecked")
73+
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
74+
LambdaSafe
75+
.callbacks(WebServerFactoryCustomizer.class, getCustomizers(),
76+
webServerFactory)
77+
.withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
78+
.invoke((customizer) -> customizer.customize(webServerFactory));
11779
}
11880

11981
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {

0 commit comments

Comments
 (0)