ca11ab3f72c71f13aa92341afb6f5ba13fd064e2
[utils] / support / src / main / java / wamblee / cache / EhCache.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.cache;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.Serializable;
22
23 import net.sf.ehcache.Cache;
24 import net.sf.ehcache.CacheException;
25 import net.sf.ehcache.CacheManager;
26 import net.sf.ehcache.Element;
27
28 import org.apache.log4j.Logger;
29 import org.wamblee.io.InputResource;
30
31 /**
32  * Cache implemented on top of EhCache.
33  */
34 public class EhCache<KeyType extends Serializable, ValueType extends Serializable>
35         implements org.wamblee.cache.Cache<KeyType, ValueType> {
36
37     private static final Logger LOGGER = Logger.getLogger(EhCache.class);
38
39     /**
40      * EH Cache manager.
41      */
42     private CacheManager _manager;
43
44     /**
45      * EH cache.
46      */
47     private Cache _cache;
48
49     /**
50      * Constructs a cache based on EHCache.
51      * 
52      * @param aResource
53      *            Resource containing the configuration file for EHCache.
54      * @param aCacheName
55      *            Name of the cache to use. If a cache with this name does not
56      *            exist, one is created based on default settings.
57      * @throws IOException
58      * @throws CacheException
59      */
60     public EhCache(InputResource aResource, String aCacheName)
61             throws IOException, CacheException {
62         InputStream is = aResource.getInputStream();
63         try {
64             _manager = CacheManager.create(is);
65             _cache = _manager.getCache(aCacheName);
66             if (_cache == null) {
67                 LOGGER.warn("Creating cache '" + aCacheName
68                         + "' because it is not configured");
69                 _manager.addCache(aCacheName);
70                 _cache = _manager.getCache(aCacheName);
71             }
72             assert _cache != null;
73         } finally {
74             is.close();
75         }
76
77     }
78
79     /*
80      * (non-Javadoc)
81      * 
82      * @see org.wamblee.cache.Cache#put(KeyType, ValueType)
83      */
84     public void put(KeyType aKey, ValueType aValue) {
85         _cache.put(new Element(aKey, aValue));
86     }
87
88     /*
89      * (non-Javadoc)
90      * 
91      * @see org.wamblee.cache.Cache#get(KeyType)
92      */
93     public ValueType get(KeyType aKey) {
94         try {
95             Element element = _cache.get(aKey);
96             if (element == null) {
97                 return null;
98             }
99             return (ValueType) element.getValue();
100         } catch (CacheException e) {
101             throw new RuntimeException("Cache problem key = '" + aKey + "'", e);
102         }
103     }
104
105     /*
106      * (non-Javadoc)
107      * 
108      * @see org.wamblee.cache.Cache#remove(KeyType)
109      */
110     public void remove(KeyType aKey) {
111         _cache.remove(aKey);
112     }
113
114     /*
115      * (non-Javadoc)
116      * 
117      * @see org.wamblee.cache.Cache#clear()
118      */
119     public void clear() {
120         try {
121             _cache.removeAll();
122         } catch (IOException e) {
123             throw new RuntimeException("Problem removing items from cache", e);
124         }
125     }
126 }