Search in sources :

Example 11 with ServletRequestListener

use of javax.servlet.ServletRequestListener 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 12 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project tomee by apache.

the class HttpRequestImpl method init.

public void init() {
    if (begin != null && getAttribute("openejb_requestInitialized") == null) {
        // if called again we loose the request scope
        setAttribute("openejb_requestInitialized", "ok");
        begin.requestInitialized(new ServletRequestEvent(getServletContext(), this));
    }
    listeners = LightweightWebAppBuilderListenerExtractor.findByTypeForContext(contextPath, ServletRequestListener.class);
    if (!listeners.isEmpty()) {
        final ServletRequestEvent event = new ServletRequestEvent(getServletContext(), this);
        for (final ServletRequestListener listener : listeners) {
            listener.requestInitialized(event);
        }
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent)

Example 13 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project undertow by undertow-io.

the class ApplicationListeners method requestInitialized.

public void requestInitialized(final ServletRequest request) {
    if (!started) {
        return;
    }
    if (servletRequestListeners.length > 0) {
        int i = 0;
        final ServletRequestEvent sre = new ServletRequestEvent(servletContext, request);
        try {
            for (; i < servletRequestListeners.length; ++i) {
                this.<ServletRequestListener>get(servletRequestListeners[i]).requestInitialized(sre);
            }
        } catch (RuntimeException e) {
            UndertowServletLogger.REQUEST_LOGGER.errorInvokingListener("requestInitialized", servletRequestListeners[i].getListenerInfo().getListenerClass(), e);
            for (; i >= 0; i--) {
                ManagedListener listener = servletRequestListeners[i];
                try {
                    this.<ServletRequestListener>get(listener).requestDestroyed(sre);
                } catch (Throwable t) {
                    UndertowServletLogger.REQUEST_LOGGER.errorInvokingListener("requestDestroyed", listener.getListenerInfo().getListenerClass(), e);
                }
            }
            throw e;
        }
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent)

Example 14 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project Payara by payara.

the class StandardContext method fireRequestInitializedEvent.

@Override
public void fireRequestInitializedEvent(ServletRequest request) {
    List<EventListener> listeners = getApplicationEventListeners();
    ServletRequestEvent event = null;
    if (!listeners.isEmpty()) {
        event = new ServletRequestEvent(getServletContext(), request);
        // create pre-service event
        Iterator<EventListener> iter = listeners.iterator();
        while (iter.hasNext()) {
            EventListener eventListener = iter.next();
            if (!(eventListener instanceof ServletRequestListener)) {
                continue;
            }
            ServletRequestListener listener = (ServletRequestListener) eventListener;
            // START SJSAS 6329662
            fireContainerEvent(ContainerEvent.BEFORE_REQUEST_INITIALIZED, listener);
            // END SJSAS 6329662
            try {
                listener.requestInitialized(event);
            } catch (Throwable t) {
                String msg = MessageFormat.format(rb.getString(LogFacade.REQUEST_INIT_EXCEPTION), listener.getClass().getName());
                log.log(Level.WARNING, msg, t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
            // START SJSAS 6329662
            } finally {
                fireContainerEvent(ContainerEvent.AFTER_REQUEST_INITIALIZED, listener);
            // END SJSAS 6329662
            }
        }
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent) EventListener(java.util.EventListener)

Example 15 with ServletRequestListener

use of javax.servlet.ServletRequestListener in project Payara by payara.

the class StandardContext method fireRequestDestroyedEvent.

@Override
public void fireRequestDestroyedEvent(ServletRequest request) {
    List<EventListener> listeners = getApplicationEventListeners();
    if (!listeners.isEmpty()) {
        // create post-service event
        ServletRequestEvent event = new ServletRequestEvent(getServletContext(), request);
        int len = listeners.size();
        for (int i = 0; i < len; i++) {
            EventListener eventListener = listeners.get((len - 1) - i);
            if (!(eventListener instanceof ServletRequestListener)) {
                continue;
            }
            ServletRequestListener listener = (ServletRequestListener) eventListener;
            // START SJSAS 6329662
            fireContainerEvent(ContainerEvent.BEFORE_REQUEST_DESTROYED, listener);
            // END SJSAS 6329662
            try {
                listener.requestDestroyed(event);
            } catch (Throwable t) {
                String msg = MessageFormat.format(rb.getString(LogFacade.REQUEST_DESTROY_EXCEPTION), listener.getClass().getName());
                log.log(Level.WARNING, msg, t);
                request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
            // START SJSAS 6329662
            } finally {
                fireContainerEvent(ContainerEvent.AFTER_REQUEST_DESTROYED, listener);
            // END SJSAS 6329662
            }
        }
    }
}
Also used : ServletRequestListener(javax.servlet.ServletRequestListener) ServletRequestEvent(javax.servlet.ServletRequestEvent) EventListener(java.util.EventListener) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Aggregations

ServletRequestListener (javax.servlet.ServletRequestListener)15 ServletRequestEvent (javax.servlet.ServletRequestEvent)11 ArrayList (java.util.ArrayList)4 ServletException (javax.servlet.ServletException)4 EventListener (java.util.EventListener)3 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)3 ServletContextListener (javax.servlet.ServletContextListener)3 ServletRequestAttributeListener (javax.servlet.ServletRequestAttributeListener)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpSessionAttributeListener (javax.servlet.http.HttpSessionAttributeListener)3 HttpSessionListener (javax.servlet.http.HttpSessionListener)3 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)3 Test (org.junit.Test)3 ListenerInfo (io.undertow.servlet.api.ListenerInfo)2 ManagedListener (io.undertow.servlet.core.ManagedListener)2 ImmediateInstanceFactory (io.undertow.servlet.util.ImmediateInstanceFactory)2 IOException (java.io.IOException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSessionIdListener (javax.servlet.http.HttpSessionIdListener)2 File (java.io.File)1