Search in sources :

Example 6 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project tomcat70 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
    ApplicationListener[] listeners = applicationListeners;
    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 {
            ApplicationListener listener = listeners[i];
            results[i] = getInstanceManager().newInstance(listener.getClassName());
            if (listener.isPluggabilityBlocked()) {
                noPluggabilityListeners.add(results[i]);
            }
        } catch (Throwable t) {
            t = ExceptionUtils.unwrapInvocationTargetException(t);
            ExceptionUtils.handleThrowable(t);
            getLogger().error(sm.getString("standardContext.applicationListener", listeners[i].getClassName()), t);
            ok = false;
        }
    }
    if (!ok) {
        getLogger().error(sm.getString("standardContext.applicationSkipped"));
        return (false);
    }
    // Sort listeners in two arrays
    ArrayList<Object> eventListeners = new ArrayList<Object>();
    ArrayList<Object> lifecycleListeners = new ArrayList<Object>();
    for (int i = 0; i < results.length; i++) {
        if ((results[i] instanceof ServletContextAttributeListener) || (results[i] instanceof ServletRequestAttributeListener) || (results[i] instanceof ServletRequestListener) || (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] == null)
            continue;
        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) ArrayList(java.util.ArrayList) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint) ApplicationListener(org.apache.catalina.deploy.ApplicationListener) HttpSessionAttributeListener(javax.servlet.http.HttpSessionAttributeListener) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 7 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project tomcat70 by apache.

the class Request method notifyAttributeAssigned.

/**
 * Notify interested listeners that attribute has been assigned a value.
 */
private void notifyAttributeAssigned(String name, Object value, Object oldValue) {
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0)) {
        return;
    }
    boolean replaced = (oldValue != null);
    ServletRequestAttributeEvent event = null;
    if (replaced) {
        event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, oldValue);
    } else {
        event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, value);
    }
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletRequestAttributeListener)) {
            continue;
        }
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) listeners[i];
        try {
            if (replaced) {
                listener.attributeReplaced(event);
            } else {
                listener.attributeAdded(event);
            }
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            context.getLogger().error(sm.getString("coyoteRequest.attributeEvent"), t);
            // Error valve will pick this exception up and display it to user
            attributes.put(RequestDispatcher.ERROR_EXCEPTION, t);
        }
    }
}
Also used : ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent)

Example 8 with ServletRequestAttributeListener

use of javax.servlet.ServletRequestAttributeListener in project tomcat70 by apache.

the class Request method notifyAttributeRemoved.

/**
 * Notify interested listeners that attribute has been removed.
 */
private void notifyAttributeRemoved(String name, Object value) {
    Object[] listeners = context.getApplicationEventListeners();
    if ((listeners == null) || (listeners.length == 0)) {
        return;
    }
    ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(context.getServletContext(), getRequest(), name, value);
    for (int i = 0; i < listeners.length; i++) {
        if (!(listeners[i] instanceof ServletRequestAttributeListener)) {
            continue;
        }
        ServletRequestAttributeListener listener = (ServletRequestAttributeListener) listeners[i];
        try {
            listener.attributeRemoved(event);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            context.getLogger().error(sm.getString("coyoteRequest.attributeEvent"), t);
            // Error valve will pick this exception up and display it to user
            attributes.put(RequestDispatcher.ERROR_EXCEPTION, t);
        }
    }
}
Also used : ServletRequestAttributeListener(javax.servlet.ServletRequestAttributeListener) ServletRequestAttributeEvent(javax.servlet.ServletRequestAttributeEvent)

Aggregations

ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)8 ServletRequestAttributeEvent (javax.servlet.ServletRequestAttributeEvent)5 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)3 ServletContextListener (javax.servlet.ServletContextListener)3 ServletRequestListener (javax.servlet.ServletRequestListener)3 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)3 HttpSessionListener (javax.servlet.http.HttpSessionListener)3 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)2 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 EventListener (java.util.EventListener)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 ApplicationListener (org.apache.catalina.deploy.ApplicationListener)1 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)1 AttributesMap (org.eclipse.jetty.util.AttributesMap)1 WebEventListenerFace (org.nutz.boot.starter.WebEventListenerFace)1 WebFilterFace (org.nutz.boot.starter.WebFilterFace)1 WebServletFace (org.nutz.boot.starter.WebServletFace)1