(no commit message)
[utils] / support / general / src / main / java / org / 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  * @author Erik Brakkee
35  */
36 public class EhCache<KeyType extends Serializable, ValueType extends Serializable>
37         implements org.wamblee.cache.Cache<KeyType, ValueType> {
38
39     private static final Logger LOGGER = Logger.getLogger(EhCache.class);
40
41     /**
42      * EH Cache manager.
43      */
44     private CacheManager _manager;
45
46     /**
47      * EH cache.
48      */
49     private Cache _cache;
50
51     /**
52      * Constructs a cache based on EHCache.
53      * 
54      * @param aResource
55      *            Resource containing the configuration file for EHCache.
56      * @param aCacheName
57      *            Name of the cache to use. If a cache with this name does not
58      *            exist, one is created based on default settings.
59      * @throws IOException
60      * @throws CacheException
61      */
62     public EhCache(InputResource aResource, String aCacheName)
63             throws IOException, CacheException {
64         InputStream is = aResource.getInputStream();
65         try {
66                 _manager = new CacheManager(is);
67             _cache = _manager.getCache(aCacheName);
68             if (_cache == null) {
69                 LOGGER.warn("Creating cache '" + aCacheName
70                         + "' because it is not configured");
71                 _manager.addCache(aCacheName);
72                 _cache = _manager.getCache(aCacheName);
73             }
74             assert _cache != null;
75         } finally {
76             is.close();
77         }
78
79     }
80
81     /*
82      * (non-Javadoc)
83      * 
84      * @see org.wamblee.cache.Cache#put(KeyType, ValueType)
85      */
86     public void put(KeyType aKey, ValueType aValue) {
87         _cache.put(new Element(aKey, aValue));
88     }
89
90     /*
91      * (non-Javadoc)
92      * 
93      * @see org.wamblee.cache.Cache#get(KeyType)
94      */
95     public ValueType get(KeyType aKey) {
96         try {
97             Element element = _cache.get(aKey);
98             if (element == null) {
99                 return null;
100             }
101             return (ValueType) element.getValue();
102         } catch (CacheException e) {
103             throw new RuntimeException("Cache problem key = '" + aKey + "'", e);
104         }
105     }
106
107     /*
108      * (non-Javadoc)
109      * 
110      * @see org.wamblee.cache.Cache#remove(KeyType)
111      */
112     public void remove(KeyType aKey) {
113         _cache.remove(aKey);
114     }
115
116     /*
117      * (non-Javadoc)
118      * 
119      * @see org.wamblee.cache.Cache#clear()
120      */
121     public void clear() {
122         _cache.removeAll();
123     }
124 }