Skip to content

Commit

Permalink
[CAMEL-13468]Exception tag is missing when Camel Java DSL is converte…
Browse files Browse the repository at this point in the history
…d into XML using dumpRouteAsXml() operation
  • Loading branch information
davsclaus committed Apr 30, 2019
1 parent b5fddaa commit 7385c77
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 60 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;


import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
Expand Down Expand Up @@ -70,8 +71,6 @@ public class OnExceptionDefinition extends ProcessorDefinition<OnExceptionDefini
@XmlElementRef @XmlElementRef
private List<ProcessorDefinition<?>> outputs = new ArrayList<>(); private List<ProcessorDefinition<?>> outputs = new ArrayList<>();
@XmlTransient @XmlTransient
private List<Class<? extends Throwable>> exceptionClasses;
@XmlTransient
private Predicate handledPolicy; private Predicate handledPolicy;
@XmlTransient @XmlTransient
private Predicate continuedPolicy; private Predicate continuedPolicy;
Expand All @@ -91,12 +90,11 @@ public OnExceptionDefinition() {
} }


public OnExceptionDefinition(List<Class<? extends Throwable>> exceptionClasses) { 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) { public OnExceptionDefinition(Class<? extends Throwable> exceptionType) {
exceptionClasses = new ArrayList<>(); this.exceptions.add(exceptionType.getName());
exceptionClasses.add(exceptionType);
} }


public void setRouteScoped(boolean routeScoped) { public void setRouteScoped(boolean routeScoped) {
Expand All @@ -118,7 +116,7 @@ public String toString() {
} }


protected String description() { protected String description() {
return getExceptionClasses() + (onWhen != null ? " " + onWhen : ""); return getExceptions() + (onWhen != null ? " " + onWhen : "");
} }


@Override @Override
Expand Down Expand Up @@ -146,7 +144,6 @@ public void validateConfiguration() {
throw new IllegalArgumentException(this + " cannot have the inheritErrorHandler option set to true"); throw new IllegalArgumentException(this + " cannot have the inheritErrorHandler option set to true");
} }


List<Class<? extends Throwable>> exceptions = getExceptionClasses();
if (exceptions == null || exceptions.isEmpty()) { if (exceptions == null || exceptions.isEmpty()) {
throw new IllegalArgumentException("At least one exception must be configured on " + this); throw new IllegalArgumentException("At least one exception must be configured on " + this);
} }
Expand Down Expand Up @@ -177,7 +174,6 @@ public void validateConfiguration() {


@Override @Override
public OnExceptionDefinition onException(Class<? extends Throwable> exceptionType) { public OnExceptionDefinition onException(Class<? extends Throwable> exceptionType) {
getExceptionClasses().add(exceptionType);
getExceptions().add(exceptionType.getName()); getExceptions().add(exceptionType.getName());
return this; return this;
} }
Expand Down Expand Up @@ -725,14 +721,6 @@ public boolean isOutputSupported() {
return true; 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() { public List<String> getExceptions() {
return exceptions; return exceptions;
} }
Expand Down
Expand Up @@ -249,7 +249,6 @@ public InterceptSendToEndpointDefinition interceptSendToEndpoint(@AsEndpointUri
public OnExceptionDefinition onException(Class<? extends Throwable> exception) { public OnExceptionDefinition onException(Class<? extends Throwable> exception) {
OnExceptionDefinition answer = new OnExceptionDefinition(exception); OnExceptionDefinition answer = new OnExceptionDefinition(exception);
answer.setRouteScoped(false); answer.setRouteScoped(false);
answer.getExceptions().add(exception.getName());
getOnExceptions().add(answer); getOnExceptions().add(answer);
return answer; return answer;
} }
Expand Down
Expand Up @@ -16,18 +16,21 @@
*/ */
package org.apache.camel.processor; package org.apache.camel.processor;


import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;


import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.Processor; import org.apache.camel.Processor;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.model.OnExceptionDefinition; import org.apache.camel.model.OnExceptionDefinition;
import org.apache.camel.model.ProcessorDefinitionHelper; import org.apache.camel.model.ProcessorDefinitionHelper;
import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.RouteDefinition;
import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy; import org.apache.camel.processor.exceptionpolicy.DefaultExceptionPolicyStrategy;
import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey; import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyKey;
import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy; import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
import org.apache.camel.spi.ClassResolver;
import org.apache.camel.spi.RouteContext; import org.apache.camel.spi.RouteContext;
import org.apache.camel.support.ChildServiceSupport; import org.apache.camel.support.ChildServiceSupport;


Expand All @@ -46,23 +49,41 @@ public void addExceptionPolicy(RouteContext routeContext, OnExceptionDefinition
if (errorHandler != null) { if (errorHandler != null) {
addChildService(errorHandler); addChildService(errorHandler);
} }
}


List<Class<? extends Throwable>> list = exceptionType.getExceptionClasses(); // load exception classes
for (Class<? extends Throwable> clazz : list) { List<Class<? extends Throwable>> list = null;
String routeId = null; if (exceptionType.getExceptions() != null && !exceptionType.getExceptions().isEmpty()) {
// only get the route id, if the exception type is route scoped list = createExceptionClasses(exceptionType, routeContext.getCamelContext().getClassResolver());
if (exceptionType.isRouteScoped()) { for (Class<? extends Throwable> clazz : list) {
RouteDefinition route = ProcessorDefinitionHelper.getRoute(exceptionType); String routeId = null;
if (route != null) { // only get the route id, if the exception type is route scoped
routeId = route.getId(); 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 RuntimeCamelException.wrapRuntimeCamelException(e);
}
}
return answer;
}

/** /**
* Attempts to find the best suited {@link OnExceptionDefinition} to be used for handling the given thrown exception. * Attempts to find the best suited {@link OnExceptionDefinition} to be used for handling the given thrown exception.
* *
Expand Down
Expand Up @@ -17,12 +17,10 @@
package org.apache.camel.reifier; package org.apache.camel.reifier;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;


import org.apache.camel.Predicate; import org.apache.camel.Predicate;
import org.apache.camel.Processor; import org.apache.camel.Processor;
import org.apache.camel.Route;
import org.apache.camel.builder.ErrorHandlerBuilder; import org.apache.camel.builder.ErrorHandlerBuilder;
import org.apache.camel.model.OnExceptionDefinition; import org.apache.camel.model.OnExceptionDefinition;
import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.ProcessorDefinition;
Expand Down Expand Up @@ -57,11 +55,6 @@ public void addRoutes(RouteContext routeContext) throws Exception {
setOnRedeliveryFromRedeliveryRef(routeContext); setOnRedeliveryFromRedeliveryRef(routeContext);
setOnExceptionOccurredFromOnExceptionOccurredRef(routeContext); setOnExceptionOccurredFromOnExceptionOccurredRef(routeContext);


// load exception classes
if (definition.getExceptions() != null && !definition.getExceptions().isEmpty()) {
definition.setExceptionClasses(createExceptionClasses(routeContext.getCamelContext().getClassResolver()));
}

// must validate configuration before creating processor // must validate configuration before creating processor
definition.validateConfiguration(); definition.validateConfiguration();


Expand All @@ -87,8 +80,9 @@ public void addRoutes(RouteContext routeContext) throws Exception {
@Override @Override
public CatchProcessor createProcessor(RouteContext routeContext) throws Exception { public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
// load exception classes // load exception classes
List<Class<? extends Throwable>> classes = null;
if (definition.getExceptions() != null && !definition.getExceptions().isEmpty()) { if (definition.getExceptions() != null && !definition.getExceptions().isEmpty()) {
definition.setExceptionClasses(createExceptionClasses(routeContext.getCamelContext().getClassResolver())); classes = createExceptionClasses(routeContext.getCamelContext().getClassResolver());
} }


if (definition.getUseOriginalMessagePolicy() != null && definition.getUseOriginalMessagePolicy()) { if (definition.getUseOriginalMessagePolicy() != null && definition.getUseOriginalMessagePolicy()) {
Expand All @@ -111,7 +105,7 @@ public CatchProcessor createProcessor(RouteContext routeContext) throws Exceptio
handle = definition.getHandled().createPredicate(routeContext); handle = definition.getHandled().createPredicate(routeContext);
} }


return new CatchProcessor(definition.getExceptionClasses(), childProcessor, when, handle); return new CatchProcessor(classes, childProcessor, when, handle);
} }


protected List<Class<? extends Throwable>> createExceptionClasses(ClassResolver resolver) throws ClassNotFoundException { protected List<Class<? extends Throwable>> createExceptionClasses(ClassResolver resolver) throws ClassNotFoundException {
Expand Down
Expand Up @@ -19,13 +19,14 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;


import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange; import org.apache.camel.Exchange;
import org.apache.camel.Processor; import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.OnExceptionDefinition; import org.apache.camel.model.OnExceptionDefinition;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;


public class ErrorHandlerSupportTest extends Assert { public class ErrorHandlerSupportTest extends ContextTestSupport {


@Test @Test
public void testOnePolicyChildFirst() { public void testOnePolicyChildFirst() {
Expand All @@ -34,10 +35,10 @@ public void testOnePolicyChildFirst() {
exceptions.add(ParentException.class); exceptions.add(ParentException.class);


ErrorHandlerSupport support = new ShuntErrorHandlerSupport(); 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(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 1)); assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 1));
} }


@Test @Test
Expand All @@ -47,35 +48,35 @@ public void testOnePolicyChildLast() {
exceptions.add(ChildException.class); exceptions.add(ChildException.class);


ErrorHandlerSupport support = new ShuntErrorHandlerSupport(); 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(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 1));
assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 0)); assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 0));
} }


@Test @Test
public void testTwoPolicyChildFirst() { public void testTwoPolicyChildFirst() {
ErrorHandlerSupport support = new ShuntErrorHandlerSupport(); ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
support.addExceptionPolicy(null, new OnExceptionDefinition(ChildException.class)); support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ChildException.class));
support.addExceptionPolicy(null, new OnExceptionDefinition(ParentException.class)); support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ParentException.class));


assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 0)); assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 0)); assertEquals(ParentException.class.getName(), getExceptionPolicyFor(support, new ParentException(), 0));
} }


@Test @Test
public void testTwoPolicyChildLast() { public void testTwoPolicyChildLast() {
ErrorHandlerSupport support = new ShuntErrorHandlerSupport(); ErrorHandlerSupport support = new ShuntErrorHandlerSupport();
support.addExceptionPolicy(null, new OnExceptionDefinition(ParentException.class)); support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ParentException.class));
support.addExceptionPolicy(null, new OnExceptionDefinition(ChildException.class)); support.addExceptionPolicy(context.getRoute("foo").getRouteContext(), new OnExceptionDefinition(ChildException.class));


assertEquals(ChildException.class, getExceptionPolicyFor(support, new ChildException(), 0)); assertEquals(ChildException.class.getName(), getExceptionPolicyFor(support, new ChildException(), 0));
assertEquals(ParentException.class, getExceptionPolicyFor(support, new ParentException(), 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) { int index) {
return support.getExceptionPolicy(null, childException).getExceptionClasses().get(index); return support.getExceptionPolicy(null, childException).getExceptions().get(index);
} }


private static class ParentException extends Exception { private static class ParentException extends Exception {
Expand Down Expand Up @@ -106,4 +107,13 @@ public void process(Exchange exchange) throws Exception {
} }
} }


@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:foo").to("mock:foo").routeId("foo");
}
};
}
} }
Expand Up @@ -50,7 +50,7 @@ public void configure() throws Exception {
fail("Should have thrown exception"); fail("Should have thrown exception");
} catch (FailedToCreateRouteException e) { } catch (FailedToCreateRouteException e) {
IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); 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());
} }
} }


Expand All @@ -69,7 +69,7 @@ public void configure() throws Exception {
fail("Should have thrown exception"); fail("Should have thrown exception");
} catch (FailedToCreateRouteException e) { } catch (FailedToCreateRouteException e) {
IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); 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());
} }
} }


Expand All @@ -89,7 +89,7 @@ public void configure() throws Exception {
fail("Should have thrown exception"); fail("Should have thrown exception");
} catch (FailedToCreateRouteException e) { } catch (FailedToCreateRouteException e) {
IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); 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());
} }
} }


Expand All @@ -109,7 +109,7 @@ public void configure() throws Exception {
fail("Should have thrown exception"); fail("Should have thrown exception");
} catch (FailedToCreateRouteException e) { } catch (FailedToCreateRouteException e) {
IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); 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());
} }
} }


Expand Down
Expand Up @@ -144,7 +144,8 @@ public void configure() throws Exception {
Endpoint bar = context.getEndpoint("mock:bar"); Endpoint bar = context.getEndpoint("mock:bar");
bindToRegistry("bar", bar); bindToRegistry("bar", bar);


onException(Exception.class).log("${exception.stacktrace}").logStackTrace(true).handled(true); onException(Exception.class)
.log("${exception.stacktrace}").logStackTrace(true).handled(true);


from("direct:start").routeId("myRoute") from("direct:start").routeId("myRoute")
.log("Got ${body}") .log("Got ${body}")
Expand Down

0 comments on commit 7385c77

Please sign in to comment.