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.quartz;
020    
021    import org.quartz.Job;
022    import org.quartz.JobDataMap;
023    import org.quartz.JobExecutionContext;
024    import org.quartz.JobExecutionException;
025    
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    import org.apache.shiro.session.mgt.ValidatingSessionManager;
030    
031    /**
032     * A quartz job that basically just calls the {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions()}
033     * method on a configured session manager.  The session manager will automatically be injected by the
034     * superclass if it is in the job data map or the scheduler map.
035     *
036     * @since 0.1
037     */
038    public class QuartzSessionValidationJob implements Job {
039    
040        /*--------------------------------------------
041        |             C O N S T A N T S             |
042        ============================================*/
043        /**
044         * Key used to store the session manager in the job data map for this job.
045         */
046        static final String SESSION_MANAGER_KEY = "sessionManager";
047    
048        /*--------------------------------------------
049        |    I N S T A N C E   V A R I A B L E S    |
050        ============================================*/
051        private static final Logger log = LoggerFactory.getLogger(QuartzSessionValidationJob.class);
052    
053        /*--------------------------------------------
054        |         C O N S T R U C T O R S           |
055        ============================================*/
056    
057        /*--------------------------------------------
058        |  A C C E S S O R S / M O D I F I E R S    |
059        ============================================*/
060    
061        /*--------------------------------------------
062        |               M E T H O D S               |
063        ============================================*/
064    
065        /**
066         * Called when the job is executed by quartz.  This method delegates to the
067         * <tt>validateSessions()</tt> method on the associated session manager.
068         *
069         * @param context the Quartz job execution context for this execution.
070         */
071        public void execute(JobExecutionContext context) throws JobExecutionException {
072    
073            JobDataMap jobDataMap = context.getMergedJobDataMap();
074            ValidatingSessionManager sessionManager = (ValidatingSessionManager) jobDataMap.get(SESSION_MANAGER_KEY);
075    
076            if (log.isDebugEnabled()) {
077                log.debug("Executing session validation Quartz job...");
078            }
079    
080            sessionManager.validateSessions();
081    
082            if (log.isDebugEnabled()) {
083                log.debug("Session validation Quartz job complete.");
084            }
085        }
086    }