package org.springframework.cache.ehcache;
import java.util.Collection;
import java.util.LinkedHashSet;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Status;
import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager {
@Nullable
private net.sf.ehcache.CacheManager cacheManager;
public EhCacheCacheManager() {
}
public EhCacheCacheManager(net.sf.ehcache.CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
public void setCacheManager(@Nullable net.sf.ehcache.CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
@Nullable
public net.sf.ehcache.CacheManager getCacheManager() {
return this.cacheManager;
}
@Override
public void afterPropertiesSet() {
if (getCacheManager() == null) {
setCacheManager(EhCacheManagerUtils.buildCacheManager());
}
super.afterPropertiesSet();
}
@Override
protected Collection<Cache> loadCaches() {
net.sf.ehcache.CacheManager cacheManager = getCacheManager();
Assert.state(cacheManager != null, "No CacheManager set");
Status status = cacheManager.getStatus();
if (!Status.STATUS_ALIVE.equals(status)) {
throw new IllegalStateException(
"An 'alive' EhCache CacheManager is required - current cache is " + status.toString());
}
String[] names = getCacheManager().getCacheNames();
Collection<Cache> caches = new LinkedHashSet<>(names.length);
for (String name : names) {
caches.add(new EhCacheCache(getCacheManager().getEhcache(name)));
}
return caches;
}
@Override
protected Cache getMissingCache(String name) {
net.sf.ehcache.CacheManager cacheManager = getCacheManager();
Assert.state(cacheManager != null, "No CacheManager set");
Ehcache ehcache = cacheManager.getEhcache(name);
if (ehcache != null) {
return new EhCacheCache(ehcache);
}
return null;
}
}