camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jans...@apache.org
Subject svn commit: r806533 - in /camel/trunk/components/camel-web/src: main/java/org/apache/camel/web/resources/ main/java/org/apache/camel/web/util/ test/java/org/apache/camel/web/groovy/ test/resources/
Date Fri, 21 Aug 2009 12:48:13 GMT
Author: janstey
Date: Fri Aug 21 12:48:13 2009
New Revision: 806533

URL: http://svn.apache.org/viewvc?rev=806533&view=rev
Log:
CAMEL-1392 - get rid of temp context during a route post and add support for xpath filtering.
Thanks to Xueqiang

Modified:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/RouteResource.java
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/ExpressionNodeRenderer.java
    camel/trunk/components/camel-web/src/test/java/org/apache/camel/web/groovy/FilterDSLTest.java
    camel/trunk/components/camel-web/src/test/resources/testedRoutes.txt

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/RouteResource.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/RouteResource.java?rev=806533&r1=806532&r2=806533&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/RouteResource.java
(original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/resources/RouteResource.java
Fri Aug 21 12:48:13 2009
@@ -157,21 +157,19 @@
      * Allows a routes builder to be updated
      */
     public void postRoutes(RouteBuilder builder) throws Exception {
-        // add the route builder into a temporary camel context
-        CamelContext tempContext = new DefaultCamelContext();
-        tempContext.addRoutes(builder);
-        // get all the added routes and add them into current context
-        List<RouteDefinition> routeDefinitions = tempContext.getRouteDefinitions();
-        for (int i = 0; i < routeDefinitions.size(); i++) {
-            RouteDefinition routeDefinition = routeDefinitions.get(i);
-            // set id only for the first route
-            if (i == 0) {
-                routeDefinition.setId(id);
-            }
-            
-            // add or update the route
-            getCamelContext().addRouteDefinitions(Collections.singletonList(routeDefinition));
-        }
+        DefaultCamelContext defaultCamelContext = (DefaultCamelContext)getCamelContext();
+        // stop and remove the original route
+        defaultCamelContext.stopRoute(id);
+        defaultCamelContext.removeRouteDefinition(id);
+
+        // add the routes in a route builder
+        defaultCamelContext.addRoutes(builder);
+
+        // reserve the id on the newest route
+        List<RouteDefinition> routeDefinitions = defaultCamelContext.getRouteDefinitions();
+        RouteDefinition route = routeDefinitions.get(routeDefinitions.size() - 1);
+        route.setId(id);
+        defaultCamelContext.startRoute(route);
     }
 
     /**

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/ExpressionNodeRenderer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/ExpressionNodeRenderer.java?rev=806533&r1=806532&r2=806533&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/ExpressionNodeRenderer.java
(original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/ExpressionNodeRenderer.java
Fri Aug 21 12:48:13 2009
@@ -44,126 +44,180 @@
 public final class ExpressionNodeRenderer {
     private ExpressionNodeRenderer() {
         // Utility class, no public or protected default constructor
-    }    
+    }
 
     public static void render(StringBuilder buffer, ProcessorDefinition<?> processor)
{
         ExpressionNode expNode = (ExpressionNode)processor;
-        ExpressionDefinition expression = expNode.getExpression();
-
         buffer.append(".").append(expNode.getShortName());
         if (expNode instanceof DelayDefinition) {
-            String delay = expression.getExpressionValue().toString();
-            if (!delay.contains("(")) {
-                String delayTime = expression.getExpressionValue().toString();
-                buffer.append("(").append(delayTime).append(")");
-            } else {
-                buffer.append("()");
-                ExpressionRenderer.render(buffer, expression);
-            }
+            renderDelay(buffer, expNode);
         } else if (expNode instanceof FilterDefinition) {
-            if (expression.getPredicate() != null) {
-                buffer.append("(");
-                PredicateRenderer.render(buffer, expression.getPredicate());
-                buffer.append(")");
-            } else if (expression.getLanguage() != null) {
-                buffer.append("()");
-                ExpressionRenderer.render(buffer, expression);
-            } else {
-                buffer.append("()");
-                ExpressionRenderer.render(buffer, expression);
-            }
+            renderFilter(buffer, expNode);
         } else if (expNode instanceof IdempotentConsumerDefinition) {
-            IdempotentConsumerDefinition idempotentConsume = (IdempotentConsumerDefinition)expNode;
-            buffer.append("(");
-            ExpressionRenderer.render(buffer, expression);
-            buffer.append(", ");
-            IdempotentRepository repository = idempotentConsume.getMessageIdRepository();
-            if (repository instanceof FileIdempotentRepository) {
-                // TODO need to be improved
-                buffer.append("FileIdempotentRepository.fileIdempotentRepository()");
-            } else if (repository instanceof MemoryIdempotentRepository) {
-                buffer.append("MemoryIdempotentRepository.memoryIdempotentRepository()");
-            }
-            buffer.append(")");
-            if (!idempotentConsume.isEager()) {
-                buffer.append(".eager(false)");
-            }
-
+            renderIdempotentConsumer(buffer, expNode);
         } else if (expNode instanceof LoopDefinition) {
-            if (expression instanceof ConstantExpression) {
-                buffer.append("(").append(expression.getExpression()).append(")");
-            } else {
-                buffer.append("()");
-                ExpressionRenderer.render(buffer, expression);
-            }
+            renderLoop(buffer, expNode);
         } else if (expNode instanceof RecipientListDefinition) {
+            ExpressionDefinition expression = expNode.getExpression();
             buffer.append("(");
             ExpressionRenderer.render(buffer, expression);
             buffer.append(")");
         } else if (expNode instanceof SetBodyDefinition) {
-            String expValue = expression.getExpressionValue().toString();
-            if (expValue.startsWith("append")) {
-                buffer.append("(");
-                ExpressionRenderer.render(buffer, expression);
-                buffer.append(")");
-            } else {
-                buffer.append("()");
-                ExpressionRenderer.renderConstant(buffer, expression);
-            }
+            renderSetBody(buffer, expNode);
         } else if (expNode instanceof SetHeaderDefinition) {
-            SetHeaderDefinition set = (SetHeaderDefinition)expNode;
-            buffer.append("(\"").append(set.getHeaderName()).append("\")");
-            if (expression.getExpressionValue() != null) {
-                ExpressionRenderer.renderConstant(buffer, expression);
-            } else if (expression.getExpressionType() != null) {
-                ExpressionRenderer.render(buffer, expression);
-            }
+            renderSetHeader(buffer, expNode);
         } else if (expNode instanceof SetOutHeaderDefinition) {
-            buffer.append("(\"unspported expressions in SetOutHeaderDefinition\")");
+            // TODO unsupported expression node
+            buffer.append("(\"setOutHeaderDefinition\")");
         } else if (expNode instanceof SetPropertyDefinition) {
-            SetPropertyDefinition set = (SetPropertyDefinition)expNode;
-            buffer.append("(\"").append(set.getPropertyName()).append("\")");
-            if (expression.getExpressionValue() != null) {
-                ExpressionRenderer.renderConstant(buffer, expression);
-            } else if (expression.getExpressionType() != null) {
-                ExpressionRenderer.render(buffer, expression);
-            }
+            renderSetProperty(buffer, expNode);
         } else if (expNode instanceof SplitDefinition) {
-            if (expression.getExpressionValue() != null) {
-                buffer.append("(");
-                ExpressionRenderer.render(buffer, expression);
-                buffer.append(")");
-            } else if (expression.getExpressionType() != null) {
-                buffer.append("().");
-                ExpressionRenderer.render(buffer, expression);
-            }
-
-            SplitDefinition split = (SplitDefinition)expNode;
-            if (split.isStreaming()) {
-                buffer.append(".streaming()");
-            }
+            renderSplit(buffer, expNode);
         } else if (expNode instanceof TransformDefinition) {
-            String expValue = expression.getExpressionValue().toString();
-            if (expValue.startsWith("append") || expValue.startsWith("prepend") || expValue.startsWith("to"))
{
-                buffer.append("(");
-                ExpressionRenderer.render(buffer, expression);
-                buffer.append(")");
-            } else if (expValue.startsWith("xpath")) {
-                buffer.append("()");
-                ExpressionRenderer.render(buffer, expression);
-            } else {
-                buffer.append("(constant(\"").append(expression.getExpressionValue().toString()).append("\"))");
-            }
+            renderTransform(buffer, expNode);
         } else if (expNode instanceof WhenDefinition) {
+            renderWhen(buffer, expNode);
+        }
+    }
+
+    private static void renderDelay(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        String delay = expression.getExpressionValue().toString();
+        if (!delay.contains("(")) {
+            String delayTime = expression.getExpressionValue().toString();
+            buffer.append("(").append(delayTime).append(")");
+        } else {
+            buffer.append("()");
+            ExpressionRenderer.render(buffer, expression);
+        }
+    }
+
+    private static void renderFilter(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        if (!(expression instanceof ExpressionClause)) {
             if (expression.getPredicate() != null) {
                 buffer.append("(");
                 PredicateRenderer.render(buffer, expression.getPredicate());
                 buffer.append(")");
             }
-            if (expression instanceof ExpressionClause) {
+        } else {
+            String language = expression.getLanguage();
+            if (language != null && !language.equals("")) {
+                buffer.append("()");
+                ExpressionRenderer.render(buffer, expression);
+            } else {
                 buffer.append("()");
-                ExpressionRenderer.render(buffer, (ExpressionClause)expression);
+                ExpressionRenderer.render(buffer, expression);
             }
         }
     }
+
+    private static void renderIdempotentConsumer(StringBuilder buffer, ExpressionNode expNode)
{
+        ExpressionDefinition expression = expNode.getExpression();
+        IdempotentConsumerDefinition idempotentConsume = (IdempotentConsumerDefinition)expNode;
+        buffer.append("(");
+        ExpressionRenderer.render(buffer, expression);
+        buffer.append(", ");
+        IdempotentRepository repository = idempotentConsume.getMessageIdRepository();
+        if (repository instanceof FileIdempotentRepository) {
+            // TODO need to be improved
+            buffer.append("FileIdempotentRepository.fileIdempotentRepository()");
+        } else if (repository instanceof MemoryIdempotentRepository) {
+            buffer.append("MemoryIdempotentRepository.memoryIdempotentRepository()");
+        }
+        buffer.append(")");
+        if (!idempotentConsume.isEager()) {
+            buffer.append(".eager(false)");
+        }
+    }
+
+    private static void renderLoop(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        if (expression instanceof ConstantExpression) {
+            buffer.append("(").append(expression.getExpression()).append(")");
+        } else {
+            buffer.append("()");
+            ExpressionRenderer.render(buffer, expression);
+        }
+    }
+
+    private static void renderSetBody(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        String expValue = expression.getExpressionValue().toString();
+        if (expValue.startsWith("append")) {
+            buffer.append("(");
+            ExpressionRenderer.render(buffer, expression);
+            buffer.append(")");
+        } else {
+            buffer.append("()");
+            ExpressionRenderer.renderConstant(buffer, expression);
+        }
+    }
+
+    private static void renderSetHeader(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        SetHeaderDefinition set = (SetHeaderDefinition)expNode;
+        buffer.append("(\"").append(set.getHeaderName()).append("\")");
+        if (expression.getExpressionValue() != null) {
+            ExpressionRenderer.renderConstant(buffer, expression);
+        } else if (expression.getExpressionType() != null) {
+            ExpressionRenderer.render(buffer, expression);
+        }
+    }
+
+    private static void renderSetProperty(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        SetPropertyDefinition set = (SetPropertyDefinition)expNode;
+        buffer.append("(\"").append(set.getPropertyName()).append("\")");
+        if (expression.getExpressionValue() != null) {
+            ExpressionRenderer.renderConstant(buffer, expression);
+        } else if (expression.getExpressionType() != null) {
+            ExpressionRenderer.render(buffer, expression);
+        }
+    }
+
+    private static void renderSplit(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        if (expression.getExpressionValue() != null) {
+            buffer.append("(");
+            ExpressionRenderer.render(buffer, expression);
+            buffer.append(")");
+        } else if (expression.getExpressionType() != null) {
+            buffer.append("().");
+            ExpressionRenderer.render(buffer, expression);
+        }
+
+        SplitDefinition split = (SplitDefinition)expNode;
+        if (split.isStreaming()) {
+            buffer.append(".streaming()");
+        }
+    }
+
+    private static void renderTransform(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        String expValue = expression.getExpressionValue().toString();
+        if (expValue.startsWith("append") || expValue.startsWith("prepend") || expValue.startsWith("to"))
{
+            buffer.append("(");
+            ExpressionRenderer.render(buffer, expression);
+            buffer.append(")");
+        } else if (expValue.startsWith("xpath")) {
+            buffer.append("()");
+            ExpressionRenderer.render(buffer, expression);
+        } else {
+            buffer.append("(constant(\"").append(expression.getExpressionValue().toString()).append("\"))");
+        }
+    }
+
+    private static void renderWhen(StringBuilder buffer, ExpressionNode expNode) {
+        ExpressionDefinition expression = expNode.getExpression();
+        if (expression.getPredicate() != null) {
+            buffer.append("(");
+            PredicateRenderer.render(buffer, expression.getPredicate());
+            buffer.append(")");
+        }
+        if (expression instanceof ExpressionClause) {
+            buffer.append("()");
+            ExpressionRenderer.render(buffer, (ExpressionClause)expression);
+        }
+    }
 }

Modified: camel/trunk/components/camel-web/src/test/java/org/apache/camel/web/groovy/FilterDSLTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/test/java/org/apache/camel/web/groovy/FilterDSLTest.java?rev=806533&r1=806532&r2=806533&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/test/java/org/apache/camel/web/groovy/FilterDSLTest.java
(original)
+++ camel/trunk/components/camel-web/src/test/java/org/apache/camel/web/groovy/FilterDSLTest.java
Fri Aug 21 12:48:13 2009
@@ -36,4 +36,9 @@
         String dsl = "from(\"direct:start\").filter().method(\"myBean\", \"matches\").to(\"mock:result\")";
         assertEquals(dsl, render(dsl));
     }
+
+    public void testFilterXPath() throws Exception {
+        String dsl = "from(\"direct:start\").filter().xpath(\"/person[@Name='James']\").to(\"mock:result\")";
+        assertEquals(dsl, render(dsl));
+    }
 }

Modified: camel/trunk/components/camel-web/src/test/resources/testedRoutes.txt
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/test/resources/testedRoutes.txt?rev=806533&r1=806532&r2=806533&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/test/resources/testedRoutes.txt (original)
+++ camel/trunk/components/camel-web/src/test/resources/testedRoutes.txt Fri Aug 21 12:48:13
2009
@@ -1,7 +1,7 @@
 #########################aggregate#########################
 from("direct:start").aggregate().header("cheese").to("mock:result")
 from("direct:start").aggregate(header("cheese")).to("mock:result")
-#errorHandler(deadLetterChannel("mock:error"));onException(CamelException.class).maximumRedeliveries(2);from("direct:start").aggregate(header("id")).to("mock:result")
+#errorHandler(deadLetterChannel("mock://error"));onException(CamelException.class).maximumRedeliveries(2);from("direct:start").aggregate(header("id")).to("mock:result")
 from("direct:start").aggregate().simple("id").batchTimeout(500L).groupExchanges().to("mock:result")
 from("direct:start").aggregate(header("id")).batchTimeout(3000).batchSize(0).to("mock:result")
 from("timer://kickoff?period=9999910000").setHeader("id").constant("foo").setBody().constant("a
b c").split(body().tokenize(" ")).to("seda:splitted");from("seda:splitted").aggregate(header("id")).to("mock:result")
@@ -35,10 +35,10 @@
 #########################detour#########################
 from("direct:start").choice().when().method("controlBean", "isDetour").to("mock:detour").end().to("mock:result")
 #########################deadLetter/errorHandler#########################
-errorHandler(deadLetterChannel("mock:failed").maximumRedeliveries(0).handled(false));from("direct:start").to("mock:result")
-errorHandler(deadLetterChannel("mock:failed").maximumRedeliveries(3).redeliverDelay(5000));from("direct:start").to("mock:result")
-errorHandler(deadLetterChannel("mock:failed").maximumRedeliveries(3).redeliverDelay(5000).handled(false));from("direct:start").to("mock:result")
-#errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(4).loggingLevel(LoggingLevel.DEBUG));from("direct:start").to("mock:result")
+errorHandler(deadLetterChannel("mock://failed").maximumRedeliveries(0).handled(false));from("direct:start").to("mock:result")
+errorHandler(deadLetterChannel("mock://failed").maximumRedeliveries(3).redeliverDelay(5000));from("direct:start").to("mock:result")
+errorHandler(deadLetterChannel("mock://failed").maximumRedeliveries(3).redeliverDelay(5000).handled(false));from("direct:start").to("mock:result")
+#errorHandler(deadLetterChannel("mock://error").maximumRedeliveries(4).loggingLevel(LoggingLevel.DEBUG));from("direct:start").to("mock:result")
 #########################enricher#########################
 from("direct:start").setBody(body().append(" World!")).to("mock:result")
 #########################filter#########################



Mime
View raw message