camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/4] camel git commit: CAMEL-10180: weaveByToUri - To make it easier to match sending to uris
Date Mon, 25 Jul 2016 11:58:25 GMT
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 <davsclaus@apache.org>
Authored: Mon Jul 25 12:29:33 2016 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
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<T extends ProcessorDefinition<?>>
{
     private final AdviceWithRouteBuilder builder;
     private final String id;
     private final String toString;
+    private final String toUri;
     private final Class<T> type;
     private boolean selectFirst;
     private boolean selectLast;
@@ -35,14 +36,15 @@ public class AdviceWithBuilder<T extends ProcessorDefinition<?>>
{
     private int selectTo = -1;
     private int maxDeep = -1;
 
-    public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString,
Class<T> type) {
+    public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString,
String toUri, Class<T> 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<T extends ProcessorDefinition<?>>
{
             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<T extends ProcessorDefinition<?>>
{
             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<T extends ProcessorDefinition<?>>
{
             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<T extends ProcessorDefinition<?>>
{
             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 <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveById(String
pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder<T>(this, pattern, null, null);
+        return new AdviceWithBuilder<T>(this, pattern, null, null, null);
     }
 
     /**
@@ -148,7 +149,22 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveByToString(String
pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder<T>(this, null, pattern, null);
+        return new AdviceWithBuilder<T>(this, null, pattern, null, null);
+    }
+
+    /**
+     * Weaves by matching sending to endpoints with the given uri of the nodes in the route.
+     * <p/>
+     * 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 <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveByToUri(String
pattern) {
+        ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        return new AdviceWithBuilder<T>(this, null, null, pattern, null);
     }
 
     /**
@@ -159,7 +175,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> AdviceWithBuilder<T> weaveByType(Class<T>
type) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder<T>(this, null, null, type);
+        return new AdviceWithBuilder<T>(this, null, null, null, type);
     }
 
     /**
@@ -169,7 +185,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> ProcessorDefinition<?> weaveAddFirst()
{
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder<T>(this, "*", null, null).selectFirst().before();
+        return new AdviceWithBuilder<T>(this, "*", null, null, null).selectFirst().before();
     }
 
     /**
@@ -179,7 +195,7 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
      */
     public <T extends ProcessorDefinition<?>> ProcessorDefinition<?> weaveAddLast()
{
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder<T>(this, "*", null, null).maxDeep(1).selectLast().after();
+        return new AdviceWithBuilder<T>(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<ProcessorDefinition<?>> 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<ProcessorDefinition<?>> 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<ProcessorDefinition<?>> 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<ProcessorDefinition<?>> 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<ChoiceDefinition>
{
     
     @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");
+            }
+        };
+    }
+
+}


Mime
View raw message