Reorder the start sequence
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 21 Jul 2009 23:05:47 +0000 (23:05 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 21 Jul 2009 23:05:47 +0000 (23:05 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@796575 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/connector/AsyncContextImpl.java
java/org/apache/catalina/connector/Request.java

index daf9442..cda1ee9 100644 (file)
@@ -121,10 +121,24 @@ public class AsyncContextImpl implements AsyncContext {
         return getServletResponse();
     }
 
-    public void start(Runnable run) {
-        // TODO SERVLET3 - async
-        this.dispatch = run;
-        request.coyoteRequest.action(ActionCode.ACTION_ASYNC_DISPATCH, null );
+    public void start(final Runnable run) {
+        if (state.compareAndSet(AsyncState.STARTED, AsyncState.DISPATCHING) ||
+            state.compareAndSet(AsyncState.DISPATCHED, AsyncState.DISPATCHING)) {
+            // TODO SERVLET3 - async
+            Runnable r = new Runnable() {
+                public void run() {
+                    try {
+                        run.run();
+                    }catch (Exception x) {
+                        log.error("Unable to run async task.",x);
+                    }
+                }
+            };
+            this.dispatch = r;
+            request.coyoteRequest.action(ActionCode.ACTION_ASYNC_DISPATCH, null );
+        } else {
+            throw new IllegalStateException("Dispatch not allowed. Invalid state:"+state.get());
+        }
     }
     
     public void addAsyncListener(AsyncListener listener) {
index 722fe3f..af0a23c 100644 (file)
@@ -46,6 +46,7 @@ import javax.servlet.ServletInputStream;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletRequestAttributeEvent;
 import javax.servlet.ServletRequestAttributeListener;
+import javax.servlet.ServletRequestWrapper;
 import javax.servlet.ServletResponse;
 import javax.servlet.SessionCookieConfig;
 import javax.servlet.SessionTrackingMode;
@@ -1464,9 +1465,9 @@ public class Request
         if (!isAsyncSupported()) throw new IllegalStateException("Not supported.");
         if (asyncContext==null) asyncContext = new AsyncContextImpl(this);
         else if (asyncContext.isStarted()) throw new IllegalStateException("Already started.");
+        asyncContext.setStarted(getContext());
         asyncContext.setServletRequest(getRequest());
         asyncContext.setServletResponse(response.getResponse());
-        asyncContext.setStarted(getContext());
         return asyncContext;
     }
 
@@ -1474,6 +1475,9 @@ public class Request
         startAsync();
         asyncContext.setServletRequest(request);
         asyncContext.setServletResponse(response);
+        //TODO SERVLET3 - async - need to retrieve the ServletContext here
+        //or just the webapp classloader associated with to do 
+        //run with start(Runnable)
         asyncContext.setHasOriginalRequestAndResponse(request==getRequest() && response==getResponse().getResponse());
         return asyncContext;
     }