
org.simpleframework.http.session.Maintainer Maven / Gradle / Ivy
/*
* Maintainer.java May 2007
*
* Copyright (C) 2007, Niall Gallagher
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
package org.simpleframework.http.session;
import java.util.concurrent.TimeUnit;
import org.simpleframework.util.lease.Lease;
import org.simpleframework.util.lease.LeaseException;
import org.simpleframework.util.lease.LeaseManager;
import org.simpleframework.util.lease.LeaseMap;
/**
* The Maintainer
object is used to manage all life cycle
* events for sessions. This is used to start and renew all leases
* issued. The start life cycle event is used when the session is first
* created, this will lease the new session for some fixed duration.
* The renew event is performed when an existing session is accessed
* again so that the session can be maintained for the default period.
*
* @author Niall Gallagher
*/
class Maintainer implements Controller {
/**
* This is the lease manager used to create the lease objects.
*/
private final LeaseManager manager;
/**
* This is the lease map used to maintain the leases by key.
*/
private final LeaseMap map;
/**
* This is the time unit used for the fixed lease duration.
*/
private final TimeUnit unit;
/**
* This is the duration the lease objects are renewed for.
*/
private final long duration;
/**
* Constructor for the Maintainer
object. This is used
* to maintain a lease for a resource using only the key for that
* resource. Each renewal of the lease is performed using a fixed
* duration so that the the time does not need to be specified.
*
* @param manager this is the manager used to create the leases
* @param duration this is the fixed duration to renew for
* @param unit this is the unit of measurement for the duration
*/
public Maintainer(LeaseManager manager, long duration, TimeUnit unit) {
this.map = new LeaseMap();
this.manager = manager;
this.duration = duration;
this.unit = unit;
}
/**
* The start
method is used when a session is to
* be created for the first time. This will ensure that the key
* specified is used to dispose of the session when its idle
* timeout has expired.
*
* @param key this is the unique key identifying the session
*/
public Lease start(T key) throws LeaseException {
Lease lease = manager.lease(key, duration, unit);
if(lease != null) {
map.put(key, lease);
}
return lease;
}
/**
* The renew
method is used when a session has been
* accessed for a again. This ensures that the key specified is
* used to dispose of the session when its idle timeout expires.
*
* @param key this is the unique key identifying the session
*/
public void renew(T key) throws LeaseException {
Lease lease = map.get(key);
if(lease == null) {
throw new SessionException("Session does not exist");
}
lease.renew(duration, unit);
}
/**
* The cancel
method is used when a session is no
* longer required and is to be disposed of. This is typically
* invoked by a web application to release occupied resources.
*
* @param key this is the unique key identifying the session
*/
public void cancel(T key) throws LeaseException {
Lease lease = map.remove(key);
if(lease == null) {
throw new SessionException("Session does not exist");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy