001/* 002 * Copyright 2002-2018 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 * https://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.cache.annotation; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.context.annotation.AdviceMode; 023import org.springframework.context.annotation.AdviceModeImportSelector; 024import org.springframework.context.annotation.AutoProxyRegistrar; 025import org.springframework.util.ClassUtils; 026import org.springframework.util.StringUtils; 027 028/** 029 * Selects which implementation of {@link AbstractCachingConfiguration} should 030 * be used based on the value of {@link EnableCaching#mode} on the importing 031 * {@code @Configuration} class. 032 * 033 * <p>Detects the presence of JSR-107 and enables JCache support accordingly. 034 * 035 * @author Chris Beams 036 * @author Stephane Nicoll 037 * @since 3.1 038 * @see EnableCaching 039 * @see ProxyCachingConfiguration 040 */ 041public class CachingConfigurationSelector extends AdviceModeImportSelector<EnableCaching> { 042 043 private static final String PROXY_JCACHE_CONFIGURATION_CLASS = 044 "org.springframework.cache.jcache.config.ProxyJCacheConfiguration"; 045 046 private static final String CACHE_ASPECT_CONFIGURATION_CLASS_NAME = 047 "org.springframework.cache.aspectj.AspectJCachingConfiguration"; 048 049 private static final String JCACHE_ASPECT_CONFIGURATION_CLASS_NAME = 050 "org.springframework.cache.aspectj.AspectJJCacheConfiguration"; 051 052 053 private static final boolean jsr107Present = ClassUtils.isPresent( 054 "javax.cache.Cache", CachingConfigurationSelector.class.getClassLoader()); 055 056 private static final boolean jcacheImplPresent = ClassUtils.isPresent( 057 PROXY_JCACHE_CONFIGURATION_CLASS, CachingConfigurationSelector.class.getClassLoader()); 058 059 060 /** 061 * Returns {@link ProxyCachingConfiguration} or {@code AspectJCachingConfiguration} 062 * for {@code PROXY} and {@code ASPECTJ} values of {@link EnableCaching#mode()}, 063 * respectively. Potentially includes corresponding JCache configuration as well. 064 */ 065 @Override 066 public String[] selectImports(AdviceMode adviceMode) { 067 switch (adviceMode) { 068 case PROXY: 069 return getProxyImports(); 070 case ASPECTJ: 071 return getAspectJImports(); 072 default: 073 return null; 074 } 075 } 076 077 /** 078 * Return the imports to use if the {@link AdviceMode} is set to {@link AdviceMode#PROXY}. 079 * <p>Take care of adding the necessary JSR-107 import if it is available. 080 */ 081 private String[] getProxyImports() { 082 List<String> result = new ArrayList<String>(3); 083 result.add(AutoProxyRegistrar.class.getName()); 084 result.add(ProxyCachingConfiguration.class.getName()); 085 if (jsr107Present && jcacheImplPresent) { 086 result.add(PROXY_JCACHE_CONFIGURATION_CLASS); 087 } 088 return StringUtils.toStringArray(result); 089 } 090 091 /** 092 * Return the imports to use if the {@link AdviceMode} is set to {@link AdviceMode#ASPECTJ}. 093 * <p>Take care of adding the necessary JSR-107 import if it is available. 094 */ 095 private String[] getAspectJImports() { 096 List<String> result = new ArrayList<String>(2); 097 result.add(CACHE_ASPECT_CONFIGURATION_CLASS_NAME); 098 if (jsr107Present && jcacheImplPresent) { 099 result.add(JCACHE_ASPECT_CONFIGURATION_CLASS_NAME); 100 } 101 return StringUtils.toStringArray(result); 102 } 103 104}