camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1101352 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ test/java/org/apache/camel/processor/interceptor/
Date Tue, 10 May 2011 07:29:54 GMT
Author: davsclaus
Date: Tue May 10 07:29:54 2011
New Revision: 1101352

URL: http://svn.apache.org/viewvc?rev=1101352&view=rev
Log:
CAMEL-3958: Added weaveByType

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java
      - copied, changed from r1101122, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java?rev=1101352&r1=1101351&r2=1101352&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithBuilder.java Tue
May 10 07:29:54 2011
@@ -22,19 +22,21 @@ import org.apache.camel.model.ProcessorD
 /**
  * A builder when using the <a href="http://camel.apache.org/advicewith.html">advice
with</a> feature.
  */
-public class AdviceWithBuilder {
+public class AdviceWithBuilder<T extends ProcessorDefinition> {
 
     private final AdviceWithRouteBuilder builder;
     private final String id;
     private final String toString;
+    private final Class<T> type;
 
-    public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString)
{
+    public AdviceWithBuilder(AdviceWithRouteBuilder builder, String id, String toString,
Class<T> type) {
         this.builder = builder;
         this.id = id;
         this.toString = toString;
+        this.type = type;
 
-        if (id == null && toString == null) {
-            throw new IllegalArgumentException("Either id or toString must be specified");
+        if (id == null && toString == null && type == null) {
+            throw new IllegalArgumentException("Either id, toString or type must be specified");
         }
     }
 
@@ -49,6 +51,8 @@ public class AdviceWithBuilder {
             builder.getAdviceWithTasks().add(AdviceWithTasks.replaceById(builder.getOriginalRoute(),
id, answer));
         } else if (toString != null) {
             builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByToString(builder.getOriginalRoute(),
toString, answer));
+        } else if (type != null) {
+            builder.getAdviceWithTasks().add(AdviceWithTasks.replaceByType(builder.getOriginalRoute(),
type, answer));
         }
         return answer;
     }
@@ -61,6 +65,8 @@ public class AdviceWithBuilder {
             builder.getAdviceWithTasks().add(AdviceWithTasks.removeById(builder.getOriginalRoute(),
id));
         } else if (toString != null) {
             builder.getAdviceWithTasks().add(AdviceWithTasks.removeByToString(builder.getOriginalRoute(),
toString));
+        } else if (type != null) {
+            builder.getAdviceWithTasks().add(AdviceWithTasks.removeByType(builder.getOriginalRoute(),
type));
         }
     }
 
@@ -75,6 +81,8 @@ public class AdviceWithBuilder {
             builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(builder.getOriginalRoute(),
id, answer));
         } else if (toString != null) {
             builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(builder.getOriginalRoute(),
toString, answer));
+        } else if (type != null) {
+            builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(builder.getOriginalRoute(),
type, answer));
         }
         return answer;
     }
@@ -90,6 +98,8 @@ public class AdviceWithBuilder {
             builder.getAdviceWithTasks().add(AdviceWithTasks.afterById(builder.getOriginalRoute(),
id, answer));
         } else if (toString != null) {
             builder.getAdviceWithTasks().add(AdviceWithTasks.afterByToString(builder.getOriginalRoute(),
toString, answer));
+        } else if (type != null) {
+            builder.getAdviceWithTasks().add(AdviceWithTasks.afterByType(builder.getOriginalRoute(),
type, answer));
         }
         return answer;
     }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java?rev=1101352&r1=1101351&r2=1101352&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
Tue May 10 07:29:54 2011
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
+import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.util.ObjectHelper;
 
@@ -91,9 +92,9 @@ public abstract class AdviceWithRouteBui
      * @return the builder
      * @see org.apache.camel.util.EndpointHelper#matchPattern(String, String)
      */
-    public AdviceWithBuilder weaveById(String pattern) {
+    public <T extends ProcessorDefinition> AdviceWithBuilder weaveById(String pattern)
{
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder(this, pattern, null);
+        return new AdviceWithBuilder<T>(this, pattern, null, null);
     }
 
     /**
@@ -105,9 +106,20 @@ public abstract class AdviceWithRouteBui
      * @return the builder
      * @see org.apache.camel.util.EndpointHelper#matchPattern(String, String)
      */
-    public AdviceWithBuilder weaveByToString(String pattern) {
+    public <T extends ProcessorDefinition> AdviceWithBuilder weaveByToString(String
pattern) {
         ObjectHelper.notNull(originalRoute, "originalRoute", this);
-        return new AdviceWithBuilder(this, null, pattern);
+        return new AdviceWithBuilder<T>(this, null, pattern, null);
+    }
+
+    /**
+     * Weaves by matching type of the nodes in the route.
+     *
+     * @param type the processor type
+     * @return the builder
+     */
+    public <T extends ProcessorDefinition> AdviceWithBuilder weaveByType(Class<T>
type) {
+        ObjectHelper.notNull(originalRoute, "originalRoute", this);
+        return new AdviceWithBuilder<T>(this, null, null, type);
     }
 
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java?rev=1101352&r1=1101351&r2=1101352&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java Tue
May 10 07:29:54 2011
@@ -86,6 +86,26 @@ public final class AdviceWithTasks {
         }
     }
 
+    /**
+     * Will match by the type of the processor.
+     */
+    private static final class MatchByType implements MatchBy {
+
+        private final Class type;
+
+        private MatchByType(Class<?> type) {
+            this.type = type;
+        }
+
+        public String getId() {
+            return type.getSimpleName();
+        }
+
+        public boolean match(ProcessorDefinition processor) {
+            return type.isAssignableFrom(processor.getClass());
+        }
+    }
+
 
     public static AdviceWithTask replaceByToString(final RouteDefinition route, final String
toString, final ProcessorDefinition replace) {
         return doReplace(route, new MatchByToString(toString), replace);
@@ -95,6 +115,10 @@ public final class AdviceWithTasks {
         return doReplace(route, new MatchById(id), replace);
     }
 
+    public static AdviceWithTask replaceByType(final RouteDefinition route, final Class type,
final ProcessorDefinition replace) {
+        return doReplace(route, new MatchByType(type), replace);
+    }
+
     @SuppressWarnings("unchecked")
     private static AdviceWithTask doReplace(final RouteDefinition route, final MatchBy matchBy,
final ProcessorDefinition replace) {
         return new AdviceWithTask() {
@@ -132,6 +156,10 @@ public final class AdviceWithTasks {
         return doRemove(route, new MatchById(id));
     }
 
+    public static AdviceWithTask removeByType(final RouteDefinition route, final Class type)
{
+        return doRemove(route, new MatchByType(type));
+    }
+
     private static AdviceWithTask doRemove(final RouteDefinition route, final MatchBy matchBy)
{
         return new AdviceWithTask() {
             public void task() throws Exception {
@@ -167,6 +195,10 @@ public final class AdviceWithTasks {
         return doBefore(route, new MatchById(id), before);
     }
 
+    public static AdviceWithTask beforeByType(final RouteDefinition route, final Class type,
final ProcessorDefinition before) {
+        return doBefore(route, new MatchByType(type), before);
+    }
+
     @SuppressWarnings("unchecked")
     private static AdviceWithTask doBefore(final RouteDefinition route, final MatchBy matchBy,
final ProcessorDefinition before) {
         return new AdviceWithTask() {
@@ -204,6 +236,10 @@ public final class AdviceWithTasks {
         return doAfter(route, new MatchById(id), after);
     }
 
+    public static AdviceWithTask afterByType(final RouteDefinition route, final Class type,
final ProcessorDefinition after) {
+        return doAfter(route, new MatchByType(type), after);
+    }
+
     @SuppressWarnings("unchecked")
     private static AdviceWithTask doAfter(final RouteDefinition route, final MatchBy matchBy,
final ProcessorDefinition after) {
         return new AdviceWithTask() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java?rev=1101352&r1=1101351&r2=1101352&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
Tue May 10 07:29:54 2011
@@ -148,6 +148,15 @@ public abstract class ScheduledPollConsu
         return timeUnit;
     }
 
+    /**
+     * Sets the time unit to use.
+     * <p/>
+     * Notice that both {@link #getDelay()} and {@link #getInitialDelay()} are using
+     * the same time unit. So if you change this value, then take into account that the
+     * default value of {@link #getInitialDelay()} is 1000. So you may to adjust this value
accordingly.
+     *
+     * @param timeUnit the time unit.
+     */
     public void setTimeUnit(TimeUnit timeUnit) {
         this.timeUnit = timeUnit;
     }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java
(from r1101122, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java&r1=1101122&r2=1101352&rev=1101352&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTypeTest.java
Tue May 10 07:29:54 2011
@@ -19,23 +19,27 @@ package org.apache.camel.processor.inter
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.AdviceWithRouteBuilder;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.LogDefinition;
+import org.apache.camel.model.SplitDefinition;
+import org.apache.camel.model.ToDefinition;
+import org.apache.camel.model.TransformDefinition;
 
 /**
  * Advice with tests
  */
-public class AdviceWithTasksTest extends ContextTestSupport {
+public class AdviceWithTypeTest extends ContextTestSupport {
 
-    public void testUnknownId() throws Exception {
+    public void testUnknownType() throws Exception {
         try {
             context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
                 @Override
                 public void configure() throws Exception {
-                    weaveById("xxx").replace().to("mock:xxx");
+                    weaveByType(SplitDefinition.class).replace().to("mock:xxx");
                 }
             });
             fail("Should hve thrown exception");
         } catch (IllegalArgumentException e) {
-            assertTrue(e.getMessage(), e.getMessage().startsWith("There are no outputs which
matches: xxx in the route"));
+            assertTrue(e.getMessage(), e.getMessage().startsWith("There are no outputs which
matches: SplitDefinition in the route"));
         }
     }
 
@@ -44,20 +48,18 @@ public class AdviceWithTasksTest extends
         context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
             @Override
             public void configure() throws Exception {
-                // weave the node in the route which has id = bar
+                // weave by type in the route
                 // and replace it with the following route path
-                weaveById("bar").replace().multicast().to("mock:a").to("mock:b");
+                weaveByType(LogDefinition.class).replace().multicast().to("mock:a").to("mock:b");
             }
         });
         // END SNIPPET: e1
 
-        getMockEndpoint("mock:foo").expectedMessageCount(1);
-        getMockEndpoint("mock:bar").expectedMessageCount(0);
         getMockEndpoint("mock:a").expectedMessageCount(1);
         getMockEndpoint("mock:b").expectedMessageCount(1);
-        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
-        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "World");
 
         assertMockEndpointsSatisfied();
     }
@@ -67,20 +69,17 @@ public class AdviceWithTasksTest extends
         context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
             @Override
             public void configure() throws Exception {
-                // weave the node in the route which has id = bar and remove it
-                weaveById("bar").remove();
+                // weave the type in the route and remove it
+                weaveByType(TransformDefinition.class).remove();
             }
         });
         // END SNIPPET: e2
 
-        getMockEndpoint("mock:foo").expectedMessageCount(1);
-        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedBodiesReceived("World");
 
-        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "World");
 
         assertMockEndpointsSatisfied();
-
-        assertFalse("Should not have removed id", context.hasEndpoint("mock:bar") == null);
     }
 
     public void testBefore() throws Exception {
@@ -88,19 +87,16 @@ public class AdviceWithTasksTest extends
         context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
             @Override
             public void configure() throws Exception {
-                // weave the node in the route which has id = bar
+                // weave the type in the route and remove it
                 // and insert the following route path before the adviced node
-                weaveById("bar").before().to("mock:a").transform(constant("Bye World"));
+                weaveByType(ToDefinition.class).before().transform(constant("Bye World"));
             }
         });
         // END SNIPPET: e3
 
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:a").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World");
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
-        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "World");
 
         assertMockEndpointsSatisfied();
     }
@@ -110,19 +106,17 @@ public class AdviceWithTasksTest extends
         context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
             @Override
             public void configure() throws Exception {
-                // weave the node in the route which has id = bar
-                // and insert the following route path after the advice node
-                weaveById("bar").after().to("mock:a").transform(constant("Bye World"));
+                // weave the type in the route and remove it
+                // and insert the following route path after the adviced node
+                weaveByType(ToDefinition.class).after().transform(constant("Bye World"));
             }
         });
         // END SNIPPET: e4
 
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:a").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
 
-        template.sendBody("direct:start", "Hello World");
+        Object out = template.requestBody("direct:start", "World");
+        assertEquals("Bye World", out);
 
         assertMockEndpointsSatisfied();
     }
@@ -134,8 +128,8 @@ public class AdviceWithTasksTest extends
             public void configure() throws Exception {
                 // START SNIPPET: e5
                 from("direct:start")
-                    .to("mock:foo")
-                    .to("mock:bar").id("bar")
+                    .transform(simple("Hello ${body}"))
+                    .log("Got ${body}")
                     .to("mock:result");
                 // END SNIPPET: e5
             }



Mime
View raw message