📄 persistentmanagerbase.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/session/PersistentManagerBase.java,v 1.17 2004/02/06 11:51:13 remm Exp $
* $Revision: 1.17 $
* $Date: 2004/02/06 11:51:13 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.catalina.session;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Session;
import org.apache.catalina.Store;
import org.apache.catalina.util.LifecycleSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Extends the <b>ManagerBase</b> class to implement most of the
* functionality required by a Manager which supports any kind of
* persistence, even if onlyfor restarts.
* <p>
* <b>IMPLEMENTATION NOTE</b>: Correct behavior of session storing and
* reloading depends upon external calls to the <code>start()</code> and
* <code>stop()</code> methods of this class at the correct times.
*
* @author Craig R. McClanahan
* @author Jean-Francois Arcand
* @version $Revision: 1.17 $ $Date: 2004/02/06 11:51:13 $
*/
public abstract class PersistentManagerBase
extends ManagerBase
implements Lifecycle, PropertyChangeListener {
private static Log log = LogFactory.getLog(PersistentManagerBase.class);
// ---------------------------------------------------- Security Classes
private class PrivilegedStoreClear
implements PrivilegedExceptionAction {
PrivilegedStoreClear() {
}
public Object run() throws Exception{
store.clear();
return null;
}
}
private class PrivilegedStoreRemove
implements PrivilegedExceptionAction {
private String id;
PrivilegedStoreRemove(String id) {
this.id = id;
}
public Object run() throws Exception{
store.remove(id);
return null;
}
}
private class PrivilegedStoreLoad
implements PrivilegedExceptionAction {
private String id;
PrivilegedStoreLoad(String id) {
this.id = id;
}
public Object run() throws Exception{
return store.load(id);
}
}
private class PrivilegedStoreSave
implements PrivilegedExceptionAction {
private Session session;
PrivilegedStoreSave(Session session) {
this.session = session;
}
public Object run() throws Exception{
store.save(session);
return null;
}
}
private class PrivilegedStoreKeys
implements PrivilegedExceptionAction {
PrivilegedStoreKeys() {
}
public Object run() throws Exception{
return store.keys();
}
}
// ----------------------------------------------------- Instance Variables
/**
* The descriptive information about this implementation.
*/
private static final String info = "PersistentManagerBase/1.0";
/**
* The lifecycle event support for this component.
*/
protected LifecycleSupport lifecycle = new LifecycleSupport(this);
/**
* The maximum number of active Sessions allowed, or -1 for no limit.
*/
protected int maxActiveSessions = -1;
/**
* The descriptive name of this Manager implementation (for logging).
*/
private static String name = "PersistentManagerBase";
/**
* Has this component been started yet?
*/
protected boolean started = false;
/**
* Store object which will manage the Session store.
*/
protected Store store = null;
/**
* Whether to save and reload sessions when the Manager <code>unload</code>
* and <code>load</code> methods are called.
*/
protected boolean saveOnRestart = true;
/**
* How long a session must be idle before it should be backed up.
* -1 means sessions won't be backed up.
*/
protected int maxIdleBackup = -1;
/**
* Minimum time a session must be idle before it is swapped to disk.
* This overrides maxActiveSessions, to prevent thrashing if there are lots
* of active sessions. Setting to -1 means it's ignored.
*/
protected int minIdleSwap = -1;
/**
* The maximum time a session may be idle before it should be swapped
* to file just on general principle. Setting this to -1 means sessions
* should not be forced out.
*/
protected int maxIdleSwap = -1;
/**
* Number of session creations that failed due to maxActiveSessions.
*/
protected int rejectedSessions = 0;
/**
* Number of sessions that expired.
*/
protected int expiredSessions = 0;
/**
* Processing time during session expiration and passivation.
*/
protected long processingTime = 0;
// ------------------------------------------------------------- Properties
/**
* Perform the background processes for this Manager
*/
public void backgroundProcess() {
long timeNow = System.currentTimeMillis();
this.processExpires();
this.processPersistenceChecks();
if ((this.getStore() != null)
&& (this.getStore() instanceof StoreBase)) {
((StoreBase) this.getStore()).processExpires();
}
long timeEnd = System.currentTimeMillis();
processingTime += ( timeEnd - timeNow );
}
/**
* Indicates how many seconds old a session can get, after its last
* use in a request, before it should be backed up to the store. -1
* means sessions are not backed up.
*/
public int getMaxIdleBackup() {
return maxIdleBackup;
}
/**
* Sets the option to back sessions up to the Store after they
* are used in a request. Sessions remain available in memory
* after being backed up, so they are not passivated as they are
* when swapped out. The value set indicates how old a session
* may get (since its last use) before it must be backed up: -1
* means sessions are not backed up.
* <p>
* Note that this is not a hard limit: sessions are checked
* against this age limit periodically according to <b>checkInterval</b>.
* This value should be considered to indicate when a session is
* ripe for backing up.
* <p>
* So it is possible that a session may be idle for maxIdleBackup +
* checkInterval seconds, plus the time it takes to handle other
* session expiration, swapping, etc. tasks.
*
* @param backup The number of seconds after their last accessed
* time when they should be written to the Store.
*/
public void setMaxIdleBackup (int backup) {
if (backup == this.maxIdleBackup)
return;
int oldBackup = this.maxIdleBackup;
this.maxIdleBackup = backup;
support.firePropertyChange("maxIdleBackup",
new Integer(oldBackup),
new Integer(this.maxIdleBackup));
}
/**
* The time in seconds after which a session should be swapped out of
* memory to disk.
*/
public int getMaxIdleSwap() {
return maxIdleSwap;
}
/**
* Sets the time in seconds after which a session should be swapped out of
* memory to disk.
*/
public void setMaxIdleSwap(int max) {
if (max == this.maxIdleSwap)
return;
int oldMaxIdleSwap = this.maxIdleSwap;
this.maxIdleSwap = max;
support.firePropertyChange("maxIdleSwap",
new Integer(oldMaxIdleSwap),
new Integer(this.maxIdleSwap));
}
/**
* The minimum time in seconds that a session must be idle before
* it can be swapped out of memory, or -1 if it can be swapped out
* at any time.
*/
public int getMinIdleSwap() {
return minIdleSwap;
}
/**
* Sets the minimum time in seconds that a session must be idle before
* it can be swapped out of memory due to maxActiveSession. Set it to -1
* if it can be swapped out at any time.
*/
public void setMinIdleSwap(int min) {
if (this.minIdleSwap == min)
return;
int oldMinIdleSwap = this.minIdleSwap;
this.minIdleSwap = min;
support.firePropertyChange("minIdleSwap",
new Integer(oldMinIdleSwap),
new Integer(this.minIdleSwap));
}
/**
* Set the Container with which this Manager has been associated. If
* it is a Context (the usual case), listen for changes to the session
* timeout property.
*
* @param container The associated Container
*/
public void setContainer(Container container) {
// De-register from the old Container (if any)
if ((this.container != null) && (this.container instanceof Context))
((Context) this.container).removePropertyChangeListener(this);
// Default processing provided by our superclass
super.setContainer(container);
// Register with the new Container (if any)
if ((this.container != null) && (this.container instanceof Context)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -