Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D64FC200B52 for ; Mon, 25 Jul 2016 13:58:27 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id D4E96160A7D; Mon, 25 Jul 2016 11:58:27 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id ABD00160A78 for ; Mon, 25 Jul 2016 13:58:26 +0200 (CEST) Received: (qmail 82543 invoked by uid 500); 25 Jul 2016 11:58:25 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 82534 invoked by uid 99); 25 Jul 2016 11:58:25 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Jul 2016 11:58:25 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id C3180E00A7; Mon, 25 Jul 2016 11:58:25 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Mon, 25 Jul 2016 11:58:25 -0000 Message-Id: <1e4a6074e4d745a3afd554650fc7b412@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/4] camel git commit: CAMEL-10180: weaveByToUri - To make it easier to match sending to uris archived-at: Mon, 25 Jul 2016 11:58:28 -0000 Repository: camel Updated Branches: refs/heads/camel-2.17.x 9c529bed3 -> 6aa84254f refs/heads/master 548d7a0df -> 91a7a02db CAMEL-10180: weaveByToUri - To make it easier to match sending to uris Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/91a7a02d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/91a7a02d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/91a7a02d Branch: refs/heads/master Commit: 91a7a02dbe35fec7d8b3adcafe5a313fbe37c150 Parents: 285a9e4 Author: Claus Ibsen Authored: Mon Jul 25 12:29:33 2016 +0200 Committer: Claus Ibsen Committed: Mon Jul 25 13:57:38 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/builder/AdviceWithBuilder.java | 16 +++++- .../camel/builder/AdviceWithRouteBuilder.java | 26 +++++++-- .../apache/camel/builder/AdviceWithTasks.java | 54 ++++++++++++++++++ .../apache/camel/model/ChoiceDefinition.java | 3 +- .../AdviceWithWeaveByToStringCBRTest.java | 60 -------------------- .../issues/AdviceWithWeaveByToUriCBRTest.java | 57 +++++++++++++++++++ 6 files changed, 146 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java index 75d643d..a9eff2f 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java @@ -28,6 +28,7 @@ public class AdviceWithBuilder> { private final AdviceWithRouteBuilder builder; private final String id; private final String toString; + private final String toUri; private final Class type; private boolean selectFirst; private boolean selectLast; @@ -35,14 +36,15 @@ public class AdviceWithBuilder> { private int selectTo = -1; private int maxDeep = -1; - public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString, Class type) { + public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString, String toUri, Class type) { this.builder = builder; this.id = id; this.toString = toString; + this.toUri = toUri; this.type = type; - if (id == null && toString == null && type == null) { - throw new IllegalArgumentException("Either id, toString or type must be specified"); + if (id == null && toString == null && toUri == null && type == null) { + throw new IllegalArgumentException("Either id, toString, toUri or type must be specified"); } } @@ -133,6 +135,8 @@ public class AdviceWithBuilder> { builder.getAdviceWithTasks().add(AdviceWithTasks.replaceById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (toString != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); + } else if (toUri != null) { + builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToUri(route, toUri, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (type != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } @@ -148,6 +152,8 @@ public class AdviceWithBuilder> { builder.getAdviceWithTasks().add(AdviceWithTasks.removeById(route, id, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (toString != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToString(route, toString, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); + } else if (toUri != null) { + builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToUri(route, toUri, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (type != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.removeByType(route, type, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } @@ -165,6 +171,8 @@ public class AdviceWithBuilder> { builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (toString != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); + } else if (toUri != null) { + builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToUri(route, toUri, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (type != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } @@ -183,6 +191,8 @@ public class AdviceWithBuilder> { builder.getAdviceWithTasks().add(AdviceWithTasks.afterById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (toString != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); + } else if (toUri != null) { + builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToUri(route, toUri, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } else if (type != null) { builder.getAdviceWithTasks().add(AdviceWithTasks.afterByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep)); } http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java index df80247..e26ed56 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java @@ -23,6 +23,7 @@ import org.apache.camel.Endpoint; import org.apache.camel.impl.InterceptSendToMockEndpointStrategy; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.ToDefinition; import org.apache.camel.util.ObjectHelper; /** @@ -134,7 +135,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder { */ public > AdviceWithBuilder weaveById(String pattern) { ObjectHelper.notNull(originalRoute, "originalRoute", this); - return new AdviceWithBuilder(this, pattern, null, null); + return new AdviceWithBuilder(this, pattern, null, null, null); } /** @@ -148,7 +149,22 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder { */ public > AdviceWithBuilder weaveByToString(String pattern) { ObjectHelper.notNull(originalRoute, "originalRoute", this); - return new AdviceWithBuilder(this, null, pattern, null); + return new AdviceWithBuilder(this, null, pattern, null, null); + } + + /** + * Weaves by matching sending to endpoints with the given uri of the nodes in the route. + *

+ * Uses the {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)} matching algorithm. + * + * @param pattern the pattern + * @return the builder + * @see org.apache.camel.util.EndpointHelper#matchPattern(String, String) + */ + @SuppressWarnings("unchecked") + public > AdviceWithBuilder weaveByToUri(String pattern) { + ObjectHelper.notNull(originalRoute, "originalRoute", this); + return new AdviceWithBuilder(this, null, null, pattern, null); } /** @@ -159,7 +175,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder { */ public > AdviceWithBuilder weaveByType(Class type) { ObjectHelper.notNull(originalRoute, "originalRoute", this); - return new AdviceWithBuilder(this, null, null, type); + return new AdviceWithBuilder(this, null, null, null, type); } /** @@ -169,7 +185,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder { */ public > ProcessorDefinition weaveAddFirst() { ObjectHelper.notNull(originalRoute, "originalRoute", this); - return new AdviceWithBuilder(this, "*", null, null).selectFirst().before(); + return new AdviceWithBuilder(this, "*", null, null, null).selectFirst().before(); } /** @@ -179,7 +195,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder { */ public > ProcessorDefinition weaveAddLast() { ObjectHelper.notNull(originalRoute, "originalRoute", this); - return new AdviceWithBuilder(this, "*", null, null).maxDeep(1).selectLast().after(); + return new AdviceWithBuilder(this, "*", null, null, null).maxDeep(1).selectLast().after(); } } http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java index bc1d066..ef1531d 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java +++ b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java @@ -21,7 +21,9 @@ import java.util.Iterator; import java.util.List; import org.apache.camel.Endpoint; +import org.apache.camel.EndpointAware; import org.apache.camel.model.ChoiceDefinition; +import org.apache.camel.model.EndpointRequiredDefinition; import org.apache.camel.model.FromDefinition; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.ProcessorDefinitionHelper; @@ -96,6 +98,30 @@ public final class AdviceWithTasks { } /** + * Will match by the sending to endpoint uri representation of the processor. + */ + private static final class MatchByToUri implements MatchBy { + + private final String toUri; + + private MatchByToUri(String toUri) { + this.toUri = toUri; + } + + public String getId() { + return toUri; + } + + public boolean match(ProcessorDefinition processor) { + if (processor instanceof EndpointRequiredDefinition) { + String uri = ((EndpointRequiredDefinition) processor).getEndpointUri(); + return EndpointHelper.matchPattern(uri, toUri); + } + return false; + } + } + + /** * Will match by the type of the processor. */ private static final class MatchByType implements MatchBy { @@ -122,6 +148,13 @@ public final class AdviceWithTasks { return doReplace(route, new MatchByToString(toString), replace, it); } + public static AdviceWithTask replaceByToUri(final RouteDefinition route, final String toUri, final ProcessorDefinition replace, + boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { + MatchBy matchBy = new MatchByToUri(toUri); + Iterator> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep); + return doReplace(route, new MatchByToUri(toUri), replace, it); + } + public static AdviceWithTask replaceById(final RouteDefinition route, final String id, final ProcessorDefinition replace, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { MatchBy matchBy = new MatchById(id); @@ -171,6 +204,13 @@ public final class AdviceWithTasks { return doRemove(route, matchBy, it); } + public static AdviceWithTask removeByToUri(final RouteDefinition route, final String toUri, + boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { + MatchBy matchBy = new MatchByToUri(toUri); + Iterator> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep); + return doRemove(route, matchBy, it); + } + public static AdviceWithTask removeById(final RouteDefinition route, final String id, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { MatchBy matchBy = new MatchById(id); @@ -219,6 +259,13 @@ public final class AdviceWithTasks { return doBefore(route, matchBy, before, it); } + public static AdviceWithTask beforeByToUri(final RouteDefinition route, final String toUri, final ProcessorDefinition before, + boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { + MatchBy matchBy = new MatchByToUri(toUri); + Iterator> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep); + return doBefore(route, matchBy, before, it); + } + public static AdviceWithTask beforeById(final RouteDefinition route, final String id, final ProcessorDefinition before, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { MatchBy matchBy = new MatchById(id); @@ -268,6 +315,13 @@ public final class AdviceWithTasks { return doAfter(route, matchBy, after, it); } + public static AdviceWithTask afterByToUri(final RouteDefinition route, final String toUri, final ProcessorDefinition after, + boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { + MatchBy matchBy = new MatchByToUri(toUri); + Iterator> it = AdviceWithTasks.createMatchByIterator(route, matchBy, selectFirst, selectLast, selectFrom, selectTo, maxDeep); + return doAfter(route, matchBy, after, it); + } + public static AdviceWithTask afterById(final RouteDefinition route, final String id, final ProcessorDefinition after, boolean selectFirst, boolean selectLast, int selectFrom, int selectTo, int maxDeep) { MatchBy matchBy = new MatchById(id); http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java index 6ec26cd..60370cc 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java @@ -125,8 +125,7 @@ public class ChoiceDefinition extends ProcessorDefinition { @Override public String toString() { - // when and otherwise has detailed output in their toString so keep this toString short - return "Choice"; + return "Choice[" + getWhenClauses() + (getOtherwise() != null ? " " + getOtherwise() : "") + "]"; } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java deleted file mode 100644 index 124b1fc..0000000 --- a/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * 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.issues; - -import org.apache.camel.ContextTestSupport; -import org.apache.camel.builder.AdviceWithRouteBuilder; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.model.RouteDefinition; - -/** - * @version - */ -public class AdviceWithWeaveByToStringCBRTest extends ContextTestSupport { - - public void testAdviceCBR() throws Exception { - RouteDefinition route = context.getRouteDefinitions().get(0); - route.adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - weaveByToString("direct:branch.*").replace().to("mock:foo"); - mockEndpointsAndSkip("direct:branch*"); - } - }); - - getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start") - .choice() - .when(header("foo")).to("direct:branch-1") - .otherwise() - .to("direct:branch-2"); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/91a7a02d/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToUriCBRTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToUriCBRTest.java b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToUriCBRTest.java new file mode 100644 index 0000000..d647b4e --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToUriCBRTest.java @@ -0,0 +1,57 @@ +/** + * 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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.RouteDefinition; + +public class AdviceWithWeaveByToUriCBRTest extends ContextTestSupport { + + public void testAdviceCBR() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + weaveByToUri("direct:branch*").replace().to("mock:foo"); + mockEndpointsAndSkip("direct:branch*"); + } + }); + + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .choice() + .when(header("foo")).to("direct:branch-1") + .otherwise() + .to("direct:branch-2"); + } + }; + } + +}