KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > spring > CacheFactoryBean


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius.spring;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.servlet.ServletContext JavaDoc;
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 /**
38  * Factory bean that creates a new {@link Cache Cache} instance in the
39  * specified directory.
40  */

41 public class CacheFactoryBean extends AbstractFactoryBean
42         implements ServletContextAware {
43
44     public static final int DEFAULT_CAPACITY = 10000;
45
46     public static final String JavaDoc DEFAULT_CACHE_DIR_NAME = "cache";
47
48     private int capacity = DEFAULT_CAPACITY;
49
50     private File JavaDoc cacheDir;
51
52     private String JavaDoc cacheDirName = DEFAULT_CACHE_DIR_NAME;
53
54     private ServletContext JavaDoc servletContext;
55
56     private boolean restore = true;
57
58     public void setCacheDirName(String JavaDoc cacheDirName) {
59         this.cacheDirName = cacheDirName;
60     }
61
62     /**
63      * @throws IOException if the resource cannot be resolved as absolute
64      * file path, i.e. if the resource is not available in a file system.
65      */

66     public void setCacheDir(Resource cacheDir) throws IOException JavaDoc {
67         this.cacheDir = cacheDir.getFile();
68     }
69
70     /**
71      * Sets the capacity of the Cache. If not set, the capacity will default
72      * to <code>DEFAULT_CAPACITY</code> (10000).
73      */

74     public void setCapacity(int capacity) {
75         this.capacity = capacity;
76     }
77
78     /**
79      * Sets whether the factory should try to restore a previously persisted
80      * version of the cache. Default is <code>true</code>.
81      */

82     public void setRestore(boolean restore) {
83         this.restore = restore;
84     }
85
86     public void setServletContext(ServletContext JavaDoc servletContext) {
87         this.servletContext = servletContext;
88     }
89
90     /**
91      * Returns <code>Cache.class</code>.
92      */

93     public Class JavaDoc getObjectType() {
94         return Cache.class;
95     }
96
97     protected Object JavaDoc createInstance() throws Exception JavaDoc {
98         if (cacheDir == null) {
99             File JavaDoc tempDir = WebUtils.getTempDir(servletContext);
100             cacheDir = new File JavaDoc(tempDir, cacheDirName);
101         }
102         return Cache.newInstance(capacity, cacheDir, restore);
103     }
104
105 }
106
Popular Tags