📄 deltasession.java
字号:
// Notify interested application event listeners
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationLifecycleListeners();
if (listeners != null) {
HttpSessionEvent event =
new HttpSessionEvent(getSession());
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof HttpSessionListener))
continue;
HttpSessionListener listener =
(HttpSessionListener) listeners[i];
try {
fireContainerEvent(context,
"beforeSessionCreated",
listener);
listener.sessionCreated(event);
fireContainerEvent(context,
"afterSessionCreated",
listener);
} catch (Throwable t) {
try {
fireContainerEvent(context,
"afterSessionCreated",
listener);
} catch (Exception e) {
;
}
// FIXME - should we do anything besides log these?
log.error(sm.getString("standardSession.sessionEvent"), t);
}
}
}
}
/**
* Return descriptive information about this Session implementation and
* the corresponding version number, in the format
* <code><description>/<version></code>.
*/
public String getInfo() {
return (this.info);
}
/**
* Return the last time the client sent a request associated with this
* session, as the number of milliseconds since midnight, January 1, 1970
* GMT. Actions that your application takes, such as getting or setting
* a value associated with the session, do not affect the access time.
*/
public long getLastAccessedTime() {
if ( !isValid ) {
throw new IllegalStateException(
sm.getString("standardSession.getLastAccessedTime"));
}
return (this.lastAccessedTime);
}
/**
* Return the Manager within which this Session is valid.
*/
public Manager getManager() {
return (this.manager);
}
/**
* Set the Manager within which this Session is valid.
*
* @param manager The new Manager
*/
public void setManager(Manager manager) {
this.manager = manager;
}
/**
* Return the maximum time interval, in seconds, between client requests
* before the servlet container will invalidate the session. A negative
* time indicates that the session should never time out.
*/
public int getMaxInactiveInterval() {
return (this.maxInactiveInterval);
}
/**
* Set the maximum time interval, in seconds, between client requests
* before the servlet container will invalidate the session. A negative
* time indicates that the session should never time out.
*
* @param interval The new maximum interval
*/
public void setMaxInactiveInterval(int interval) {
setMaxInactiveInterval(interval,true);
}
public void setMaxInactiveInterval(int interval, boolean addDeltaRequest) {
this.maxInactiveInterval = interval;
if (isValid && interval == 0) {
expire();
} else {
if ( addDeltaRequest && (deltaRequest!=null) ) deltaRequest.setMaxInactiveInterval(interval);
}
}
/**
* Set the <code>isNew</code> flag for this session.
*
* @param isNew The new value for the <code>isNew</code> flag
*/
public void setNew(boolean isNew) {
setNew(isNew,true);
}
public void setNew(boolean isNew, boolean addDeltaRequest) {
this.isNew = isNew;
if (addDeltaRequest && (deltaRequest!=null)) deltaRequest.setNew(isNew);
}
/**
* Return the authenticated Principal that is associated with this Session.
* This provides an <code>Authenticator</code> with a means to cache a
* previously authenticated Principal, and avoid potentially expensive
* <code>Realm.authenticate()</code> calls on every request. If there
* is no current associated Principal, return <code>null</code>.
*/
public Principal getPrincipal() {
return (this.principal);
}
/**
* Set the authenticated Principal that is associated with this Session.
* This provides an <code>Authenticator</code> with a means to cache a
* previously authenticated Principal, and avoid potentially expensive
* <code>Realm.authenticate()</code> calls on every request.
*
* @param principal The new Principal, or <code>null</code> if none
*/
public void setPrincipal(Principal principal) {
setPrincipal(principal,true);
}
public void setPrincipal(Principal principal,boolean addDeltaRequest) {
Principal oldPrincipal = this.principal;
this.principal = principal;
support.firePropertyChange("principal", oldPrincipal, this.principal);
if (addDeltaRequest && (deltaRequest!=null)) deltaRequest.setPrincipal(principal);
}
/**
* Return the <code>HttpSession</code> for which this object
* is the facade.
*/
public HttpSession getSession() {
if (facade == null){
if (System.getSecurityManager() != null){
final DeltaSession fsession = this;
facade = (DeltaSessionFacade)AccessController.doPrivileged(new PrivilegedAction(){
public Object run(){
return new DeltaSessionFacade(fsession);
}
});
} else {
facade = new DeltaSessionFacade(this);
}
}
return (facade);
}
/**
* Return the <code>isValid</code> flag for this session.
*/
public boolean isValid() {
if (this.expiring){
return true;
}
if (!this.isValid ) {
return false;
}
if (accessCount > 0 ) {
return true;
}
if (maxInactiveInterval >= 0) {
long timeNow = System.currentTimeMillis();
int timeIdle = (int) ((timeNow - lastAccessedTime) / 1000L);
if ( (timeIdle >= maxInactiveInterval) && (isPrimarySession()) ) {
expire(true);
} else if ( timeIdle >= (2*maxInactiveInterval) ) {
//if the session has been idle twice as long as allowed,
//the primary session has probably crashed, and no other
//requests are coming in. that is why we do this. otherwise
//we would have a memory leak
expire(true,false);
}
}
return (this.isValid);
}
/**
* Set the <code>isValid</code> flag for this session.
*
* @param isValid The new value for the <code>isValid</code> flag
*/
public void setValid(boolean isValid) {
this.isValid = isValid;
}
// ------------------------------------------------- Session Public Methods
/**
* Update the accessed time information for this session. This method
* should be called by the context when a request comes in for a particular
* session, even if the application does not reference it.
*/
public void access() {
this.isNew = false;
this.lastAccessedTime = this.thisAccessedTime;
this.thisAccessedTime = System.currentTimeMillis();
evaluateIfValid();
accessCount++;
}
public void endAccess() {
isNew = false;
accessCount--;
}
/**
* Add a session event listener to this component.
*/
public void addSessionListener(SessionListener listener) {
synchronized (listeners) {
listeners.add(listener);
}
}
/**
* Perform the internal processing required to invalidate this session,
* without triggering an exception if the session has already expired.
*/
public void expire() {
expire(true);
}
/**
* Perform the internal processing required to invalidate this session,
* without triggering an exception if the session has already expired.
*
* @param notify Should we notify listeners about the demise of
* this session?
*/
public void expire(boolean notify) {
expire(notify,true);
}
public void expire(boolean notify, boolean notifyCluster) {
// Mark this session as "being expired" if needed
if (expiring)
return;
String expiredId = getId();
synchronized (this) {
if (manager == null)
return;
expiring = true;
// Notify interested application event listeners
// FIXME - Assumes we call listeners in reverse order
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationLifecycleListeners();
if (notify && (listeners != null)) {
HttpSessionEvent event =
new HttpSessionEvent(getSession());
for (int i = 0; i < listeners.length; i++) {
int j = (listeners.length - 1) - i;
if (!(listeners[j] instanceof HttpSessionListener))
continue;
HttpSessionListener listener =
(HttpSessionListener) listeners[j];
try {
fireContainerEvent(context,
"beforeSessionDestroyed",
listener);
listener.sessionDestroyed(event);
fireContainerEvent(context,
"afterSessionDestroyed",
listener);
} catch (Throwable t) {
try {
fireContainerEvent(context,
"afterSessionDestroyed",
listener);
} catch (Exception e) {
;
}
// FIXME - should we do anything besides log these?
log.error(sm.getString("standardSession.sessionEvent"), t);
}
}
}
accessCount=0;
setValid(false);
// Remove this session from our manager's active sessions
if (manager != null)
manager.remove(this);
// Notify interested session event listeners
if (notify) {
fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);
}
// We have completed expire of this session
expiring = false;
// Unbind any objects associated with this session
String keys[] = keys();
for (int i = 0; i < keys.length; i++)
removeAttributeInternal(keys[i], notify, false);
if ( notifyCluster ) {
log.debug("Notifying cluster of expiration primary=" +
isPrimarySession() + " id=" + expiredId);
( (DeltaManager) manager).sessionExpired(expiredId);
}
}
}
/**
* Return the object bound with the specified name to the internal notes
* for this session, or <code>null</code> if no such binding exists.
*
* @param name Name of the note to be returned
*/
public Object getNote(String name) {
synchronized (notes) {
return (notes.get(name));
}
}
/**
* Return an Iterator containing the String names of all notes bindings
* that exist for this session.
*/
public Iterator getNoteNames() {
synchronized (notes) {
return (notes.keySet().iterator());
}
}
/**
* Release all object references, and initialize instance variables, in
* preparation for reuse of this object.
*/
public void recycle() {
// Reset the instance variables associated with this Session
attributes.clear();
setAuthType(null);
creationTime = 0L;
expiring = false;
id = null;
lastAccessedTime = 0L;
maxInactiveInterval = -1;
accessCount = 1;
notes.clear();
setPrincipal(null);
isNew = false;
isValid = false;
manager = null;
deltaRequest.clear();
}
/**
* Remove any object bound to the specified name in the internal notes
* for this session.
*
* @param name Name of the note to be removed
*/
public void removeNote(String name) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -