tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1024381 - in /tomcat/trunk/java/org/apache/catalina: Context.java core/StandardContext.java core/StandardContextValve.java
Date Tue, 19 Oct 2010 19:52:29 GMT
Author: markt
Date: Tue Oct 19 19:52:28 2010
New Revision: 1024381

URL: http://svn.apache.org/viewvc?rev=1024381&view=rev
Log:
Re-factor ServletRequestListener events with an eye to https://issues.apache.org/bugzilla/show_bug.cgi?id=49991
and any possible future changes in the Servlet spec

Modified:
    tomcat/trunk/java/org/apache/catalina/Context.java
    tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/trunk/java/org/apache/catalina/core/StandardContextValve.java

Modified: tomcat/trunk/java/org/apache/catalina/Context.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/Context.java?rev=1024381&r1=1024380&r2=1024381&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/Context.java (original)
+++ tomcat/trunk/java/org/apache/catalina/Context.java Tue Oct 19 19:52:28 2010
@@ -24,6 +24,7 @@ import java.util.Set;
 
 import javax.servlet.ServletContainerInitializer;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
 import javax.servlet.ServletSecurityElement;
 import javax.servlet.descriptor.JspConfigDescriptor;
 
@@ -1005,6 +1006,20 @@ public interface Context extends Contain
 
 
     /**
+     * Notify all {@link ServletRequestListener}s that a request has started.
+     * @return <code>true</code> if the listeners fire successfully, else
+     *         <code>false</code>
+     */
+    public boolean fireRequestInitEvent(ServletRequest request);
+
+    /**
+     * Notify all {@link ServletRequestListener}s that a request has ended.
+     * @return <code>true</code> if the listeners fire successfully, else
+     *         <code>false</code>
+     */
+    public boolean fireRequestDestroyEvent(ServletRequest request);
+
+    /**
      * Reload this web application, if reloading is supported.
      *
      * @exception IllegalStateException if the <code>reloadable</code>

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContext.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1024381&r1=1024380&r2=1024381&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContext.java Tue Oct 19 19:52:28 2010
@@ -48,6 +48,7 @@ import javax.management.NotificationList
 import javax.management.ObjectName;
 import javax.naming.NamingException;
 import javax.naming.directory.DirContext;
+import javax.servlet.DispatcherType;
 import javax.servlet.FilterConfig;
 import javax.servlet.Servlet;
 import javax.servlet.ServletContainerInitializer;
@@ -57,7 +58,9 @@ import javax.servlet.ServletContextEvent
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration;
+import javax.servlet.ServletRequest;
 import javax.servlet.ServletRequestAttributeListener;
+import javax.servlet.ServletRequestEvent;
 import javax.servlet.ServletRequestListener;
 import javax.servlet.ServletSecurityElement;
 import javax.servlet.descriptor.JspConfigDescriptor;
@@ -5503,6 +5506,83 @@ public class StandardContext extends Con
         return hostName;
     }
 
+
+    @Override
+    public boolean fireRequestInitEvent(ServletRequest request) {
+
+        Object instances[] = getApplicationEventListeners();
+
+        if ((instances != null) && (instances.length > 0)) {
+
+            ServletRequestEvent event = 
+                    new ServletRequestEvent(getServletContext(), request);
+
+            for (int i = 0; i < instances.length; i++) {
+                if (instances[i] == null)
+                    continue;
+                if (!(instances[i] instanceof ServletRequestListener))
+                    continue;
+                ServletRequestListener listener =
+                    (ServletRequestListener) instances[i];
+                
+                try {
+                    // Don't fire the listener for async requests
+                    if (!DispatcherType.ASYNC.equals(
+                            request.getDispatcherType())) {
+                        listener.requestInitialized(event);
+                    }
+                } catch (Throwable t) {
+                    ExceptionUtils.handleThrowable(t);
+                    getLogger().error(sm.getString(
+                            "standardContext.requestListener.requestInit",
+                            instances[i].getClass().getName()), t);
+                    request.setAttribute(Globals.EXCEPTION_ATTR,t);
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+
+    @Override
+    public boolean fireRequestDestroyEvent(ServletRequest request) {
+        Object instances[] = getApplicationEventListeners();
+
+        if ((instances != null) && (instances.length > 0)) {
+
+            ServletRequestEvent event = 
+                    new ServletRequestEvent(getServletContext(), request);
+
+            for (int i = 0; i < instances.length; i++) {
+                int j = (instances.length -1) -i;
+                if (instances[j] == null)
+                    continue;
+                if (!(instances[j] instanceof ServletRequestListener))
+                    continue;
+                ServletRequestListener listener =
+                    (ServletRequestListener) instances[j];
+                
+                try {
+                    // Don't fire the listener for async requests
+                    if (!DispatcherType.ASYNC.equals(
+                            request.getDispatcherType())) {
+                        listener.requestDestroyed(event);
+                    }
+                } catch (Throwable t) {
+                    ExceptionUtils.handleThrowable(t);
+                    getLogger().error(sm.getString(
+                            "standardContext.requestListener.requestInit",
+                            instances[j].getClass().getName()), t);
+                    request.setAttribute(Globals.EXCEPTION_ATTR,t);
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+
     /**
      * Set the appropriate context attribute for our work directory.
      */

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardContextValve.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardContextValve.java?rev=1024381&r1=1024380&r2=1024381&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardContextValve.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardContextValve.java Tue Oct 19 19:52:28
2010
@@ -166,66 +166,15 @@ final class StandardContextValve
             }
         }
 
-        // Normal request processing
-        Object instances[] = context.getApplicationEventListeners();
-
-        ServletRequestEvent event = null;
-
-        if ((instances != null) && (instances.length > 0)) {
-            event = new ServletRequestEvent
-                (((Context) container).getServletContext(), 
-                 request.getRequest());
-            // create pre-service event
-            for (int i = 0; i < instances.length; i++) {
-                if (instances[i] == null)
-                    continue;
-                if (!(instances[i] instanceof ServletRequestListener))
-                    continue;
-                ServletRequestListener listener =
-                    (ServletRequestListener) instances[i];
-                try {
-                    if (!request.isAsyncDispatching()) {
-                        listener.requestInitialized(event);
-                    }
-                } catch (Throwable t) {
-                    ExceptionUtils.handleThrowable(t);
-                    container.getLogger().error(sm.getString(
-                            "standardContext.requestListener.requestInit",
-                            instances[i].getClass().getName()), t);
-                    ServletRequest sreq = request.getRequest();
-                    sreq.setAttribute(Globals.EXCEPTION_ATTR,t);
-                    return;
-                }
+        // If a request init listener throws an exception, the request is
+        // aborted
+        if (context.fireRequestInitEvent(request)) {
+            if (request.isAsyncSupported()) {
+                request.setAsyncSupported(wrapper.getPipeline().isAsyncSupported());
             }
-        }
-        if (request.isAsyncSupported()) {
-            request.setAsyncSupported(wrapper.getPipeline().isAsyncSupported());
-        }
-        wrapper.getPipeline().getFirst().invoke(request, response);
+            wrapper.getPipeline().getFirst().invoke(request, response);
 
-        if ((instances !=null ) && (instances.length > 0)) {
-            // create post-service event
-            for (int i = 0; i < instances.length; i++) {
-                int j = (instances.length -1) -i;
-                if (instances[j] == null)
-                    continue;
-                if (!(instances[j] instanceof ServletRequestListener))
-                    continue;
-                ServletRequestListener listener =
-                    (ServletRequestListener) instances[j];
-                try {
-                    if (!request.isAsyncDispatching()) {
-                        listener.requestDestroyed(event);
-                    }
-                } catch (Throwable t) {
-                    ExceptionUtils.handleThrowable(t);
-                    container.getLogger().error(sm.getString(
-                            "standardContext.requestListener.requestDestroy",
-                            instances[j].getClass().getName()), t);
-                    ServletRequest sreq = request.getRequest();
-                    sreq.setAttribute(Globals.EXCEPTION_ATTR,t);
-                }
-            }
+            context.fireRequestDestroyEvent(request);
         }
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message