You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/06/04 16:47:49 UTC

svn commit: r1131411 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/test/java/org/apache/camel/management/ camel-core/src/test/java/org/apache/camel/processor/ camel-core/src/test/java/org/apache/camel/processor/inte...

Author: davsclaus
Date: Sat Jun  4 14:47:49 2011
New Revision: 1131411

URL: http://svn.apache.org/viewvc?rev=1131411&view=rev
Log:
CAMEL-4050: Fixed CBR setting ids on its when/otherwise nodes. Improved CBR parent/child relationship in model to be more fine grained and pin point the actual when/otherwise, instead of being coarse grained and refer to the ChoiceDefinition. Made the end() a bit more intelligent to work with CBR. CAMEL-4044: Fixed CBR not having its child nodes enlisted in JMX.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCBRTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/ParentChildInterceptStrategyTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml
      - copied, changed from r1131378, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenDefinition.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRWithRecipientListTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java?rev=1131411&r1=1131410&r2=1131411&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java Sat Jun  4 14:47:49 2011
@@ -19,7 +19,6 @@ package org.apache.camel.model;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
@@ -37,7 +36,7 @@ import org.apache.camel.util.CollectionS
 /**
  * Represents an XML <choice/> element
  *
- * @version 
+ * @version
  */
 @XmlRootElement(name = "choice")
 @XmlAccessorType(XmlAccessType.FIELD)
@@ -59,11 +58,12 @@ public class ChoiceDefinition extends Pr
 
         }
     }
+
     @Override
     public String getShortName() {
         return "choice";
     }
-           
+
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
         List<FilterProcessor> filters = new ArrayList<FilterProcessor>();
@@ -79,10 +79,11 @@ public class ChoiceDefinition extends Pr
 
     // Fluent API
     // -------------------------------------------------------------------------
+
     /**
      * Sets the predicate for the when node
      *
-     * @param predicate  the predicate
+     * @param predicate the predicate
      * @return the builder
      */
     public ChoiceDefinition when(Predicate predicate) {
@@ -108,7 +109,7 @@ public class ChoiceDefinition extends Pr
 
     /**
      * Sets the otherwise node
-     * 
+     *
      * @return the builder
      */
     public ChoiceDefinition otherwise() {
@@ -118,6 +119,33 @@ public class ChoiceDefinition extends Pr
         return this;
     }
 
+    @Override
+    public void setId(String value) {
+        // when setting id, we should set it on the fine grained element, if possible
+        if (otherwise != null) {
+            otherwise.setId(value);
+        } else if (!getWhenClauses().isEmpty()) {
+            int size = getWhenClauses().size();
+            getWhenClauses().get(size - 1).setId(value);
+        } else {
+            super.setId(value);
+        }
+    }
+
+    @Override
+    public void addOutput(ProcessorDefinition output) {
+        super.addOutput(output);
+        // re-configure parent as its a tad more complex for the CNR
+        if (otherwise != null) {
+            output.setParent(otherwise);
+        } else if (!getWhenClauses().isEmpty()) {
+            int size = getWhenClauses().size();
+            output.setParent(getWhenClauses().get(size - 1));
+        } else {
+            output.setParent(this);
+        }
+    }
+
     // Properties
     // -------------------------------------------------------------------------
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java?rev=1131411&r1=1131410&r2=1131411&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinition.java Sat Jun  4 14:47:49 2011
@@ -1094,11 +1094,24 @@ public abstract class ProcessorDefinitio
 
         if (blocks.isEmpty()) {
             if (parent == null) {
-                return this; 
+                return this.endParent();
             }
-            return parent;
+            return parent.endParent();
         }
         popBlock();
+        return this.endParent();
+    }
+
+    /**
+     * Strategy to allow {@link ProcessorDefinition}s to have special logic when using end() in the DSL
+     * to return back to the intended parent.
+     * <p/>
+     * For example a content based router we return back to the {@link ChoiceDefinition} when we end()
+     * from a {@link WhenDefinition}.
+     *
+     * @return the end
+     */
+    public ProcessorDefinition endParent() {
         return this;
     }
 
@@ -1108,7 +1121,14 @@ public abstract class ProcessorDefinitio
      * @return the builder
      */
     public ChoiceDefinition endChoice() {
-        return (ChoiceDefinition) end();
+        ProcessorDefinition def = end();
+        if (def instanceof WhenDefinition) {
+            return (ChoiceDefinition) def.getParent();
+        } else if (def instanceof OtherwiseDefinition) {
+            return (ChoiceDefinition) def.getParent();
+        } else {
+            return (ChoiceDefinition) def;
+        }
     }
 
     /**

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenDefinition.java?rev=1131411&r1=1131410&r2=1131411&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/WhenDefinition.java Sat Jun  4 14:47:49 2011
@@ -67,5 +67,11 @@ public class WhenDefinition extends Expr
     public FilterProcessor createProcessor(RouteContext routeContext) throws Exception {
         return createFilterProcessor(routeContext);
     }
-    
+
+    @Override
+    public ProcessorDefinition endParent() {
+        // when using when in the DSL we don't want to end back to this when, but instead
+        // the parent of this, so return the parent
+        return this.getParent();
+    }
 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCBRTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCBRTest.java?rev=1131411&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCBRTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedCBRTest.java Sat Jun  4 14:47:49 2011
@@ -0,0 +1,78 @@
+/**
+ * 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.management;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class ManagedCBRTest extends ManagementTestSupport {
+
+    public void testManagedCBR() throws Exception {
+        MBeanServer mbeanServer = getMBeanServer();
+
+        ObjectName on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=routes,name=\"route\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-a\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"choice\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-b\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-c\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-d\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-e\"");
+        mbeanServer.isRegistered(on);
+
+        on = ObjectName.getInstance("org.apache.camel:context=localhost/camel-1,type=processors,name=\"task-done\"");
+        mbeanServer.isRegistered(on);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").routeId("route")
+                    .to("mock:a").id("task-a")
+                    .choice().id("choice")
+                        .when(simple("${body} contains Camel")).id("when")
+                            .to("mock:b").id("task-b")
+                            .to("mock:c").id("task-c")
+                        .when(simple("${body} contains Donkey")).id("when2")
+                            .to("mock:d").id("task-d")
+                        .otherwise().id("otherwise")
+                            .to("mock:e").id("task-e")
+                    .end()
+                    .to("mock:done").id("task-done");
+            }
+        };
+    }
+
+}

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRWithRecipientListTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRWithRecipientListTest.java?rev=1131411&r1=1131410&r2=1131411&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRWithRecipientListTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/CBRWithRecipientListTest.java Sat Jun  4 14:47:49 2011
@@ -64,7 +64,8 @@ public class CBRWithRecipientListTest ex
                         .when(body().contains("Camel"))
                             .recipientList(header("foo")).end()
                         .when(body().contains("Donkey"))
-                            .recipientList(header("bar")).end()
+                            // we can do either end() or endChoice()
+                            .recipientList(header("bar")).endChoice()
                         .otherwise()
                             .to("mock:result");
             }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/ParentChildInterceptStrategyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/ParentChildInterceptStrategyTest.java?rev=1131411&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/ParentChildInterceptStrategyTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/ParentChildInterceptStrategyTest.java Sat Jun  4 14:47:49 2011
@@ -0,0 +1,96 @@
+/**
+ * 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.processor.intercept;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.spi.InterceptStrategy;
+
+/**
+ *
+ */
+public class ParentChildInterceptStrategyTest extends ContextTestSupport {
+
+    protected static final List<String> LIST = new ArrayList<String>();
+
+    public void testParentChild() throws Exception {
+        getMockEndpoint("mock:done").expectedMessageCount(1);
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:c").expectedMessageCount(1);
+        getMockEndpoint("mock:d").expectedMessageCount(0);
+        getMockEndpoint("mock:e").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello Camel");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(7, LIST.size());
+        assertEquals("Parent route -> target task-a", LIST.get(0));
+        assertEquals("Parent when -> target task-b", LIST.get(1));
+        assertEquals("Parent when -> target task-c", LIST.get(2));
+        assertEquals("Parent when2 -> target task-d", LIST.get(3));
+        assertEquals("Parent otherwise -> target task-e", LIST.get(4));
+        assertEquals("Parent route -> target choice", LIST.get(5));
+        assertEquals("Parent route -> target task-done", LIST.get(6));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addInterceptStrategy(new MyParentChildInterceptStrategy());
+
+                from("direct:start").routeId("route")
+                    .to("mock:a").id("task-a")
+                    .choice().id("choice")
+                        .when(simple("${body} contains Camel")).id("when")
+                            .to("mock:b").id("task-b")
+                            .to("mock:c").id("task-c")
+                        .when(simple("${body} contains Donkey")).id("when2")
+                            .to("mock:d").id("task-d")
+                        .otherwise().id("otherwise")
+                            .to("mock:e").id("task-e")
+                    .end()
+                    .to("mock:done").id("task-done");
+            }
+        };
+    }
+
+    public static final class MyParentChildInterceptStrategy implements InterceptStrategy {
+
+        @Override
+        public Processor wrapProcessorInInterceptors(final CamelContext context, final ProcessorDefinition<?> definition,
+                                                     final Processor target, final Processor nextTarget) throws Exception {
+            String targetId = definition.getId();
+            String parentId = definition.getParent() != null ? definition.getParent().getId() : "";
+
+            LIST.add("Parent " + parentId + " -> target " + targetId);
+
+            return target;
+        }
+
+    }
+
+}

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.java?rev=1131411&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.java Sat Jun  4 14:47:49 2011
@@ -0,0 +1,48 @@
+/**
+ * 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.spring.processor.intercept;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.processor.intercept.ParentChildInterceptStrategyTest;
+
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ *
+ */
+public class SpringParentChildInterceptStrategyTest extends ParentChildInterceptStrategyTest {
+
+    public void testParentChild() throws Exception {
+        getMockEndpoint("mock:done").expectedMessageCount(1);
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:c").expectedMessageCount(1);
+        getMockEndpoint("mock:d").expectedMessageCount(0);
+        getMockEndpoint("mock:e").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "Hello Camel");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(7, LIST.size());
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml");
+    }
+
+}

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml (from r1131378, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml&r1=1131378&r2=1131411&rev=1131411&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aopbefore.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/intercept/SpringParentChildInterceptStrategyTest.xml Sat Jun  4 14:47:49 2011
@@ -22,16 +22,29 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <!-- START SNIPPET: e1 -->
-    <camelContext xmlns="http://camel.apache.org/schema/spring">
-        <route>
-            <from uri="direct:start"/>
-            <aop beforeUri="mock:before">
-                <transform><constant>Bye World</constant></transform>
-                <to uri="mock:result"/>
-            </aop>
-        </route>
-    </camelContext>
-    <!-- END SNIPPET: e1 -->
+  <bean id="parentChildIntercept"
+        class="org.apache.camel.processor.intercept.ParentChildInterceptStrategyTest$MyParentChildInterceptStrategy"/>
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+    <route id="route">
+      <from uri="direct:start"/>
+      <to uri="mock:a" id="task-a"/>
+      <choice id="choice">
+        <when id="when">
+          <simple>${body} contains Camel</simple>
+          <to uri="mock:b" id="task-b"/>
+          <to uri="mock:c" id="task-c"/>
+        </when>
+        <when id="when2">
+          <simple>${body} contains Donkey</simple>
+          <to uri="mock:d" id="task-d"/>
+        </when>
+        <otherwise id="otherwise">
+          <to uri="mock:e" id="task-e"/>
+        </otherwise>
+      </choice>
+      <to uri="mock:done"/>
+    </route>
+  </camelContext>
 
 </beans>