blob: 50a50db6417d56e478f8cc318d9c1100b29e7147 [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>MemorySessionDAO.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 :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.session.mgt.eis</a> &gt; <span class="el_source">MemorySessionDAO.java</span></div><h1>MemorySessionDAO.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.session.mgt.eis;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Simple memory-based implementation of the SessionDAO that stores all of its sessions in an in-memory
* {@link ConcurrentMap}. &lt;b&gt;This implementation does not page to disk and is therefore unsuitable for applications
* that could experience a large amount of sessions&lt;/b&gt; and would therefore cause {@code OutOfMemoryException}s. It is
* &lt;em&gt;not&lt;/em&gt; recommended for production use in most environments.
* &lt;h2&gt;Memory Restrictions&lt;/h2&gt;
* If your application is expected to host many sessions beyond what can be stored in the
* memory available to the JVM, it is highly recommended to use a different {@code SessionDAO} implementation which
* uses a more expansive or permanent backing data store.
* &lt;p/&gt;
* In this case, it is recommended to instead use a custom
* {@link CachingSessionDAO} implementation that communicates with a higher-capacity data store of your choice
* (file system, database, etc).
* &lt;h2&gt;Changes in 1.0&lt;/h2&gt;
* This implementation prior to 1.0 used to subclass the {@link CachingSessionDAO}, but this caused problems with many
* cache implementations that would expunge entries due to TTL settings, resulting in Sessions that would be randomly
* (and permanently) lost. The Shiro 1.0 release refactored this implementation to be 100% memory-based (without
* {@code Cache} usage to avoid this problem.
*
* @see CachingSessionDAO
* @since 0.1
*/
public class MemorySessionDAO extends AbstractSessionDAO {
<span class="fc" id="L58"> private static final Logger log = LoggerFactory.getLogger(MemorySessionDAO.class);</span>
private ConcurrentMap&lt;Serializable, Session&gt; sessions;
<span class="fc" id="L62"> public MemorySessionDAO() {</span>
<span class="fc" id="L63"> this.sessions = new ConcurrentHashMap&lt;Serializable, Session&gt;();</span>
<span class="fc" id="L64"> }</span>
protected Serializable doCreate(Session session) {
<span class="fc" id="L67"> Serializable sessionId = generateSessionId(session);</span>
<span class="fc" id="L68"> assignSessionId(session, sessionId);</span>
<span class="fc" id="L69"> storeSession(sessionId, session);</span>
<span class="fc" id="L70"> return sessionId;</span>
}
protected Session storeSession(Serializable id, Session session) {
<span class="pc bpc" id="L74" title="1 of 2 branches missed."> if (id == null) {</span>
<span class="nc" id="L75"> throw new NullPointerException(&quot;id argument cannot be null.&quot;);</span>
}
<span class="fc" id="L77"> return sessions.putIfAbsent(id, session);</span>
}
protected Session doReadSession(Serializable sessionId) {
<span class="fc" id="L81"> return sessions.get(sessionId);</span>
}
public void update(Session session) throws UnknownSessionException {
<span class="fc" id="L85"> storeSession(session.getId(), session);</span>
<span class="fc" id="L86"> }</span>
public void delete(Session session) {
<span class="pc bpc" id="L89" title="1 of 2 branches missed."> if (session == null) {</span>
<span class="nc" id="L90"> throw new NullPointerException(&quot;session argument cannot be null.&quot;);</span>
}
<span class="fc" id="L92"> Serializable id = session.getId();</span>
<span class="pc bpc" id="L93" title="1 of 2 branches missed."> if (id != null) {</span>
<span class="fc" id="L94"> sessions.remove(id);</span>
}
<span class="fc" id="L96"> }</span>
public Collection&lt;Session&gt; getActiveSessions() {
<span class="fc" id="L99"> Collection&lt;Session&gt; values = sessions.values();</span>
<span class="fc bfc" id="L100" title="All 2 branches covered."> if (CollectionUtils.isEmpty(values)) {</span>
<span class="fc" id="L101"> return Collections.emptySet();</span>
} else {
<span class="fc" id="L103"> return Collections.unmodifiableCollection(values);</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>