camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r881279 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/model/ test/java/org/apache/camel/processor/interceptor/
Date Tue, 17 Nov 2009 13:32:59 GMT
Author: davsclaus
Date: Tue Nov 17 13:32:59 2009
New Revision: 881279

URL: http://svn.apache.org/viewvc?rev=881279&view=rev
Log:
CAMEL-2184: Added adviceWith on RouteDefinition to advice a route with a route builder. This
allows you to add interceptors to an existing route for testing purpose.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?rev=881279&r1=881278&r2=881279&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java Tue Nov
17 13:32:59 2009
@@ -233,8 +233,23 @@
     }
 
     public void addRoutesToCamelContext(CamelContext context) throws Exception {
+        configureRoutes(context);
+        // add routes to Camel by populating them
+        populateRoutes();
+    }
+
+    /**
+     * Configures the routes
+     *
+     * @param context the Camel context
+     * @return the routes configured
+     * @throws Exception can be thrown during configuration
+     */
+    public RoutesDefinition configureRoutes(CamelContext context) throws Exception {
         setContext(context);
         checkInitialized();
+        routeCollection.setCamelContext(context);
+        return routeCollection;
     }
 
     /**
@@ -278,7 +293,6 @@
                 setErrorHandlerBuilder(camelContext.getErrorHandlerBuilder());
             }
             configure();
-            populateRoutes();
         }
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=881279&r1=881278&r2=881279&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Tue
Nov 17 13:32:59 2009
@@ -577,7 +577,9 @@
 
     public void removeRouteDefinition(RouteDefinition routeDefinition) throws Exception {
         String key = routeDefinition.idOrCreate(nodeIdFactory);
+        // stop and remove the route
         stopRoute(key);
+        this.routeServices.remove(key);
         removeRouteDefinition(key);
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java?rev=881279&r1=881278&r2=881279&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java Tue Nov
17 13:32:59 2009
@@ -35,6 +35,7 @@
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.ErrorHandlerBuilderRef;
+import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.DefaultRouteContext;
 import org.apache.camel.processor.interceptor.Delayer;
 import org.apache.camel.processor.interceptor.HandleFault;
@@ -144,6 +145,51 @@
         return CamelContextHelper.getMandatoryEndpoint(context, uri);
     }
 
+    /**
+     * Advices this route with the route builder.
+     * <p/>
+     * The advice process will add the interceptors, on exceptions, on completions etc. configured
+     * from the route builder to this route.
+     * <p/>
+     * This is mostly used for testing purpose to add interceptors and the likes to an existing
route.
+     * <p/>
+     * Will stop and remove the old route from camel context and add and start this new advised
route.
+     *
+     * @param builder the route builder
+     * @return a new route which is this route merged with the route builder
+     * @throws Exception can be thrown from the route builder
+     */
+    public RouteDefinition adviceWith(RouteBuilder builder) throws Exception {
+        CamelContext context = getCamelContext();
+        if (context == null) {
+            throw new IllegalArgumentException("CamelContext has not been injected!");
+        }
+
+        // configure and prepare the routes from the builder
+        RoutesDefinition routes = builder.configureRoutes(context);
+
+        // we can only advice with a route builder without any routes
+        if (!routes.getRoutes().isEmpty()) {
+            throw new IllegalArgumentException("You can only advice from a RouteBuilder which
has no existing routes."
+                    + " Remove all routes from the route builder.");
+        }
+
+        // stop and remove this existing route
+        List<RouteDefinition> list = new ArrayList<RouteDefinition>();
+        list.add(this);
+        context.removeRouteDefinitions(list);
+
+        // now merge which also ensures that interceptors and the likes get mixed in correctly
as well
+        RouteDefinition merged = routes.route(this);
+
+        // add the new merged route
+        context.getRouteDefinitions().add(0, merged);
+
+        // and start it
+        context.startRoute(merged);
+        return merged;
+    }
+
     // Fluent API
     // -----------------------------------------------------------------------
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java?rev=881279&r1=881278&r2=881279&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java Tue
Nov 17 13:32:59 2009
@@ -203,7 +203,7 @@
 
         // configure intercept
         for (InterceptDefinition intercept : getIntercepts()) {
-            // add as first output so intercept is handled before the acutal route and that
gives
+            // add as first output so intercept is handled before the actual route and that
gives
             // us the needed head start to init and be able to intercept all the remaining
processing steps
             route.getOutputs().add(0, intercept);
         }
@@ -232,7 +232,7 @@
 
         // configure intercept send to endpoint
         for (InterceptSendToEndpointDefinition sendTo : getInterceptSendTos()) {
-            // add as first output so intercept is handled before the acutal route and that
gives
+            // add as first output so intercept is handled before the actual route and that
gives
             // us the needed head start to init and be able to intercept all the remaining
processing steps
             route.getOutputs().add(0, sendTo);
         }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java?rev=881279&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
Tue Nov 17 13:32:59 2009
@@ -0,0 +1,65 @@
+/**
+ * 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.interceptor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version $Revision$
+ */
+public class AdviceWithTest extends ContextTestSupport {
+
+    public void testNoAdvised() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testAdvised() throws Exception {
+        context.getRouteDefinitions().get(0).adviceWith(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                interceptSendToEndpoint("mock:foo")
+                        .skipSendToOriginalEndpoint()
+                        .to("log:foo")
+                        .to("mock:advised");
+            }
+        });
+
+        getMockEndpoint("mock:foo").expectedMessageCount(0);
+        getMockEndpoint("mock:advised").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        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").to("mock:foo").to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message