camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r771296 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/spi/ camel-core/src/test/java/o...
Date Mon, 04 May 2009 13:04:16 GMT
Author: davsclaus
Date: Mon May  4 13:04:15 2009
New Revision: 771296

URL: http://svn.apache.org/viewvc?rev=771296&view=rev
Log:
CAMEL-1584: Added support for intercept by wildcard,regexp for intercept send to endpoint.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java   (with
props)
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java
      - copied, changed from r771262, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointDynamicTest.java
      - copied, changed from r771262, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml
      - copied, changed from r771262, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpoint.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointWhen.xml
  (props changed)
      - copied unchanged from r771262, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEendpointWhen.xml
Removed:
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEendpointWhen.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.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/InterceptSendToEndpointDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/RoutesDefinition.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointWhenTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringRouteStopTest.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=771296&r1=771295&r2=771296&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/CamelContext.java Mon May  4 13:04:15
2009
@@ -35,6 +35,7 @@
 import org.apache.camel.spi.PackageScanClassResolver;
 import org.apache.camel.spi.Registry;
 import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.spi.EndpointStrategy;
 
 /**
  * Interface used to represent the context used to configure routes and the
@@ -181,6 +182,16 @@
      */
     Collection<Endpoint> removeEndpoints(String uri) throws Exception;
 
+    /**
+     * Registers a {@link org.apache.camel.spi.EndpointStrategy callback} to allow you to
do custom
+     * logic when an {@link Endpoint} is about to be registered to the {@link CamelContext}
endpoint registry.
+     * <p/>
+     * When a callback is added it will be executed on the already registered endpoints allowing
you to catch-up
+     *
+     * @param strategy  callback to be invoked
+     */
+    void addRegisterEndpointCallback(EndpointStrategy strategy);
+
     // Route Management Methods
     //-----------------------------------------------------------------------
 

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=771296&r1=771295&r2=771296&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 Mon
May  4 13:04:15 2009
@@ -35,6 +35,7 @@
 import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.spi.EndpointStrategy;
 import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.Route;
 import org.apache.camel.Routes;
@@ -88,6 +89,7 @@
     private boolean routeDefinitionInitiated;
     private String name;  
     private final Map<String, Endpoint> endpoints = new HashMap<String, Endpoint>();
+    private final List<EndpointStrategy> endpointStrategies = new ArrayList<EndpointStrategy>();
     private final Map<String, Component> components = new HashMap<String, Component>();
     private List<Route> routes;
     private final List<Service> servicesToClose = new ArrayList<Service>();
@@ -310,7 +312,7 @@
         synchronized (endpoints) {
             startServices(endpoint);
             oldEndpoint = endpoints.remove(uri);
-            endpoints.put(getEndpointKey(uri, endpoint), endpoint);
+            addEndpointToRegistry(uri, endpoint);
             if (oldEndpoint != null) {
                 stopServices(oldEndpoint);
             }
@@ -381,8 +383,9 @@
 
                     if (answer != null) {
                         addService(answer);
-                        endpoints.put(getEndpointKey(uri, answer), answer);
+                        Endpoint newAnswer = addEndpointToRegistry(uri, answer);
                         lifecycleStrategy.onEndpointAdd(answer);
+                        answer = newAnswer;
                     }
                 } catch (Exception e) {
                     throw new ResolveEndpointFailedException(uri, e);
@@ -412,6 +415,34 @@
         }
     }
 
+    public void addRegisterEndpointCallback(EndpointStrategy strategy) {
+        if (!endpointStrategies.contains(strategy)) {
+            // let it be invoked for already registered endpoints so it can catch-up.
+            endpointStrategies.add(strategy);
+            for (Endpoint endpoint : getEndpoints()) {
+                Endpoint newEndpoint = strategy.registerEndpoint(endpoint.getEndpointUri(),
endpoint);
+                if (newEndpoint != endpoint) {
+                    endpoints.put(getEndpointKey(newEndpoint.getEndpointUri(), newEndpoint),
newEndpoint);
+                }
+            }
+        }
+    }
+
+    /**
+     * Strategy to add the given endpoint to the internal endpoint registry
+     *
+     * @param uri  uri of endpoint
+     * @param endpoint the endpoint to add
+     * @return the added endpoint
+     */
+    protected Endpoint addEndpointToRegistry(String uri, Endpoint endpoint) {
+        for (EndpointStrategy strategy : endpointStrategies) {
+            endpoint = strategy.registerEndpoint(uri, endpoint);
+        }
+        endpoints.put(getEndpointKey(uri, endpoint), endpoint);
+        return endpoint;
+    }
+
     // Route Management Methods
     // -----------------------------------------------------------------------
     public synchronized List<Route> getRoutes() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptSendToEndpointDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptSendToEndpointDefinition.java?rev=771296&r1=771295&r2=771296&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptSendToEndpointDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptSendToEndpointDefinition.java
Mon May  4 13:04:15 2009
@@ -22,14 +22,14 @@
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
-import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.InterceptSendToEndpoint;
 import org.apache.camel.processor.InterceptEndpointProcessor;
+import org.apache.camel.spi.EndpointStrategy;
 import org.apache.camel.spi.RouteContext;
-import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.EndpointHelper;
 
 /**
  * Represents an XML &lt;interceptToEndpoint/&gt; element
@@ -41,7 +41,6 @@
 public class InterceptSendToEndpointDefinition extends OutputDefinition<ProcessorDefinition>
{
 
     // TODO: Support lookup endpoint by ref (requires a bit more work)
-    // TODO: Support wildcards for endpoints so you can match by scheme, eg jms:*
 
     @XmlAttribute(required = true)
     private String uri;
@@ -72,18 +71,27 @@
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {
-        // we have to reuse the createProcessor method to build the detour route
-        // afterwards we remove this interceptor from the route so the route will
-        // not use regular intercptor. The interception by endpoint is triggered
-        // by the InterceptSendToEndpoint that handles the intercept routing logic
-        Endpoint endpoint = lookupEndpoint(routeContext.getCamelContext());
-
         // create the detour
-        Processor detour = routeContext.createProcessor(this);
+        final Processor detour = routeContext.createProcessor(this);
+
+        // register endpoint callback so we can proxy the endpoint
+        routeContext.getCamelContext().addRegisterEndpointCallback(new EndpointStrategy()
{
+            public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
+                // only proxy if the uri is matched
+                boolean match = getUri() == null || EndpointHelper.matchEndpoint(uri, getUri());

+                if (match) {
+                    // decorate endpoint with our proxy
+                    boolean skip = skipSendToOriginalEndpoint != null ? skipSendToOriginalEndpoint
: false;
+                    InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint,
skip);
+                    proxy.setDetour(detour);
+                    return proxy;
+                } else {
+                    // no proxy so return regular endpoint
+                    return endpoint;
+                }
+            }
+        });
 
-        // set the detour on the endpoint proxy
-        InterceptSendToEndpoint proxy = routeContext.getCamelContext().getTypeConverter().mandatoryConvertTo(InterceptSendToEndpoint.class,
endpoint);
-        proxy.setDetour(detour);
 
         // remove the original intercepted route from the outputs as we do not intercept
as the regular interceptor
         // instead we use the proxy endpoints producer do the triggering. That is we trigger
when someone sends
@@ -95,25 +103,6 @@
         return new InterceptEndpointProcessor(uri, detour);
     }
 
-    public void proxyEndpoint(CamelContext context) {
-        // TODO: Add dyanmic proxy support in that sence it should be some callback hooks
in CamelContext to
-        // you can register to do custom endpoint lookup or add to registry
-
-        // proxy the endpoint by using the InterceptSendToEndpoint that will proxy
-        // the producer so it processes the detour first
-        Endpoint endpoint = lookupEndpoint(context);
-
-        // decorate endpoint with our proxy
-        boolean skip = skipSendToOriginalEndpoint != null ? skipSendToOriginalEndpoint :
false;
-        InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint, skip);
-        try {
-            // add will replace the old one
-            context.addEndpoint(proxy.getEndpointUri(), proxy);
-        } catch (Exception e) {
-            throw ObjectHelper.wrapRuntimeCamelException(e);
-        }
-    }
-
     /**
      * Applies this interceptor only if the given predicate is true
      *
@@ -166,11 +155,6 @@
         }
     }
 
-    private Endpoint lookupEndpoint(CamelContext context) {
-        ObjectHelper.notNull(uri, "uri", this);
-        return context.getEndpoint(uri);
-    }
-
     public Boolean getSkipSendToOriginalEndpoint() {
         return skipSendToOriginalEndpoint;
     }
@@ -178,4 +162,12 @@
     public void setSkipSendToOriginalEndpoint(Boolean skipSendToOriginalEndpoint) {
         this.skipSendToOriginalEndpoint = skipSendToOriginalEndpoint;
     }
+
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String uri) {
+        this.uri = uri;
+    }
 }

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=771296&r1=771295&r2=771296&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 Mon
May  4 13:04:15 2009
@@ -193,7 +193,9 @@
 
         // configure intercepts
         for (InterceptDefinition intercept : getIntercepts()) {
-            route.addOutput(intercept);
+            // add as first output so intercept is handled before the acutal 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);
         }
 
         // configure intercept from
@@ -212,15 +214,17 @@
             }
 
             if (match) {
-                route.addOutput(intercept);
+                // add as first output so intercept is handled before the acutal 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);
             }
         }
 
         // configure intercept send to endpoint
         for (InterceptSendToEndpointDefinition sendTo : getInterceptSendTos()) {
-            // init interceptor by letting it proxy the real endpoint
-            sendTo.proxyEndpoint(getCamelContext());
-            route.addOutput(sendTo);
+            // add as first output so intercept is handled before the acutal 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);
         }
 
         // add on exceptions

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java?rev=771296&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java Mon May
 4 13:04:15 2009
@@ -0,0 +1,42 @@
+/**
+ * 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.spi;
+
+import org.apache.camel.Endpoint;
+
+/**
+ * Endpoint strategy with callback invoked when an {@link org.apache.camel.Endpoint} is about
to be registered to the
+ * endopoint registry in {@link org.apache.camel.CamelContext}. This callback allows you
to interveave
+ * and return a mixed in {@link org.apache.camel.Endpoint}.
+ * <p/>
+ * The {@link org.apache.camel.model.InterceptSendToEndpointDefinition} uses this to allow
it to proxy
+ * endpoints so it can intercept sending to the given endpoint.
+ *
+ * @version $Revision$
+ */
+public interface EndpointStrategy {
+
+    /**
+     * Register the endpoint.
+     *
+     * @param uri  uri of endpoint
+     * @param endpoint the current endpoint to register
+     * @return the real endpoint to register, for instance a wrapped/enhanced endpoint.
+     */
+    Endpoint registerEndpoint(String uri, Endpoint endpoint);
+
+}

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/EndpointStrategy.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java
(from r771262, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java&r1=771262&r2=771296&rev=771296&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointDynamicTest.java
Mon May  4 13:04:15 2009
@@ -17,35 +17,44 @@
 package org.apache.camel.processor.intercept;
 
 import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 
 /**
- * Unit test for intercepting sending to endpoint
- * 
+ * Unit test for intercepting sending to endpoint with dynamic endpoints
+ * and uri matching
+ *
  * @version $Revision$
  */
-public class InterceptSendToEndpointTest extends ContextTestSupport {
+public class InterceptSendToEndpointDynamicTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        deleteDirectory("target/foo");
+        deleteDirectory("target/bar");
+        deleteDirectory("target/cheese");
+        super.setUp();
+    }
 
     @Override
     public boolean isUseRouteBuilder() {
         return false;
     }
 
-    public void testInterceptEndpoint() throws Exception {
+    public void testSendToWildcard() throws Exception {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
                 // START SNIPPET: e1
-                // we intercept by endpoint, that means that whenever an exchange is about
to be sent to
-                // this endpoint, its intercepted and routed with this detour route beforehand
-                // afterwards its send to the original intended destination. So this is kinda
AOP before.
-                // That means mock:foo will receive the message (Bye World).
-                interceptSendToEndpoint("mock:foo")
-                    .to("mock:detour").transform(constant("Bye World"));
+                // intercept sending to any file endpoint, send it to our mock instead
+                // and do not send it to the original intended endpoint
+                interceptSendToEndpoint("file:*").skipSendToOriginalEndpoint()
+                    .to("mock:detour");
 
                 from("direct:first")
-                    .to("mock:bar")
-                    .to("mock:foo")
+                    .to("file://foo")
+                    .to("file://bar")
                     .to("mock:result");
                 // END SNIPPET: e1
 
@@ -53,127 +62,69 @@
         });
         context.start();
 
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:detour").expectedMessageCount(2);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
 
         template.sendBody("direct:first", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testInterceptEndpointWithPredicate() throws Exception {
+    public void testSendToRegex() throws Exception {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // START SNIPPET: e2
-                // we can also attach a predicate to the endpoint interceptor. So in this
example the exchange is
-                // only intercepted if the body is Hello World
-                interceptSendToEndpoint("mock:foo").when(body().isEqualTo("Hello World"))
-                    .to("mock:detour").transform(constant("Bye World"));
-
-                from("direct:second")
-                    .to("mock:bar")
-                    .to("mock:foo")
-                    .to("mock:result");
-                // END SNIPPET: e2
-
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World", "Hi");
-        // Hi is filtered out with the predicate on the intercept
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World", "Hi");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World", "Hi");
-
-        template.sendBody("direct:second", "Hello World");
-        template.sendBody("direct:second", "Hi");
-
-        assertMockEndpointsSatisfied();
-    }
+                // START SNIPPET: e1
+                // intercept sending to to either target/foo or target/bar directory
+                interceptSendToEndpoint("file://target/(foo|bar)").skipSendToOriginalEndpoint()
+                    .to("mock:detour");
 
-    public void testInterceptEndpointStop() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // START SNIPPET: e3
-                // since we use the skipSendToOriginalEndpoint() we instruct Camel to skip
-                // sending the exchange to the original intended destination after the intercept
-                // route is complete.
-                // That means that mock:foo will NOT receive the message, but the message
-                // is skipped and continued in the original route, so mock:result will receive
-                // the message.
-                interceptSendToEndpoint("mock:foo").skipSendToOriginalEndpoint()
-                    .transform(constant("Bye World")).to("mock:detour");
-
-                from("direct:third")
-                    .to("mock:bar")
-                    .to("mock:foo")
+                from("direct:first")
+                    .to("file://target/foo")
+                    .to("file://target/bar")
+                    .to("file://target/cheese")
                     .to("mock:result");
-                // END SNIPPET: e3
+                // END SNIPPET: e1
             }
         });
         context.start();
 
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Bye World");
-        // as we stop the original destination do not receive the exchange
-        getMockEndpoint("mock:foo").expectedMessageCount(0);
-        // but we continue to route afterwards
+        getMockEndpoint("mock:detour").expectedMessageCount(2);
         getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedFileExists("target/cheese/cheese.txt");
 
-        template.sendBody("direct:third", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testInterceptEndpointDirectly() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                interceptSendToEndpoint("direct:start")
-                    .to("mock:detour").transform(constant("Bye World"));
-
-                from("direct:start")
-                    .to("mock:foo")
-                    .to("mock:result");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
-        template.sendBody("direct:start", "Hello World");
+        template.sendBodyAndHeader("direct:first", "Hello World", Exchange.FILE_NAME, "cheese.txt");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testInterceptEndpointWithStop() throws Exception {
+    public void testSendToDynamicEndpoint() throws Exception {
         context.addRoutes(new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                interceptSendToEndpoint("direct:start")
-                    .to("mock:detour").stop();
+                interceptSendToEndpoint("file:*")
+                    .to("mock:detour");
 
-                from("direct:start")
-                    .to("mock:foo")
+                from("direct:first")
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            // we use a dynamic endpoint URI that Camel does not know beforehand
+                            // but it should still be intercepted as we intercept all file
endpoints
+                            template.sendBodyAndHeader("file://target/foo", "Hello Foo",
Exchange.FILE_NAME, "foo.txt");
+                        }
+                    })
                     .to("mock:result");
             }
         });
         context.start();
 
-        getMockEndpoint("mock:detour").expectedMessageCount(1);
-        getMockEndpoint("mock:foo").expectedMessageCount(0);
-        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello Foo");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:result").expectedFileExists("target/foo/foo.txt");
 
-        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:first", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
-}
+}
\ 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=771296&r1=771295&r2=771296&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
Mon May  4 13:04:15 2009
@@ -233,8 +233,8 @@
         // this is needed as JAXB does not build excaclty the same model definition as Spring
DSL would do
         // using route builders. So we have here a little custom code to fix the JAXB gaps
         for (RouteDefinition route : routes) {
-            initOnExceptions(route);
             initInterceptors(route);
+            initOnExceptions(route);
             initPolicies(route);
         }
 
@@ -320,19 +320,10 @@
 
         // configure intercept send to endpoint
         for (InterceptSendToEndpointDefinition intercept : getInterceptSendToEndpoints())
{
-            // special intercept for intercepting sending to an endpoint
-
-            // add the interceptor but we must do some pre configuration beforehand
             intercept.afterPropertiesSet();
-
-            // replace proceed with the rest of the route
-            try {
-                // init interceptor by letting it proxy the real endpoint
-                intercept.proxyEndpoint(getContext());
-            } catch (Exception e) {
-                throw ObjectHelper.wrapRuntimeCamelException(e);
-            }
-            route.addOutput(intercept);
+            // add as first output so intercept is handled before the acutal 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);
         }
 
     }

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointDynamicTest.java
(from r771262, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointDynamicTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointDynamicTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointTest.java&r1=771262&r2=771296&rev=771296&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointDynamicTest.java
Mon May  4 13:04:15 2009
@@ -23,21 +23,29 @@
 /**
  * @version $Revision$
  */
-public class SpringInterceptSendToEndpointTest extends ContextTestSupport {
+public class SpringInterceptSendToEndpointDynamicTest extends ContextTestSupport {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/interceptSendToEndpoint.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml");
     }
 
-    public void testInterceptEndpoint() throws Exception {
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+    public void testInterceptFile() throws Exception {
+        getMockEndpoint("mock:intercepted").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
 
         template.sendBody("direct:first", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
+    public void testNoInterceptFile() throws Exception {
+        getMockEndpoint("mock:intercepted").expectedMessageCount(0);
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:second", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointWhenTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointWhenTest.java?rev=771296&r1=771295&r2=771296&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointWhenTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringInterceptSendToEndpointWhenTest.java
Mon May  4 13:04:15 2009
@@ -26,7 +26,7 @@
 public class SpringInterceptSendToEndpointWhenTest extends ContextTestSupport {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/interceptSendToEendpointWhen.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/interceptSendToEndpointWhen.xml");
     }
 
     public void testInterceptEndpointWhen() throws Exception {

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringRouteStopTest.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringRouteStopTest.xml?rev=771296&r1=771295&r2=771296&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringRouteStopTest.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringRouteStopTest.xml
Mon May  4 13:04:15 2009
@@ -41,6 +41,16 @@
             </choice>
             <to uri="mock:result"/>
         </route>
+        
+        <route>
+            <from uri="direct:foo"/>
+            <to uri="mock:foo"/>
+            <!-- you can also set the Exchange#ROUTE_STOP property on Exchange to true
-->
+            <setProperty propertyName="CamelRouteStop">
+                <constant>true</constant>
+            </setProperty>
+            <to uri="mock:result"/>
+        </route>
     </camelContext>
     <!-- END SNIPPET: example -->
 </beans>

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml
(from r771262, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpoint.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpoint.xml&r1=771262&r2=771296&rev=771296&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpoint.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointDynamic.xml
Mon May  4 13:04:15 2009
@@ -25,20 +25,20 @@
     <!-- START SNIPPET: e1 -->
     <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
 
-        <!-- we intercept by endpoint, that means that whenever an exchange is about to
be sent to
-             this endpoint, its intercepted and routed with this detour route beforehand
-             afterwards its send to the original intended destination. So this is kinda AOP
before.
-             That means mock:foo will receive the message (Bye World). -->
-        <interceptSendToEndpoint uri="mock:foo">
-            <to uri="mock:detour"/>
-            <transform>
-                <constant>Bye World</constant>
-            </transform>
+        <!-- intercept sending to any file component -->
+        <interceptSendToEndpoint uri="file:*" skipSendToOriginalEndpoint="true">
+            <!-- send it to the mock instead -->
+            <to uri="mock:intercepted"/>
         </interceptSendToEndpoint>
 
         <route>
             <from uri="direct:first"/>
-            <to uri="mock:bar"/>
+            <to uri="file://target/foo"/>
+            <to uri="mock:result"/>
+        </route>
+
+        <route>
+            <from uri="direct:second"/>
             <to uri="mock:foo"/>
             <to uri="mock:result"/>
         </route>

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointWhen.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointWhen.xml
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptSendToEndpointWhen.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message