📄 persistentmanagerbase.java
字号:
/*
* 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 "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.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.catalina.security.SecurityUtil;
import org.apache.juli.logging.Log;
import org.apache.juli.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: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
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.1";
/**
* 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;
/**
* Processing time during session expiration and passivation.
*/
protected long processingTime = 0;
// ------------------------------------------------------------- Properties
/**
* 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>processExpiresFrequency</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 +
* processExpiresFrequency * engine.backgroundProcessorDelay 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)) {
setMaxInactiveInterval
( ((Context) this.container).getSessionTimeout()*60 );
((Context) this.container).addPropertyChangeListener(this);
}
}
/**
* Return descriptive information about this Manager implementation and
* the corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo() {
return (info);
}
/**
* Return true, if the session id is loaded in memory
* otherwise false is returned
*
* @param id The session id for the session to be searched for
*/
public boolean isLoaded( String id ){
try {
if ( super.findSession(id) != null )
return true;
} catch (IOException e) {
log.error("checking isLoaded for id, " + id + ", "+e.getMessage(), e);
}
return false;
}
/**
* Return the maximum number of active Sessions allowed, or -1 for
* no limit.
*/
public int getMaxActiveSessions() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -