Search in sources :

Example 1 with HttpSessionAttributeListener

use of javax.servlet.http.HttpSessionAttributeListener in project tomcat by apache.

the class StandardContext method listenerStart.

/**
     * Configure the set of instantiated application event listeners
     * for this Context.
     * @return <code>true</code> if all listeners wre
     * initialized successfully, or <code>false</code> otherwise.
     */
public boolean listenerStart() {
    if (log.isDebugEnabled())
        log.debug("Configuring application event listeners");
    // Instantiate the required listeners
    String[] listeners = findApplicationListeners();
    Object[] results = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
        if (getLogger().isDebugEnabled())
            getLogger().debug(" Configuring event listener class '" + listeners[i] + "'");
        try {
            String listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener);
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i]), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return false;
    }
    // Sort listeners in two arrays
    ArrayList<Object> eventListeners = new ArrayList<>();
    ArrayList<Object> lifecycleListeners = new ArrayList<>();
    for (int i = 0; i < results.length; i++) {
        if ((results[i] instanceof ServletContextAttributeListener) || (results[i] instanceof ServletRequestAttributeListener) || (results[i] instanceof ServletRequestListener) || (results[i] instanceof HttpSessionIdListener) || (results[i] instanceof HttpSessionAttributeListener)) {
            eventListeners.add(results[i]);
        }
        if ((results[i] instanceof ServletContextListener) || (results[i] instanceof HttpSessionListener)) {
            lifecycleListeners.add(results[i]);
        }
    }
    // list.
    for (Object eventListener : getApplicationEventListeners()) {
        eventListeners.add(eventListener);
    }
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener : getApplicationLifecycleListeners()) {
        lifecycleListeners.add(lifecycleListener);
        if (lifecycleListener instanceof ServletContextListener) {
            noPluggabilityListeners.add(lifecycleListener);
        }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    if (getLogger().isDebugEnabled())
        getLogger().debug("Sending application start events");
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object[] instances = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
        return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
        noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
        tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (int i = 0; i < instances.length; i++) {
        if (!(instances[i] instanceof ServletContextListener))
            continue;
        ServletContextListener listener = (ServletContextListener) instances[i];
        try {
            fireContainerEvent("beforeContextInitialized", listener);
            if (noPluggabilityListeners.contains(listener)) {
                listener.contextInitialized(tldEvent);
            } else {
                listener.contextInitialized(event);
            }
            fireContainerEvent("afterContextInitialized", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            fireContainerEvent("afterContextInitialized", listener);
            getLogger().error(sm.getString("standardContext.listenerStart", instances[i].getClass().getName()), t);
            ok = false;
        }
    }
    return (ok);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) HttpSessionListener(javax.servlet.http.HttpSessionListener) ServletContextListener(javax.servlet.ServletContextListener) ServletRequestListener(javax.servlet.ServletRequestListener) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) HttpSessionIdListener(javax.servlet.http.HttpSessionIdListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 2 with HttpSessionAttributeListener

use of javax.servlet.http.HttpSessionAttributeListener in project tomcat by apache.

the class StandardSessionContext method setAttribute.

/**
     * Bind an object to this session, using the specified name.  If an object
     * of the same name is already bound to this session, the object is
     * replaced.
     * <p>
     * After this method executes, and if the object implements
     * <code>HttpSessionBindingListener</code>, the container calls
     * <code>valueBound()</code> on the object.
     *
     * @param name Name to which the object is bound, cannot be null
     * @param value Object to be bound, cannot be null
     * @param notify whether to notify session listeners
     * @exception IllegalArgumentException if an attempt is made to add a
     *  non-serializable object in an environment marked distributable.
     * @exception IllegalStateException if this method is called on an
     *  invalidated session
     */
public void setAttribute(String name, Object value, boolean notify) {
    // 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);
        return;
    }
    // Validate our current state
    if (!isValidInternal()) {
        throw new IllegalStateException(sm.getString("standardSession.setAttribute.ise", getIdInternal()));
    }
    if ((manager != null) && manager.getContext().getDistributable() && !isAttributeDistributable(name, value) && !exclude(name, value)) {
        throw new IllegalArgumentException(sm.getString("standardSession.setAttribute.iae", name));
    }
    // Construct an event with the new value
    HttpSessionBindingEvent event = null;
    // Call the valueBound() method if necessary
    if (notify && value instanceof HttpSessionBindingListener) {
        // Don't call any notification if replacing with the same value
        Object oldValue = attributes.get(name);
        if (value != oldValue) {
            event = new HttpSessionBindingEvent(getSession(), name, value);
            try {
                ((HttpSessionBindingListener) value).valueBound(event);
            } catch (Throwable t) {
                manager.getContext().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
            }
        }
    }
    // Replace or add this attribute
    Object unbound = attributes.put(name, value);
    // Call the valueUnbound() method if necessary
    if (notify && (unbound != null) && (unbound != value) && (unbound instanceof HttpSessionBindingListener)) {
        try {
            ((HttpSessionBindingListener) unbound).valueUnbound(new HttpSessionBindingEvent(getSession(), name));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            manager.getContext().getLogger().error(sm.getString("standardSession.bindingEvent"), t);
        }
    }
    if (!notify)
        return;
    // Notify interested application event listeners
    Context context = manager.getContext();
    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) {
                context.fireContainerEvent("beforeSessionAttributeReplaced", listener);
                if (event == null) {
                    event = new HttpSessionBindingEvent(getSession(), name, unbound);
                }
                listener.attributeReplaced(event);
                context.fireContainerEvent("afterSessionAttributeReplaced", listener);
            } else {
                context.fireContainerEvent("beforeSessionAttributeAdded", listener);
                if (event == null) {
                    event = new HttpSessionBindingEvent(getSession(), name, value);
                }
                listener.attributeAdded(event);
                context.fireContainerEvent("afterSessionAttributeAdded", listener);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                if (unbound != null) {
                    context.fireContainerEvent("afterSessionAttributeReplaced", listener);
                } else {
                    context.fireContainerEvent("afterSessionAttributeAdded", listener);
                }
            } catch (Exception e) {
            // Ignore
            }
            manager.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Example 3 with HttpSessionAttributeListener

use of javax.servlet.http.HttpSessionAttributeListener in project tomcat by apache.

the class StandardSessionContext method removeAttributeInternal.

/**
     * Remove the object bound with the specified name from this session.  If
     * the session does not have an object bound with this name, this method
     * does nothing.
     * <p>
     * After this method executes, and if the object implements
     * <code>HttpSessionBindingListener</code>, the container calls
     * <code>valueUnbound()</code> on the object.
     *
     * @param name Name of the object to remove from this session.
     * @param notify Should we notify interested listeners that this
     *  attribute is being removed?
     */
protected void removeAttributeInternal(String name, boolean notify) {
    // Avoid NPE
    if (name == null)
        return;
    // Remove this attribute from our collection
    Object value = attributes.remove(name);
    // Do we need to do valueUnbound() and attributeRemoved() notification?
    if (!notify || (value == null)) {
        return;
    }
    // Call the valueUnbound() method if necessary
    HttpSessionBindingEvent event = null;
    if (value instanceof HttpSessionBindingListener) {
        event = new HttpSessionBindingEvent(getSession(), name, value);
        ((HttpSessionBindingListener) value).valueUnbound(event);
    }
    // Notify interested application event listeners
    Context context = manager.getContext();
    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 {
            context.fireContainerEvent("beforeSessionAttributeRemoved", listener);
            if (event == null) {
                event = new HttpSessionBindingEvent(getSession(), name, value);
            }
            listener.attributeRemoved(event);
            context.fireContainerEvent("afterSessionAttributeRemoved", listener);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            try {
                context.fireContainerEvent("afterSessionAttributeRemoved", listener);
            } catch (Exception e) {
            // Ignore
            }
            manager.getContext().getLogger().error(sm.getString("standardSession.attributeEvent"), t);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) HttpSessionBindingListener(javax.servlet.http.HttpSessionBindingListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) WriteAbortedException(java.io.WriteAbortedException)

Example 4 with HttpSessionAttributeListener

use of javax.servlet.http.HttpSessionAttributeListener in project jetty.project by eclipse.

the class SessionHandler method doSessionAttributeListeners.

public void doSessionAttributeListeners(Session session, String name, Object old, Object value) {
    if (!_sessionAttributeListeners.isEmpty()) {
        HttpSessionBindingEvent event = new HttpSessionBindingEvent(session, name, old == null ? value : old);
        for (HttpSessionAttributeListener l : _sessionAttributeListeners) {
            if (old == null)
                l.attributeAdded(event);
            else if (value == null)
                l.attributeRemoved(event);
            else
                l.attributeReplaced(event);
        }
    }
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener)

Aggregations

HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)4 HttpSessionBindingEvent (javax.servlet.http.HttpSessionBindingEvent)3 IOException (java.io.IOException)2 NotSerializableException (java.io.NotSerializableException)2 WriteAbortedException (java.io.WriteAbortedException)2 ServletContext (javax.servlet.ServletContext)2 HttpSessionBindingListener (javax.servlet.http.HttpSessionBindingListener)2 Context (org.apache.catalina.Context)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)1 ServletRequestListener (javax.servlet.ServletRequestListener)1 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)1 HttpSessionListener (javax.servlet.http.HttpSessionListener)1 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)1