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;
020
021import org.apache.shiro.session.Session;
022
023/**
024 * Base abstract class of the {@link SessionManager SessionManager} interface, enabling configuration of an
025 * application-wide {@link #getGlobalSessionTimeout() globalSessionTimeout}.  Default global session timeout is
026 * {@code 30} minutes.
027 *
028 * @since 0.1
029 */
030//TODO - deprecate this class (see SHIRO-240):
031//This is only here to make available a common attribute 'globalSessionTimeout' to subclasses, particularly to make it
032//available to both AbstractNativeSessionManager and ServletContainerSessionManager subclass trees.  However, the
033//ServletContainerSessionManager implementation does not use this value
034//(see https://issues.apache.org/jira/browse/SHIRO-240 for why).  That means that only the Native session managers
035//need a globalSessionTimeout property, making this class unnecessary.
036public abstract class AbstractSessionManager implements SessionManager {
037
038    protected static final long MILLIS_PER_SECOND = 1000;
039    protected static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
040    protected static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
041
042    /**
043     * Default main session timeout value, equal to {@code 30} minutes.
044     */
045    public static final long DEFAULT_GLOBAL_SESSION_TIMEOUT = 30 * MILLIS_PER_MINUTE;
046
047    private long globalSessionTimeout = DEFAULT_GLOBAL_SESSION_TIMEOUT;
048
049    public AbstractSessionManager() {
050    }
051
052    /**
053     * Returns the system-wide default time in milliseconds that any session may remain idle before expiring. This
054     * value is the main default for all sessions and may be overridden on a <em>per-session</em> basis by calling
055     * {@code Subject.getSession().}{@link Session#setTimeout setTimeout(long)} if so desired.
056     * <ul>
057     * <li>A negative return value means sessions never expire.</li>
058     * <li>A non-negative return value (0 or greater) means session timeout will occur as expected.</li>
059     * </ul>
060     * <p/>
061     * Unless overridden via the {@link #setGlobalSessionTimeout} method, the default value is
062     * {@link #DEFAULT_GLOBAL_SESSION_TIMEOUT}.
063     *
064     * @return the time in milliseconds that any session may remain idle before expiring.
065     */
066    public long getGlobalSessionTimeout() {
067        return this.globalSessionTimeout;
068    }
069
070    /**
071     * Sets the system-wide default time in milliseconds that any session may remain idle before expiring. This
072     * value is the main default for all sessions and may be overridden on a <em>per-session</em> basis by calling
073     * {@code Subject.getSession().}{@link Session#setTimeout setTimeout(long)} if so desired.
074     * <p/>
075     * <ul>
076     * <li>A negative return value means sessions never expire.</li>
077     * <li>A non-negative return value (0 or greater) means session timeout will occur as expected.</li>
078     * </ul>
079     * <p/>
080     * Unless overridden by calling this method, the default value is {@link #DEFAULT_GLOBAL_SESSION_TIMEOUT}.
081     *
082     * @param globalSessionTimeout the time in milliseconds that any session may remain idel before expiring.
083     */
084    public void setGlobalSessionTimeout(long globalSessionTimeout) {
085        this.globalSessionTimeout = globalSessionTimeout;
086    }
087}