001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.cachius.spring;
025:
026: import java.io.File;
027:
028: import javax.servlet.ServletContext;
029:
030: import org.riotfamily.cachius.Cache;
031: import org.riotfamily.cachius.CacheFactory;
032: import org.springframework.beans.factory.BeanNameAware;
033: import org.springframework.beans.factory.DisposableBean;
034: import org.springframework.beans.factory.FactoryBean;
035: import org.springframework.beans.factory.InitializingBean;
036: import org.springframework.web.context.ServletContextAware;
037: import org.springframework.web.util.WebUtils;
038:
039: /**
040: * Factory that creates a new {@link Cache Cache} instance in the
041: * specified directory.
042: */
043: public class CacheFactoryBean extends CacheFactory implements
044: FactoryBean, ServletContextAware, BeanNameAware,
045: InitializingBean, DisposableBean {
046:
047: private String beanName;
048:
049: private String cacheDirName;
050:
051: private ServletContext servletContext;
052:
053: private Cache cache;
054:
055: public void setBeanName(String name) {
056: this .beanName = name;
057: }
058:
059: public void setCacheDirName(String cacheDirName) {
060: this .cacheDirName = cacheDirName;
061: }
062:
063: public void setServletContext(ServletContext servletContext) {
064: this .servletContext = servletContext;
065: }
066:
067: public void afterPropertiesSet() throws Exception {
068: if (getCacheDir() == null) {
069: if (cacheDirName == null) {
070: cacheDirName = beanName;
071: }
072: setCacheDir(new File(WebUtils.getTempDir(servletContext),
073: cacheDirName));
074: }
075:
076: }
077:
078: public Class getObjectType() {
079: return Cache.class;
080: }
081:
082: public boolean isSingleton() {
083: return true;
084: }
085:
086: public Object getObject() throws Exception {
087: if (cache == null) {
088: cache = createInstance();
089: }
090: return cache;
091: }
092:
093: public void destroy() throws Exception {
094: if (cache != null) {
095: cache.shutdown();
096: if (isRestore()) {
097: persist(cache);
098: }
099: }
100: }
101:
102: }
|