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