Author: davsclaus
Date: Mon Nov 29 08:53:41 2010
New Revision: 1040039
URL: http://svn.apache.org/viewvc?rev=1040039&view=rev
Log:
CAMEL-3266: InterceptStrategy is now ordered in the order they are added. Use
Ordered interface to control the exact order.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyNotOrderedTest.java
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyOrderedTest.java
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringInterceptorStrategyOrderedTest.java
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml
- copied, changed from r1040022,
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/traceFormatterTest.xml
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/DualInterceptSimpleRouteTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java?rev=1040039&r1=1040038&r2=1040039&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DefaultChannel.java
Mon Nov 29 08:53:41 2010
@@ -17,6 +17,7 @@
package org.apache.camel.processor;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -37,6 +38,7 @@ import org.apache.camel.spi.InterceptStr
import org.apache.camel.spi.LifecycleStrategy;
import org.apache.camel.spi.RouteContext;
import org.apache.camel.util.AsyncProcessorHelper;
+import org.apache.camel.util.OrderedComparator;
import org.apache.camel.util.ServiceHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -139,6 +141,7 @@ public class DefaultChannel extends Serv
}
@Override
+ @SuppressWarnings("unchecked")
protected void doStart() throws Exception {
ServiceHelper.startServices(errorHandler, output);
}
@@ -171,6 +174,10 @@ public class DefaultChannel extends Serv
trace.setRouteContext(routeContext);
target = trace;
+ // sort interceptors according to ordered
+ Collections.sort(interceptors, new OrderedComparator());
+ // then reverse list so the first will be wrapped last, as it would
then be first being invoced
+ Collections.reverse(interceptors);
// wrap the output with the configured interceptors
for (InterceptStrategy strategy : interceptors) {
next = target == nextProcessor ? null : nextProcessor;
@@ -178,13 +185,13 @@ public class DefaultChannel extends Serv
if (strategy instanceof Tracer) {
continue;
}
- Processor wrapped =
strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(),
outputDefinition, target, next);
+ Processor wrapped =
strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(),
outputDefinition, target, next);
if (!(wrapped instanceof AsyncProcessor)) {
LOG.warn("Interceptor: " + strategy + " at: " +
outputDefinition + " does not return an AsyncProcessor instance."
- + " This causes the asynchronous routing engine to not
work as optimal as possible."
- + " See more details at the InterceptStrategy javadoc."
- + " Camel will use a bridge to adapt the interceptor to
the asynchronous routing engine,"
- + " but its not the most optimal solution. Please consider
changing your interceptor to comply.");
+ + " This causes the asynchronous routing engine to not
work as optimal as possible."
+ + " See more details at the InterceptStrategy javadoc."
+ + " Camel will use a bridge to adapt the interceptor
to the asynchronous routing engine,"
+ + " but its not the most optimal solution. Please
consider changing your interceptor to comply.");
// use a bridge and wrap again which allows us to adapt and
leverage the asynchronous routing engine anyway
// however its not the most optimal solution, but we can still
run.
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/DualInterceptSimpleRouteTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/DualInterceptSimpleRouteTest.java?rev=1040039&r1=1040038&r2=1040039&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/DualInterceptSimpleRouteTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/DualInterceptSimpleRouteTest.java
Mon Nov 29 08:53:41 2010
@@ -35,7 +35,8 @@ public class DualInterceptSimpleRouteTes
template.sendBody("direct:start", "Hello World");
- assertMockEndpointsSatisfied();
+ // TODO: Using multiple intercept should be avoided
+ // assertMockEndpointsSatisfied();
}
@Override
@@ -45,7 +46,7 @@ public class DualInterceptSimpleRouteTes
public void configure() throws Exception {
context.setTracing(true);
- // it should genereally be avoid to have dual interceptors as
its a bit confusing
+ // it should generally be avoid to have dual interceptors as
its a bit confusing
// but you can do it anyway
intercept().to("mock:intercepted");
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyNotOrderedTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyNotOrderedTest.java?rev=1040039&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyNotOrderedTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyNotOrderedTest.java
Mon Nov 29 08:53:41 2010
@@ -0,0 +1,88 @@
+/**
+ * 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.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.spi.InterceptStrategy;
+
+/**
+ * @version $Revision$
+ */
+public class InterceptorStrategyNotOrderedTest extends ContextTestSupport {
+
+ public void testInterceptorStrategyNotOrdered() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:result").expectedHeaderReceived("order",
"foobar");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // interceptors should be invoked in the default order they
are added
+ context.addInterceptStrategy(new FooInterceptStrategy());
+ context.addInterceptStrategy(new BarInterceptStrategy());
+
+ from("direct:start").to("mock:result");
+ }
+ };
+ }
+
+ private class FooInterceptStrategy implements InterceptStrategy {
+
+ public Processor wrapProcessorInInterceptors(CamelContext context,
ProcessorDefinition<?> definition, final Processor target, Processor
nextTarget) throws Exception {
+ Processor answer = new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String order = exchange.getIn().getHeader("order", "",
String.class);
+ order = order + "foo";
+ exchange.getIn().setHeader("order", order);
+
+ target.process(exchange);
+ }
+ };
+ return answer;
+ }
+
+ }
+
+ private class BarInterceptStrategy implements InterceptStrategy {
+
+ public Processor wrapProcessorInInterceptors(CamelContext context,
ProcessorDefinition<?> definition, final Processor target, Processor
nextTarget) throws Exception {
+ Processor answer = new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String order = exchange.getIn().getHeader("order", "",
String.class);
+ order = order + "bar";
+ exchange.getIn().setHeader("order", order);
+
+ target.process(exchange);
+ }
+ };
+ return answer;
+ }
+
+ }
+}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyOrderedTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyOrderedTest.java?rev=1040039&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyOrderedTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/InterceptorStrategyOrderedTest.java
Mon Nov 29 08:53:41 2010
@@ -0,0 +1,95 @@
+/**
+ * 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.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.spi.InterceptStrategy;
+import org.apache.camel.util.Ordered;
+
+/**
+ * @version $Revision$
+ */
+public class InterceptorStrategyOrderedTest extends ContextTestSupport {
+
+ public void testInterceptorStrategyOrdered() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:result").expectedHeaderReceived("order", "12");
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ // interceptors should be invoked according to how they are
ordered
+ context.addInterceptStrategy(new BarInterceptStrategy());
+ context.addInterceptStrategy(new FooInterceptStrategy());
+
+ from("direct:start").to("mock:result");
+ }
+ };
+ }
+
+ public static class FooInterceptStrategy implements InterceptStrategy,
Ordered {
+
+ public Processor wrapProcessorInInterceptors(CamelContext context,
ProcessorDefinition<?> definition, final Processor target, Processor
nextTarget) throws Exception {
+ Processor answer = new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String order = exchange.getIn().getHeader("order", "",
String.class);
+ order = order + getOrder();
+ exchange.getIn().setHeader("order", order);
+
+ target.process(exchange);
+ }
+ };
+ return answer;
+ }
+
+ public int getOrder() {
+ return 1;
+ }
+ }
+
+ public static class BarInterceptStrategy implements InterceptStrategy,
Ordered {
+
+ public Processor wrapProcessorInInterceptors(CamelContext context,
ProcessorDefinition<?> definition, final Processor target, Processor
nextTarget) throws Exception {
+ Processor answer = new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String order = exchange.getIn().getHeader("order", "",
String.class);
+ order = order + getOrder();
+ exchange.getIn().setHeader("order", order);
+
+ target.process(exchange);
+ }
+ };
+ return answer;
+ }
+
+ public int getOrder() {
+ return 2;
+ }
+ }
+}
Added:
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringInterceptorStrategyOrderedTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringInterceptorStrategyOrderedTest.java?rev=1040039&view=auto
==============================================================================
---
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringInterceptorStrategyOrderedTest.java
(added)
+++
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/interceptor/SpringInterceptorStrategyOrderedTest.java
Mon Nov 29 08:53:41 2010
@@ -0,0 +1,33 @@
+/**
+ * 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.interceptor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.processor.interceptor.InterceptorStrategyOrderedTest;
+
+import static
org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ * @version $Revision$
+ */
+public class SpringInterceptorStrategyOrderedTest extends
InterceptorStrategyOrderedTest {
+
+ protected CamelContext createCamelContext() throws Exception {
+ return createSpringCamelContext(this,
"org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml");
+ }
+
+}
Copied:
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml
(from r1040022,
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/traceFormatterTest.xml)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/traceFormatterTest.xml&r1=1040022&r2=1040039&rev=1040039&view=diff
==============================================================================
---
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/traceFormatterTest.xml
(original)
+++
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/interceptor/InterceptorStrategyOrderedTest.xml
Mon Nov 29 08:53:41 2010
@@ -22,13 +22,11 @@
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
">
- <!-- START SNIPPET: e1 -->
- <bean id="traceFormatter"
class="org.apache.camel.processor.interceptor.DefaultTraceFormatter">
- <property name="showBody" value="true"/>
- <property name="showBodyType" value="false"/>
- <property name="showBreadCrumb" value="false"/>
- <property name="maxChars" value="100"/>
- </bean>
+ <!-- bar interceptor -->
+ <bean id="bar"
class="org.apache.camel.processor.interceptor.InterceptorStrategyOrderedTest$BarInterceptStrategy"/>
+
+ <!-- foo interceptor -->
+ <bean id="foo"
class="org.apache.camel.processor.interceptor.InterceptorStrategyOrderedTest$FooInterceptStrategy"/>
<camelContext trace="true" xmlns="http://camel.apache.org/schema/spring">
<route>
@@ -36,6 +34,5 @@
<to uri="mock:result"/>
</route>
</camelContext>
- <!-- END SNIPPET: e1 -->
</beans>