(no commit message)
[utils] / support / general / src / main / java / org / wamblee / cache / EhCache.java
index 6a631866c109f405a0e47d10231da478ff006c01..795a95b4070c8e5d454346a05d27dcd04a2780a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 the original author or authors.
+ * Copyright 2005-2010 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.
  * 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;
+import java.util.logging.Logger;
+
 /**
  * Cache implemented on top of EhCache.
- *
+ * 
  * @author Erik Brakkee
+ * 
  */
 public class EhCache<KeyType extends Serializable, ValueType extends Serializable>
-        implements org.wamblee.cache.Cache<KeyType, ValueType> {
-
-    private static final Logger LOGGER = Logger.getLogger(EhCache.class);
+    implements org.wamblee.cache.Cache<KeyType, ValueType> {
+    private static final Logger LOGGER = Logger.getLogger(EhCache.class
+        .getName());
 
     /**
      * EH Cache manager.
      */
-    private CacheManager _manager;
+    private CacheManager manager;
 
     /**
      * EH cache.
      */
-    private Cache _cache;
+    private Cache cache;
 
     /**
      * Constructs a cache based on EHCache.
@@ -60,22 +60,23 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
      * @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);
-            if (_cache == null) {
-                LOGGER.warn("Creating cache '" + aCacheName
-                        + "' because it is not configured");
-                _manager.addCache(aCacheName);
-                _cache = _manager.getCache(aCacheName);
+            manager = new CacheManager(is);
+            cache = manager.getCache(aCacheName);
+
+            if (cache == null) {
+                LOGGER.warning("Creating cache '" + aCacheName +
+                    "' because it is not configured");
+                manager.addCache(aCacheName);
+                cache = manager.getCache(aCacheName);
             }
-            assert _cache != null;
+            assert cache != null;
         } finally {
             is.close();
         }
-
     }
 
     /*
@@ -84,7 +85,7 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
      * @see org.wamblee.cache.Cache#put(KeyType, ValueType)
      */
     public void put(KeyType aKey, ValueType aValue) {
-        _cache.put(new Element(aKey, aValue));
+        cache.put(new Element(aKey, aValue));
     }
 
     /*
@@ -94,10 +95,12 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
      */
     public ValueType get(KeyType aKey) {
         try {
-            Element element = _cache.get(aKey);
+            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);
@@ -110,7 +113,7 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
      * @see org.wamblee.cache.Cache#remove(KeyType)
      */
     public void remove(KeyType aKey) {
-        _cache.remove(aKey);
+        cache.remove(aKey);
     }
 
     /*
@@ -119,6 +122,6 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
      * @see org.wamblee.cache.Cache#clear()
      */
     public void clear() {
-        _cache.removeAll();
+        cache.removeAll();
     }
 }