You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2005/09/22 00:05:02 UTC

svn commit: r290848 - in /cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location: LocatedException.java LocatedRuntimeException.java

Author: sylvain
Date: Wed Sep 21 15:04:58 2005
New Revision: 290848

URL: http://svn.apache.org/viewcvs?rev=290848&view=rev
Log:
Ensure exceptions are correctly nested for JDK 1.4. This is mainly needed for SAXException which hasn't a getCause() method

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java?rev=290848&r1=290847&r2=290848&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java Wed Sep 21 15:04:58 2005
@@ -15,6 +15,7 @@
  */
 package org.apache.cocoon.util.location;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -48,8 +49,41 @@
     
     public LocatedException(String message, Throwable cause, Location location) {
         super(message, cause);
+        ensureCauseChainIsSet(cause);
         addCauseLocations(this, cause);
         addLocation(location);
+    }
+    
+    private static Method INIT_CAUSE_METHOD = null;
+    static {
+        try {
+            INIT_CAUSE_METHOD = Throwable.class.getMethod("initCause", new Class[] { Throwable.class} );
+        } catch(Exception e) {
+            // JDK < 1.4: ignore
+        }
+    }
+    
+    /**
+     * Crawl the cause chain and ensure causes are properly set using "initCause" on JDK >= 1.4.
+     * This is needed because some exceptions (e.g. SAXException) don't have a getCause() that is
+     * used to print stacktraces.
+     */
+    static void ensureCauseChainIsSet(Throwable thr) {
+        if (INIT_CAUSE_METHOD == null)
+            return;
+        
+        // Loop either until null or encountering exceptions that use this method.
+        while(thr != null && !(thr instanceof LocatedRuntimeException) && !(thr instanceof LocatedException)) {
+            Throwable parent = ExceptionUtils.getCause(thr);
+            if (parent != null) {
+                try {
+                    INIT_CAUSE_METHOD.invoke(thr, new Object[]{ parent });
+                } catch(Exception e) {
+                    // unlikely, so ignore
+                }
+            }
+            thr = parent;
+        }
     }
     
     /**

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java?rev=290848&r1=290847&r2=290848&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java Wed Sep 21 15:04:58 2005
@@ -62,7 +62,8 @@
             // Rethrow the cause
             throw lreCause;
         }
-        
+
+        LocatedException.ensureCauseChainIsSet(cause);
         LocatedException.addCauseLocations(this, cause);
         addLocation(location);
     }