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 2008/11/23 12:20:47 UTC

svn commit: r719978 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/model/ main/java/org/apache/camel/processor/ main/java/org/apache/camel/processor/exceptionpolicy/ test/java/org/apache/camel/processor/exceptionpolicy/

Author: davsclaus
Date: Sun Nov 23 03:20:43 2008
New Revision: 719978

URL: http://svn.apache.org/viewvc?rev=719978&view=rev
Log:
CAMEL-1104: onException now supports an optional when expression to only trigger if the expression evaluates to true

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java   (contents, props changed)
      - copied, changed from r719868, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyStrategy.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceType.java Sun Nov 23 03:20:43 2008
@@ -88,7 +88,6 @@
         return this;
     }
 
-
     public ExpressionClause<ChoiceType> when() {
         WhenType when = new WhenType();
         getWhenClauses().add(when);
@@ -97,7 +96,6 @@
         return clause;
     }
 
-
     public ChoiceType otherwise() {
         OtherwiseType answer = new OtherwiseType();
         setOtherwise(answer);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ExceptionType.java Sun Nov 23 03:20:43 2008
@@ -33,6 +33,7 @@
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
 import org.apache.camel.builder.ErrorHandlerBuilder;
+import org.apache.camel.builder.ExpressionClause;
 import org.apache.camel.language.constant.ConstantLanguage;
 import org.apache.camel.processor.CatchProcessor;
 import org.apache.camel.processor.RedeliveryPolicy;
@@ -52,6 +53,8 @@
 
     @XmlElement(name = "exception")
     private List<String> exceptions = new ArrayList<String>();
+    @XmlElement(name = "when", required = false)
+    private WhenType when;
     @XmlElement(name = "redeliveryPolicy", required = false)
     private RedeliveryPolicyType redeliveryPolicy;
     @XmlElement(name = "handled", required = false)
@@ -79,7 +82,7 @@
 
     @Override
     public String toString() {
-        return "Exception[" + getExceptionClasses() + " -> " + getOutputs() + "]";
+        return "Exception[" + getExceptionClasses() + (when != null ? " " + when : "") + " -> " + getOutputs() + "]";
     }
     
     /**
@@ -142,6 +145,18 @@
         return this;
     }
 
+    public ExceptionType when(Predicate predicate) {
+        setWhen(new WhenType(predicate));
+        return this;
+    }
+
+    public ExpressionClause<ExceptionType> when() {
+        when = new WhenType();
+        ExpressionClause<ExceptionType> clause = new ExpressionClause<ExceptionType>(this);
+        when.setExpression(clause);
+        return clause;
+    }
+
     public ExceptionType backOffMultiplier(double backOffMultiplier) {
         getOrCreateRedeliveryPolicy().backOffMultiplier(backOffMultiplier);
         return this;
@@ -255,6 +270,14 @@
         this.handledPolicy = handledPolicy;
     }
 
+    public WhenType getWhen() {
+        return when;
+    }
+
+    public void setWhen(WhenType when) {
+        this.when = when;
+    }
+
     // Implementation methods
     //-------------------------------------------------------------------------
     protected RedeliveryPolicyType getOrCreateRedeliveryPolicy() {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java Sun Nov 23 03:20:43 2008
@@ -63,7 +63,6 @@
         // default behavior which can be overloaded on a per exception basis
         RedeliveryPolicy currentRedeliveryPolicy = redeliveryPolicy;
         Processor failureProcessor = deadLetter;
-        
     }
 
     public DeadLetterChannel(Processor output, Processor deadLetter) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java Sun Nov 23 03:20:43 2008
@@ -25,6 +25,7 @@
 import org.apache.camel.impl.ServiceSupport;
 import org.apache.camel.model.ExceptionType;
 import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy;
+import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey;
 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
 
 /**
@@ -33,17 +34,18 @@
  * @version $Revision$
  */
 public abstract class ErrorHandlerSupport extends ServiceSupport implements ErrorHandler {
-    private Map<Class, ExceptionType> exceptionPolicies = new LinkedHashMap<Class, ExceptionType>();
+    private Map<ExceptionPolicyKey, ExceptionType> exceptionPolicies = new LinkedHashMap<ExceptionPolicyKey, ExceptionType>();
     private ExceptionPolicyStrategy exceptionPolicy = createDefaultExceptionPolicyStrategy();
 
-    public void addExceptionPolicy(ExceptionType exception) {
-        Processor processor = exception.getErrorHandler();
+    public void addExceptionPolicy(ExceptionType exceptionType) {
+        Processor processor = exceptionType.getErrorHandler();
         addChildService(processor);
 
-        List<Class> list = exception.getExceptionClasses();
+        List<Class> list = exceptionType.getExceptionClasses();
 
-        for (Class exceptionType : list) {
-            exceptionPolicies.put(exceptionType, exception);
+        for (Class clazz : list) {
+            ExceptionPolicyKey key = new ExceptionPolicyKey(clazz, exceptionType.getWhen());
+            exceptionPolicies.put(key, exceptionType);
         }
     }
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategy.java Sun Nov 23 03:20:43 2008
@@ -28,19 +28,27 @@
  * The default strategy used in Camel to resolve the {@link org.apache.camel.model.ExceptionType} that should
  * handle the thrown exception.
  * <p/>
- * This strategy applies the following rules:
+ * <b>Selection strategy:</b>
+ * <br/>This strategy applies the following rules:
  * <ul>
- *   <li>The exception type must be configured with an Exception that is an instance of the thrown exception</li>
- *   <li>If the exception type has exactly the thrown exception then its selected</li>
- *   <li>Otherwise the type that has an exception that is super of the thrown exception is selected
- *       (recurring up the exception hierarchy)
- *  </ul>
+ *   <li>The exception type must be configured with an Exception that is an instance of the thrown exception, this
+ *  is tested using the {@link #filter(org.apache.camel.model.ExceptionType, Class, Throwable)} method. </li>
+ *   <li>If the exception type has exactly the thrown exception then its selected as its an exact match</li>
+ *   <li>Otherwise the type that has an exception that is the closests super of the thrown exception is selected
+ *       (recurring up the exception hierarchy)</li>
+ * </ul>
+ * <p/>
+ * <b>Fine grained matching:</b>
+ * <br/> If the {@link ExceptionType} has a when defined with an expression the type is also matches against
+ * the current exchange using the {@link #matchesWhen(org.apache.camel.model.ExceptionType, org.apache.camel.Exchange)}
+ * method. This can be used to for more fine grained matching, so you can e.g. define multiple sets of
+ * exception types with the same exception class(es) but have a predicate attached to select which to select at runtime.
  */
 public class DefaultExceptionPolicyStrategy implements ExceptionPolicyStrategy {
 
     private static final transient Log LOG = LogFactory.getLog(DefaultExceptionPolicyStrategy.class);
 
-    public ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices, Exchange exchange,
+    public ExceptionType getExceptionPolicy(Map<ExceptionPolicyKey, ExceptionType> exceptionPolicices, Exchange exchange,
                                             Throwable exception) {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Finding best suited exception policy for thrown exception " + exception.getClass().getName());
@@ -54,15 +62,22 @@
         int candidateDiff = Integer.MAX_VALUE;
 
         // loop through all the entries and find the best candidates to use
-        Set<Map.Entry<Class, ExceptionType>> entries = exceptionPolicices.entrySet();
-        for (Map.Entry<Class, ExceptionType> entry : entries) {
-            Class clazz = entry.getKey();
+        Set<Map.Entry<ExceptionPolicyKey, ExceptionType>> entries = exceptionPolicices.entrySet();
+        for (Map.Entry<ExceptionPolicyKey, ExceptionType> entry : entries) {
+            Class clazz = entry.getKey().getExceptionClass();
             ExceptionType type = entry.getValue();
 
-            // must be instance of check to ensure that the clazz is one type of the thrown exception
-            if (clazz.isInstance(exception)) {
+            if (filter(type, clazz, exception)) {
+
+                // must match
+                if (!matchesWhen(type, exchange)) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("The type did not match when: " + type);
+                    }
+                    continue;
+                }
 
-                // exact match
+                // exact match then break
                 if (clazz.equals(exception.getClass())) {
                     candidate = type;
                     break;
@@ -91,6 +106,40 @@
         return candidate;
     }
 
+    /**
+     * Strategy to filter the given type exception class with the thrown exception
+     *
+     * @param type  the exception type
+     * @param exceptionClass  the current exception class for testing
+     * @param exception  the thrown exception
+     * @return <tt>true</tt> if the to current exception class is a candidate, <tt>false</tt> to skip it. 
+     */
+    protected boolean filter(ExceptionType type, Class exceptionClass, Throwable exception) {
+        // must be instance of check to ensure that the exceptionClass is one type of the thrown exception
+        return exceptionClass.isInstance(exception);
+    }
+
+    /**
+     * Strategy method for matching the exception type with the current exchange.
+     * <p/>
+     * This default implementation will match as:
+     * <ul>
+     *   <li>Always true if no when predicate on the exception type
+     *   <li>Otherwise the when predicate is matches against the current exchange
+     * </ul>
+     *
+     * @param type  the exception type
+     * @param exchange  the current {@link Exchange}
+     * @return <tt>true</tt> if matched, <tt>false</tt> otherwise.
+     */
+    protected boolean matchesWhen(ExceptionType type, Exchange exchange) {
+        if (type.getWhen() == null || type.getWhen().getExpression() == null) {
+            // if no predicate then it's always a match
+            return true;
+        }
+        return type.getWhen().getExpression().matches(exchange);
+    }
+
     private static int getInheritanceLevel(Class clazz) {
         if (clazz == null || "java.lang.Object".equals(clazz.getName())) {
             return 0;

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java?rev=719978&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java Sun Nov 23 03:20:43 2008
@@ -0,0 +1,85 @@
+/**
+ * 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.processor.exceptionpolicy;
+
+import org.apache.camel.model.WhenType;
+
+/**
+ * Exception policy key is a compound key for storing:
+ * <b>exception class</b> + <b>when</b> => <b>exception type</b>.
+ * <p/>
+ * This is used by Camel to store the onException types configued that has or has not predicates attached (when).
+ */
+public final class ExceptionPolicyKey {
+
+    private final Class exceptionClass;
+    private final WhenType when;
+
+    public ExceptionPolicyKey(Class exceptionClass, WhenType when) {
+        this.exceptionClass = exceptionClass;
+        this.when = when;
+    }
+
+    public Class getExceptionClass() {
+        return exceptionClass;
+    }
+
+    public WhenType getWhen() {
+        return when;
+    }
+
+    public static ExceptionPolicyKey newInstance(Class exceptionClass) {
+        return new ExceptionPolicyKey(exceptionClass, null);
+    }
+
+    public static ExceptionPolicyKey newInstance(Class exceptionClass, WhenType when) {
+        return new ExceptionPolicyKey(exceptionClass, when);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        ExceptionPolicyKey that = (ExceptionPolicyKey) o;
+
+        if (!exceptionClass.equals(that.exceptionClass)) {
+            return false;
+        }
+        if (when != null ? !when.equals(that.when) : that.when != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = exceptionClass.hashCode();
+        result = 31 * result + (when != null ? when.hashCode() : 0);
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "ExceptionPolicyKey[" + exceptionClass + (when != null ? " " + when : "") + "]";
+    }
+}

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

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyKey.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyStrategy.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyStrategy.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/exceptionpolicy/ExceptionPolicyStrategy.java Sun Nov 23 03:20:43 2008
@@ -37,7 +37,7 @@
      * @param exception          the exception that was thrown
      * @return the resolved exception type to handle this exception, <tt>null</tt> if none found.
      */
-    ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices, Exchange exchange,
+    ExceptionType getExceptionPolicy(Map<ExceptionPolicyKey, ExceptionType> exceptionPolicices, Exchange exchange,
                                             Throwable exception);
 
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java Sun Nov 23 03:20:43 2008
@@ -41,12 +41,12 @@
     // START SNIPPET e2
     public static class MyPolicy implements ExceptionPolicyStrategy {
 
-        public ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices,
+        public ExceptionType getExceptionPolicy(Map<ExceptionPolicyKey, ExceptionType> exceptionPolicices,
                                                 Exchange exchange,
                                                 Throwable exception) {
             // This is just an example that always forces the exception type configured
             // with MyPolicyException to win.
-            return exceptionPolicices.get(MyPolicyException.class);
+            return exceptionPolicices.get(ExceptionPolicyKey.newInstance(MyPolicyException.class));
         }
     }
     // END SNIPPET e2

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java?rev=719978&r1=719977&r2=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyTest.java Sun Nov 23 03:20:43 2008
@@ -36,30 +36,30 @@
 public class DefaultExceptionPolicyStrategyTest extends TestCase {
 
     private DefaultExceptionPolicyStrategy strategy;
-    private HashMap<Class, ExceptionType> policies;
+    private HashMap<ExceptionPolicyKey, ExceptionType> policies;
     private ExceptionType type1;
     private ExceptionType type2;
     private ExceptionType type3;
 
     private void setupPolicies() {
         strategy = new DefaultExceptionPolicyStrategy();
-        policies = new HashMap<Class, ExceptionType>();
+        policies = new HashMap<ExceptionPolicyKey, ExceptionType>();
         type1 = new ExceptionType(CamelExchangeException.class);
         type2 = new ExceptionType(Exception.class);
         type3 = new ExceptionType(IOException.class);
-        policies.put(CamelExchangeException.class, type1);
-        policies.put(Exception.class, type2);
-        policies.put(IOException.class, type3);
+        policies.put(ExceptionPolicyKey.newInstance(CamelExchangeException.class), type1);
+        policies.put(ExceptionPolicyKey.newInstance(Exception.class), type2);
+        policies.put(ExceptionPolicyKey.newInstance(IOException.class), type3);
     }
 
     private void setupPoliciesNoTopLevelException() {
         // without the top level exception that can be used as fallback
         strategy = new DefaultExceptionPolicyStrategy();
-        policies = new HashMap<Class, ExceptionType>();
+        policies = new HashMap<ExceptionPolicyKey, ExceptionType>();
         type1 = new ExceptionType(CamelExchangeException.class);
         type3 = new ExceptionType(IOException.class);
-        policies.put(CamelExchangeException.class, type1);
-        policies.put(IOException.class, type3);
+        policies.put(ExceptionPolicyKey.newInstance(CamelExchangeException.class), type1);
+        policies.put(ExceptionPolicyKey.newInstance(IOException.class), type3);
     }
 
     public void testDirectMatch1() {

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java?rev=719978&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java Sun Nov 23 03:20:43 2008
@@ -0,0 +1,90 @@
+/**
+ * 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.processor.exceptionpolicy;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test for the when expression on the exception type.
+ */
+public class DefaultExceptionPolicyStrategyUsingOnlyWhenTest extends ContextTestSupport {
+
+    private static final String ERROR_QUEUE = "mock:error";
+    private static final String ERROR_USER_QUEUE = "mock:usererror";
+
+    public static class MyUserException extends Exception {
+
+        public MyUserException(String message) {
+            super(message);
+        }
+    }
+
+    public void testNoWhen() throws Exception {
+        MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
+        mock.expectedMessageCount(1);
+
+        try {
+            template.sendBody("direct:a", "Hello Camel");
+            fail("Should have thrown an Exception");
+        } catch (Exception e) {
+            // expected
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testWithWhen() throws Exception {
+        MockEndpoint mock = getMockEndpoint(ERROR_USER_QUEUE);
+        mock.expectedMessageCount(1);
+
+        try {
+            template.sendBodyAndHeader("direct:a", "Hello Camel", "user", "admin");
+            fail("Should have thrown an Exception");
+        } catch (Exception e) {
+            // expected
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                errorHandler(deadLetterChannel(ERROR_QUEUE).maximumRedeliveries(0));
+
+                onException(MyUserException.class).when(header("user").isNotNull())
+                    .maximumRedeliveries(1)
+                    .to(ERROR_USER_QUEUE);
+
+                from("direct:a").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        String s = exchange.getIn().getBody(String.class);
+                        if ("Hello Camel".equals(s)) {
+                            throw new MyUserException("Forced for testing");
+                        }
+                        exchange.getOut().setBody("Hello World");
+                    }
+                }).to("mock:result");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingOnlyWhenTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java (from r719868, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java&r1=719868&r2=719978&rev=719978&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/CustomExceptionPolicyStrategyTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java Sun Nov 23 03:20:43 2008
@@ -16,70 +16,72 @@
  */
 package org.apache.camel.processor.exceptionpolicy;
 
-import java.util.Map;
-
-import org.apache.camel.CamelException;
-import org.apache.camel.CamelExchangeException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.model.ExceptionType;
 
 /**
- * Unit test with a user plugged in exception policy to use instead of default.
+ * Unit test for the when expression on the exception type.
  */
-public class CustomExceptionPolicyStrategyTest extends ContextTestSupport {
+public class DefaultExceptionPolicyStrategyUsingWhenTest extends ContextTestSupport {
 
-    private static final String MESSAGE_INFO = "messageInfo";
     private static final String ERROR_QUEUE = "mock:error";
+    private static final String ERROR_USER_QUEUE = "mock:usererror";
 
-    public static class MyPolicyException extends Exception {
-    }
-
-    // START SNIPPET e2
-    public static class MyPolicy implements ExceptionPolicyStrategy {
+    public static class MyUserException extends Exception {
 
-        public ExceptionType getExceptionPolicy(Map<Class, ExceptionType> exceptionPolicices,
-                                                Exchange exchange,
-                                                Throwable exception) {
-            // This is just an example that always forces the exception type configured
-            // with MyPolicyException to win.
-            return exceptionPolicices.get(MyPolicyException.class);
+        public MyUserException(String message) {
+            super(message);
         }
     }
-    // END SNIPPET e2
 
-    public void testCustomPolicy() throws Exception {
+    public void testNoWhen() throws Exception {
         MockEndpoint mock = getMockEndpoint(ERROR_QUEUE);
         mock.expectedMessageCount(1);
-        mock.expectedHeaderReceived(MESSAGE_INFO, "Damm my policy exception");
 
         try {
             template.sendBody("direct:a", "Hello Camel");
+            fail("Should have thrown an Exception");
+        } catch (Exception e) {
+            // expected
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testWithWhen() throws Exception {
+        MockEndpoint mock = getMockEndpoint(ERROR_USER_QUEUE);
+        mock.expectedMessageCount(1);
+
+        try {
+            template.sendBodyAndHeader("direct:a", "Hello Camel", "user", "admin");
+            fail("Should have thrown an Exception");
         } catch (Exception e) {
             // expected
         }
 
-        mock.assertIsSatisfied();
+        assertMockEndpointsSatisfied();
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             // START SNIPPET e1
             public void configure() throws Exception {
-                // configure the error handler to use my policy instead of the default from Camel
-                errorHandler(deadLetterChannel().exceptionPolicyStrategy(new MyPolicy()));
-
-                onException(MyPolicyException.class)
+                // here we define our onException to catch MyUserException when
+                // there is a header[user] on the exchange that is not null
+                onException(MyUserException.class).when(header("user").isNotNull())
                     .maximumRedeliveries(1)
-                    .setHeader(MESSAGE_INFO, constant("Damm my policy exception"))
-                    .to(ERROR_QUEUE);
+                    .to(ERROR_USER_QUEUE);
 
-                onException(CamelException.class)
-                    .maximumRedeliveries(3)
-                    .setHeader(MESSAGE_INFO, constant("Damm a Camel exception"))
+                // here we define onException to catch MyUserException as a kind
+                // of fallback when the above did not match.
+                // Noitce: The order how we have defined these onException is
+                // important as Camel will resolve in the same order as they
+                // have been defined
+                onException(MyUserException.class)
+                    .maximumRedeliveries(2)
                     .to(ERROR_QUEUE);
                 // END SNIPPET e1
 
@@ -87,7 +89,7 @@
                     public void process(Exchange exchange) throws Exception {
                         String s = exchange.getIn().getBody(String.class);
                         if ("Hello Camel".equals(s)) {
-                            throw new CamelExchangeException("Forced for testing", exchange);
+                            throw new MyUserException("Forced for testing");
                         }
                         exchange.getOut().setBody("Hello World");
                     }
@@ -96,4 +98,4 @@
         };
     }
 
-}
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/exceptionpolicy/DefaultExceptionPolicyStrategyUsingWhenTest.java
------------------------------------------------------------------------------
    svn:mergeinfo =