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) 2007
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;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.io.InvalidClassException;
031: import java.io.ObjectInputStream;
032: import java.io.ObjectOutputStream;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: /**
038: * @author Felix Gnass [fgnass at neteye dot de]
039: * @since 6.5
040: */
041: public class CacheFactory {
042:
043: public static final int DEFAULT_CAPACITY = 10000;
044:
045: public static final String DEFAULT_CACHE_DIR_NAME = "cache";
046:
047: private static final String CACHE_FILE = "cache-info";
048:
049: private static Log log = LogFactory.getLog(CacheFactory.class);
050:
051: private int capacity = DEFAULT_CAPACITY;
052:
053: private File cacheDir;
054:
055: private boolean restore = true;
056:
057: private boolean enabled = true;
058:
059: /**
060: * Sets the capacity of the Cache. If not set, the capacity will default
061: * to <code>DEFAULT_CAPACITY</code> (10000).
062: */
063: public void setCapacity(int capacity) {
064: this .capacity = capacity;
065: }
066:
067: public void setCacheDir(File cacheDir) {
068: this .cacheDir = cacheDir;
069: }
070:
071: protected File getCacheDir() {
072: return this .cacheDir;
073: }
074:
075: /**
076: * Sets whether the factory should try to restore a previously persisted
077: * version of the cache. Default is <code>true</code>.
078: */
079: public void setRestore(boolean restore) {
080: this .restore = restore;
081: }
082:
083: public boolean isRestore() {
084: return this .restore;
085: }
086:
087: public void setEnabled(boolean enabled) {
088: this .enabled = enabled;
089: }
090:
091: public Cache createInstance() throws Exception {
092: Cache cache = null;
093: if (!cacheDir.exists() && !cacheDir.mkdirs()) {
094: throw new IOException("Can't create cache directory: "
095: + cacheDir);
096: }
097: File f = new File(cacheDir, CACHE_FILE);
098: if (restore && f.exists()) {
099: log.info("Trying to build cache from file: " + f);
100: cache = deserialize(f);
101: }
102:
103: if (cache == null) {
104: cache = new Cache(cacheDir, capacity, enabled);
105: } else {
106: cache.setCacheDir(cacheDir);
107: cache.setCapacity(capacity);
108: cache.setEnabled(enabled);
109: }
110: return cache;
111: }
112:
113: protected void persist(Cache cache) {
114: File f = new File(cacheDir, CACHE_FILE);
115: if (!f.exists()) {
116: try {
117: log.info("Persisting the cache state ...");
118: ObjectOutputStream out = new ObjectOutputStream(
119: new FileOutputStream(f));
120:
121: out.writeObject(cache);
122: out.close();
123: log.info("Cache state saved in " + f);
124: } catch (IOException e) {
125: log.error("Can't save cache state", e);
126: }
127: }
128: }
129:
130: protected Cache deserialize(File f) {
131: try {
132: ObjectInputStream in = new ObjectInputStream(
133: new FileInputStream(f));
134:
135: Cache cache = (Cache) in.readObject();
136: in.close();
137: log.info("Cache has been successfully deserialized.");
138:
139: return cache;
140: } catch (InvalidClassException e) {
141: log.info("Serialized cache has been discarded due to "
142: + "version incompatibilies.");
143: } catch (IOException e) {
144: log.warn("Deserialization failed.");
145: } catch (ClassNotFoundException e) {
146: log.warn("Deserialization failed.", e);
147: } finally {
148: f.delete();
149: }
150: return null;
151: }
152: }
|