blob: 5f07672021340e47762aaf7e70925ab16244e2d1 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>QuartzSessionValidationScheduler.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> &gt; <a href="../index.html" class="el_bundle">shiro-quartz</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.session.mgt.quartz</a> &gt; <span class="el_source">QuartzSessionValidationScheduler.java</span></div><h1>QuartzSessionValidationScheduler.java</h1><pre class="source lang-java linenums">/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* &quot;License&quot;); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.shiro.session.mgt.quartz;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionValidationScheduler;
import org.apache.shiro.session.mgt.ValidatingSessionManager;
/**
* An implementation of the {@link org.apache.shiro.session.mgt.SessionValidationScheduler SessionValidationScheduler} that uses Quartz to schedule a
* job to call {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions()} on
* a regular basis.
*
* @since 0.1
*/
public class QuartzSessionValidationScheduler implements SessionValidationScheduler {
//TODO - complete JavaDoc
/*--------------------------------------------
| C O N S T A N T S |
============================================*/
/**
* The default interval at which sessions will be validated (1 hour);
* This can be overridden by calling {@link #setSessionValidationInterval(long)}
*/
public static final long DEFAULT_SESSION_VALIDATION_INTERVAL = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL;
/**
* The name assigned to the quartz job.
*/
private static final String JOB_NAME = &quot;SessionValidationJob&quot;;
/*--------------------------------------------
| I N S T A N C E V A R I A B L E S |
============================================*/
<span class="nc" id="L63"> private static final Logger log = LoggerFactory.getLogger(QuartzSessionValidationScheduler.class);</span>
/**
* The configured Quartz scheduler to use to schedule the Quartz job. If no scheduler is
* configured, the scheduler will be retrieved by calling {@link StdSchedulerFactory#getDefaultScheduler()}
*/
private Scheduler scheduler;
<span class="nc" id="L71"> private boolean schedulerImplicitlyCreated = false;</span>
<span class="nc" id="L73"> private boolean enabled = false;</span>
/**
* The session manager used to validate sessions.
*/
private ValidatingSessionManager sessionManager;
/**
* The session validation interval in milliseconds.
*/
<span class="nc" id="L83"> private long sessionValidationInterval = DEFAULT_SESSION_VALIDATION_INTERVAL;</span>
/*--------------------------------------------
| C O N S T R U C T O R S |
============================================*/
/**
* Default constructor.
*/
<span class="nc" id="L92"> public QuartzSessionValidationScheduler() {</span>
<span class="nc" id="L93"> }</span>
/**
* Constructor that specifies the session manager that should be used for validating sessions.
*
* @param sessionManager the &lt;tt&gt;SessionManager&lt;/tt&gt; that should be used to validate sessions.
*/
<span class="nc" id="L100"> public QuartzSessionValidationScheduler(ValidatingSessionManager sessionManager) {</span>
<span class="nc" id="L101"> this.sessionManager = sessionManager;</span>
<span class="nc" id="L102"> }</span>
/*--------------------------------------------
| A C C E S S O R S / M O D I F I E R S |
============================================*/
protected Scheduler getScheduler() throws SchedulerException {
<span class="nc bnc" id="L109" title="All 2 branches missed."> if (scheduler == null) {</span>
<span class="nc" id="L110"> scheduler = StdSchedulerFactory.getDefaultScheduler();</span>
<span class="nc" id="L111"> schedulerImplicitlyCreated = true;</span>
}
<span class="nc" id="L113"> return scheduler;</span>
}
public void setScheduler(Scheduler scheduler) {
<span class="nc" id="L117"> this.scheduler = scheduler;</span>
<span class="nc" id="L118"> }</span>
public void setSessionManager(ValidatingSessionManager sessionManager) {
<span class="nc" id="L121"> this.sessionManager = sessionManager;</span>
<span class="nc" id="L122"> }</span>
public boolean isEnabled() {
<span class="nc" id="L125"> return this.enabled;</span>
}
/**
* Specifies how frequently (in milliseconds) this Scheduler will call the
* {@link org.apache.shiro.session.mgt.ValidatingSessionManager#validateSessions() ValidatingSessionManager#validateSessions()} method.
*
* &lt;p&gt;Unless this method is called, the default value is {@link #DEFAULT_SESSION_VALIDATION_INTERVAL}.
*
* @param sessionValidationInterval
*/
public void setSessionValidationInterval(long sessionValidationInterval) {
<span class="nc" id="L137"> this.sessionValidationInterval = sessionValidationInterval;</span>
<span class="nc" id="L138"> }</span>
/*--------------------------------------------
| M E T H O D S |
============================================*/
/**
* Starts session validation by creating a Quartz simple trigger, linking it to
* the {@link QuartzSessionValidationJob}, and scheduling it with the Quartz scheduler.
*/
public void enableSessionValidation() {
<span class="nc bnc" id="L150" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L151"> log.debug(&quot;Scheduling session validation job using Quartz with &quot; +</span>
&quot;session validation interval of [&quot; + sessionValidationInterval + &quot;]ms...&quot;);
}
try {
<span class="nc" id="L156"> SimpleTrigger trigger = new SimpleTrigger(getClass().getName(),</span>
Scheduler.DEFAULT_GROUP,
SimpleTrigger.REPEAT_INDEFINITELY,
sessionValidationInterval);
<span class="nc" id="L161"> JobDetail detail = new JobDetail(JOB_NAME, Scheduler.DEFAULT_GROUP, QuartzSessionValidationJob.class);</span>
<span class="nc" id="L162"> detail.getJobDataMap().put(QuartzSessionValidationJob.SESSION_MANAGER_KEY, sessionManager);</span>
<span class="nc" id="L164"> Scheduler scheduler = getScheduler();</span>
<span class="nc" id="L166"> scheduler.scheduleJob(detail, trigger);</span>
<span class="nc bnc" id="L167" title="All 2 branches missed."> if (schedulerImplicitlyCreated) {</span>
<span class="nc" id="L168"> scheduler.start();</span>
<span class="nc bnc" id="L169" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L170"> log.debug(&quot;Successfully started implicitly created Quartz Scheduler instance.&quot;);</span>
}
}
<span class="nc" id="L173"> this.enabled = true;</span>
<span class="nc bnc" id="L175" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L176"> log.debug(&quot;Session validation job successfully scheduled with Quartz.&quot;);</span>
}
<span class="nc" id="L179"> } catch (SchedulerException e) {</span>
<span class="nc bnc" id="L180" title="All 2 branches missed."> if (log.isErrorEnabled()) {</span>
<span class="nc" id="L181"> log.error(&quot;Error starting the Quartz session validation job. Session validation may not occur.&quot;, e);</span>
}
<span class="nc" id="L183"> }</span>
<span class="nc" id="L184"> }</span>
public void disableSessionValidation() {
<span class="nc bnc" id="L187" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L188"> log.debug(&quot;Stopping Quartz session validation job...&quot;);</span>
}
Scheduler scheduler;
try {
<span class="nc" id="L193"> scheduler = getScheduler();</span>
<span class="nc bnc" id="L194" title="All 2 branches missed."> if (scheduler == null) {</span>
<span class="nc bnc" id="L195" title="All 2 branches missed."> if (log.isWarnEnabled()) {</span>
<span class="nc" id="L196"> log.warn(&quot;getScheduler() method returned a null Quartz scheduler, which is unexpected. Please &quot; +</span>
&quot;check your configuration and/or implementation. Returning quietly since there is no &quot; +
&quot;validation job to remove (scheduler does not exist).&quot;);
}
<span class="nc" id="L200"> return;</span>
}
<span class="nc" id="L202"> } catch (SchedulerException e) {</span>
<span class="nc bnc" id="L203" title="All 2 branches missed."> if (log.isWarnEnabled()) {</span>
<span class="nc" id="L204"> log.warn(&quot;Unable to acquire Quartz Scheduler. Ignoring and returning (already stopped?)&quot;, e);</span>
}
<span class="nc" id="L206"> return;</span>
<span class="nc" id="L207"> }</span>
try {
<span class="nc" id="L210"> scheduler.unscheduleJob(JOB_NAME, Scheduler.DEFAULT_GROUP);</span>
<span class="nc bnc" id="L211" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L212"> log.debug(&quot;Quartz session validation job stopped successfully.&quot;);</span>
}
<span class="nc" id="L214"> } catch (SchedulerException e) {</span>
<span class="nc bnc" id="L215" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span>
<span class="nc" id="L216"> log.debug(&quot;Could not cleanly remove SessionValidationJob from Quartz scheduler. &quot; +</span>
&quot;Ignoring and stopping.&quot;, e);
}
<span class="nc" id="L219"> }</span>
<span class="nc" id="L221"> this.enabled = false;</span>
<span class="nc bnc" id="L223" title="All 2 branches missed."> if (schedulerImplicitlyCreated) {</span>
try {
<span class="nc" id="L225"> scheduler.shutdown();</span>
<span class="nc" id="L226"> } catch (SchedulerException e) {</span>
<span class="nc bnc" id="L227" title="All 2 branches missed."> if (log.isWarnEnabled()) {</span>
<span class="nc" id="L228"> log.warn(&quot;Unable to cleanly shutdown implicitly created Quartz Scheduler instance.&quot;, e);</span>
}
} finally {
<span class="nc" id="L231"> setScheduler(null);</span>
<span class="nc" id="L232"> schedulerImplicitlyCreated = false;</span>
<span class="nc" id="L233"> }</span>
}
<span class="nc" id="L237"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>