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 2019/04/30 14:57:41 UTC

[camel] 01/01: [CAMEL-13468]Exception tag is missing when Camel Java DSL is converted into XML using dumpRouteAsXml() operation

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch patch
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 2567e9aacb56bd57134c50ed009daabb82e5b6ef
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Apr 30 16:54:45 2019 +0200

    [CAMEL-13468]Exception tag is missing when Camel Java DSL is converted into XML using dumpRouteAsXml() operation
---
 .../apache/camel/model/OnExceptionDefinition.java  | 29 ++++----------
 .../camel/processor/ErrorHandlerSupport.java       | 42 ++++++++++++++------
 .../management/ManagedRouteDumpRouteAsXmlTest.java |  3 ++
 .../camel/processor/ErrorHandlerSupportTest.java   | 45 ++++++++++++++--------
 .../onexception/OnExceptionMisconfiguredTest.java  |  8 ++--
 5 files changed, 73 insertions(+), 54 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java b/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
index db80e60..8f9bd92 100644
--- a/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
@@ -21,6 +21,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
@@ -81,8 +82,6 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
     @XmlElementRef
     private List<ProcessorDefinition<?>> outputs = new ArrayList<>();
     @XmlTransient
-    private List<Class<? extends Throwable>> exceptionClasses;
-    @XmlTransient
     private Predicate handledPolicy;
     @XmlTransient
     private Predicate continuedPolicy;
@@ -104,12 +103,11 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
     }
 
     public OnExceptionDefinition(List<Class<? extends Throwable>> exceptionClasses) {
-        this.exceptionClasses = exceptionClasses;
+        this.exceptions.addAll(exceptionClasses.stream().map(Class::getName).collect(Collectors.toList()));
     }
 
     public OnExceptionDefinition(Class<? extends Throwable> exceptionType) {
-        exceptionClasses = new ArrayList<>();
-        exceptionClasses.add(exceptionType);
+        this.exceptions.add(exceptionType.getName());
     }
 
     public void setRouteScoped(boolean routeScoped) {
@@ -127,7 +125,7 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
     }
     
     protected String description() {
-        return getExceptionClasses() + (onWhen != null ? " " + onWhen : "");
+        return getExceptions() + (onWhen != null ? " " + onWhen : "");
     }
 
     @Override
@@ -193,11 +191,6 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
         setOnRedeliveryFromRedeliveryRef(routeContext);
         setOnExceptionOccurredFromOnExceptionOccurredRef(routeContext);
 
-        // load exception classes
-        if (exceptions != null && !exceptions.isEmpty()) {
-            exceptionClasses = createExceptionClasses(routeContext.getCamelContext().getClassResolver());
-        }
-
         // must validate configuration before creating processor
         validateConfiguration();
 
@@ -223,6 +216,7 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
     @Override
     public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
         // load exception classes
+        List<Class<? extends Throwable>> exceptionClasses = null;
         if (exceptions != null && !exceptions.isEmpty()) {
             exceptionClasses = createExceptionClasses(routeContext.getCamelContext().getClassResolver());
         }
@@ -247,7 +241,7 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
             handle = handled.createPredicate(routeContext);
         }
 
-        return new CatchProcessor(getExceptionClasses(), childProcessor, when, handle);
+        return new CatchProcessor(exceptionClasses, childProcessor, when, handle);
     }
 
     protected void validateConfiguration() {
@@ -255,7 +249,6 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
             throw new IllegalArgumentException(this + " cannot have the inheritErrorHandler option set to true");
         }
 
-        List<Class<? extends Throwable>> exceptions = getExceptionClasses();
         if (exceptions == null || exceptions.isEmpty()) {
             throw new IllegalArgumentException("At least one exception must be configured on " + this);
         }
@@ -287,7 +280,7 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
 
     @Override
     public OnExceptionDefinition onException(Class<? extends Throwable> exceptionType) {
-        getExceptionClasses().add(exceptionType);
+        getExceptions().add(exceptionType.getName());
         return this;
     }
 
@@ -868,14 +861,6 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
         return true;
     }
 
-    public List<Class<? extends Throwable>> getExceptionClasses() {
-        return exceptionClasses;
-    }
-
-    public void setExceptionClasses(List<Class<? extends Throwable>> exceptionClasses) {
-        this.exceptionClasses = exceptionClasses;
-    }
-
     public List<String> getExceptions() {
         return exceptions;
     }
diff --git a/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java b/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
index 90ba3c1..27bfe3f 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/ErrorHandlerSupport.java
@@ -16,18 +16,21 @@
  */
 package org.apache.camel.processor;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.model.ProcessorDefinitionHelper;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy;
 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey;
 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
+import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.support.ChildServiceSupport;
 import org.slf4j.Logger;
@@ -52,23 +55,40 @@ public abstract class ErrorHandlerSupport extends ChildServiceSupport implements
             if (errorHandler != null) {
                 addChildService(errorHandler);
             }
-        }
 
-        List<Class<? extends Throwable>> list = exceptionType.getExceptionClasses();
-        for (Class<? extends Throwable> clazz : list) {
-            String routeId = null;
-            // only get the route id, if the exception type is route scoped
-            if (exceptionType.isRouteScoped()) {
-                RouteDefinition route = ProcessorDefinitionHelper.getRoute(exceptionType);
-                if (route != null) {
-                    routeId = route.getId();
+            List<Class<? extends Throwable>> list = null;
+            if (exceptionType.getExceptions() != null && !exceptionType.getExceptions().isEmpty()) {
+                list = createExceptionClasses(exceptionType, routeContext.getCamelContext().getClassResolver());
+                for (Class<? extends Throwable> clazz : list) {
+                    String routeId = null;
+                    // only get the route id, if the exception type is route scoped
+                    if (exceptionType.isRouteScoped()) {
+                        RouteDefinition route = ProcessorDefinitionHelper.getRoute(exceptionType);
+                        if (route != null) {
+                            routeId = route.getId();
+                        }
+                    }
+                    ExceptionPolicyKey key = new ExceptionPolicyKey(routeId, clazz, exceptionType.getOnWhen());
+                    exceptionPolicies.put(key, exceptionType);
                 }
             }
-            ExceptionPolicyKey key = new ExceptionPolicyKey(routeId, clazz, exceptionType.getOnWhen());
-            exceptionPolicies.put(key, exceptionType);
         }
     }
 
+    protected List<Class<? extends Throwable>> createExceptionClasses(OnExceptionDefinition exceptionType, ClassResolver resolver) {
+        List<String> list = exceptionType.getExceptions();
+        List<Class<? extends Throwable>> answer = new ArrayList<>(list.size());
+        for (String name : list) {
+            try {
+                Class<? extends Throwable> type = resolver.resolveMandatoryClass(name, Throwable.class);
+                answer.add(type);
+            } catch (ClassNotFoundException e) {
+                throw new RuntimeCamelException(e);
+            }
+        }
+        return answer;
+    }
+
     /**
      * Attempts to find the best suited {@link OnExceptionDefinition} to be used for handling the given thrown exception.
      *
diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteDumpRouteAsXmlTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteDumpRouteAsXmlTest.java
index 1d83150..79fdb0c 100644
--- a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteDumpRouteAsXmlTest.java
+++ b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteDumpRouteAsXmlTest.java
@@ -58,6 +58,7 @@ public class ManagedRouteDumpRouteAsXmlTest extends ManagementTestSupport {
         assertTrue(xml.contains("route"));
         assertTrue(xml.contains("myRoute"));
         assertTrue(xml.contains("mock:result"));
+        assertTrue(xml.contains("java.lang.Exception"));
     }
 
     @Test
@@ -91,6 +92,8 @@ public class ManagedRouteDumpRouteAsXmlTest extends ManagementTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
+                onException(Exception.class).log("${exception.stacktrace}").logStackTrace(true).handled(true);
+
                 from("direct:start").routeId("myRoute")
                     .log("Got ${body}")
                     .to("mock:result");
diff --git a/camel-core/src/test/java/org/apache/camel/processor/ErrorHandlerSupportTest.java b/camel-core/src/test/java/org/apache/camel/processor/ErrorHandlerSupportTest.java
index 5f79c84..3bf708f 100644
--- a/camel-core/src/test/java/org/apache/camel/processor/ErrorHandlerSupportTest.java
+++ b/camel-core/src/test/java/org/apache/camel/processor/ErrorHandlerSupportTest.java
@@ -19,13 +19,15 @@ package org.apache.camel.processor;
 import java.util.ArrayList;
 import java.util.List;
 
+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.model.OnExceptionDefinition;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class ErrorHandlerSupportTest extends Assert {
+public class ErrorHandlerSupportTest extends ContextTestSupport {
 
     @Test
     public void testOnePolicyChildFirst() {
@@ -34,10 +36,10 @@ public class ErrorHandlerSupportTest extends Assert {
         exceptions.add(ParentException.class);
 
         ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
-        support.addExceptionPolicy(null, new OnExceptionDefinition(exceptions));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(exceptions));
 
-        assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 0));
-        assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 1));
+        assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
+        assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 1));
     }
 
     @Test
@@ -47,35 +49,35 @@ public class ErrorHandlerSupportTest extends Assert {
         exceptions.add(ChildException.class);
 
         ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
-        support.addExceptionPolicy(null, new OnExceptionDefinition(exceptions));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(exceptions));
 
-        assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 1));
-        assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 0));
+        assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 1));
+        assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 0));
     }
 
     @Test
     public void testTwoPolicyChildFirst() {
         ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
-        support.addExceptionPolicy(null, new OnExceptionDefinition(ChildException.class));
-        support.addExceptionPolicy(null, new OnExceptionDefinition(ParentException.class));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ChildException.class));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ParentException.class));
 
-        assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 0));
-        assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 0));
+        assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
+        assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 0));
     }
 
     @Test
     public void testTwoPolicyChildLast() {
         ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
-        support.addExceptionPolicy(null, new OnExceptionDefinition(ParentException.class));
-        support.addExceptionPolicy(null, new OnExceptionDefinition(ChildException.class));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ParentException.class));
+        support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ChildException.class));
 
-        assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 0));
-        assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 0));
+        assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
+        assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 0));
     }
 
-    private static Class<? extends Throwable> getExceptionPolicyFor(ErrorHandlerSupport support, Throwable childException,
+    private static String getExceptionPolicyFor(ErrorHandlerSupport support, Throwable childException,
                                                int index) {
-        return support.getExceptionPolicy(null, childException).getExceptionClasses().get(index);
+        return support.getExceptionPolicy(null, childException).getExceptions().get(index);
     }
 
     private static class ParentException extends Exception {
@@ -106,4 +108,13 @@ public class ErrorHandlerSupportTest extends Assert {
         }
     }
 
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").to("mock:foo").routeId("foo");
+            }
+        };
+    }
 }
diff --git a/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java
index 1db9f40..44f21a2 100644
--- a/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java
+++ b/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionMisconfiguredTest.java
@@ -50,7 +50,7 @@ public class OnExceptionMisconfiguredTest extends ContextTestSupport {
             fail("Should have thrown exception");
         } catch (FailedToCreateRouteException e) {
             IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+            assertEquals("OnException[[java.lang.Exception] -> []] is not configured.", iae.getMessage());
         }
     }
 
@@ -69,7 +69,7 @@ public class OnExceptionMisconfiguredTest extends ContextTestSupport {
             fail("Should have thrown exception");
         } catch (FailedToCreateRouteException e) {
             IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+            assertEquals("OnException[[java.lang.Exception] -> []] is not configured.", iae.getMessage());
         }
     }
 
@@ -89,7 +89,7 @@ public class OnExceptionMisconfiguredTest extends ContextTestSupport {
             fail("Should have thrown exception");
         } catch (FailedToCreateRouteException e) {
             IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+            assertEquals("OnException[[java.lang.Exception] -> []] is not configured.", iae.getMessage());
         }
     }
 
@@ -109,7 +109,7 @@ public class OnExceptionMisconfiguredTest extends ContextTestSupport {
             fail("Should have thrown exception");
         } catch (FailedToCreateRouteException e) {
             IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
-            assertEquals("OnException[[class java.lang.Exception] -> []] is not configured.", iae.getMessage());
+            assertEquals("OnException[[java.lang.Exception] -> []] is not configured.", iae.getMessage());
         }
     }