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     */
019    package org.apache.shiro.session.mgt;
020    
021    import 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     * @author Les Hazlewood
029     * @since 0.1
030     */
031    public abstract class AbstractSessionManager implements SessionManager {
032    
033        protected static final long MILLIS_PER_SECOND = 1000;
034        protected static final long MILLIS_PER_MINUTE = 60 * MILLIS_PER_SECOND;
035        protected static final long MILLIS_PER_HOUR = 60 * MILLIS_PER_MINUTE;
036    
037        /**
038         * Default main session timeout value, equal to {@code 30} minutes.
039         */
040        public static final long DEFAULT_GLOBAL_SESSION_TIMEOUT = 30 * MILLIS_PER_MINUTE;
041    
042        private long globalSessionTimeout = DEFAULT_GLOBAL_SESSION_TIMEOUT;
043    
044        public AbstractSessionManager() {
045        }
046    
047        /**
048         * Returns the system-wide default time in milliseconds that any session may remain idle before expiring. This
049         * value is the main default for all sessions and may be overridden on a <em>per-session</em> basis by calling
050         * {@code Subject.getSession().}{@link Session#setTimeout setTimeout(long)} if so desired.
051         * <ul>
052         * <li>A negative return value means sessions never expire.</li>
053         * <li>A non-negative return value (0 or greater) means session timeout will occur as expected.</li>
054         * </ul>
055         * <p/>
056         * Unless overridden via the {@link #setGlobalSessionTimeout} method, the default value is
057         * {@link #DEFAULT_GLOBAL_SESSION_TIMEOUT}.
058         *
059         * @return the time in milliseconds that any session may remain idle before expiring.
060         */
061        public long getGlobalSessionTimeout() {
062            return this.globalSessionTimeout;
063        }
064    
065        /**
066         * Sets the system-wide default time in milliseconds that any session may remain idle before expiring. This
067         * value is the main default for all sessions and may be overridden on a <em>per-session</em> basis by calling
068         * {@code Subject.getSession().}{@link Session#setTimeout setTimeout(long)} if so desired.
069         * <p/>
070         * <ul>
071         * <li>A negative return value means sessions never expire.</li>
072         * <li>A non-negative return value (0 or greater) means session timeout will occur as expected.</li>
073         * </ul>
074         * <p/>
075         * Unless overridden by calling this method, the default value is {@link #DEFAULT_GLOBAL_SESSION_TIMEOUT}.
076         *
077         * @param globalSessionTimeout the time in milliseconds that any session may remain idel before expiring.
078         */
079        public void setGlobalSessionTimeout(long globalSessionTimeout) {
080            this.globalSessionTimeout = globalSessionTimeout;
081        }
082    }