001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.session.mgt.eis;
020
021import org.apache.shiro.session.Session;
022import org.apache.shiro.session.UnknownSessionException;
023import org.apache.shiro.util.CollectionUtils;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027import java.io.Serializable;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.concurrent.ConcurrentHashMap;
031import java.util.concurrent.ConcurrentMap;
032
033
034/**
035 * Simple memory-based implementation of the SessionDAO that stores all of its sessions in an in-memory
036 * {@link ConcurrentMap}.  <b>This implementation does not page to disk and is therefore unsuitable for applications
037 * that could experience a large amount of sessions</b> and would therefore cause {@code OutOfMemoryException}s.  It is
038 * <em>not</em> recommended for production use in most environments.
039 * <h2>Memory Restrictions</h2>
040 * If your application is expected to host many sessions beyond what can be stored in the
041 * memory available to the JVM, it is highly recommended to use a different {@code SessionDAO} implementation which
042 * uses a more expansive or permanent backing data store.
043 * <p/>
044 * In this case, it is recommended to instead use a custom
045 * {@link CachingSessionDAO} implementation that communicates with a higher-capacity data store of your choice
046 * (file system, database, etc).
047 * <h2>Changes in 1.0</h2>
048 * This implementation prior to 1.0 used to subclass the {@link CachingSessionDAO}, but this caused problems with many
049 * cache implementations that would expunge entries due to TTL settings, resulting in Sessions that would be randomly
050 * (and permanently) lost.  The Shiro 1.0 release refactored this implementation to be 100% memory-based (without
051 * {@code Cache} usage to avoid this problem.
052 *
053 * @see CachingSessionDAO
054 * @since 0.1
055 */
056public class MemorySessionDAO extends AbstractSessionDAO {
057
058    private static final Logger log = LoggerFactory.getLogger(MemorySessionDAO.class);
059
060    private ConcurrentMap<Serializable, Session> sessions;
061
062    public MemorySessionDAO() {
063        this.sessions = new ConcurrentHashMap<Serializable, Session>();
064    }
065
066    protected Serializable doCreate(Session session) {
067        Serializable sessionId = generateSessionId(session);
068        assignSessionId(session, sessionId);
069        storeSession(sessionId, session);
070        return sessionId;
071    }
072
073    protected Session storeSession(Serializable id, Session session) {
074        if (id == null) {
075            throw new NullPointerException("id argument cannot be null.");
076        }
077        return sessions.putIfAbsent(id, session);
078    }
079
080    protected Session doReadSession(Serializable sessionId) {
081        return sessions.get(sessionId);
082    }
083
084    public void update(Session session) throws UnknownSessionException {
085        storeSession(session.getId(), session);
086    }
087
088    public void delete(Session session) {
089        if (session == null) {
090            throw new NullPointerException("session argument cannot be null.");
091        }
092        Serializable id = session.getId();
093        if (id != null) {
094            sessions.remove(id);
095        }
096    }
097
098    public Collection<Session> getActiveSessions() {
099        Collection<Session> values = sessions.values();
100        if (CollectionUtils.isEmpty(values)) {
101            return Collections.emptySet();
102        } else {
103            return Collections.unmodifiableCollection(values);
104        }
105    }
106
107}