Coverage Report - org.apache.shiro.cache.ehcache.EhCache
 
Classes in this File Line Coverage Branch Coverage Complexity
EhCache
28%
20/71
29%
7/24
4.75
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.cache.ehcache;
 20  
 
 21  
 import net.sf.ehcache.Element;
 22  
 import org.apache.shiro.cache.Cache;
 23  
 import org.apache.shiro.cache.CacheException;
 24  
 import org.apache.shiro.util.CollectionUtils;
 25  
 import org.slf4j.Logger;
 26  
 import org.slf4j.LoggerFactory;
 27  
 
 28  
 import java.util.*;
 29  
 
 30  
 /**
 31  
  * Shiro {@link org.apache.shiro.cache.Cache} implementation that wraps an {@link net.sf.ehcache.Ehcache} instance.
 32  
  *
 33  
  * @since 0.2
 34  
  */
 35  
 public class EhCache<K, V> implements Cache<K, V> {
 36  
 
 37  
     /**
 38  
      * Private internal log instance.
 39  
      */
 40  1
     private static final Logger log = LoggerFactory.getLogger(EhCache.class);
 41  
 
 42  
     /**
 43  
      * The wrapped Ehcache instance.
 44  
      */
 45  
     private net.sf.ehcache.Ehcache cache;
 46  
 
 47  
     /**
 48  
      * Constructs a new EhCache instance with the given cache.
 49  
      *
 50  
      * @param cache - delegate EhCache instance this Shiro cache instance will wrap.
 51  
      */
 52  1
     public EhCache(net.sf.ehcache.Ehcache cache) {
 53  1
         if (cache == null) {
 54  0
             throw new IllegalArgumentException("Cache argument cannot be null.");
 55  
         }
 56  1
         this.cache = cache;
 57  1
     }
 58  
 
 59  
     /**
 60  
      * Gets a value of an element which matches the given key.
 61  
      *
 62  
      * @param key the key of the element to return.
 63  
      * @return The value placed into the cache with an earlier put, or null if not found or expired
 64  
      */
 65  
     public V get(K key) throws CacheException {
 66  
         try {
 67  2
             if (log.isTraceEnabled()) {
 68  2
                 log.trace("Getting object from cache [" + cache.getName() + "] for key [" + key + "]");
 69  
             }
 70  2
             if (key == null) {
 71  0
                 return null;
 72  
             } else {
 73  2
                 Element element = cache.get(key);
 74  2
                 if (element == null) {
 75  1
                     if (log.isTraceEnabled()) {
 76  1
                         log.trace("Element for [" + key + "] is null.");
 77  
                     }
 78  1
                     return null;
 79  
                 } else {
 80  
                     //noinspection unchecked
 81  1
                     return (V) element.getObjectValue();
 82  
                 }
 83  
             }
 84  0
         } catch (Throwable t) {
 85  0
             throw new CacheException(t);
 86  
         }
 87  
     }
 88  
 
 89  
     /**
 90  
      * Puts an object into the cache.
 91  
      *
 92  
      * @param key   the key.
 93  
      * @param value the value.
 94  
      */
 95  
     public V put(K key, V value) throws CacheException {
 96  1
         if (log.isTraceEnabled()) {
 97  1
             log.trace("Putting object in cache [" + cache.getName() + "] for key [" + key + "]");
 98  
         }
 99  
         try {
 100  1
             V previous = get(key);
 101  1
             Element element = new Element(key, value);
 102  1
             cache.put(element);
 103  1
             return previous;
 104  0
         } catch (Throwable t) {
 105  0
             throw new CacheException(t);
 106  
         }
 107  
     }
 108  
 
 109  
     /**
 110  
      * Removes the element which matches the key.
 111  
      *
 112  
      * <p>If no element matches, nothing is removed and no Exception is thrown.</p>
 113  
      *
 114  
      * @param key the key of the element to remove
 115  
      */
 116  
     public V remove(K key) throws CacheException {
 117  0
         if (log.isTraceEnabled()) {
 118  0
             log.trace("Removing object from cache [" + cache.getName() + "] for key [" + key + "]");
 119  
         }
 120  
         try {
 121  0
             V previous = get(key);
 122  0
             cache.remove(key);
 123  0
             return previous;
 124  0
         } catch (Throwable t) {
 125  0
             throw new CacheException(t);
 126  
         }
 127  
     }
 128  
 
 129  
     /**
 130  
      * Removes all elements in the cache, but leaves the cache in a useable state.
 131  
      */
 132  
     public void clear() throws CacheException {
 133  0
         if (log.isTraceEnabled()) {
 134  0
             log.trace("Clearing all objects from cache [" + cache.getName() + "]");
 135  
         }
 136  
         try {
 137  0
             cache.removeAll();
 138  0
         } catch (Throwable t) {
 139  0
             throw new CacheException(t);
 140  0
         }
 141  0
     }
 142  
 
 143  
     public int size() {
 144  
         try {
 145  0
             return cache.getSize();
 146  0
         } catch (Throwable t) {
 147  0
             throw new CacheException(t);
 148  
         }
 149  
     }
 150  
 
 151  
     public Set<K> keys() {
 152  
         try {
 153  
             @SuppressWarnings({"unchecked"})
 154  0
             List<K> keys = cache.getKeys();
 155  0
             if (!CollectionUtils.isEmpty(keys)) {
 156  0
                 return Collections.unmodifiableSet(new LinkedHashSet<K>(keys));
 157  
             } else {
 158  0
                 return Collections.emptySet();
 159  
             }
 160  0
         } catch (Throwable t) {
 161  0
             throw new CacheException(t);
 162  
         }
 163  
     }
 164  
 
 165  
     public Collection<V> values() {
 166  
         try {
 167  
             @SuppressWarnings({"unchecked"})
 168  0
             List<K> keys = cache.getKeys();
 169  0
             if (!CollectionUtils.isEmpty(keys)) {
 170  0
                 List<V> values = new ArrayList<V>(keys.size());
 171  0
                 for (K key : keys) {
 172  0
                     V value = get(key);
 173  0
                     if (value != null) {
 174  0
                         values.add(value);
 175  
                     }
 176  0
                 }
 177  0
                 return Collections.unmodifiableList(values);
 178  
             } else {
 179  0
                 return Collections.emptyList();
 180  
             }
 181  0
         } catch (Throwable t) {
 182  0
             throw new CacheException(t);
 183  
         }
 184  
     }
 185  
 
 186  
     /**
 187  
      * Returns the size (in bytes) that this EhCache is using in memory (RAM), or <code>-1</code> if that
 188  
      * number is unknown or cannot be calculated.
 189  
      *
 190  
      * @return the size (in bytes) that this EhCache is using in memory (RAM), or <code>-1</code> if that
 191  
      *         number is unknown or cannot be calculated.
 192  
      */
 193  
     public long getMemoryUsage() {
 194  
         try {
 195  0
             return cache.calculateInMemorySize();
 196  
         }
 197  0
         catch (Throwable t) {
 198  0
             return -1;
 199  
         }
 200  
     }
 201  
 
 202  
     /**
 203  
      * Returns the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if
 204  
      * that number is unknown or cannot be calculated.
 205  
      *
 206  
      * @return the size (in bytes) that this EhCache's memory store is using (RAM), or <code>-1</code> if
 207  
      *         that number is unknown or cannot be calculated.
 208  
      */
 209  
     public long getMemoryStoreSize() {
 210  
         try {
 211  0
             return cache.getMemoryStoreSize();
 212  
         }
 213  0
         catch (Throwable t) {
 214  0
             throw new CacheException(t);
 215  
         }
 216  
     }
 217  
 
 218  
     /**
 219  
      * Returns the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if
 220  
      * that number is unknown or cannot be calculated.
 221  
      *
 222  
      * @return the size (in bytes) that this EhCache's disk store is consuming or <code>-1</code> if
 223  
      *         that number is unknown or cannot be calculated.
 224  
      */
 225  
     public long getDiskStoreSize() {
 226  
         try {
 227  0
             return cache.getDiskStoreSize();
 228  0
         } catch (Throwable t) {
 229  0
             throw new CacheException(t);
 230  
         }
 231  
     }
 232  
 
 233  
     /**
 234  
      * Returns &quot;EhCache [&quot; + cache.getName() + &quot;]&quot;
 235  
      *
 236  
      * @return &quot;EhCache [&quot; + cache.getName() + &quot;]&quot;
 237  
      */
 238  
     public String toString() {
 239  0
         return "EhCache [" + cache.getName() + "]";
 240  
     }
 241  
 }