📄 standardmanager.java
字号:
* @exception ClassNotFoundException if a deserialization error occurs
* while processing this request
* @exception IllegalStateException if a new session cannot be
* instantiated for any reason
* @exception IOException if an input/output error occurs while
* processing this request
*/
public HttpSession findSession(String id) {
if (id == null)
return (null);
HttpSession s=((HttpSession) sessions.get(id));
if( s==null ) return s;
access( s );
return s;
}
/**
* Return the set of active Sessions associated with this Manager.
* If this Manager has no active Sessions, a zero-length array is returned.
*/
public HttpSession[] findSessions() {
synchronized (sessions) {
Vector keys = new Vector();
Enumeration ids = sessions.keys();
while (ids.hasMoreElements()) {
String id = (String) ids.nextElement();
keys.addElement(id);
}
HttpSession results[] = new HttpSession[keys.size()];
for (int i = 0; i < results.length; i++) {
String key = (String) keys.elementAt(i);
results[i] = (HttpSession) sessions.get(key);
}
return (results);
}
}
/**
This method will return a Hashtable of HttpSession
objects.
*/
public Hashtable getSessions() {
return this.sessions;
}
/**
This method will replace the sessions Hashtable with
that given.
*/
public void setSessions(Hashtable sessions) {
// make sure all the sessions know they belong to us
Enumeration e = sessions.keys();
while (e.hasMoreElements())
{
StandardSession sess
= (StandardSession)sessions.get(e.nextElement());
sess.setManager(this);
}
this.sessions = sessions;
}
/**
* Construct and return a new session object, based on the default
* settings specified by this Manager's properties. The session
* id will be assigned by this method, and available via the getId()
* method of the returned session. If a new session cannot be created
* for any reason, return <code>null</code>.
*
* @exception IllegalStateException if a new session cannot be
* instantiated for any reason
*/
public HttpSession getNewSession(String jsIdent) {
if ((maxActiveSessions >= 0) &&
(sessions.size() >= maxActiveSessions))
throw new IllegalStateException
(sm.getString("standardManager.createSession.ise"));
// Recycle or create a Session instance
StandardSession session = null;
synchronized (recycled) {
int size = recycled.size();
if (size > 0) {
session = (StandardSession) recycled.elementAt(size - 1);
recycled.removeElementAt(size - 1);
}
}
if (session == null)
session = new StandardSession(this);
// Initialize the properties of the new session and return it
session.setNew(true);
session.setValid(true);
session.setCreationTime(System.currentTimeMillis());
session.setMaxInactiveInterval(this.maxInactiveInterval);
session.setId(SessionUtil.generateSessionId(jsIdent));
return (session);
}
public void handleReload(Request req, ClassLoader newLoader) {
SessionSerializer.doSerialization(req, newLoader, this);
}
/**
* Prepare for the beginning of active use of the public methods of this
* component. This method should be called after <code>configure()</code>,
* and before any of the public methods of the component are utilized.
*
* @exception IllegalStateException if this component has not yet been
* configured (if required for this component)
* @exception IllegalStateException if this component has already been
* started
*/
public void start() {
// Cause the context's PRNG to be initialized
String sDummy = SessionUtil.generateSessionId(null);
// Start the background reaper thread
threadStart();
}
/**
* Gracefully terminate the active use of the public methods of this
* component. This method should be the last one called on a given
* instance of this component.
*
* @exception IllegalStateException if this component has not been started
* @exception IllegalStateException if this component has already
* been stopped
*/
public void stop() {
// Stop the background reaper thread
threadStop();
// Expire all active sessions
HttpSession sessions[] = findSessions();
for (int i = 0; i < sessions.length; i++) {
StandardSession session = (StandardSession) sessions[i];
if (!session.isValid())
continue;
session.expire();
}
}
// -------------------------------------------------------- Package Methods
/**
* Add this Session to the set of active Sessions for this Manager.
*
* @param session Session to be added
*/
void add(StandardSession session) {
sessions.put(session.getId(), session);
}
/**
* Add this Session to the recycle collection for this Manager.
*
* @param session Session to be recycled
*/
void recycle(StandardSession session) {
recycled.addElement(session);
}
/**
* Remove this Session from the active Sessions for this Manager.
*
* @param session Session to be removed
*/
void remove(StandardSession session) {
sessions.remove(session.getId());
}
// -------------------------------------------------------- Private Methods
/**
* Invalidate all sessions that have expired.
*/
private void processExpires() {
long timeNow = System.currentTimeMillis();
HttpSession sessions[] = findSessions();
for (int i = 0; i < sessions.length; i++) {
StandardSession session = (StandardSession) sessions[i];
if (!session.isValid())
continue;
int maxInactiveInterval = session.getMaxInactiveInterval();
if (maxInactiveInterval < 0)
continue;
int timeIdle = // Truncate, do not round up
(int) ((timeNow - session.getLatestAccessedTime()) / 1000L);
if (timeIdle >= maxInactiveInterval)
session.expire();
}
}
/**
* Sleep for the duration specified by the <code>checkInterval</code>
* property.
*/
private void threadSleep() {
try {
Thread.sleep(checkInterval * 1000L);
} catch (InterruptedException e) {
;
}
}
/**
* Start the background thread that will periodically check for
* session timeouts.
*/
private void threadStart() {
if (thread != null)
return;
threadDone = false;
thread = new Thread(this, threadName);
thread.setDaemon(true);
thread.start();
}
/**
* Stop the background thread that is periodically checking for
* session timeouts.
*/
private void threadStop() {
if (thread == null)
return;
threadDone = true;
thread.interrupt();
try {
thread.join();
} catch (InterruptedException e) {
;
}
thread = null;
}
// ------------------------------------------------------ Background Thread
/**
* The background thread that checks for session timeouts and shutdown.
*/
public void run() {
// Loop until the termination semaphore is set
while (!threadDone) {
threadSleep();
processExpires();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -