1 package org.shiftone.cache.util; 2 3 4 5 import org.shiftone.cache.Cache; 6 import org.shiftone.cache.CacheException; 7 import org.shiftone.cache.CacheFactory; 8 import org.shiftone.cache.policy.zero.ZeroCacheFactory; 9 10 11 15 public abstract class AbstractDecoratorCacheFactory implements CacheFactory 16 { 17 18 private static final Log LOG = new Log(AbstractDecoratorCacheFactory.class); 19 private CacheFactory delegate; 20 21 public Cache newInstance(String cacheName, long timeoutMs, int maxSize) 22 { 23 24 CacheFactory factory = getDelegate(); 25 Cache cache = null; 26 27 try 28 { 29 cache = factory.newInstance(cacheName, timeoutMs, maxSize); 30 31 if (factory != null) 32 { 33 cache = wrapDelegate(cacheName, cache); 34 } 35 } 36 catch (Exception e) 37 { 38 LOG.error("unable to create cache decorator : " + cacheName, e); 39 } 40 41 if (cache == null) 42 { 43 LOG.info("returning null cache for : " + cacheName); 44 45 cache = ZeroCacheFactory.NULL_CACHE; 46 } 47 48 return cache; 49 } 50 51 52 protected abstract Cache wrapDelegate(String cacheName, Cache delegateCache) throws CacheException; 53 54 55 public CacheFactory getDelegate() 56 { 57 return delegate; 58 } 59 60 61 public void setDelegate(CacheFactory delegate) 62 { 63 this.delegate = delegate; 64 } 65 66 67 public String toString() 68 { 69 return getClass().getName() + "[" + delegate + "]"; 70 } 71 } 72 | Popular Tags |