Author: mcucchiara
Date: Tue Jan 18 22:42:54 2011
New Revision: 1060603

URL: http://svn.apache.org/viewvc?rev=1060603&view=rev
Log:
WW-3559 - PrepareInterceptor is eating exceptions that occur in 
prepare{methodName} methods

Modified:
    struts/struts2/trunk/xwork-core/   (props changed)
    
struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/PrepareInterceptor.java
    
struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/PrepareInterceptorTest.java

Propchange: struts/struts2/trunk/xwork-core/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Jan 18 22:42:54 2011
@@ -1,11 +1,10 @@
+cobertura.ser
 *.ipr
+.classpath
 *.iws
-*.iml
-build
-dist
 .project
-.classpath
+target
 ivyrep.properties
-cobertura.ser
+build
 .settings
-target
+dist

Modified: 
struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/PrepareInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/PrepareInterceptor.java?rev=1060603&r1=1060602&r2=1060603&view=diff
==============================================================================
--- 
struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/PrepareInterceptor.java
 (original)
+++ 
struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/interceptor/PrepareInterceptor.java
 Tue Jan 18 22:42:54 2011
@@ -1,4 +1,5 @@
 /*
+ * $Id$
  * Copyright 2002-2007,2009 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -99,8 +100,6 @@ public class PrepareInterceptor extends 
 
     private static final long serialVersionUID = -5216969014510719786L;
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(PrepareInterceptor.class);
-
     private final static String PREPARE_PREFIX = "prepare";
     private final static String ALT_PREPARE_PREFIX = "prepareDo";
 
@@ -144,18 +143,24 @@ public class PrepareInterceptor extends 
                 PrefixMethodInvocationUtil.invokePrefixMethod(invocation, 
prefixes);
             }
             catch (InvocationTargetException e) {
-                // just in case there's an exception while doing reflection,
-                // we still want prepare() to be able to get called.
-                LOG.warn("an exception occured while trying to execute 
prefixed method", e);
-            }
-            catch (IllegalAccessException e) {
-                // just in case there's an exception while doing reflection,
-                // we still want prepare() to be able to get called.
-                LOG.warn("an exception occured while trying to execute 
prefixed method", e);
-            } catch (Exception e) {
-                // just in case there's an exception while doing reflection,
-                // we still want prepare() to be able to get called.
-                LOG.warn("an exception occured while trying to execute 
prefixed method", e);
+                /*
+                 * The invoked method threw an exception and reflection 
wrapped it
+                 * in an InvocationTargetException.
+                 * If possible re-throw the original exception so that normal
+                 * exception handling will take place.
+                 */
+                Throwable cause = e.getCause();
+                if (cause instanceof Exception) {
+                    throw (Exception) cause;
+                } else if(cause instanceof Error) {
+                    throw (Error) cause;
+                } else {
+                    /*
+                     * The cause is not an Exception or Error (must be 
Throwable) so
+                     * just re-throw the wrapped exception.
+                     */
+                    throw e;
+                }
             }
 
             if (alwaysInvokePrepare) {

Modified: 
struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/PrepareInterceptorTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/PrepareInterceptorTest.java?rev=1060603&r1=1060602&r2=1060603&view=diff
==============================================================================
--- 
struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/PrepareInterceptorTest.java
 (original)
+++ 
struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/interceptor/PrepareInterceptorTest.java
 Tue Jan 18 22:42:54 2011
@@ -1,4 +1,5 @@
 /*
+ * $Id$
  * Copyright 2002-2007,2009 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -175,7 +176,25 @@ public class PrepareInterceptorTest exte
        controlActionProxy.verify();
        controlActionInvocation.verify();
     }
-    
+
+    public void testPrepareThrowException() throws Exception {
+        MockActionInvocation mai = new MockActionInvocation();
+        MockActionProxy mockActionProxy = new MockActionProxy();
+        mockActionProxy.setMethod("submit");
+        mai.setProxy(mockActionProxy);
+        mai.setAction(mock.proxy());
+
+        IllegalAccessException illegalAccessException = new 
IllegalAccessException();
+        mock.expectAndThrow("prepareSubmit", illegalAccessException);
+        mock.matchAndThrow("prepare", new RuntimeException());
+
+        try {
+            interceptor.intercept(mai);
+            fail("Should not have reached this point.");
+        } catch (Throwable t) {
+            assertSame(illegalAccessException, t);
+        }
+    }
 
     @Override
     protected void setUp() throws Exception {
@@ -196,7 +215,7 @@ public class PrepareInterceptorTest exte
      * @author tm_jee
      */
     public interface ActionInterface extends Action, Preparable {
-       void prepareSubmit();
+       void prepareSubmit() throws Exception;
     }
 
 }


Reply via email to