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