camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r771041 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/management/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/processor/interceptor/ camel-core/src/test/java/org/apache/camel/pro...
Date Sun, 03 May 2009 08:58:12 GMT
Author: davsclaus
Date: Sun May  3 08:58:11 2009
New Revision: 771041

URL: http://svn.apache.org/viewvc?rev=771041&view=rev
Log:
CAMEL-1569: Added when() to intercept(). Its nice when the code is nice and clean now, and
easy to add when predicate. (unlike the old code in InterceptFromDefinition, that got to complex)

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteWhenTest.java
      - copied, changed from r770908, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.java
      - copied, changed from r770908, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml
      - copied, changed from r770908, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Delayer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/HandleFault.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/InstrumentationInterceptStrategy.java
Sun May  3 08:58:11 2009
@@ -60,4 +60,8 @@
         return target;
     }
 
+    @Override
+    public String toString() {
+        return "Instrument";
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptDefinition.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptDefinition.java Sun
May  3 08:58:11 2009
@@ -16,14 +16,13 @@
  */
 package org.apache.camel.model;
 
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.processor.Pipeline;
@@ -40,7 +39,6 @@
 public class InterceptDefinition extends OutputDefinition<ProcessorDefinition> {
 
     // TODO: support stop later (its a bit hard as it needs to break entire processing of
route)
-    // TODO: add support for when predicate
 
     @XmlTransient
     protected Processor output;
@@ -81,6 +79,11 @@
                     return output;
                 }
             }
+
+            @Override
+            public String toString() {
+                return "intercept[" + output + "]";
+            }
         });
 
         // remove me from the route so I am not invoked in a regular route path
@@ -90,6 +93,16 @@
     }
 
     /**
+     * Applies this interceptor only if the given predicate is true
+     *
+     * @param predicate  the predicate
+     * @return the builder
+     */
+    public ChoiceDefinition when(Predicate predicate) {
+        return choice().when(predicate);
+    }
+
+    /**
      * This method is <b>only</b> for handling some post configuration
      * that is needed from the Spring DSL side as JAXB does not invoke the fluent
      * builders, so we need to manually handle this afterwards, and since this is
@@ -97,7 +110,25 @@
      * with or without proceed/stop set as well.
      */
     public void afterPropertiesSet() {
-        // TODO: is needed when we add support for when predicate
+        if (getOutputs().size() == 0) {
+            // no outputs
+            return;
+        }
+
+        ProcessorDefinition first = getOutputs().get(0);
+        if (first instanceof WhenDefinition) {
+            WhenDefinition when = (WhenDefinition) first;
+            // move this outputs to the when, expect the first one
+            // as the first one is the interceptor itself
+            for (int i = 1; i < outputs.size(); i++) {
+                ProcessorDefinition out = outputs.get(i);
+                when.addOutput(out);
+            }
+            // remove the moved from the original output, by just keeping the first one
+            ProcessorDefinition keep = outputs.get(0);
+            clearOutput();
+            outputs.add(keep);
+        }
     }
 
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java
Sun May  3 08:58:11 2009
@@ -20,9 +20,9 @@
 import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
-import javax.xml.bind.annotation.XmlAttribute;
 
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
@@ -45,7 +45,7 @@
     // TODO: Support wildcards for endpoints so you can match by scheme, eg jms:*
 
     @XmlAttribute(required = false)
-    private String uri;
+    protected String uri;
     @XmlTransient
     protected ProceedDefinition proceed = new ProceedDefinition();
     @XmlTransient

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Debugger.java
Sun May  3 08:58:11 2009
@@ -145,5 +145,8 @@
         return new ArrayList<ExceptionEvent>();
     }
 
-
+    @Override
+    public String toString() {
+        return "Debugger";
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Delayer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Delayer.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Delayer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Delayer.java
Sun May  3 08:58:11 2009
@@ -74,4 +74,9 @@
     public void setDelay(long delay) {
         this.delay = delay;
     }
+
+    @Override
+    public String toString() {
+        return "Delayer[" + delay + "]";
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/HandleFault.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/HandleFault.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/HandleFault.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/HandleFault.java
Sun May  3 08:58:11 2009
@@ -50,4 +50,8 @@
         return null;
     }
 
+    @Override
+    public String toString() {
+        return "HandleFault";
+    }
 }
\ No newline at end of file

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCaching.java
Sun May  3 08:58:11 2009
@@ -50,4 +50,8 @@
         return null;
     }
 
+    @Override
+    public String toString() {
+        return "StreamCaching";
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/Tracer.java
Sun May  3 08:58:11 2009
@@ -198,4 +198,9 @@
     public void setUseJpa(boolean useJpa) {
         this.useJpa = useJpa;
     }
+
+    @Override
+    public String toString() {
+        return "Tracer";
+    }
 }

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteWhenTest.java
(from r770908, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteWhenTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteWhenTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteTest.java&r1=770908&r2=771041&rev=771041&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSimpleRouteWhenTest.java
Sun May  3 08:58:11 2009
@@ -22,16 +22,17 @@
 /**
  * @version $Revision$
  */
-public class InterceptSimpleRouteTest extends ContextTestSupport {
+public class InterceptSimpleRouteWhenTest extends ContextTestSupport {
 
-    public void testIntercept() throws Exception {
-        getMockEndpoint("mock:foo").expectedMessageCount(1);
-        getMockEndpoint("mock:bar").expectedMessageCount(1);
-        getMockEndpoint("mock:result").expectedMessageCount(1);
+    public void testInterceptWhen() throws Exception {
+        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World", "Hi");
+        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World", "Hi");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World", "Hi");
 
-        getMockEndpoint("mock:intercepted").expectedMessageCount(3);
+        getMockEndpoint("mock:intercepted").expectedBodiesReceived("Hello World", "Hello
World", "Hello World");
 
         template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "Hi");
 
         assertMockEndpointsSatisfied();
     }
@@ -41,11 +42,11 @@
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                intercept().to("mock:intercepted");
+                intercept().when(body().contains("Hello")).to("mock:intercepted");
 
                 from("direct:start")
                     .to("mock:foo", "mock:bar", "mock:result");
             }
         };
     }
-}
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java?rev=771041&r1=771040&r2=771041&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
(original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
Sun May  3 08:58:11 2009
@@ -28,7 +28,6 @@
 
 import org.apache.camel.CamelException;
 import org.apache.camel.Routes;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultLifecycleStrategy;
@@ -59,7 +58,6 @@
 import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.Registry;
-import org.apache.camel.spi.InterceptStrategy;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ProcessorDefinitionHelper;
 import org.apache.commons.logging.Log;

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.java
(from r770908, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.java&r1=770908&r2=771041&rev=771041&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.java
Sun May  3 08:58:11 2009
@@ -18,11 +18,10 @@
 
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.processor.intercept.InterceptFromUriSimpleLogTest;
-import org.apache.camel.processor.intercept.InterceptSimpleRouteTest;
+import org.apache.camel.processor.intercept.InterceptSimpleRouteWhenTest;
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
-public class SpringInterceptSimpleRouteTest extends InterceptSimpleRouteTest {
+public class SpringInterceptSimpleRouteWhenTest extends InterceptSimpleRouteWhenTest {
 
     @Override
     public boolean isUseRouteBuilder() {
@@ -30,7 +29,7 @@
     }
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml");
     }
 
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml
(from r770908, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.xml&r1=770908&r2=771041&rev=771041&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptSimpleRouteWhenTest.xml
Sun May  3 08:58:11 2009
@@ -22,13 +22,16 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: example -->
+    <!-- START SNIPPET: e1 -->
     <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
         <!-- here we intercept each processing step in the routing and do a detour
              routing where we route the exhange to the mock:intercepted endpoint.
-             Think the intercept as a AOP around that is weaven around each and
-             every route tag in the route path below -->
+             We have applied a when predicate so the interceptor only applies
+             if the message body contains the string word 'Hello' -->
         <intercept>
+            <when>
+                <simple>${in.body} contains 'Hello'</simple>
+            </when>
             <to uri="mock:intercepted"/>
         </intercept>
 
@@ -40,5 +43,5 @@
             <to uri="mock:result"/>
         </route>
     </camelContext>
-    <!-- END SNIPPET: example -->
+    <!-- END SNIPPET: e1 -->
 </beans>



Mime
View raw message