1 24 package org.riotfamily.cachius.spring; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 29 import javax.servlet.ServletContext ; 30 31 import org.riotfamily.cachius.Cache; 32 import org.springframework.beans.factory.config.AbstractFactoryBean; 33 import org.springframework.core.io.Resource; 34 import org.springframework.web.context.ServletContextAware; 35 import org.springframework.web.util.WebUtils; 36 37 41 public class CacheFactoryBean extends AbstractFactoryBean 42 implements ServletContextAware { 43 44 public static final int DEFAULT_CAPACITY = 10000; 45 46 public static final String DEFAULT_CACHE_DIR_NAME = "cache"; 47 48 private int capacity = DEFAULT_CAPACITY; 49 50 private File cacheDir; 51 52 private String cacheDirName = DEFAULT_CACHE_DIR_NAME; 53 54 private ServletContext servletContext; 55 56 private boolean restore = true; 57 58 public void setCacheDirName(String cacheDirName) { 59 this.cacheDirName = cacheDirName; 60 } 61 62 66 public void setCacheDir(Resource cacheDir) throws IOException { 67 this.cacheDir = cacheDir.getFile(); 68 } 69 70 74 public void setCapacity(int capacity) { 75 this.capacity = capacity; 76 } 77 78 82 public void setRestore(boolean restore) { 83 this.restore = restore; 84 } 85 86 public void setServletContext(ServletContext servletContext) { 87 this.servletContext = servletContext; 88 } 89 90 93 public Class getObjectType() { 94 return Cache.class; 95 } 96 97 protected Object createInstance() throws Exception { 98 if (cacheDir == null) { 99 File tempDir = WebUtils.getTempDir(servletContext); 100 cacheDir = new File (tempDir, cacheDirName); 101 } 102 return Cache.newInstance(capacity, cacheDir, restore); 103 } 104 105 } 106 | Popular Tags |