blob: 8178366232706b680b05878a8d3eb2bdd2799075 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>EhCache.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-ehcache</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.cache.ehcache</a> &gt; <span class="el_source">EhCache.java</span></div><h1>EhCache.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.apache.shiro.cache.ehcache;
import net.sf.ehcache.Element;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* Shiro {@link org.apache.shiro.cache.Cache} implementation that wraps an {@link net.sf.ehcache.Ehcache} instance.
*
* @since 0.2
*/
public class EhCache&lt;K, V&gt; implements Cache&lt;K, V&gt; {
/**
* Private internal log instance.
*/
<span class="fc" id="L39"> private static final Logger log = LoggerFactory.getLogger(EhCache.class);</span>
/**
* The wrapped Ehcache instance.
*/
private net.sf.ehcache.Ehcache cache;
/**
* Constructs a new EhCache instance with the given cache.
*
* @param cache - delegate EhCache instance this Shiro cache instance will wrap.
*/
<span class="fc" id="L51"> public EhCache(net.sf.ehcache.Ehcache cache) {</span>
<span class="pc bpc" id="L52" title="1 of 2 branches missed."> if (cache == null) {</span>
<span class="nc" id="L53"> throw new IllegalArgumentException(&quot;Cache argument cannot be null.&quot;);</span>
}
<span class="fc" id="L55"> this.cache = cache;</span>
<span class="fc" id="L56"> }</span>
/**
* Gets a value of an element which matches the given key.
*
* @param key the key of the element to return.
* @return The value placed into the cache with an earlier put, or null if not found or expired
*/
public V get(K key) throws CacheException {
try {
<span class="pc bpc" id="L66" title="1 of 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L67"> log.trace(&quot;Getting object from cache [&quot; + cache.getName() + &quot;] for key [&quot; + key + &quot;]&quot;);</span>
}
<span class="pc bpc" id="L69" title="1 of 2 branches missed."> if (key == null) {</span>
<span class="nc" id="L70"> return null;</span>
} else {
<span class="fc" id="L72"> Element element = cache.get(key);</span>
<span class="fc bfc" id="L73" title="All 2 branches covered."> if (element == null) {</span>
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L75"> log.trace(&quot;Element for [&quot; + key + &quot;] is null.&quot;);</span>
}
<span class="fc" id="L77"> return null;</span>
} else {
//noinspection unchecked
<span class="fc" id="L80"> return (V) element.getObjectValue();</span>
}
}
<span class="nc" id="L83"> } catch (Throwable t) {</span>
<span class="nc" id="L84"> throw new CacheException(t);</span>
}
}
/**
* Puts an object into the cache.
*
* @param key the key.
* @param value the value.
*/
public V put(K key, V value) throws CacheException {
<span class="pc bpc" id="L95" title="1 of 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="fc" id="L96"> log.trace(&quot;Putting object in cache [&quot; + cache.getName() + &quot;] for key [&quot; + key + &quot;]&quot;);</span>
}
try {
<span class="fc" id="L99"> V previous = get(key);</span>
<span class="fc" id="L100"> Element element = new Element(key, value);</span>
<span class="fc" id="L101"> cache.put(element);</span>
<span class="fc" id="L102"> return previous;</span>
<span class="nc" id="L103"> } catch (Throwable t) {</span>
<span class="nc" id="L104"> throw new CacheException(t);</span>
}
}
/**
* Removes the element which matches the key.
*
* &lt;p&gt;If no element matches, nothing is removed and no Exception is thrown.&lt;/p&gt;
*
* @param key the key of the element to remove
*/
public V remove(K key) throws CacheException {
<span class="nc bnc" id="L116" title="All 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="nc" id="L117"> log.trace(&quot;Removing object from cache [&quot; + cache.getName() + &quot;] for key [&quot; + key + &quot;]&quot;);</span>
}
try {
<span class="nc" id="L120"> V previous = get(key);</span>
<span class="nc" id="L121"> cache.remove(key);</span>
<span class="nc" id="L122"> return previous;</span>
<span class="nc" id="L123"> } catch (Throwable t) {</span>
<span class="nc" id="L124"> throw new CacheException(t);</span>
}
}
/**
* Removes all elements in the cache, but leaves the cache in a useable state.
*/
public void clear() throws CacheException {
<span class="nc bnc" id="L132" title="All 2 branches missed."> if (log.isTraceEnabled()) {</span>
<span class="nc" id="L133"> log.trace(&quot;Clearing all objects from cache [&quot; + cache.getName() + &quot;]&quot;);</span>
}
try {
<span class="nc" id="L136"> cache.removeAll();</span>
<span class="nc" id="L137"> } catch (Throwable t) {</span>
<span class="nc" id="L138"> throw new CacheException(t);</span>
<span class="nc" id="L139"> }</span>
<span class="nc" id="L140"> }</span>
public int size() {
try {
<span class="nc" id="L144"> return cache.getSize();</span>
<span class="nc" id="L145"> } catch (Throwable t) {</span>
<span class="nc" id="L146"> throw new CacheException(t);</span>
}
}
public Set&lt;K&gt; keys() {
try {
@SuppressWarnings({&quot;unchecked&quot;})
<span class="nc" id="L153"> List&lt;K&gt; keys = cache.getKeys();</span>
<span class="nc bnc" id="L154" title="All 2 branches missed."> if (!isEmpty(keys)) {</span>
<span class="nc" id="L155"> return Collections.unmodifiableSet(new LinkedHashSet&lt;K&gt;(keys));</span>
} else {
<span class="nc" id="L157"> return Collections.emptySet();</span>
}
<span class="nc" id="L159"> } catch (Throwable t) {</span>
<span class="nc" id="L160"> throw new CacheException(t);</span>
}
}
public Collection&lt;V&gt; values() {
try {
@SuppressWarnings({&quot;unchecked&quot;})
<span class="nc" id="L167"> List&lt;K&gt; keys = cache.getKeys();</span>
<span class="nc bnc" id="L168" title="All 2 branches missed."> if (!isEmpty(keys)) {</span>
<span class="nc" id="L169"> List&lt;V&gt; values = new ArrayList&lt;V&gt;(keys.size());</span>
<span class="nc bnc" id="L170" title="All 2 branches missed."> for (K key : keys) {</span>
<span class="nc" id="L171"> V value = get(key);</span>
<span class="nc bnc" id="L172" title="All 2 branches missed."> if (value != null) {</span>
<span class="nc" id="L173"> values.add(value);</span>
}
<span class="nc" id="L175"> }</span>
<span class="nc" id="L176"> return Collections.unmodifiableList(values);</span>
} else {
<span class="nc" id="L178"> return Collections.emptyList();</span>
}
<span class="nc" id="L180"> } catch (Throwable t) {</span>
<span class="nc" id="L181"> throw new CacheException(t);</span>
}
}
/**
* Returns the size (in bytes) that this EhCache is using in memory (RAM), or &lt;code&gt;-1&lt;/code&gt; if that
* number is unknown or cannot be calculated.
*
* @return the size (in bytes) that this EhCache is using in memory (RAM), or &lt;code&gt;-1&lt;/code&gt; if that
* number is unknown or cannot be calculated.
*/
public long getMemoryUsage() {
try {
<span class="nc" id="L194"> return cache.calculateInMemorySize();</span>
}
<span class="nc" id="L196"> catch (Throwable t) {</span>
<span class="nc" id="L197"> return -1;</span>
}
}
/**
* Returns the size (in bytes) that this EhCache's memory store is using (RAM), or &lt;code&gt;-1&lt;/code&gt; if
* that number is unknown or cannot be calculated.
*
* @return the size (in bytes) that this EhCache's memory store is using (RAM), or &lt;code&gt;-1&lt;/code&gt; if
* that number is unknown or cannot be calculated.
*/
public long getMemoryStoreSize() {
try {
<span class="nc" id="L210"> return cache.getMemoryStoreSize();</span>
}
<span class="nc" id="L212"> catch (Throwable t) {</span>
<span class="nc" id="L213"> throw new CacheException(t);</span>
}
}
/**
* Returns the size (in bytes) that this EhCache's disk store is consuming or &lt;code&gt;-1&lt;/code&gt; if
* that number is unknown or cannot be calculated.
*
* @return the size (in bytes) that this EhCache's disk store is consuming or &lt;code&gt;-1&lt;/code&gt; if
* that number is unknown or cannot be calculated.
*/
public long getDiskStoreSize() {
try {
<span class="nc" id="L226"> return cache.getDiskStoreSize();</span>
<span class="nc" id="L227"> } catch (Throwable t) {</span>
<span class="nc" id="L228"> throw new CacheException(t);</span>
}
}
/**
* Returns &amp;quot;EhCache [&amp;quot; + cache.getName() + &amp;quot;]&amp;quot;
*
* @return &amp;quot;EhCache [&amp;quot; + cache.getName() + &amp;quot;]&amp;quot;
*/
public String toString() {
<span class="nc" id="L238"> return &quot;EhCache [&quot; + cache.getName() + &quot;]&quot;;</span>
}
//////////////////////////
// From CollectionUtils //
//////////////////////////
// CollectionUtils cannot be removed from shiro-core until 2.0 as it has a dependency on PrincipalCollection
private static boolean isEmpty(Collection c) {
<span class="nc bnc" id="L247" title="All 4 branches missed."> return c == null || c.isEmpty();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html>