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

@@ -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));
}
}