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