Coverage Report - org.apache.shiro.session.mgt.DelegatingSession
 
Classes in this File Line Coverage Branch Coverage Complexity
DelegatingSession
62%
22/35
33%
4/12
1.692
 
 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;
 20  
 
 21  
 import org.apache.shiro.session.InvalidSessionException;
 22  
 import org.apache.shiro.session.Session;
 23  
 
 24  
 import java.io.Serializable;
 25  
 import java.util.Collection;
 26  
 import java.util.Date;
 27  
 
 28  
 /**
 29  
  * A DelegatingSession is a client-tier representation of a server side
 30  
  * {@link org.apache.shiro.session.Session Session}.
 31  
  * This implementation is basically a proxy to a server-side {@link NativeSessionManager NativeSessionManager},
 32  
  * which will return the proper results for each method call.
 33  
  * <p/>
 34  
  * <p>A <tt>DelegatingSession</tt> will cache data when appropriate to avoid a remote method invocation,
 35  
  * only communicating with the server when necessary.
 36  
  * <p/>
 37  
  * <p>Of course, if used in-process with a NativeSessionManager business POJO, as might be the case in a
 38  
  * web-based application where the web classes and server-side business pojos exist in the same
 39  
  * JVM, a remote method call will not be incurred.
 40  
  *
 41  
  * @since 0.1
 42  
  */
 43  
 public class DelegatingSession implements Session, Serializable {
 44  
 
 45  
     //TODO - complete JavaDoc
 46  
 
 47  
     private final SessionKey key;
 48  
 
 49  
     //cached fields to avoid a server-side method call if out-of-process:
 50  29
     private Date startTimestamp = null;
 51  29
     private String host = null;
 52  
 
 53  
     /**
 54  
      * Handle to the target NativeSessionManager that will support the delegate calls.
 55  
      */
 56  
     private final transient NativeSessionManager sessionManager;
 57  
 
 58  
 
 59  29
     public DelegatingSession(NativeSessionManager sessionManager, SessionKey key) {
 60  29
         if (sessionManager == null) {
 61  0
             throw new IllegalArgumentException("sessionManager argument cannot be null.");
 62  
         }
 63  29
         if (key == null) {
 64  0
             throw new IllegalArgumentException("sessionKey argument cannot be null.");
 65  
         }
 66  29
         if (key.getSessionId() == null) {
 67  0
             String msg = "The " + DelegatingSession.class.getName() + " implementation requires that the " +
 68  
                     "SessionKey argument returns a non-null sessionId to support the " +
 69  
                     "Session.getId() invocations.";
 70  0
             throw new IllegalArgumentException(msg);
 71  
         }
 72  29
         this.sessionManager = sessionManager;
 73  29
         this.key = key;
 74  29
     }
 75  
 
 76  
     /**
 77  
      * @see org.apache.shiro.session.Session#getId()
 78  
      */
 79  
     public Serializable getId() {
 80  167
         return key.getSessionId();
 81  
     }
 82  
 
 83  
     /**
 84  
      * @see org.apache.shiro.session.Session#getStartTimestamp()
 85  
      */
 86  
     public Date getStartTimestamp() {
 87  0
         if (startTimestamp == null) {
 88  0
             startTimestamp = sessionManager.getStartTimestamp(key);
 89  
         }
 90  0
         return startTimestamp;
 91  
     }
 92  
 
 93  
     /**
 94  
      * @see org.apache.shiro.session.Session#getLastAccessTime()
 95  
      */
 96  
     public Date getLastAccessTime() {
 97  
         //can't cache - only business pojo knows the accurate time:
 98  0
         return sessionManager.getLastAccessTime(key);
 99  
     }
 100  
 
 101  
     public long getTimeout() throws InvalidSessionException {
 102  4
         return sessionManager.getTimeout(key);
 103  
     }
 104  
 
 105  
     public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
 106  4
         sessionManager.setTimeout(key, maxIdleTimeInMillis);
 107  3
     }
 108  
 
 109  
     public String getHost() {
 110  0
         if (host == null) {
 111  0
             host = sessionManager.getHost(key);
 112  
         }
 113  0
         return host;
 114  
     }
 115  
 
 116  
     /**
 117  
      * @see org.apache.shiro.session.Session#touch()
 118  
      */
 119  
     public void touch() throws InvalidSessionException {
 120  1
         sessionManager.touch(key);
 121  1
     }
 122  
 
 123  
     /**
 124  
      * @see org.apache.shiro.session.Session#stop()
 125  
      */
 126  
     public void stop() throws InvalidSessionException {
 127  10
         sessionManager.stop(key);
 128  10
     }
 129  
 
 130  
     /**
 131  
      * @see org.apache.shiro.session.Session#getAttributeKeys
 132  
      */
 133  
     public Collection<Object> getAttributeKeys() throws InvalidSessionException {
 134  0
         return sessionManager.getAttributeKeys(key);
 135  
     }
 136  
 
 137  
     /**
 138  
      * @see org.apache.shiro.session.Session#getAttribute(Object key)
 139  
      */
 140  
     public Object getAttribute(Object attributeKey) throws InvalidSessionException {
 141  107
         return sessionManager.getAttribute(this.key, attributeKey);
 142  
     }
 143  
 
 144  
     /**
 145  
      * @see Session#setAttribute(Object key, Object value)
 146  
      */
 147  
     public void setAttribute(Object attributeKey, Object value) throws InvalidSessionException {
 148  44
         if (value == null) {
 149  0
             removeAttribute(attributeKey);
 150  
         } else {
 151  44
             sessionManager.setAttribute(this.key, attributeKey, value);
 152  
         }
 153  44
     }
 154  
 
 155  
     /**
 156  
      * @see Session#removeAttribute(Object key)
 157  
      */
 158  
     public Object removeAttribute(Object attributeKey) throws InvalidSessionException {
 159  28
         return sessionManager.removeAttribute(this.key, attributeKey);
 160  
     }
 161  
 }