blob: d8561469e4a386f6dcfba25fea85d457c28bf84d [file] [log] [blame]
/*
* Copyright 2005-2008 Les Hazlewood
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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 "AS IS" 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.jsecurity.session.mgt;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* SessionValidationScheduler implementation that uses a
* {@link ScheduledExecutorService} to call {@link ValidatingSessionManager#validateSessions()} every
* <em>{@link #getInterval interval}</em> milliseconds.
*
* @author Les Hazlewood
* @since 0.9
*/
public class ExecutorServiceSessionValidationScheduler implements SessionValidationScheduler, Runnable {
protected transient final Log log = LogFactory.getLog(getClass());
ValidatingSessionManager sessionManager;
private ScheduledExecutorService service;
private long interval = DefaultSessionManager.DEFAULT_SESSION_VALIDATION_INTERVAL;
public ExecutorServiceSessionValidationScheduler() {
super();
}
public ExecutorServiceSessionValidationScheduler( ValidatingSessionManager sessionManager ) {
this.sessionManager = sessionManager;
}
public ValidatingSessionManager getSessionManager() {
return sessionManager;
}
public void setSessionManager(ValidatingSessionManager sessionManager) {
this.sessionManager = sessionManager;
}
public long getInterval() {
return interval;
}
public void setInterval(long interval) {
this.interval = interval;
}
public void startSessionValidation() {
if ( this.interval > 0l ) {
this.service = Executors.newSingleThreadScheduledExecutor();
this.service.scheduleAtFixedRate(this, interval, interval, TimeUnit.MILLISECONDS );
}
}
public void run() {
if( log.isDebugEnabled() ) {
log.debug( "Executing session validation..." );
}
long startTime = System.currentTimeMillis();
this.sessionManager.validateSessions();
long stopTime = System.currentTimeMillis();
if ( log.isDebugEnabled() ) {
log.debug( "Session validation completed successfully in " + (stopTime - startTime) + " milliseconds.");
}
}
public void stopSessionValidation() {
this.service.shutdownNow();
}
}