1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.cache;
16
17 import org.figure8.join.util.LogUtil;
18
19 import org.apache.commons.logging.Log;
20 import net.sf.ehcache.Element;
21
22 import java.io.Serializable;
23 import java.util.List;
24 import java.util.ArrayList;
25 /**
26 * Implementation of <code>CacheManager</code> using EHCache provider
27 * (see http://ehcache.sourceforge.net). It encapsulates a EH Cache.
28 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29 * @version $Revision: 1.2 $
30 */
31 public class EhCache implements Cache{
32
33
34
35 /** Get a commons logger */
36 private static final Log log = LogUtil.getLog(EhCache.class);
37
38
39
40
41 /** This cache name */
42 private String key = null;
43 /** The encapsulated EH Cache */
44 private net.sf.ehcache.Cache ehcache = null;
45 /** The encapsulated EH Cache manager */
46 private net.sf.ehcache.CacheManager ehcacheManager = null;
47
48
49
50
51 /**
52 * Create a new instance of EhCache, wrapping a EH cache object.
53 * @param key The cache name
54 * @param ehcache The encapsulated EH cache
55 */
56 public EhCache(String key, net.sf.ehcache.Cache ehcache, net.sf.ehcache.CacheManager ehcacheManager){
57 this.key = key;
58 this.ehcache = ehcache;
59 this.ehcacheManager = ehcacheManager;
60 }
61
62
63
64
65 /** @return The encapsulated EH Cache instance */
66 public net.sf.ehcache.Cache getWrappedCache(){
67 return ehcache;
68 }
69
70
71
72
73 /**
74 * Get the key this cache is registred within CacheManager
75 * @return This cache key (ie. its name)
76 */
77 public String getKey(){
78 return key;
79 }
80
81 /**
82 * Get all the elements of this cache. This method returns a list
83 * that do not prevent from having duplicates !
84 * @return A List of the cache elements
85 */
86 public List getElements(){
87
88 ArrayList list = new ArrayList(ehcache.getMaxElementsInMemory());
89 try{
90
91 List keys = ehcache.getKeys();
92 for (int i=0; i < keys.size(); i++){
93 Serializable key = (Serializable)keys.get(i);
94 try{
95
96 Element element = ehcache.get(key);
97 list.add(element.getValue());
98 }
99 catch (Exception e){
100 if (log.isTraceEnabled())
101 log.trace(getKey() + " EH Cache element is no more available for key: " + key);
102 }
103 }
104 }
105 catch (Exception e){
106 log.warn(getKey() + " EH Cache throws an exception while getting its keys");
107 log.warn("Here's the exception message: " + e.getMessage());
108 }
109 return list;
110 }
111
112 /**
113 * Get the value of a cache element which matches the key
114 * @param key The key of the element to return
115 * @return The value placed into the cache with an earlier put, or null if not found or expired
116 */
117 public Serializable get(Serializable key){
118 try{
119 if (key == null)
120 return null;
121
122 Element element = ehcache.get(key);
123 if (log.isTraceEnabled())
124 log.trace("Element for '" + key + "' is '" + element + "'");
125
126 if (element != null)
127 return element.getValue();
128 }
129 catch (Exception e){
130 log.warn(getKey() + " EH Cache throws an exception while getting element for key: " + key);
131 log.warn("Here's the exception message: " + e.getMessage());
132 }
133 return null;
134 }
135
136 /**
137 * Puts an object into the cache
138 * @param key a serializable key
139 * @param value a serializable value
140 * @throws CacheException if object cannot be put into cache
141 */
142 public void put(Serializable key, Serializable value) throws CacheException{
143 try{
144
145 Element element = new Element(key, value);
146 ehcache.put(element);
147 }
148 catch (Exception e){
149 log.error(getKey() + " EH Cache throws an exception while putting element: " + e.getMessage());
150 throw new CacheException("Exception while putting element", e);
151 }
152 }
153
154 /**
155 * Remove an object from the cache
156 * @param key a serializable key
157 * @throws CacheException if something went wrong during removal
158 */
159 public void remove(Serializable key) throws CacheException{
160 try{
161 ehcache.remove(key);
162 }
163 catch (Exception e){
164 log.error(getKey() + " EH Cache throws an exception while removing element: " + e.getMessage());
165 throw new CacheException("Exception while removing element", e);
166 }
167 }
168
169 /**
170 * Remove all elements from this cache but leave it in a usable state
171 * @throws CacheException if cache cannot be cleared
172 */
173 public void clear() throws CacheException{
174 try{
175 ehcache.removeAll();
176 }
177 catch (Exception e){
178 log.error(getKey() + " EH Cache throws an exception while clearing: " + e.getMessage());
179 throw new CacheException("Exception while clearing cache", e);
180 }
181 }
182
183 /**
184 * Remove all elements from this cache and the cache itself. It is
185 * no longer usable. <code>CacheManager</code> should no longer offer
186 * access to this cache until explicit cache re-creation.
187 * @throws CacheException if cache cannot be cleaned up
188 */
189 public void cleanUp() throws CacheException{
190 try{
191
192 ehcacheManager.removeCache(ehcache.getName());
193 }
194 catch (Exception e){
195 log.error(getKey() + " EH Cache throws an exception while cleaning: " + e.getMessage());
196 throw new CacheException("Exception while cleaning up cache", e);
197 }
198 }
199 }