Coverage Report - org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
EnterpriseCacheSessionDAO
77%
7/9
N/A
1
EnterpriseCacheSessionDAO$1
50%
1/2
N/A
1
 
 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.session.mgt.eis;
 20  
 
 21  
 import org.apache.shiro.cache.AbstractCacheManager;
 22  
 import org.apache.shiro.cache.Cache;
 23  
 import org.apache.shiro.cache.CacheException;
 24  
 import org.apache.shiro.cache.MapCache;
 25  
 import org.apache.shiro.session.Session;
 26  
 
 27  
 import java.io.Serializable;
 28  
 import java.util.concurrent.ConcurrentHashMap;
 29  
 
 30  
 /**
 31  
  * SessionDAO implementation that relies on an enterprise caching product as the EIS system of record for all sessions.
 32  
  * It is expected that an injected {@link org.apache.shiro.cache.Cache Cache} or
 33  
  * {@link org.apache.shiro.cache.CacheManager CacheManager} is backed by an enterprise caching product that can support
 34  
  * all application sessions and/or provide disk paging for resilient data storage.
 35  
  * <h2>Production Note</h2>
 36  
  * This implementation defaults to using an in-memory map-based {@code CacheManager}, which is great for testing but
 37  
  * will typically not scale for production environments and could easily cause {@code OutOfMemoryException}s.  Just
 38  
  * don't forget to configure<b>*</b> an instance of this class with a production-grade {@code CacheManager} that can
 39  
  * handle disk paging for large numbers of sessions and you'll be fine.
 40  
  * <p/>
 41  
  * <b>*</b>If you configure Shiro's {@code SecurityManager} instance with such a {@code CacheManager}, it will be
 42  
  * automatically applied to an instance of this class and you won't need to explicitly set it in configuration.
 43  
  * <h3>Implementation Details</h3>
 44  
  * This implementation relies heavily on the {@link CachingSessionDAO parent class}'s transparent caching behavior for
 45  
  * all storage operations with the enterprise caching product.  Because the parent class uses a {@code Cache} or
 46  
  * {@code CacheManager} to perform caching, and the cache is considered the system of record, nothing further needs to
 47  
  * be done for the {@link #doReadSession}, {@link #doUpdate} and {@link #doDelete} method implementations.  This class
 48  
  * implements those methods as required by the parent class, but they essentially do nothing.
 49  
  *
 50  
  * @since 1.0
 51  
  */
 52  
 public class EnterpriseCacheSessionDAO extends CachingSessionDAO {
 53  
 
 54  1
     public EnterpriseCacheSessionDAO() {
 55  1
         setCacheManager(new AbstractCacheManager() {
 56  
             @Override
 57  
             protected Cache<Serializable, Session> createCache(String name) throws CacheException {
 58  0
                 return new MapCache<Serializable, Session>(name, new ConcurrentHashMap<Serializable, Session>());
 59  
             }
 60  
         });
 61  1
     }
 62  
 
 63  
     protected Serializable doCreate(Session session) {
 64  1
         Serializable sessionId = generateSessionId(session);
 65  1
         assignSessionId(session, sessionId);
 66  1
         return sessionId;
 67  
     }
 68  
 
 69  
     protected Session doReadSession(Serializable sessionId) {
 70  0
         return null; //should never execute because this implementation relies on parent class to access cache, which
 71  
         //is where all sessions reside - it is the cache implementation that determines if the
 72  
         //cache is memory only or disk-persistent, etc.
 73  
     }
 74  
 
 75  
     protected void doUpdate(Session session) {
 76  
         //does nothing - parent class persists to cache.
 77  4
     }
 78  
 
 79  
     protected void doDelete(Session session) {
 80  
         //does nothing - parent class removes from cache.
 81  0
     }
 82  
 }