You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2008/11/21 14:19:07 UTC

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

Author: janstey
Date: Fri Nov 21 05:19:05 2008
New Revision: 719574

URL: http://svn.apache.org/viewvc?rev=719574&view=rev
Log:
CAMEL-985 - add bean annotation to grab Exception set on an exchange

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java?rev=719574&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java Fri Nov 21 05:19:05 2008
@@ -0,0 +1,30 @@
+/**
+ * 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.camel;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks a parameter as being the exception set on an exchange
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.PARAMETER })
+public @interface ExchangeException {
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/ExchangeException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?rev=719574&r1=719573&r2=719574&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Fri Nov 21 05:19:05 2008
@@ -32,6 +32,7 @@
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.language.bean.BeanLanguage;
 import org.apache.camel.language.simple.SimpleLanguage;
+import org.apache.camel.processor.DeadLetterChannel;
 
 /**
  * A helper class for working with <a href="http://activemq.apache.org/camel/expression.html">expressions</a>.
@@ -137,6 +138,29 @@
     }
 
     /**
+     * Returns an expression for an exception set on the exchange
+     *
+     * @see Exchange#getException()
+     * @return an expression object which will return the exception set on the exchange
+     */
+    public static Expression exchangeExceptionExpression() {
+        return new Expression() {
+            public Object evaluate(Exchange exchange) {
+                Throwable exception = exchange.getException();
+                if (exception == null) {
+                    exception = exchange.getProperty(DeadLetterChannel.EXCEPTION_CAUSE_PROPERTY, Throwable.class);
+                }
+                return exception;
+            }
+
+            @Override
+            public String toString() {
+                return "exchangeException";
+            }
+        };
+    }   
+    
+    /**
      * Returns an expression for the property value with the given name
      *
      * @see Exchange#getProperty(String)

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=719574&r1=719573&r2=719574&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Fri Nov 21 05:19:05 2008
@@ -31,6 +31,7 @@
 import org.apache.camel.Body;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
+import org.apache.camel.ExchangeException;
 import org.apache.camel.Expression;
 import org.apache.camel.Header;
 import org.apache.camel.Headers;
@@ -147,7 +148,7 @@
 
         MethodInfo methodInfo = createMethodInfo(clazz, method);
 
-        // methods already registered should be prefered to use instead of super classes of existing methods
+        // methods already registered should be preferred to use instead of super classes of existing methods
         // we want to us the method from the sub class over super classes, so if we have already registered
         // the method then use it (we are traversing upwards: sub (child) -> super (farther) )
         MethodInfo existingMethodInfo = overridesExistingMethod(methodInfo);
@@ -204,7 +205,7 @@
                 }
             }
 
-            // sanme name, same parameters, then its overrides an existing class
+            // same name, same parameters, then its overrides an existing class
             return info;
         }
 
@@ -310,14 +311,14 @@
 
             List<MethodInfo> possibles = new ArrayList<MethodInfo>();
             for (MethodInfo methodInfo : operationList) {
-                // TODO: AOP proxies have additioan methods - consider having a static
+                // TODO: AOP proxies have additional methods - consider having a static
                 // method exclude list to skip all known AOP proxy methods
                 // TODO: This class could use some TRACE logging
 
                 // test for MEP pattern matching
                 boolean out = exchange.getPattern().isOutCapable();
                 if (out && methodInfo.isReturnTypeVoid()) {
-                    // skip this method as the MEP is Out so the method must return someting
+                    // skip this method as the MEP is Out so the method must return something
                     continue;
                 }
 
@@ -388,7 +389,7 @@
     /**
      * Creates an expression for the given parameter type if the parameter can
      * be mapped automatically or null if the parameter cannot be mapped due to
-     * unsufficient annotations or not fitting with the default type
+     * insufficient annotations or not fitting with the default type
      * conventions.
      */
     protected Expression createParameterUnmarshalExpression(Class clazz, Method method, Class parameterType,
@@ -439,6 +440,8 @@
             return ExpressionBuilder.headersExpression();
         } else if (annotation instanceof OutHeaders) {
             return ExpressionBuilder.outHeadersExpression();
+        } else if (annotation instanceof ExchangeException) {
+            return ExpressionBuilder.exchangeExceptionExpression();
         } else {
             LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
             if (languageAnnotation != null) {

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java?rev=719574&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java Fri Nov 21 05:19:05 2008
@@ -0,0 +1,78 @@
+/**
+ * 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.camel.component.bean;
+
+import javax.naming.Context;
+
+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 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());
+        }
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithExchangeExceptionAnnotationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java?rev=719574&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java Fri Nov 21 05:19:05 2008
@@ -0,0 +1,24 @@
+/**
+ * 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.camel.component.bean;
+
+public class MyCustomException extends Exception {
+
+    public MyCustomException(String message) {
+        super(message);
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyCustomException.java
------------------------------------------------------------------------------
    svn:eol-style = native