KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > cache > EhCacheEngine


1 /******************************************************************************
2  * Sony Online Entertainment
3  * Application Engineering
4  *
5  * Unpublished work Copyright 2005 Sony Online Entertainment Inc.
6  * All rights reserved.
7  * Created on Oct 11, 2005
8  ******************************************************************************/

9 package net.jforum.cache;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import net.jforum.util.preferences.SystemGlobals;
18 import net.sf.ehcache.Cache;
19 import net.sf.ehcache.CacheException;
20 import net.sf.ehcache.CacheManager;
21 import net.sf.ehcache.Element;
22
23 import org.apache.log4j.Logger;
24 /**
25  * The rest of the application seems to make some invalid assumptions about how
26  * things are cached. Those assumptions might be benign, but it is hard to tell
27  * without deep testing. Until this is finishe the JBossCacheEngine should be
28  * configured in a local mode.
29  *
30  * Created on Oct 11, 2005
31  *
32  * @author Jake Fear
33  * @version $Id: EhCacheEngine.java,v 1.1 2005/10/14 00:15:54 rafaelsteil Exp $
34  */

35 public class EhCacheEngine implements CacheEngine {
36
37     private static final Logger log = Logger.getLogger(EhCacheEngine.class);
38     
39     private CacheManager manager;
40     
41     public void init() {
42         try {
43             manager = CacheManager.create(SystemGlobals.getValue("ehcache.cache.properties"));
44         } catch (CacheException ce) {
45             log.error("EhCache could not be initialized", ce);
46             throw new RuntimeException JavaDoc(ce);
47         }
48     }
49
50     public void stop() {
51         manager.shutdown();
52     }
53
54     public void add(String JavaDoc key, Object JavaDoc value) {
55         if (log.isDebugEnabled()) {
56             log.debug("Caching " + value + " with key " + key);
57         }
58         add(DUMMY_FQN, key, value);
59     }
60
61     public void add(String JavaDoc fullyQualifiedName, String JavaDoc key, Object JavaDoc value) {
62         if (!manager.cacheExists(fullyQualifiedName)) {
63             try {
64                 manager.addCache(fullyQualifiedName);
65             } catch (CacheException ce) {
66                 log.error(ce, ce);
67                 throw new RuntimeException JavaDoc(ce);
68             }
69         }
70         Cache cache = manager.getCache(fullyQualifiedName);
71         
72         Element element = new Element(key, (Serializable JavaDoc)value);
73         cache.put(element);
74     }
75
76     public Object JavaDoc get(String JavaDoc fullyQualifiedName, String JavaDoc key) {
77         try {
78             if (!manager.cacheExists(fullyQualifiedName)) {
79                 manager.addCache(fullyQualifiedName);
80                 return null;
81             }
82             Cache cache = manager.getCache(fullyQualifiedName);
83             Element element = cache.get(key);
84             if (element != null) {
85                 return element.getValue();
86             }
87             
88             return null;
89         } catch (CacheException ce) {
90             log.error("EhCache could not be shutdown", ce);
91             throw new RuntimeException JavaDoc(ce);
92         }
93     }
94
95     public Object JavaDoc get(String JavaDoc fullyQualifiedName) {
96         if (!manager.cacheExists(fullyQualifiedName)) {
97             try {
98                 manager.addCache(fullyQualifiedName);
99             } catch (CacheException ce) {
100                 log.error("EhCache could not be shutdown", ce);
101                 throw new RuntimeException JavaDoc(ce);
102             }
103         }
104         Cache cache = manager.getCache(fullyQualifiedName);
105         return cache;
106     }
107
108     public Collection JavaDoc getValues(String JavaDoc fullyQualifiedName) {
109         try {
110             if (!manager.cacheExists(fullyQualifiedName)) {
111                 manager.addCache(fullyQualifiedName);
112                 return new ArrayList JavaDoc();
113             }
114             Cache cache = manager.getCache(fullyQualifiedName);
115             List JavaDoc values = new ArrayList JavaDoc(cache.getSize());
116             List JavaDoc keys = cache.getKeys();
117             
118             for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); ) {
119                 values.add(cache.get((Serializable JavaDoc)iter.next()));
120             }
121
122             return values;
123         } catch (CacheException ce) {
124             log.error("EhCache could not be shutdown", ce);
125             throw new RuntimeException JavaDoc(ce);
126         }
127     }
128
129     public void remove(String JavaDoc fullyQualifiedName, String JavaDoc key) {
130         Cache cache = manager.getCache(fullyQualifiedName);
131         
132         if (cache != null) {
133             cache.remove(key);
134         }
135     }
136
137     public void remove(String JavaDoc fullyQualifiedName) {
138         if (manager.cacheExists(fullyQualifiedName)) {
139             manager.removeCache(fullyQualifiedName);
140         }
141     }
142
143 }
144
Popular Tags