001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.cache; 018 019import java.util.ArrayList; 020import java.util.Collections; 021import java.util.List; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025 026import org.springframework.cache.CacheManager; 027import org.springframework.core.ResolvableType; 028 029/** 030 * Invokes the available {@link CacheManagerCustomizer} instances in the context for a 031 * given {@link CacheManager}. 032 * 033 * @author Stephane Nicoll 034 * @since 1.5.0 035 */ 036public class CacheManagerCustomizers { 037 038 private static final Log logger = LogFactory.getLog(CacheManagerCustomizers.class); 039 040 private final List<CacheManagerCustomizer<?>> customizers; 041 042 public CacheManagerCustomizers( 043 List<? extends CacheManagerCustomizer<?>> customizers) { 044 this.customizers = (customizers != null 045 ? new ArrayList<CacheManagerCustomizer<?>>(customizers) 046 : Collections.<CacheManagerCustomizer<?>>emptyList()); 047 } 048 049 /** 050 * Customize the specified {@link CacheManager}. Locates all 051 * {@link CacheManagerCustomizer} beans able to handle the specified instance and 052 * invoke {@link CacheManagerCustomizer#customize(CacheManager)} on them. 053 * @param <T> the type of cache manager 054 * @param cacheManager the cache manager to customize 055 * @return the cache manager 056 */ 057 public <T extends CacheManager> T customize(T cacheManager) { 058 for (CacheManagerCustomizer<?> customizer : this.customizers) { 059 Class<?> generic = ResolvableType 060 .forClass(CacheManagerCustomizer.class, customizer.getClass()) 061 .resolveGeneric(); 062 if (generic.isInstance(cacheManager)) { 063 customize(cacheManager, customizer); 064 } 065 } 066 return cacheManager; 067 } 068 069 @SuppressWarnings({ "unchecked", "rawtypes" }) 070 private void customize(CacheManager cacheManager, CacheManagerCustomizer customizer) { 071 try { 072 customizer.customize(cacheManager); 073 } 074 catch (ClassCastException ex) { 075 // Possibly a lambda-defined customizer which we could not resolve the generic 076 // cache manager type for 077 if (logger.isDebugEnabled()) { 078 logger.debug( 079 "Non-matching cache manager type for customizer: " + customizer, 080 ex); 081 } 082 } 083 } 084 085}