Example usage for javax.servlet.http HttpSessionBindingListener valueBound

List of usage examples for javax.servlet.http HttpSessionBindingListener valueBound

Introduction

In this page you can find the example usage for javax.servlet.http HttpSessionBindingListener valueBound.

Prototype

default public void valueBound(HttpSessionBindingEvent event) 

Source Link

Document

Notifies the object that it is being bound to a session and identifies the session.

Usage

From source file:org.ireland.jnetty.server.session.HttpSessionImpl.java

/**
 * Sets a session attribute. If the value is a listener, notify it of the objectModified. If the value has changed
 * mark the session as changed for persistent sessions.
 * /*from   w  ww . j  a va2s .co  m*/
 * @param name
 *            the name of the attribute
 * @param value
 *            the value of the attribute
 */
@Override
public void setAttribute(String name, Object value) {
    if (!_isValid)
        throw new IllegalStateException(
                this + ": can't call setAttribute(String, Object) when session is no longer valid.");

    Object oldValue;

    if (value != null && !(value instanceof Serializable) && debug) {
        log.debug(this + " attribute '" + name + "' value is non-serializable type '"
                + value.getClass().getName() + "'");
    }

    synchronized (_values) {
        if (value != null)
            oldValue = _values.put(name, value);
        else
            oldValue = _values.remove(name);
    }

    if (oldValue instanceof HttpSessionBindingListener) {
        HttpSessionBindingListener listener;
        listener = (HttpSessionBindingListener) oldValue;

        listener.valueUnbound(new HttpSessionBindingEvent(HttpSessionImpl.this, name, oldValue));
    }

    if (value instanceof HttpSessionBindingListener) {
        HttpSessionBindingListener listener;
        listener = (HttpSessionBindingListener) value;

        listener.valueBound(new HttpSessionBindingEvent(HttpSessionImpl.this, name, value));
    }

    // Notify the attribute listeners
    ArrayList listeners = _manager.getAttributeListeners();

    if (listeners != null && listeners.size() > 0) {
        HttpSessionBindingEvent event;

        if (oldValue != null)
            event = new HttpSessionBindingEvent(this, name, oldValue);
        else
            event = new HttpSessionBindingEvent(this, name, value);

        for (int i = 0; i < listeners.size(); i++) {
            HttpSessionAttributeListener listener;
            listener = (HttpSessionAttributeListener) listeners.get(i);

            if (oldValue != null)
                listener.attributeReplaced(event);
            else
                listener.attributeAdded(event);
        }
    }
}