001: /******************************************************************************
002: * Sony Online Entertainment
003: * Application Engineering
004: *
005: * Unpublished work Copyright 2005 Sony Online Entertainment Inc.
006: * All rights reserved.
007: * Created on Oct 11, 2005
008: ******************************************************************************/package net.jforum.cache;
009:
010: import java.io.Serializable;
011: import java.util.ArrayList;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import net.jforum.util.preferences.SystemGlobals;
017: import net.sf.ehcache.Cache;
018: import net.sf.ehcache.CacheException;
019: import net.sf.ehcache.CacheManager;
020: import net.sf.ehcache.Element;
021:
022: import org.apache.log4j.Logger;
023:
024: /**
025: * The rest of the application seems to make some invalid assumptions about how
026: * things are cached. Those assumptions might be benign, but it is hard to tell
027: * without deep testing. Until this is finishe the JBossCacheEngine should be
028: * configured in a local mode.
029: *
030: * Created on Oct 11, 2005
031: *
032: * @author Jake Fear
033: * @version $Id: EhCacheEngine.java,v 1.1 2005/10/14 00:15:54 rafaelsteil Exp $
034: */
035: public class EhCacheEngine implements CacheEngine {
036:
037: private static final Logger log = Logger
038: .getLogger(EhCacheEngine.class);
039:
040: private CacheManager manager;
041:
042: public void init() {
043: try {
044: manager = CacheManager.create(SystemGlobals
045: .getValue("ehcache.cache.properties"));
046: } catch (CacheException ce) {
047: log.error("EhCache could not be initialized", ce);
048: throw new RuntimeException(ce);
049: }
050: }
051:
052: public void stop() {
053: manager.shutdown();
054: }
055:
056: public void add(String key, Object value) {
057: if (log.isDebugEnabled()) {
058: log.debug("Caching " + value + " with key " + key);
059: }
060: add(DUMMY_FQN, key, value);
061: }
062:
063: public void add(String fullyQualifiedName, String key, Object value) {
064: if (!manager.cacheExists(fullyQualifiedName)) {
065: try {
066: manager.addCache(fullyQualifiedName);
067: } catch (CacheException ce) {
068: log.error(ce, ce);
069: throw new RuntimeException(ce);
070: }
071: }
072: Cache cache = manager.getCache(fullyQualifiedName);
073:
074: Element element = new Element(key, (Serializable) value);
075: cache.put(element);
076: }
077:
078: public Object get(String fullyQualifiedName, String key) {
079: try {
080: if (!manager.cacheExists(fullyQualifiedName)) {
081: manager.addCache(fullyQualifiedName);
082: return null;
083: }
084: Cache cache = manager.getCache(fullyQualifiedName);
085: Element element = cache.get(key);
086: if (element != null) {
087: return element.getValue();
088: }
089:
090: return null;
091: } catch (CacheException ce) {
092: log.error("EhCache could not be shutdown", ce);
093: throw new RuntimeException(ce);
094: }
095: }
096:
097: public Object get(String fullyQualifiedName) {
098: if (!manager.cacheExists(fullyQualifiedName)) {
099: try {
100: manager.addCache(fullyQualifiedName);
101: } catch (CacheException ce) {
102: log.error("EhCache could not be shutdown", ce);
103: throw new RuntimeException(ce);
104: }
105: }
106: Cache cache = manager.getCache(fullyQualifiedName);
107: return cache;
108: }
109:
110: public Collection getValues(String fullyQualifiedName) {
111: try {
112: if (!manager.cacheExists(fullyQualifiedName)) {
113: manager.addCache(fullyQualifiedName);
114: return new ArrayList();
115: }
116: Cache cache = manager.getCache(fullyQualifiedName);
117: List values = new ArrayList(cache.getSize());
118: List keys = cache.getKeys();
119:
120: for (Iterator iter = keys.iterator(); iter.hasNext();) {
121: values.add(cache.get((Serializable) iter.next()));
122: }
123:
124: return values;
125: } catch (CacheException ce) {
126: log.error("EhCache could not be shutdown", ce);
127: throw new RuntimeException(ce);
128: }
129: }
130:
131: public void remove(String fullyQualifiedName, String key) {
132: Cache cache = manager.getCache(fullyQualifiedName);
133:
134: if (cache != null) {
135: cache.remove(key);
136: }
137: }
138:
139: public void remove(String fullyQualifiedName) {
140: if (manager.cacheExists(fullyQualifiedName)) {
141: manager.removeCache(fullyQualifiedName);
142: }
143: }
144:
145: }
|