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;
20  
21  import java.util.concurrent.Executors;
22  import java.util.concurrent.ScheduledExecutorService;
23  import java.util.concurrent.ThreadFactory;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * SessionValidationScheduler implementation that uses a
32   * {@link ScheduledExecutorService} to call {@link ValidatingSessionManager#validateSessions()} every
33   * <em>{@link #getInterval interval}</em> milliseconds.
34   *
35   * @since 0.9
36   */
37  public class ExecutorServiceSessionValidationScheduler implements SessionValidationScheduler, Runnable {
38  
39      //TODO - complete JavaDoc
40  
41      /** Private internal log instance. */
42      private static final Logger log = LoggerFactory.getLogger(ExecutorServiceSessionValidationScheduler.class);
43  
44      ValidatingSessionManager sessionManager;
45      private ScheduledExecutorService service;
46      private long interval = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL;
47      private boolean enabled = false;
48  
49      public ExecutorServiceSessionValidationScheduler() {
50          super();
51      }
52  
53      public ExecutorServiceSessionValidationScheduler(ValidatingSessionManager sessionManager) {
54          this.sessionManager = sessionManager;
55      }
56  
57      public ValidatingSessionManager getSessionManager() {
58          return sessionManager;
59      }
60  
61      public void setSessionManager(ValidatingSessionManager sessionManager) {
62          this.sessionManager = sessionManager;
63      }
64  
65      public long getInterval() {
66          return interval;
67      }
68  
69      public void setInterval(long interval) {
70          this.interval = interval;
71      }
72  
73      public boolean isEnabled() {
74          return this.enabled;
75      }
76  
77      /**
78       * Creates a single thread {@link ScheduledExecutorService} to validate sessions at fixed intervals 
79       * and enables this scheduler. The executor is created as a daemon thread to allow JVM to shut down
80       */
81      //TODO Implement an integration test to test for jvm exit as part of the standalone example
82      // (so we don't have to change the unit test execution model for the core module)
83      public void enableSessionValidation() {
84          if (this.interval > 0l) {
85              this.service = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {  
86  	        public Thread newThread(Runnable r) {  
87  	            Thread thread = new Thread(r);  
88  	            thread.setDaemon(true);  
89  	            return thread;  
90                  }  
91              });                  
92              this.service.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS);
93              this.enabled = true;
94          }
95      }
96  
97      public void run() {
98          if (log.isDebugEnabled()) {
99              log.debug("Executing session validation...");
100         }
101         long startTime = System.currentTimeMillis();
102         this.sessionManager.validateSessions();
103         long stopTime = System.currentTimeMillis();
104         if (log.isDebugEnabled()) {
105             log.debug("Session validation completed successfully in " + (stopTime - startTime) + " milliseconds.");
106         }
107     }
108 
109     public void disableSessionValidation() {
110         this.service.shutdownNow();
111         this.enabled = false;
112     }
113 }