geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1071472 - in /geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat: ./ interceptor/ listener/ valve/
Date Thu, 17 Feb 2011 01:41:16 GMT
Author: xuhaihong
Date: Thu Feb 17 01:41:15 2011
New Revision: 1071472

URL: http://svn.apache.org/viewvc?rev=1071472&view=rev
Log:
GERONIMO-5821 Make sure cleanup action is taken if any RuntimeException is thrown in the invocation
chain

Added:
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfterContext.java
Modified:
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/ComponentContextBeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/InstanceContextBeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/OWBBeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/PolicyContextBeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/UserTransactionBeforeAfter.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/listener/DispatchListener.java
    geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/valve/GeronimoBeforeAfterValve.java

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/GeronimoStandardContext.java
Thu Feb 17 01:41:15 2011
@@ -75,6 +75,7 @@ import org.apache.geronimo.security.jacc
 import org.apache.geronimo.security.jacc.ComponentPermissions;
 import org.apache.geronimo.security.jacc.RunAsSource;
 import org.apache.geronimo.tomcat.interceptor.BeforeAfter;
+import org.apache.geronimo.tomcat.interceptor.BeforeAfterContext;
 import org.apache.geronimo.tomcat.interceptor.ComponentContextBeforeAfter;
 import org.apache.geronimo.tomcat.interceptor.InstanceContextBeforeAfter;
 import org.apache.geronimo.tomcat.interceptor.OWBBeforeAfter;
@@ -129,11 +130,11 @@ public class GeronimoStandardContext ext
     private Bundle bundle;
     private ServiceRegistration serviceRegistration;
 
-    private ThreadLocal<Stack<Object[]>> beforeAfterContexts = new ThreadLocal<Stack<Object[]>>()
{
+    private ThreadLocal<Stack<BeforeAfterContext>> beforeAfterContexts = new
ThreadLocal<Stack<BeforeAfterContext>>() {
 
         @Override
-        protected Stack<Object[]> initialValue() {
-            return new Stack<Object[]>();
+        protected Stack<BeforeAfterContext> initialValue() {
+            return new Stack<BeforeAfterContext>();
         }
 
     };
@@ -410,18 +411,18 @@ public class GeronimoStandardContext ext
      */
     @Override
     public void backgroundProcess() {
-        Object context[] = null;
+        BeforeAfterContext beforeAfterContext = null;
 
         if (beforeAfter != null){
-            context = new Object[contextCount];
-            beforeAfter.before(context, null, null, BeforeAfter.EDGE_SERVLET);
+            beforeAfterContext = new BeforeAfterContext(contextCount);
+            beforeAfter.before(beforeAfterContext, null, null, BeforeAfter.EDGE_SERVLET);
         }
 
         try {
             super.backgroundProcess();
         } finally {
             if (beforeAfter != null){
-                beforeAfter.after(context, null, null, 0);
+                beforeAfter.after(beforeAfterContext, null, null, BeforeAfter.EDGE_SERVLET);
             }
         }
     }
@@ -431,11 +432,11 @@ public class GeronimoStandardContext ext
             serviceRegistration.unregister();
         }
 
-        Object context[] = null;
+        BeforeAfterContext beforeAfterContext = null;
 
         if (beforeAfter != null){
-            context = new Object[contextCount];
-            beforeAfter.before(context, null, null, BeforeAfter.EDGE_SERVLET);
+            beforeAfterContext = new BeforeAfterContext(contextCount);
+            beforeAfter.before(beforeAfterContext, null, null, BeforeAfter.EDGE_SERVLET);
         }
 
         try {
@@ -443,7 +444,7 @@ public class GeronimoStandardContext ext
             destroy();
         } finally {
             if (beforeAfter != null){
-                beforeAfter.after(context, null, null, 0);
+                beforeAfter.after(beforeAfterContext, null, null, BeforeAfter.EDGE_SERVLET);
             }
         }
     }
@@ -830,21 +831,25 @@ public class GeronimoStandardContext ext
     @Override
     protected ClassLoader bindThread() {
         ClassLoader oldClassLoader =  super.bindThread();
-        Object context[] = null;
+        BeforeAfterContext beforeAfterContext = null;
 
         if (beforeAfter != null){
-            context = new Object[contextCount];
-            beforeAfterContexts.get().push(context);
-            beforeAfter.before(context, null, null, BeforeAfter.EDGE_SERVLET);
+            beforeAfterContext = new BeforeAfterContext(contextCount);
+            beforeAfter.before(beforeAfterContext, null, null, BeforeAfter.EDGE_SERVLET);
+            //beforeAfterContext is pushed the stack only if every BeforeAfter element works
fine
+            beforeAfterContexts.get().push(beforeAfterContext);
         }
         return oldClassLoader;
     }
 
     @Override
     protected void unbindThread(ClassLoader oldContextClassLoader) {
-        super.unbindThread(oldContextClassLoader);
-        if (beforeAfter != null){
-            beforeAfter.after(beforeAfterContexts.get().pop(), null, null, 0);
+        try {
+            super.unbindThread(oldContextClassLoader);
+        } finally {
+            if (beforeAfter != null) {
+                beforeAfter.after(beforeAfterContexts.get().pop(), null, null, 0);
+            }
         }
     }
 }

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -25,8 +25,8 @@ public interface BeforeAfter {
 
     public static final int DISPATCHED = 1;
 
-    void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch);
+    void before(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest, ServletResponse
httpResponse, int dispatch);
 
-    void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch);
+    void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest, ServletResponse
httpResponse, int dispatch);
 
 }

Added: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfterContext.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfterContext.java?rev=1071472&view=auto
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfterContext.java
(added)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/BeforeAfterContext.java
Thu Feb 17 01:41:15 2011
@@ -0,0 +1,37 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.geronimo.tomcat.interceptor;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class BeforeAfterContext {
+
+    public BeforeAfterContext() {
+    }
+
+    public BeforeAfterContext(int count) {
+        contexts = new Object[count];
+        clearRequiredFlags = new boolean[count];
+    }
+
+    public Object[] contexts;
+
+    public boolean[] clearRequiredFlags;
+
+}

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/ComponentContextBeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/ComponentContextBeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/ComponentContextBeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/ComponentContextBeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -33,19 +33,36 @@ public class ComponentContextBeforeAfter
         this.componentContext = componentContext;
     }
 
-    public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        context[index] = RootContext.getComponentContext();
-        RootContext.setComponentContext(componentContext);
-        if (next != null) {
-            next.before(context, httpRequest, httpResponse, dispatch);
+    public void before(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+
+        try {
+            beforeAfterContext.contexts[index] = RootContext.getComponentContext();
+            RootContext.setComponentContext(componentContext);
+            beforeAfterContext.clearRequiredFlags[index] = true;
+
+            if (next != null) {
+                next.before(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } catch (RuntimeException e) {
+            if (beforeAfterContext.clearRequiredFlags[index]) {
+                RootContext.setComponentContext((Context) beforeAfterContext.contexts[index]);
+                beforeAfterContext.clearRequiredFlags[index] = false;
+                throw e;
+            }
         }
     }
 
-    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        if (next != null) {
-            next.after(context, httpRequest, httpResponse, dispatch);
+    public void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+        try {
+            if (next != null) {
+                next.after(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } finally {
+            if (beforeAfterContext.clearRequiredFlags[index]) {
+                RootContext.setComponentContext((Context) beforeAfterContext.contexts[index]);
+                beforeAfterContext.clearRequiredFlags[index] = false;
+            }
         }
-        RootContext.setComponentContext((Context) context[index]);
     }
 
 }

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/InstanceContextBeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/InstanceContextBeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/InstanceContextBeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/InstanceContextBeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -44,36 +44,58 @@ public class InstanceContextBeforeAfter 
         this.trackedConnectionAssociator = trackedConnectionAssociator;
     }
 
-    public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
+    public void before(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
         try {
             SharedConnectorInstanceContext newConnectorInstanceContext = new SharedConnectorInstanceContext(unshareableResources,
applicationManagedSecurityResources, false);
             ConnectorInstanceContext oldContext = trackedConnectionAssociator.enter(newConnectorInstanceContext);
             if (oldContext != null) {
                 newConnectorInstanceContext.share(oldContext);
             }
-            context[oldIndex] = oldContext;
-            context[newIndex] = newConnectorInstanceContext;
+            beforeAfterContext.contexts[oldIndex] = oldContext;
+            beforeAfterContext.clearRequiredFlags[oldIndex] = true;
+
+            beforeAfterContext.contexts[newIndex] = newConnectorInstanceContext;
+            beforeAfterContext.clearRequiredFlags[newIndex] = true;
         } catch (ResourceException e) {
             throw new RuntimeException(e);
         }
-        if (next != null) {
-            next.before(context, httpRequest, httpResponse, dispatch);
+        try {
+            if (next != null) {
+                next.before(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } catch (RuntimeException e) {
+            cleanUp(beforeAfterContext);
+            throw e;
         }
     }
 
-    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        if (next != null) {
-            next.after(context, httpRequest, httpResponse, dispatch);
-        }
+    public void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
         try {
-            ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext)
context[oldIndex];
-            SharedConnectorInstanceContext newConnectorInstanceContext = (SharedConnectorInstanceContext)
context[newIndex];
-            if (oldConnectorInstanceContext != null) {
-                newConnectorInstanceContext.hide();
+            if (next != null) {
+                next.after(beforeAfterContext, httpRequest, httpResponse, dispatch);
             }
-            trackedConnectionAssociator.exit(oldConnectorInstanceContext);
-        } catch (ResourceException e) {
-            throw new RuntimeException(e);
+        } finally {
+            cleanUp(beforeAfterContext);
         }
     }
+
+    private void cleanUp(BeforeAfterContext beforeAfterContext) {
+        if (beforeAfterContext.clearRequiredFlags[oldIndex]) {
+            try {
+                ConnectorInstanceContext oldConnectorInstanceContext = (ConnectorInstanceContext)
beforeAfterContext.contexts[oldIndex];
+                if (beforeAfterContext.clearRequiredFlags[newIndex]) {
+                    SharedConnectorInstanceContext newConnectorInstanceContext = (SharedConnectorInstanceContext)
beforeAfterContext.contexts[newIndex];
+                    if (oldConnectorInstanceContext != null) {
+                        newConnectorInstanceContext.hide();
+                    }
+                    beforeAfterContext.clearRequiredFlags[newIndex] = false;
+                }
+                trackedConnectionAssociator.exit(oldConnectorInstanceContext);
+                beforeAfterContext.clearRequiredFlags[oldIndex] = false;
+            } catch (ResourceException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
 }

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/OWBBeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/OWBBeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/OWBBeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/OWBBeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -20,11 +20,10 @@
 
 package org.apache.geronimo.tomcat.interceptor;
 
-import java.util.Map;
-
 import javax.servlet.ServletContext;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
+
 import org.apache.geronimo.openwebbeans.GeronimoSingletonService;
 import org.apache.webbeans.config.WebBeansContext;
 
@@ -44,18 +43,31 @@ public class OWBBeforeAfter implements B
     }
 
     @Override
-    public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        context[index] = GeronimoSingletonService.contextEntered(owbContext);
-        if (next != null) {
-            next.before(context, httpRequest, httpResponse, dispatch);
+    public void before(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+        try {
+            beforeAfterContext.contexts[index] = GeronimoSingletonService.contextEntered(owbContext);
+            beforeAfterContext.clearRequiredFlags[index] = true;
+            if (next != null) {
+                next.before(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } catch (RuntimeException e) {
+            GeronimoSingletonService.contextExited((WebBeansContext) beforeAfterContext.contexts[index]);
+            beforeAfterContext.clearRequiredFlags[index] = false;
+            throw e;
         }
     }
 
     @Override
-    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        if (next != null) {
-            next.after(context, httpRequest, httpResponse, dispatch);
+    public void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+        try {
+            if (next != null) {
+                next.after(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } finally {
+            if (beforeAfterContext.clearRequiredFlags[index]) {
+                GeronimoSingletonService.contextExited((WebBeansContext) beforeAfterContext.contexts[index]);
+                beforeAfterContext.clearRequiredFlags[index] = false;
+            }
         }
-        GeronimoSingletonService.contextExited((WebBeansContext) context[index]);
     }
 }

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/PolicyContextBeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/PolicyContextBeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/PolicyContextBeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/PolicyContextBeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -14,9 +14,9 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
+
 package org.apache.geronimo.tomcat.interceptor;
 
-import javax.security.jacc.PolicyContext;
 import javax.security.auth.Subject;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
@@ -24,15 +24,20 @@ import javax.servlet.ServletResponse;
 import org.apache.geronimo.security.Callers;
 import org.apache.geronimo.security.ContextManager;
 
-public class PolicyContextBeforeAfter implements BeforeAfter{
+public class PolicyContextBeforeAfter implements BeforeAfter {
 
     public static final String DEFAULT_SUBJECT = "~DEFAULT_SUBJECT";
 
     private final BeforeAfter next;
+
     private final String policyContextID;
+
     private final int policyContextIDIndex;
+
     private final int callersIndex;
+
     private final int defaultSubjectIndex;
+
     private final Subject defaultSubject;
 
     public PolicyContextBeforeAfter(BeforeAfter next, int policyContextIDIndex, int callersIndex,
int defaultSubjectIndex, String policyContextID, Subject defaultSubject) {
@@ -44,41 +49,56 @@ public class PolicyContextBeforeAfter im
         this.defaultSubject = defaultSubject;
     }
 
-    public void before(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-
-        //Save the old
-
-//        context[policyContextIDIndex] = PolicyContext.getContextID();
-        context[callersIndex] = ContextManager.getCallers();
+    public void before(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
 
-        //Set the new
-//        PolicyContext.setContextID(policyContextID);
-//        PolicyContext.setHandlerData(httpRequest);
-        if (httpRequest != null){
-            context[defaultSubjectIndex] = httpRequest.getAttribute(DEFAULT_SUBJECT);
-            httpRequest.setAttribute(DEFAULT_SUBJECT, defaultSubject);
-        }
-
-
-        if (next != null) {
-            next.before(context, httpRequest, httpResponse, dispatch);
+        try {
+            //Save the old
+            //context[policyContextIDIndex] = PolicyContext.getContextID();
+            beforeAfterContext.contexts[callersIndex] = ContextManager.getCallers();
+            beforeAfterContext.clearRequiredFlags[callersIndex] = true;
+            //Set the new
+            //        PolicyContext.setContextID(policyContextID);
+            //        PolicyContext.setHandlerData(httpRequest);
+            if (httpRequest != null) {
+                beforeAfterContext.contexts[defaultSubjectIndex] = httpRequest.getAttribute(DEFAULT_SUBJECT);
+                httpRequest.setAttribute(DEFAULT_SUBJECT, defaultSubject);
+                beforeAfterContext.clearRequiredFlags[defaultSubjectIndex] = true;
+            }
+
+            if (next != null) {
+                next.before(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } catch (RuntimeException e) {
+            if (beforeAfterContext.clearRequiredFlags[callersIndex]) {
+                ContextManager.popCallers((Callers) beforeAfterContext.contexts[callersIndex]);
+                beforeAfterContext.clearRequiredFlags[callersIndex] = false;
+            }
+            if (beforeAfterContext.clearRequiredFlags[defaultSubjectIndex]) {
+                httpRequest.setAttribute(DEFAULT_SUBJECT, beforeAfterContext.contexts[defaultSubjectIndex]);
+                beforeAfterContext.clearRequiredFlags[defaultSubjectIndex] = false;
+            }
+            throw e;
         }
     }
 
-    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        if (next != null) {
-            next.after(context, httpRequest, httpResponse, dispatch);
+    public void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+        try {
+            if (next != null) {
+                next.after(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } finally {
+            //Replace the old
+            //        PolicyContext.setContextID((String)context[policyContextIDIndex]);
+            // Must unset handler data from thread - see GERONIMO-4574
+            //        PolicyContext.setHandlerData(null);
+            if (beforeAfterContext.clearRequiredFlags[callersIndex]) {
+                ContextManager.popCallers((Callers) beforeAfterContext.contexts[callersIndex]);
+                beforeAfterContext.clearRequiredFlags[callersIndex] = false;
+            }
+            if (beforeAfterContext.clearRequiredFlags[defaultSubjectIndex]) {
+                httpRequest.setAttribute(DEFAULT_SUBJECT, beforeAfterContext.contexts[defaultSubjectIndex]);
+                beforeAfterContext.clearRequiredFlags[defaultSubjectIndex] = false;
+            }
         }
-
-        //Replace the old
-//        PolicyContext.setContextID((String)context[policyContextIDIndex]);
-        // Must unset handler data from thread - see GERONIMO-4574
-//        PolicyContext.setHandlerData(null);
-        ContextManager.popCallers((Callers) context[callersIndex]);
-        if (httpRequest != null)
-            httpRequest.setAttribute(DEFAULT_SUBJECT, context[defaultSubjectIndex]);
-
     }
-
 }
-

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/UserTransactionBeforeAfter.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/UserTransactionBeforeAfter.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/UserTransactionBeforeAfter.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/interceptor/UserTransactionBeforeAfter.java
Thu Feb 17 01:41:15 2011
@@ -42,25 +42,32 @@ public class UserTransactionBeforeAfter 
         this.userTransaction = userTransaction;
     }
 
-    public void after(Object[] context, ServletRequest httpRequest, ServletResponse httpResponse,
int dispatch) {
-        if (next != null) {
-            next.after(context, httpRequest, httpResponse, dispatch);
-        }
-        
-        boolean active = (Boolean)context[index];
-        if ((!active && isMarkedRollback()) || (dispatch == EDGE_SERVLET &&
isActive())) {
-            try {
-                userTransaction.rollback();
-            } catch (SystemException e) {
-                throw new RuntimeException("Error rolling back transaction left open by user
program", e);
+    public void after(BeforeAfterContext beforeAfterContext, ServletRequest httpRequest,
ServletResponse httpResponse, int dispatch) {
+        try {
+            if (next != null) {
+                next.after(beforeAfterContext, httpRequest, httpResponse, dispatch);
+            }
+        } finally {
+            if (beforeAfterContext.clearRequiredFlags[index]) {
+                boolean active = (Boolean) beforeAfterContext.contexts[index];
+                beforeAfterContext.clearRequiredFlags[index] = false;
+                if ((!active && isMarkedRollback()) || (dispatch == EDGE_SERVLET
&& isActive())) {
+                    try {
+                        userTransaction.rollback();
+                    } catch (SystemException e) {
+                        throw new RuntimeException("Error rolling back transaction left open
by user program", e);
+                    }
+                }
             }
         }
-
     }
 
-    public void before(Object[] context, ServletRequest request, ServletResponse response,
int dispatch) {
-        context[index] = isActive();
-        next.before(context, request, response, dispatch);
+    public void before(BeforeAfterContext beforeAfterContext, ServletRequest request, ServletResponse
response, int dispatch) {
+        beforeAfterContext.contexts[index] = isActive();
+        beforeAfterContext.clearRequiredFlags[index] = true;
+        if (next != null) {
+            next.before(beforeAfterContext, request, response, dispatch);
+        }
     }
 
     private boolean isActive() {

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/listener/DispatchListener.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/listener/DispatchListener.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/listener/DispatchListener.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/listener/DispatchListener.java
Thu Feb 17 01:41:15 2011
@@ -18,9 +18,9 @@ package org.apache.geronimo.tomcat.liste
 
 import java.util.Stack;
 
+import javax.security.jacc.PolicyContext;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-import javax.security.jacc.PolicyContext;
 
 import org.apache.catalina.Container;
 import org.apache.catalina.Globals;
@@ -29,6 +29,7 @@ import org.apache.catalina.InstanceListe
 import org.apache.catalina.core.StandardWrapper;
 import org.apache.geronimo.tomcat.GeronimoStandardContext;
 import org.apache.geronimo.tomcat.interceptor.BeforeAfter;
+import org.apache.geronimo.tomcat.interceptor.BeforeAfterContext;
 import org.apache.geronimo.tomcat.security.jacc.JACCRealm;
 import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.http.mapper.Mapper;
@@ -40,9 +41,9 @@ public class DispatchListener implements
 
     private static final Logger log = LoggerFactory.getLogger(DispatchListener.class);
 
-    private static ThreadLocal<Stack<Object[]>> currentContext = new ThreadLocal<Stack<Object[]>>()
{
-        protected Stack<Object[]> initialValue() {
-            return new Stack<Object[]>();
+    private static ThreadLocal<Stack<BeforeAfterContext>> currentContext = new
ThreadLocal<Stack<BeforeAfterContext>>() {
+        protected Stack<BeforeAfterContext> initialValue() {
+            return new Stack<BeforeAfterContext>();
         }
     };
 
@@ -50,14 +51,17 @@ public class DispatchListener implements
 
         if (event.getType().equals(InstanceEvent.BEFORE_SERVICE_EVENT)) {
             String oldWrapperName = JACCRealm.setRequestWrapperName(event.getWrapper().getName());
-            currentContext.get().push(new Object[] {oldWrapperName });
+
+            BeforeAfterContext beforeAfterContext = new BeforeAfterContext(1);
+            beforeAfterContext.contexts[0] = oldWrapperName;
+
+            currentContext.get().push(beforeAfterContext);
         }
 
         if (event.getType().equals(InstanceEvent.AFTER_SERVICE_EVENT)) {
-            JACCRealm.setRequestWrapperName((String) currentContext.get().pop()[0]);
+            JACCRealm.setRequestWrapperName((String) currentContext.get().pop().contexts[0]);
         }
 
-
         if (event.getType().equals(InstanceEvent.BEFORE_DISPATCH_EVENT)) {
             Container parent = event.getWrapper().getParent();
             if (parent instanceof GeronimoStandardContext) {
@@ -77,17 +81,19 @@ public class DispatchListener implements
 
         BeforeAfter beforeAfter = webContext.getBeforeAfter();
         if (beforeAfter != null) {
-            Stack<Object[]> stack = currentContext.get();
-            Object context[] = new Object[webContext.getContextCount() + 2];
+            Stack<BeforeAfterContext> stack = currentContext.get();
+
+            BeforeAfterContext beforeAfterContext = new BeforeAfterContext(webContext.getContextCount()
+ 2);
+
             String wrapperName = getWrapperName(request, webContext);
-            context[webContext.getContextCount()] = JACCRealm.setRequestWrapperName(wrapperName);
+            beforeAfterContext.contexts[webContext.getContextCount()] = JACCRealm.setRequestWrapperName(wrapperName);
 
-            context[webContext.getContextCount() + 1] = PolicyContext.getContextID();
+            beforeAfterContext.contexts[webContext.getContextCount() + 1] = PolicyContext.getContextID();
             PolicyContext.setContextID(webContext.getPolicyContextId());
 
-            beforeAfter.before(context, request, response, BeforeAfter.DISPATCHED);
+            beforeAfter.before(beforeAfterContext, request, response, BeforeAfter.DISPATCHED);
 
-            stack.push(context);
+            stack.push(beforeAfterContext);
         }
     }
 
@@ -95,13 +101,13 @@ public class DispatchListener implements
 
         BeforeAfter beforeAfter = webContext.getBeforeAfter();
         if (beforeAfter != null) {
-            Stack<Object[]> stack = currentContext.get();
-            Object context[] = stack.pop();
+            Stack<BeforeAfterContext> stack = currentContext.get();
+            BeforeAfterContext beforeAfterContext = stack.pop();
 
-            beforeAfter.after(context, request, response, BeforeAfter.DISPATCHED);
+            beforeAfter.after(beforeAfterContext, request, response, BeforeAfter.DISPATCHED);
 
-            JACCRealm.setRequestWrapperName((String) context[webContext.getContextCount()]);
-            PolicyContext.setContextID((String) context[webContext.getContextCount() + 1]);
+            JACCRealm.setRequestWrapperName((String) beforeAfterContext.contexts[webContext.getContextCount()]);
+            PolicyContext.setContextID((String) beforeAfterContext.contexts[webContext.getContextCount()
+ 1]);
         }
     }
 

Modified: geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/valve/GeronimoBeforeAfterValve.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/valve/GeronimoBeforeAfterValve.java?rev=1071472&r1=1071471&r2=1071472&view=diff
==============================================================================
--- geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/valve/GeronimoBeforeAfterValve.java
(original)
+++ geronimo/server/trunk/plugins/tomcat/geronimo-tomcat7/src/main/java/org/apache/geronimo/tomcat/valve/GeronimoBeforeAfterValve.java
Thu Feb 17 01:41:15 2011
@@ -24,6 +24,7 @@ import org.apache.catalina.connector.Req
 import org.apache.catalina.connector.Response;
 import org.apache.catalina.valves.ValveBase;
 import org.apache.geronimo.tomcat.interceptor.BeforeAfter;
+import org.apache.geronimo.tomcat.interceptor.BeforeAfterContext;
 
 public class GeronimoBeforeAfterValve extends ValveBase{
 
@@ -37,19 +38,19 @@ public class GeronimoBeforeAfterValve ex
     }
 
     public void invoke(Request request, Response response) throws IOException, ServletException
{
-        Object context[] = new Object[contextIndexCount];
+        BeforeAfterContext beforeAfterContext = new BeforeAfterContext(contextIndexCount);
 
         if (beforeAfter != null){
-            beforeAfter.before(context, request, response, BeforeAfter.EDGE_SERVLET);
+            beforeAfter.before(beforeAfterContext, request, response, BeforeAfter.EDGE_SERVLET);
         }
-
-        // Pass this request on to the next valve in our pipeline
-        getNext().invoke(request, response);
-
-        if (beforeAfter != null){
-            beforeAfter.after(context, request, response, 0);
+        try {
+            // Pass this request on to the next valve in our pipeline
+            getNext().invoke(request, response);
+        } finally {
+            if (beforeAfter != null) {
+                beforeAfter.after(beforeAfterContext, request, response, BeforeAfter.EDGE_SERVLET);
+            }
         }
-
     }
 
 }



Mime
View raw message