/*
* Copyright 2005 the original author or authors.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.wamblee.cache;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Serializable;
-
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
+
import org.wamblee.io.InputResource;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Serializable;
+
+
/**
* Cache implemented on top of EhCache.
*
* @author Erik Brakkee
+ *
+ * @param <KeyType> DOCUMENT ME!
+ * @param <ValueType> DOCUMENT ME!
*/
public class EhCache<KeyType extends Serializable, ValueType extends Serializable>
- implements org.wamblee.cache.Cache<KeyType, ValueType> {
-
+ implements org.wamblee.cache.Cache<KeyType, ValueType> {
+ /**
+ * DOCUMENT ME!
+ */
private static final Logger LOGGER = Logger.getLogger(EhCache.class);
/**
*/
private Cache cache;
- /**
+/**
* Constructs a cache based on EHCache.
- *
+ *
* @param aResource
* Resource containing the configuration file for EHCache.
* @param aCacheName
* @throws CacheException
*/
public EhCache(InputResource aResource, String aCacheName)
- throws IOException, CacheException {
+ throws IOException, CacheException {
InputStream is = aResource.getInputStream();
+
try {
- manager = new CacheManager(is);
- cache = manager.getCache(aCacheName);
+ manager = new CacheManager(is);
+ cache = manager.getCache(aCacheName);
+
if (cache == null) {
LOGGER.warn("Creating cache '" + aCacheName
- + "' because it is not configured");
+ + "' because it is not configured");
manager.addCache(aCacheName);
cache = manager.getCache(aCacheName);
}
} finally {
is.close();
}
-
}
/*
* (non-Javadoc)
- *
+ *
* @see org.wamblee.cache.Cache#put(KeyType, ValueType)
*/
+ /**
+ * DOCUMENT ME!
+ *
+ * @param aKey DOCUMENT ME!
+ * @param aValue DOCUMENT ME!
+ */
public void put(KeyType aKey, ValueType aValue) {
cache.put(new Element(aKey, aValue));
}
/*
* (non-Javadoc)
- *
+ *
* @see org.wamblee.cache.Cache#get(KeyType)
*/
+ /**
+ * DOCUMENT ME!
+ *
+ * @param aKey DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
public ValueType get(KeyType aKey) {
try {
Element element = cache.get(aKey);
+
if (element == null) {
return null;
}
+
return (ValueType) element.getValue();
} catch (CacheException e) {
throw new RuntimeException("Cache problem key = '" + aKey + "'", e);
/*
* (non-Javadoc)
- *
+ *
* @see org.wamblee.cache.Cache#remove(KeyType)
*/
+ /**
+ * DOCUMENT ME!
+ *
+ * @param aKey DOCUMENT ME!
+ */
public void remove(KeyType aKey) {
cache.remove(aKey);
}
/*
* (non-Javadoc)
- *
+ *
* @see org.wamblee.cache.Cache#clear()
*/
+ /**
+ * DOCUMENT ME!
+ */
public void clear() {
cache.removeAll();
}