Migrate callbacks to LambdaSafe util
Migrate existing code to the new `LambaSafe` callback handler. Closes gh-11584
This commit is contained in:
parent
b0cb728944
commit
3a12f98bab
|
@ -26,12 +26,10 @@ import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
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.BeanUtils;
|
||||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.boot.actuate.endpoint.EndpointFilter;
|
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.OperationInvoker;
|
||||||
import org.springframework.boot.actuate.endpoint.invoke.OperationInvokerAdvisor;
|
import org.springframework.boot.actuate.endpoint.invoke.OperationInvokerAdvisor;
|
||||||
import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
|
import org.springframework.boot.actuate.endpoint.invoke.ParameterValueMapper;
|
||||||
|
import org.springframework.boot.util.LambdaSafe;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
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>
|
public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O extends Operation>
|
||||||
implements EndpointsSupplier<E> {
|
implements EndpointsSupplier<E> {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(EndpointDiscoverer.class);
|
|
||||||
|
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
private final Collection<EndpointFilter<E>> filters;
|
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));
|
return isFilterMatch(filter, getFilterEndpoint(endpointBean));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private boolean isFilterMatch(EndpointFilter<E> filter, E endpoint) {
|
private boolean isFilterMatch(EndpointFilter<E> filter, E endpoint) {
|
||||||
try {
|
return LambdaSafe.callback(EndpointFilter.class, filter, endpoint)
|
||||||
return filter.match(endpoint);
|
.withLogger(EndpointDiscoverer.class).invokeAnd((f) -> f.match(endpoint))
|
||||||
}
|
.get();
|
||||||
catch (ClassCastException ex) {
|
}
|
||||||
String msg = ex.getMessage();
|
|
||||||
if (msg == null || msg.startsWith(endpoint.getClass().getName())) {
|
public <A, B> void doIt(Function<A, B> x) {
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private E getFilterEndpoint(EndpointBean endpointBean) {
|
private E getFilterEndpoint(EndpointBean endpointBean) {
|
||||||
|
|
|
@ -17,16 +17,15 @@
|
||||||
package org.springframework.boot.actuate.metrics.cache;
|
package org.springframework.boot.actuate.metrics.cache;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Tag;
|
import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.Tags;
|
import io.micrometer.core.instrument.Tags;
|
||||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
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.cache.Cache;
|
||||||
import org.springframework.core.ResolvableType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register supported {@link Cache} to a {@link MeterRegistry}.
|
* Register supported {@link Cache} to a {@link MeterRegistry}.
|
||||||
|
@ -36,8 +35,6 @@ import org.springframework.core.ResolvableType;
|
||||||
*/
|
*/
|
||||||
public class CacheMetricsRegistrar {
|
public class CacheMetricsRegistrar {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(CacheMetricsRegistrar.class);
|
|
||||||
|
|
||||||
private final MeterRegistry registry;
|
private final MeterRegistry registry;
|
||||||
|
|
||||||
private final String metricName;
|
private final String metricName;
|
||||||
|
@ -74,41 +71,15 @@ public class CacheMetricsRegistrar {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked" })
|
||||||
private MeterBinder getMeterBinder(Cache cache, Tags tags) {
|
private MeterBinder getMeterBinder(Cache cache, Tags tags) {
|
||||||
tags = tags.and(getAdditionalTags(cache));
|
Tags cacheTags = tags.and(getAdditionalTags(cache));
|
||||||
for (CacheMeterBinderProvider<?> binderProvider : this.binderProviders) {
|
return LambdaSafe
|
||||||
Class<?> cacheType = ResolvableType
|
.callbacks(CacheMeterBinderProvider.class, this.binderProviders, cache)
|
||||||
.forClass(CacheMeterBinderProvider.class, binderProvider.getClass())
|
.withLogger(CacheMetricsRegistrar.class)
|
||||||
.resolveGeneric();
|
.invokeAnd((binderProvider) -> binderProvider.getMeterBinder(cache,
|
||||||
if (cacheType.isInstance(cache)) {
|
this.metricName, cacheTags))
|
||||||
try {
|
.filter(Objects::nonNull).findFirst().orElse(null);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -20,11 +20,8 @@ import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.springframework.boot.util.LambdaSafe;
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.core.ResolvableType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Invokes the available {@link CacheManagerCustomizer} instances in the context for a
|
* Invokes the available {@link CacheManagerCustomizer} instances in the context for a
|
||||||
|
@ -35,8 +32,6 @@ import org.springframework.core.ResolvableType;
|
||||||
*/
|
*/
|
||||||
public class CacheManagerCustomizers {
|
public class CacheManagerCustomizers {
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(CacheManagerCustomizers.class);
|
|
||||||
|
|
||||||
private final List<CacheManagerCustomizer<?>> customizers;
|
private final List<CacheManagerCustomizer<?>> customizers;
|
||||||
|
|
||||||
public CacheManagerCustomizers(
|
public CacheManagerCustomizers(
|
||||||
|
@ -53,32 +48,12 @@ public class CacheManagerCustomizers {
|
||||||
* @param cacheManager the cache manager to customize
|
* @param cacheManager the cache manager to customize
|
||||||
* @return the cache manager
|
* @return the cache manager
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends CacheManager> T customize(T cacheManager) {
|
public <T extends CacheManager> T customize(T cacheManager) {
|
||||||
for (CacheManagerCustomizer<?> customizer : this.customizers) {
|
LambdaSafe.callbacks(CacheManagerCustomizer.class, this.customizers, cacheManager)
|
||||||
Class<?> generic = ResolvableType
|
.withLogger(CacheManagerCustomizers.class)
|
||||||
.forClass(CacheManagerCustomizer.class, customizer.getClass())
|
.invoke((customizer) -> customizer.customize(cacheManager));
|
||||||
.resolveGeneric();
|
|
||||||
if (generic.isInstance(cacheManager)) {
|
|
||||||
customize(cacheManager, customizer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.transaction;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.springframework.boot.util.LambdaSafe;
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import org.springframework.core.ResolvableType;
|
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,44 +32,21 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
*/
|
*/
|
||||||
public class TransactionManagerCustomizers {
|
public class TransactionManagerCustomizers {
|
||||||
|
|
||||||
private static final Log logger = LogFactory
|
|
||||||
.getLog(TransactionManagerCustomizers.class);
|
|
||||||
|
|
||||||
private final List<PlatformTransactionManagerCustomizer<?>> customizers;
|
private final List<PlatformTransactionManagerCustomizer<?>> customizers;
|
||||||
|
|
||||||
public TransactionManagerCustomizers(
|
public TransactionManagerCustomizers(
|
||||||
Collection<? extends PlatformTransactionManagerCustomizer<?>> customizers) {
|
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) {
|
public void customize(PlatformTransactionManager transactionManager) {
|
||||||
if (this.customizers != null) {
|
LambdaSafe
|
||||||
for (PlatformTransactionManagerCustomizer<?> customizer : this.customizers) {
|
.callbacks(PlatformTransactionManagerCustomizer.class, this.customizers,
|
||||||
Class<?> generic = ResolvableType
|
transactionManager)
|
||||||
.forClass(PlatformTransactionManagerCustomizer.class,
|
.withLogger(TransactionManagerCustomizers.class)
|
||||||
customizer.getClass())
|
.invoke((customizer) -> customizer.customize(transactionManager));
|
||||||
.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,15 +21,12 @@ import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
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.BeansException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
import org.springframework.beans.factory.ListableBeanFactory;
|
import org.springframework.beans.factory.ListableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
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.core.annotation.AnnotationAwareOrderComparator;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
@ -45,9 +42,6 @@ import org.springframework.util.Assert;
|
||||||
public class WebServerFactoryCustomizerBeanPostProcessor
|
public class WebServerFactoryCustomizerBeanPostProcessor
|
||||||
implements BeanPostProcessor, BeanFactoryAware {
|
implements BeanPostProcessor, BeanFactoryAware {
|
||||||
|
|
||||||
private static final Log logger = LogFactory
|
|
||||||
.getLog(WebServerFactoryCustomizerBeanPostProcessor.class);
|
|
||||||
|
|
||||||
private ListableBeanFactory beanFactory;
|
private ListableBeanFactory beanFactory;
|
||||||
|
|
||||||
private List<WebServerFactoryCustomizer<?>> customizers;
|
private List<WebServerFactoryCustomizer<?>> customizers;
|
||||||
|
@ -75,45 +69,13 @@ public class WebServerFactoryCustomizerBeanPostProcessor
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void postProcessBeforeInitialization(WebServerFactory bean) {
|
@SuppressWarnings("unchecked")
|
||||||
for (WebServerFactoryCustomizer<?> customizer : getCustomizers()) {
|
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
|
||||||
Class<?> type = ResolvableType
|
LambdaSafe
|
||||||
.forClass(WebServerFactoryCustomizer.class, customizer.getClass())
|
.callbacks(WebServerFactoryCustomizer.class, getCustomizers(),
|
||||||
.getGeneric().resolve(WebServerFactory.class);
|
webServerFactory)
|
||||||
if (type.isInstance(bean)) {
|
.withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)
|
||||||
invokeCustomizer(customizer, bean);
|
.invoke((customizer) -> customizer.customize(webServerFactory));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
|
private Collection<WebServerFactoryCustomizer<?>> getCustomizers() {
|
||||||
|
|
Loading…
Reference in New Issue