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.cache.AbstractCacheManager;
022import org.apache.shiro.cache.Cache;
023import org.apache.shiro.cache.CacheException;
024import org.apache.shiro.cache.MapCache;
025import org.apache.shiro.session.Session;
026
027import java.io.Serializable;
028import java.util.concurrent.ConcurrentHashMap;
029
030/**
031 * SessionDAO implementation that relies on an enterprise caching product as the EIS system of record for all sessions.
032 * It is expected that an injected {@link org.apache.shiro.cache.Cache Cache} or
033 * {@link org.apache.shiro.cache.CacheManager CacheManager} is backed by an enterprise caching product that can support
034 * all application sessions and/or provide disk paging for resilient data storage.
035 * <h2>Production Note</h2>
036 * This implementation defaults to using an in-memory map-based {@code CacheManager}, which is great for testing but
037 * will typically not scale for production environments and could easily cause {@code OutOfMemoryException}s.  Just
038 * don't forget to configure<b>*</b> an instance of this class with a production-grade {@code CacheManager} that can
039 * handle disk paging for large numbers of sessions and you'll be fine.
040 * <p/>
041 * <b>*</b>If you configure Shiro's {@code SecurityManager} instance with such a {@code CacheManager}, it will be
042 * automatically applied to an instance of this class and you won't need to explicitly set it in configuration.
043 * <h3>Implementation Details</h3>
044 * This implementation relies heavily on the {@link CachingSessionDAO parent class}'s transparent caching behavior for
045 * all storage operations with the enterprise caching product.  Because the parent class uses a {@code Cache} or
046 * {@code CacheManager} to perform caching, and the cache is considered the system of record, nothing further needs to
047 * be done for the {@link #doReadSession}, {@link #doUpdate} and {@link #doDelete} method implementations.  This class
048 * implements those methods as required by the parent class, but they essentially do nothing.
049 *
050 * @since 1.0
051 */
052public class EnterpriseCacheSessionDAO extends CachingSessionDAO {
053
054    public EnterpriseCacheSessionDAO() {
055        setCacheManager(new AbstractCacheManager() {
056            @Override
057            protected Cache<Serializable, Session> createCache(String name) throws CacheException {
058                return new MapCache<Serializable, Session>(name, new ConcurrentHashMap<Serializable, Session>());
059            }
060        });
061    }
062
063    protected Serializable doCreate(Session session) {
064        Serializable sessionId = generateSessionId(session);
065        assignSessionId(session, sessionId);
066        return sessionId;
067    }
068
069    protected Session doReadSession(Serializable sessionId) {
070        return null; //should never execute because this implementation relies on parent class to access cache, which
071        //is where all sessions reside - it is the cache implementation that determines if the
072        //cache is memory only or disk-persistent, etc.
073    }
074
075    protected void doUpdate(Session session) {
076        //does nothing - parent class persists to cache.
077    }
078
079    protected void doDelete(Session session) {
080        //does nothing - parent class removes from cache.
081    }
082}