You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/05/19 09:39:07 UTC

svn commit: r776221 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/component/bean/ test/java/org/apache/camel/component/bean/

Author: davsclaus
Date: Tue May 19 07:39:07 2009
New Revision: 776221

URL: http://svn.apache.org/viewvc?rev=776221&view=rev
Log:
CAMEL-1626: @ExchangeException now uses the parameter type to return the desired exception from its cased by exception hieracy

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationWrappedExceptionTest.java
      - copied, changed from r775966, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=776221&r1=776220&r2=776221&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Tue May 19 07:39:07 2009
@@ -25,6 +25,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Scanner;
 import java.util.regex.Pattern;
@@ -36,6 +37,7 @@
 import org.apache.camel.impl.ExpressionAdapter;
 import org.apache.camel.language.bean.BeanLanguage;
 import org.apache.camel.spi.Language;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * A helper class for working with <a href="http://camel.apache.org/expression.html">expressions</a>.
@@ -163,6 +165,43 @@
     }   
     
     /**
+     * Returns an expression for an exception set on the exchange
+     * <p/>
+     * Is used to get the caused exception that typically have been wrapped in some sort
+     * of Camel wrapper exception
+     * @param type the exception type
+     * @see Exchange#getException(Class)
+     * @return an expression object which will return the exception set on the exchange
+     */
+    public static Expression exchangeExceptionExpression(final Class<Exception> type) {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                Exception exception = exchange.getException(type);
+                if (exception == null) {
+                    exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
+                    // must use exception iterator to walk it and find the type we are looking for
+                    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;
+
+                }
+                return exception;
+            }
+
+            @Override
+            public String toString() {
+                return "exchangeException[" + type + "]";
+            }
+        };
+    }
+
+    /**
      * Returns an expression for the type converter
      *
      * @return an expression object which will return the type converter

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=776221&r1=776220&r2=776221&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Tue May 19 07:39:07 2009
@@ -563,7 +563,7 @@
         } else if (annotation instanceof OutHeaders) {
             return ExpressionBuilder.outHeadersExpression();
         } else if (annotation instanceof ExchangeException) {
-            return ExpressionBuilder.exchangeExceptionExpression();
+            return ExpressionBuilder.exchangeExceptionExpression(parameterType);
         } else {
             LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
             if (languageAnnotation != null) {

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationWrappedExceptionTest.java (from r775966, camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationWrappedExceptionTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationWrappedExceptionTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java&r1=775966&r2=776221&rev=776221&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationWrappedExceptionTest.java Tue May 19 07:39:07 2009
@@ -16,63 +16,26 @@
  */
 package org.apache.camel.component.bean;
 
-import javax.naming.Context;
+import java.io.IOException;
 
-import org.apache.camel.ContextTestSupport;
 import org.apache.camel.ExchangeException;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.util.jndi.JndiContext;
-
-public class BeanWithExchangeExceptionAnnotationTest extends ContextTestSupport {
-
-    public void testBeanWithAnnotationAndExchangeTest() throws Exception {
-        MockEndpoint result = getMockEndpoint("mock:result");
-        MockEndpoint error = getMockEndpoint("mock:error");
-        
-        result.expectedMessageCount(0);
-        error.expectedMessageCount(1);
-        error.expectedBodiesReceived("The Body");
-        
-        template.requestBody("direct:start", "The Body");
 
-        result.assertIsSatisfied();
-        error.assertIsSatisfied();
-    }
-
-    protected Context createJndiContext() throws Exception {
-        JndiContext answer = new JndiContext();
-        answer.bind("myBean", new MyBean());
-        return answer;
-    }
-
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            public void configure() throws Exception {
-                errorHandler(deadLetterChannel("mock:error"));
-                
-                onException(MyCustomException.class).
-                    maximumRedeliveries(0).
-                    handled(true).
-                    beanRef("myBean", "handleException").
-                    to("mock:error");
-                
-                from("direct:start").
-                    beanRef("myBean", "throwException").
-                    to("mock:result");
-            }
-        };
-    }
+public class BeanWithExchangeExceptionAnnotationWrappedExceptionTest extends BeanWithExchangeExceptionAnnotationTest {
 
     public static class MyBean {
 
-        public void throwException() throws MyCustomException {
-            throw new MyCustomException("I'm being thrown!!");
-        }       
-        
-        public void handleException(@ExchangeException Exception exception) {
-            assertNotNull(exception);
-            assertEquals("I'm being thrown!!", exception.getMessage());
+        public void throwException() throws Exception {
+            // wrap the problem in an IO exception
+            IOException io = new IOException("Forced");
+            io.initCause(new MyCustomException("I'm being thrown!!"));
+            throw io;
+        }
+
+        // to unit test that we can set a type to the @ExchangeException that we want this caused by exception
+        // in the exception hieracy
+        public void handleException(@ExchangeException MyCustomException custom) {
+            assertNotNull(custom);
+            assertEquals("I'm being thrown!!", custom.getMessage());
         }
     }
-}
+}
\ No newline at end of file