📄 deltasession.java
字号:
// Replace or add this attribute
Object unbound = attributes.put(name, value);
// Call the valueUnbound() method if necessary
if ((unbound != null) &&
(unbound instanceof HttpSessionBindingListener)) {
try {
( (HttpSessionBindingListener) unbound).valueUnbound
(new HttpSessionBindingEvent( (HttpSession)this, name));
} catch ( Exception x ) {
log.error("Session binding listener throw an exception",x);
}
}
// Notify interested application event listeners
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationEventListeners();
if (listeners == null)
return;
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof HttpSessionAttributeListener))
continue;
HttpSessionAttributeListener listener =
(HttpSessionAttributeListener) listeners[i];
try {
if (unbound != null) {
fireContainerEvent(context,
"beforeSessionAttributeReplaced",
listener);
if ( event == null ) {
event = new HttpSessionBindingEvent
(this,name,unbound);
}
listener.attributeReplaced(event);
fireContainerEvent(context,
"afterSessionAttributeReplaced",
listener);
} else {
fireContainerEvent(context,
"beforeSessionAttributeAdded",
listener);
if (event == null) {
event = new HttpSessionBindingEvent
(this, name, unbound);
}
listener.attributeAdded(event);
fireContainerEvent(context,
"afterSessionAttributeAdded",
listener);
}
} catch (Throwable t) {
try {
if (unbound != null) {
fireContainerEvent(context,
"afterSessionAttributeReplaced",
listener);
} else {
fireContainerEvent(context,
"afterSessionAttributeAdded",
listener);
}
} catch (Exception e) {
;
}
// FIXME - should we do anything besides log these?
log.error(sm.getString("standardSession.attributeEvent"), t);
}
}
}
// -------------------------------------------- HttpSession Private Methods
/**
* Read a serialized version of this session object from the specified
* object input stream.
* <p>
* <b>IMPLEMENTATION NOTE</b>: The reference to the owning Manager
* is not restored by this method, and must be set explicitly.
*
* @param stream The input stream to read from
*
* @exception ClassNotFoundException if an unknown class is specified
* @exception IOException if an input/output error occurs
*/
private void readObject(ObjectInputStream stream)
throws ClassNotFoundException, IOException {
// Deserialize the scalar instance variables (except Manager)
authType = null; // Transient only
creationTime = ((Long) stream.readObject()).longValue();
lastAccessedTime = ((Long) stream.readObject()).longValue();
maxInactiveInterval = ((Integer) stream.readObject()).intValue();
isNew = ((Boolean) stream.readObject()).booleanValue();
isValid = ((Boolean) stream.readObject()).booleanValue();
thisAccessedTime = ((Long) stream.readObject()).longValue();
boolean hasPrincipal = stream.readBoolean();
principal = null;
if ( hasPrincipal ) {
principal = SerializablePrincipal.readPrincipal(stream,getManager().getContainer().getRealm());
}
// setId((String) stream.readObject());
id = (String) stream.readObject();
if (log.isDebugEnabled())
log.debug("readObject() loading session " + id);
// Deserialize the attribute count and attribute values
if (attributes == null)
attributes = new HashMap();
int n = ((Integer) stream.readObject()).intValue();
boolean isValidSave = isValid;
isValid = true;
for (int i = 0; i < n; i++) {
String name = (String) stream.readObject();
Object value = (Object) stream.readObject();
if ((value instanceof String) && (value.equals(NOT_SERIALIZED)))
continue;
if (log.isDebugEnabled())
log.debug(" loading attribute '" + name +
"' with value '" + value + "'");
synchronized (attributes) {
attributes.put(name, value);
}
}
isValid = isValidSave;
}
/**
* Write a serialized version of this session object to the specified
* object output stream.
* <p>
* <b>IMPLEMENTATION NOTE</b>: The owning Manager will not be stored
* in the serialized representation of this Session. After calling
* <code>readObject()</code>, you must set the associated Manager
* explicitly.
* <p>
* <b>IMPLEMENTATION NOTE</b>: Any attribute that is not Serializable
* will be unbound from the session, with appropriate actions if it
* implements HttpSessionBindingListener. If you do not want any such
* attributes, be sure the <code>distributable</code> property of the
* associated Manager is set to <code>true</code>.
*
* @param stream The output stream to write to
*
* @exception IOException if an input/output error occurs
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
// Write the scalar instance variables (except Manager)
stream.writeObject(new Long(creationTime));
stream.writeObject(new Long(lastAccessedTime));
stream.writeObject(new Integer(maxInactiveInterval));
stream.writeObject(new Boolean(isNew));
stream.writeObject(new Boolean(isValid));
stream.writeObject(new Long(thisAccessedTime));
stream.writeBoolean(getPrincipal()!=null);
if (getPrincipal() != null) {
SerializablePrincipal.writePrincipal((GenericPrincipal)principal,stream);
}
stream.writeObject(id);
if (log.isDebugEnabled())
log.debug("writeObject() storing session " + id);
// Accumulate the names of serializable and non-serializable attributes
String keys[] = keys();
ArrayList saveNames = new ArrayList();
ArrayList saveValues = new ArrayList();
for (int i = 0; i < keys.length; i++) {
Object value = null;
synchronized (attributes) {
value = attributes.get(keys[i]);
}
if (value == null)
continue;
else if (value instanceof Serializable) {
saveNames.add(keys[i]);
saveValues.add(value);
}
}
// Serialize the attribute count and the Serializable attributes
int n = saveNames.size();
stream.writeObject(new Integer(n));
for (int i = 0; i < n; i++) {
stream.writeObject((String) saveNames.get(i));
try {
stream.writeObject(saveValues.get(i));
if (log.isDebugEnabled())
log.debug(" storing attribute '" + saveNames.get(i) +
"' with value '" + saveValues.get(i) + "'");
} catch (NotSerializableException e) {
log.error(sm.getString("standardSession.notSerializable",
saveNames.get(i), id), e);
stream.writeObject(NOT_SERIALIZED);
log.error(" storing attribute '" + saveNames.get(i) +
"' with value NOT_SERIALIZED");
}
}
}
private void evaluateIfValid() {
/*
* If this session has expired or is in the process of expiring or
* will never expire, return
*/
if (!this.isValid || expiring || maxInactiveInterval < 0)
return;
isValid();
}
// -------------------------------------------------------- Private Methods
/**
* Fire container events if the Context implementation is the
* <code>org.apache.catalina.core.StandardContext</code>.
*
* @param context Context for which to fire events
* @param type Event type
* @param data Event data
*
* @exception Exception occurred during event firing
*/
private void fireContainerEvent(Context context,
String type, Object data)
throws Exception {
if (!"org.apache.catalina.core.StandardContext".equals
(context.getClass().getName())) {
return; // Container events are not supported
}
// NOTE: Race condition is harmless, so do not synchronize
if (containerEventMethod == null) {
containerEventMethod =
context.getClass().getMethod("fireContainerEvent",
containerEventTypes);
}
Object containerEventParams[] = new Object[2];
containerEventParams[0] = type;
containerEventParams[1] = data;
containerEventMethod.invoke(context, containerEventParams);
}
/**
* Notify all session event listeners that a particular event has
* occurred for this Session. The default implementation performs
* this notification synchronously using the calling thread.
*
* @param type Event type
* @param data Event data
*/
public void fireSessionEvent(String type, Object data) {
if (listeners.size() < 1)
return;
SessionEvent event = new SessionEvent(this, type, data);
SessionListener list[] = new SessionListener[0];
synchronized (listeners) {
list = (SessionListener[]) listeners.toArray(list);
}
for (int i = 0; i < list.length; i++){
((SessionListener) list[i]).sessionEvent(event);
}
}
/**
* Return the names of all currently defined session attributes
* as an array of Strings. If there are no defined attributes, a
* zero-length array is returned.
*/
protected String[] keys() {
String results[] = new String[0];
synchronized (attributes) {
return ((String[]) attributes.keySet().toArray(results));
}
}
/**
* Return the value of an attribute without a check for validity.
*/
protected Object getAttributeInternal(String name) {
synchronized (attributes) {
return (attributes.get(name));
}
}
protected void removeAttributeInternal(String name, boolean notify, boolean addDeltaRequest) {
// Remove this attribute from our collection
Object value = attributes.remove(name);
if ( value == null ) return;
if (addDeltaRequest && (deltaRequest!=null)) deltaRequest.removeAttribute(name);
// Do we need to do valueUnbound() and attributeRemoved() notification?
if (!notify) {
return;
}
// Call the valueUnbound() method if necessary
HttpSessionBindingEvent event =
new HttpSessionBindingEvent((HttpSession) this, name, value);
if ((value != null) &&
(value instanceof HttpSessionBindingListener))
try {
( (HttpSessionBindingListener) value).valueUnbound(event);
} catch ( Exception x ) {
log.error("Session binding listener throw an exception",x);
}
// Notify interested application event listeners
Context context = (Context) manager.getContainer();
Object listeners[] = context.getApplicationEventListeners();
if (listeners == null)
return;
for (int i = 0; i < listeners.length; i++) {
if (!(listeners[i] instanceof HttpSessionAttributeListener))
continue;
HttpSessionAttributeListener listener =
(HttpSessionAttributeListener) listeners[i];
try {
fireContainerEvent(context,
"beforeSessionAttributeRemoved",
listener);
listener.attributeRemoved(event);
fireContainerEvent(context,
"afterSessionAttributeRemoved",
listener);
} catch (Throwable t) {
try {
fireContainerEvent(context,
"afterSessionAttributeRemoved",
listener);
} catch (Exception e) {
;
}
// FIXME - should we do anything besides log these?
log.error(sm.getString("standardSession.attributeEvent"), t);
}
}
}
protected long getLastTimeReplicated() {
return lastTimeReplicated;
}
protected void setLastTimeReplicated(long lastTimeReplicated) {
this.lastTimeReplicated = lastTimeReplicated;
}
protected void setAccessCount(int accessCount) {
this.accessCount = accessCount;
}
protected int getAccessCount() {
return accessCount;
}
}
// -------------------------------------------------------------- Private Class
/**
* This class is a dummy implementation of the <code>HttpSessionContext</code>
* interface, to conform to the requirement that such an object be returned
* when <code>HttpSession.getSessionContext()</code> is called.
*
* @author Craig R. McClanahan
*
* @deprecated As of Java Servlet API 2.1 with no replacement. The
* interface will be removed in a future version of this API.
*/
final class StandardSessionContext implements HttpSessionContext {
private HashMap dummy = new HashMap();
/**
* Return the session identifiers of all sessions defined
* within this context.
*
* @deprecated As of Java Servlet API 2.1 with no replacement.
* This method must return an empty <code>Enumeration</code>
* and will be removed in a future version of the API.
*/
public Enumeration getIds() {
return (new Enumerator(dummy));
}
/**
* Return the <code>HttpSession</code> associated with the
* specified session identifier.
*
* @param id Session identifier for which to look up a session
*
* @deprecated As of Java Servlet API 2.1 with no replacement.
* This method must return null and will be removed in a
* future version of the API.
*/
public HttpSession getSession(String id) {
return (null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -