camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r762220 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ camel-core/src/main/java/org/apache/camel/util/ camel...
Date Mon, 06 Apr 2009 06:35:19 GMT
Author: davsclaus
Date: Mon Apr  6 06:35:18 2009
New Revision: 762220

URL: http://svn.apache.org/viewvc?rev=762220&view=rev
Log:
CAMEL-1518: Added getException(type) to get the caused by exception.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpWithOnExceptionRoute.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Mon Apr  6 06:35:18
2009
@@ -196,6 +196,20 @@
     Exception getException();
 
     /**
+     * Returns the exception associated with this exchange.
+     * <p/>
+     * Is used to get the caused exception that typically have been wrapped in some sort
+     * of Camel wrapper exception
+     * <p/>
+     * The stategy is to look in the exception hieracy to find the first given cause that
matches the type.
+     * Will start from the bottom (the real cause) and walk upwards.
+     *
+     * @param type the exception type
+     * @return the exception (or null if no faults or if no caused exception matched)
+     */
+    <T> T getException(Class<T> type);
+
+    /**
      * Sets the exception associated with this exchange
      *
      * @param e  the caused exception

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Mon Apr
 6 06:35:18 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.impl;
 
+import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -28,6 +29,7 @@
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.UuidGenerator;
 
 /**
@@ -227,6 +229,22 @@
         return exception;
     }
 
+    public <T> T getException(Class<T> type) {
+        if (exception == null) {
+            return null;
+        }
+
+        Iterator<Throwable> it = ObjectHelper.createExceptionIterator(exception);
+        while (it.hasNext()) {
+            Throwable e = it.next();
+            if (type.isInstance(e)) {
+                return type.cast(e);
+            }
+        }
+        // not found
+        return null;
+    }
+
     public void setException(Exception exception) {
         this.exception = exception;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
Mon Apr  6 06:35:18 2009
@@ -16,15 +16,13 @@
  */
 package org.apache.camel.processor.exceptionpolicy;
 
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.model.OnExceptionDefinition;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -60,7 +58,7 @@
                                             Throwable exception) {
 
         // recursive up the tree using the iterator
-        Iterator<Throwable> it = createExceptionIterator(exception); 
+        Iterator<Throwable> it = createExceptionIterator(exception);
         while (it.hasNext()) {
             OnExceptionDefinition type = findMatchedExceptionPolicy(exceptionPolicices, exchange,
it.next());
             if (type != null) {
@@ -176,7 +174,7 @@
      * @return the iterator
      */
     protected Iterator<Throwable> createExceptionIterator(Throwable exception) {
-        return new ExceptionIterator(exception);
+        return ObjectHelper.createExceptionIterator(exception);
     }
 
     private static int getInheritanceLevel(Class clazz) {
@@ -186,34 +184,4 @@
         return 1 + getInheritanceLevel(clazz.getSuperclass());
     }
 
-    private class ExceptionIterator implements Iterator<Throwable> {
-        private List<Throwable> tree = new ArrayList<Throwable>();
-        private Iterator<Throwable> it;
-
-        public ExceptionIterator(Throwable exception) {
-            Throwable current = exception;
-            // spool to the bottom of the caused by tree
-            while (current != null) {
-                tree.add(current);
-                current = current.getCause();
-            }
-
-            // reverse tree so we go from bottom to top
-            Collections.reverse(tree);
-            it = tree.iterator();
-        }
-
-        public boolean hasNext() {
-            return it.hasNext();
-        }
-
-        public Throwable next() {
-            return it.next();
-        }
-
-        public void remove() {
-            it.remove();
-        }
-    }
-
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Mon Apr 
6 06:35:18 2009
@@ -901,4 +901,45 @@
         return sb.toString();
     }
 
+    /**
+     * Creates an iterator to walk the exception from the bottom up
+     * (the last caused by going upwards to the root exception).
+     *
+     * @param exception  the exception
+     * @return the iterator
+     */
+    public static Iterator<Throwable> createExceptionIterator(Throwable exception)
{
+        return new ExceptionIterator(exception);
+    }
+
+    private static class ExceptionIterator implements Iterator<Throwable> {
+        private List<Throwable> tree = new ArrayList<Throwable>();
+        private Iterator<Throwable> it;
+
+        public ExceptionIterator(Throwable exception) {
+            Throwable current = exception;
+            // spool to the bottom of the caused by tree
+            while (current != null) {
+                tree.add(current);
+                current = current.getCause();
+            }
+
+            // reverse tree so we go from bottom to top
+            Collections.reverse(tree);
+            it = tree.iterator();
+        }
+
+        public boolean hasNext() {
+            return it.hasNext();
+        }
+
+        public Throwable next() {
+            return it.next();
+        }
+
+        public void remove() {
+            it.remove();
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExchangeTest.java Mon
Apr  6 06:35:18 2009
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.impl;
 
+import java.io.IOException;
+import java.net.ConnectException;
+
 import org.apache.camel.ExchangeTestSupport;
 import org.apache.camel.InvalidPayloadException;
-import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * @version $Revision$
@@ -50,6 +54,27 @@
         }
     }
 
+    public void testExceptionAsType() throws Exception {
+        exchange.setException(ObjectHelper.wrapRuntimeCamelException(new ConnectException("Cannot
connect to remote server")));
+
+        ConnectException ce = exchange.getException(ConnectException.class);
+        assertNotNull(ce);
+        assertEquals("Cannot connect to remote server", ce.getMessage());
+
+        IOException ie = exchange.getException(IOException.class);
+        assertNotNull(ie);
+        assertEquals("Cannot connect to remote server", ie.getMessage());
+
+        Exception e = exchange.getException(Exception.class);
+        assertNotNull(e);
+        assertEquals("Cannot connect to remote server", e.getMessage());
+
+        RuntimeCamelException rce = exchange.getException(RuntimeCamelException.class);
+        assertNotNull(rce);
+        assertNotSame("Cannot connect to remote server", rce.getMessage());
+        assertEquals("Cannot connect to remote server", rce.getCause().getMessage());
+    }
+
     public void testHeader() throws Exception {
         assertNotNull(exchange.getIn().getHeaders());
 

Modified: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpWithOnExceptionRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpWithOnExceptionRoute.java?rev=762220&r1=762219&r2=762220&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpWithOnExceptionRoute.java
(original)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/tx/JmsToHttpWithOnExceptionRoute.java
Mon Apr  6 06:35:18 2009
@@ -42,12 +42,8 @@
         // if its a 404 then regard it as handled
         onException(HttpOperationFailedException.class).onWhen(new Predicate() {
             public boolean matches(Exchange exchange) {
-                if (exchange.getException() instanceof HttpOperationFailedException) {
-                    HttpOperationFailedException e = (HttpOperationFailedException) exchange.getException();
-                    return e.getStatusCode() == 404;
-                } else {
-                    return false;
-                }
+                HttpOperationFailedException e = exchange.getException(HttpOperationFailedException.class);
+                return e != null && e.getStatusCode() == 404;
             }
         }).handled(true).to("mock:404").transform(constant(noAccess));
 



Mime
View raw message