001/* 002 * Copyright 2002-2020 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.ehcache; 018 019import java.util.Collection; 020import java.util.LinkedHashSet; 021 022import net.sf.ehcache.Ehcache; 023import net.sf.ehcache.Status; 024 025import org.springframework.cache.Cache; 026import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager; 027 028/** 029 * CacheManager backed by an EhCache {@link net.sf.ehcache.CacheManager}. 030 * 031 * @author Costin Leau 032 * @author Juergen Hoeller 033 * @author Stephane Nicoll 034 * @since 3.1 035 * @see EhCacheCache 036 */ 037public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager { 038 039 private net.sf.ehcache.CacheManager cacheManager; 040 041 042 /** 043 * Create a new EhCacheCacheManager, setting the target EhCache CacheManager 044 * through the {@link #setCacheManager} bean property. 045 */ 046 public EhCacheCacheManager() { 047 } 048 049 /** 050 * Create a new EhCacheCacheManager for the given backing EhCache CacheManager. 051 * @param cacheManager the backing EhCache {@link net.sf.ehcache.CacheManager} 052 */ 053 public EhCacheCacheManager(net.sf.ehcache.CacheManager cacheManager) { 054 this.cacheManager = cacheManager; 055 } 056 057 058 /** 059 * Set the backing EhCache {@link net.sf.ehcache.CacheManager}. 060 */ 061 public void setCacheManager(net.sf.ehcache.CacheManager cacheManager) { 062 this.cacheManager = cacheManager; 063 } 064 065 /** 066 * Return the backing EhCache {@link net.sf.ehcache.CacheManager}. 067 */ 068 public net.sf.ehcache.CacheManager getCacheManager() { 069 return this.cacheManager; 070 } 071 072 @Override 073 public void afterPropertiesSet() { 074 if (getCacheManager() == null) { 075 setCacheManager(EhCacheManagerUtils.buildCacheManager()); 076 } 077 super.afterPropertiesSet(); 078 } 079 080 081 @Override 082 protected Collection<Cache> loadCaches() { 083 Status status = getCacheManager().getStatus(); 084 if (!Status.STATUS_ALIVE.equals(status)) { 085 throw new IllegalStateException( 086 "An 'alive' EhCache CacheManager is required - current cache is " + status.toString()); 087 } 088 089 String[] names = getCacheManager().getCacheNames(); 090 Collection<Cache> caches = new LinkedHashSet<Cache>(names.length); 091 for (String name : names) { 092 caches.add(new EhCacheCache(getCacheManager().getEhcache(name))); 093 } 094 return caches; 095 } 096 097 @Override 098 protected Cache getMissingCache(String name) { 099 // Check the EhCache cache again (in case the cache was added at runtime) 100 Ehcache ehcache = getCacheManager().getEhcache(name); 101 if (ehcache != null) { 102 return new EhCacheCache(ehcache); 103 } 104 return null; 105 } 106 107}