Example usage for javax.servlet.http HttpSessionAttributeListener attributeReplaced

List of usage examples for javax.servlet.http HttpSessionAttributeListener attributeReplaced

Introduction

In this page you can find the example usage for javax.servlet.http HttpSessionAttributeListener attributeReplaced.

Prototype

default public void attributeReplaced(HttpSessionBindingEvent event) 

Source Link

Document

Receives notification that an attribute has been replaced in a session.

Usage

From source file:org.apache.catalina.cluster.session.DeltaSession.java

public void setAttribute(String name, Object value, boolean addDeltaRequest) {

    // Name cannot be null
    if (name == null)
        throw new IllegalArgumentException(sm.getString("standardSession.setAttribute.namenull"));

    // Null value is the same as removeAttribute()
    if (value == null) {
        removeAttribute(name);//from   w w w . j  av  a 2 s  .c o  m
        return;
    }

    if (!(value instanceof java.io.Serializable)) {
        throw new IllegalArgumentException("Attribute [" + name + "] is not serializable");
    }

    if (addDeltaRequest && (deltaRequest != null))
        deltaRequest.setAttribute(name, value);

    // Validate our current state
    if (!isValid())
        throw new IllegalStateException(sm.getString("standardSession.setAttribute.ise"));
    if ((manager != null) && manager.getDistributable() && !(value instanceof Serializable))
        throw new IllegalArgumentException(sm.getString("standardSession.setAttribute.iae"));

    // Replace or add this attribute
    Object unbound = null;
    synchronized (attributes) {
        unbound = attributes.get(name);
        attributes.put(name, value);
    }

    // Call the valueUnbound() method if necessary
    if ((unbound != null) && (unbound instanceof HttpSessionBindingListener)) {
        ((HttpSessionBindingListener) unbound)
                .valueUnbound(new HttpSessionBindingEvent((HttpSession) this, name));
    }

    // Call the valueBound() method if necessary
    HttpSessionBindingEvent event = null;
    if (unbound != null)
        event = new HttpSessionBindingEvent((HttpSession) this, name, unbound);
    else
        event = new HttpSessionBindingEvent((HttpSession) this, name, value);
    if (value instanceof HttpSessionBindingListener)
        ((HttpSessionBindingListener) value).valueBound(event);

    // 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);
                listener.attributeReplaced(event);
                fireContainerEvent(context, "afterSessionAttributeReplaced", listener);
            } else {
                fireContainerEvent(context, "beforeSessionAttributeAdded", listener);
                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);
        }
    }

}