View Javadoc

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.quartz;
20  
21  import org.quartz.Job;
22  import org.quartz.JobDataMap;
23  import org.quartz.JobExecutionContext;
24  import org.quartz.JobExecutionException;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import org.apache.shiro.session.mgt.ValidatingSessionManager;
30  
31  /**
32   * A quartz job that basically just calls the {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions()}
33   * method on a configured session manager.  The session manager will automatically be injected by the
34   * superclass if it is in the job data map or the scheduler map.
35   *
36   * @since 0.1
37   */
38  public class QuartzSessionValidationJob implements Job {
39  
40      /*--------------------------------------------
41      |             C O N S T A N T S             |
42      ============================================*/
43      /**
44       * Key used to store the session manager in the job data map for this job.
45       */
46      static final String SESSION_MANAGER_KEY = "sessionManager";
47  
48      /*--------------------------------------------
49      |    I N S T A N C E   V A R I A B L E S    |
50      ============================================*/
51      private static final Logger log = LoggerFactory.getLogger(QuartzSessionValidationJob.class);
52  
53      /*--------------------------------------------
54      |         C O N S T R U C T O R S           |
55      ============================================*/
56  
57      /*--------------------------------------------
58      |  A C C E S S O R S / M O D I F I E R S    |
59      ============================================*/
60  
61      /*--------------------------------------------
62      |               M E T H O D S               |
63      ============================================*/
64  
65      /**
66       * Called when the job is executed by quartz.  This method delegates to the
67       * <tt>validateSessions()</tt> method on the associated session manager.
68       *
69       * @param context the Quartz job execution context for this execution.
70       */
71      public void execute(JobExecutionContext context) throws JobExecutionException {
72  
73          JobDataMap jobDataMap = context.getMergedJobDataMap();
74          ValidatingSessionManager sessionManager = (ValidatingSessionManager) jobDataMap.get(SESSION_MANAGER_KEY);
75  
76          if (log.isDebugEnabled()) {
77              log.debug("Executing session validation Quartz job...");
78          }
79  
80          sessionManager.validateSessions();
81  
82          if (log.isDebugEnabled()) {
83              log.debug("Session validation Quartz job complete.");
84          }
85      }
86  }