1 16 17 package org.springframework.cache.ehcache; 18 19 import java.io.IOException ; 20 21 import net.sf.ehcache.CacheException; 22 import net.sf.ehcache.CacheManager; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.beans.factory.DisposableBean; 27 import org.springframework.beans.factory.FactoryBean; 28 import org.springframework.beans.factory.InitializingBean; 29 import org.springframework.core.io.Resource; 30 31 57 public class EhCacheManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 58 59 protected final Log logger = LogFactory.getLog(getClass()); 60 61 private Resource configLocation; 62 63 private boolean shared = false; 64 65 private CacheManager cacheManager; 66 67 68 73 public void setConfigLocation(Resource configLocation) { 74 this.configLocation = configLocation; 75 } 76 77 84 public void setShared(boolean shared) { 85 this.shared = shared; 86 } 87 88 89 public void afterPropertiesSet() throws IOException , CacheException { 90 logger.info("Initializing EHCache CacheManager"); 91 if (this.shared) { 92 if (this.configLocation != null) { 94 this.cacheManager = CacheManager.create(this.configLocation.getInputStream()); 95 } 96 else { 97 this.cacheManager = CacheManager.create(); 98 } 99 } 100 else { 101 if (this.configLocation != null) { 103 this.cacheManager = new CacheManager(this.configLocation.getInputStream()); 104 } 105 else { 106 this.cacheManager = new CacheManager(); 107 } 108 } 109 } 110 111 112 public Object getObject() { 113 return this.cacheManager; 114 } 115 116 public Class getObjectType() { 117 return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class); 118 } 119 120 public boolean isSingleton() { 121 return true; 122 } 123 124 125 public void destroy() { 126 logger.info("Shutting down EHCache CacheManager"); 127 this.cacheManager.shutdown(); 128 } 129 130 } 131 | Popular Tags |