This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 6d3febd08495bfe85bf5b550e499f8069d55a66f Author: Claus Ibsen <[email protected]> AuthorDate: Wed Aug 4 11:46:46 2021 +0200 CAMEL-16757: Route now stores correctly which route configuration it has been applied with. --- .../org/apache/camel/model/RouteDefinition.java | 7 +++++++ .../org/apache/camel/model/RoutesDefinition.java | 19 +++++++++++++------ .../core/xml/AbstractCamelContextFactoryBean.java | 19 +++++++++++++------ .../RoutesConfigurationBuilderIdOrPatternTest.java | 21 +++++++++++++++++---- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java index 6373a26..d546772 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinition.java @@ -138,6 +138,13 @@ public class RouteDefinition extends OutputDefinition<RouteDefinition> implement prepared.set(false); } + /** + * Reset internal state before preparing route + */ + public void resetPrepare() { + appliedRouteConfigurationIds = null; + } + @Override public String toString() { if (getId() != null) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java index f764bc6..1df5154 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RoutesDefinition.java @@ -218,6 +218,9 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit return; } + // reset before preparing route + route.resetPrepare(); + // merge global and route scoped together List<OnExceptionDefinition> oe = new ArrayList<>(onExceptions); List<InterceptDefinition> icp = new ArrayList<>(intercepts); @@ -231,13 +234,17 @@ public class RoutesDefinition extends OptionalIdentifiedDefinition<RoutesDefinit globalConfigurations.stream() // global configurations have no id assigned or is a wildcard // if the route has a route configuration assigned then use pattern matching - .filter(g -> (g.getId() == null || g.getId().equals("*")) - || (PatternHelper.matchPattern(g.getId(), route.getRouteConfigurationId()))) - .forEach(g -> { - if (g.getId() != null && !g.getId().equals("*")) { - // remember the id that was used on the route - route.addAppliedRouteConfigurationId(g.getId()); + .filter(g -> { + if (route.getRouteConfigurationId() != null) { + return PatternHelper.matchPattern(g.getId(), route.getRouteConfigurationId()); + } else { + return g.getId() == null || g.getId().equals("*"); } + }) + .forEach(g -> { + String id = g.getId() == null ? "<default>" : g.getId(); + // remember the id that was used on the route + route.addAppliedRouteConfigurationId(id); oe.addAll(g.getOnExceptions()); icp.addAll(g.getIntercepts()); ifrom.addAll(g.getInterceptFroms()); diff --git a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java index 7516f2a..ab8857b 100644 --- a/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java +++ b/core/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java @@ -552,6 +552,9 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex // sanity check first as the route is created using XML RouteDefinitionHelper.sanityCheckRoute(route); + // reset before preparing route + route.resetPrepare(); + // merge global and route scoped together List<OnExceptionDefinition> oe = new ArrayList<>(getOnExceptions()); List<InterceptDefinition> icp = new ArrayList<>(getIntercepts()); @@ -565,13 +568,17 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex globalConfigurations.stream() // global configurations have no id assigned or is a wildcard // if the route has a route configuration assigned then use pattern matching - .filter(g -> (g.getId() == null || g.getId().equals("*")) - || (PatternHelper.matchPattern(g.getId(), route.getRouteConfigurationId()))) - .forEach(g -> { - if (g.getId() != null && !g.getId().equals("*")) { - // remember the id that was used on the route - route.addAppliedRouteConfigurationId(g.getId()); + .filter(g -> { + if (route.getRouteConfigurationId() != null) { + return PatternHelper.matchPattern(g.getId(), route.getRouteConfigurationId()); + } else { + return g.getId() == null || g.getId().equals("*"); } + }) + .forEach(g -> { + String id = g.getId() == null ? "<default>" : g.getId(); + // remember the id that was used on the route + route.addAppliedRouteConfigurationId(id); oe.addAll(g.getOnExceptions()); icp.addAll(g.getIntercepts()); ifrom.addAll(g.getInterceptFroms()); diff --git a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java index d79b4d0..809d5cd 100644 --- a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationBuilderIdOrPatternTest.java @@ -30,6 +30,7 @@ import org.apache.camel.support.OrderedComparator; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.fail; public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSupport { @@ -46,14 +47,14 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor routes.add(new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start") + from("direct:start").routeId("foo") .throwException(new IllegalArgumentException("Foo")); } }); routes.add(new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start2") + from("direct:start2").routeId("foo2") .routeConfigurationId("handleError") .throwException(new IllegalArgumentException("Foo2")); } @@ -93,6 +94,9 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); + + assertNull(context.getRoute("foo").getConfigurationId()); + assertEquals("handleError", context.getRoute("foo2").getConfigurationId()); } @Test @@ -102,7 +106,7 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor routes.add(new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start") + from("direct:start").routeId("foo") .routeConfigurationId("general*") .throwException(new IllegalArgumentException("Foo")); } @@ -110,7 +114,7 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor routes.add(new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start2") + from("direct:start2").routeId("foo2") .routeConfigurationId("io*") .throwException(new IOException("Foo2")); } @@ -148,6 +152,9 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); + + assertEquals("generalError", context.getRoute("foo").getConfigurationId()); + assertEquals("ioError", context.getRoute("foo2").getConfigurationId()); } @Test @@ -201,6 +208,9 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); + assertEquals("<default>", context.getRoute("foo").getConfigurationId()); + assertEquals("<default>", context.getRoute("foo2").getConfigurationId()); + context.removeRoute("foo2"); // now re-configure route2 to use ioError route configuration @@ -219,6 +229,9 @@ public class RoutesConfigurationBuilderIdOrPatternTest extends ContextTestSuppor template.sendBody("direct:start", "Hello World"); template.sendBody("direct:start2", "Bye World"); assertMockEndpointsSatisfied(); + + assertEquals("<default>", context.getRoute("foo").getConfigurationId()); + assertEquals("ioError", context.getRoute("foo2").getConfigurationId()); } @Test
