CAMEL-8785 Fixed the issue of StackOverFlowError using Custom InterceptStrategy
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/587f43e3 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/587f43e3 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/587f43e3 Branch: refs/heads/camel-2.14.x Commit: 587f43e3beafeaed732ee75876a99f866a331b29 Parents: 9199e68 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Thu May 21 11:20:13 2015 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Thu May 21 11:24:53 2015 +0800 ---------------------------------------------------------------------- .../processor/interceptor/DefaultChannel.java | 8 ++- .../camel/itest/issues/DummyInterceptor.java | 30 ++++++++++ .../issues/IntercepFromAndStrategyTest.java | 62 ++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/587f43e3/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java index c9ae2f3..40db512 100644 --- a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java +++ b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java @@ -285,7 +285,13 @@ public class DefaultChannel extends CamelInternalProcessor implements ModelChann // however its not the most optimal solution, but we can still run. InterceptorToAsyncProcessorBridge bridge = new InterceptorToAsyncProcessorBridge(target); wrapped = strategy.wrapProcessorInInterceptors(routeContext.getCamelContext(), targetOutputDef, bridge, next); - bridge.setTarget(wrapped); + // Avoid the stack overflow + if (!wrapped.equals(bridge)) { + bridge.setTarget(wrapped); + } else { + // Just skip the wrapped processor + bridge.setTarget(null); + } wrapped = bridge; } if (!(wrapped instanceof WrapProcessor)) { http://git-wip-us.apache.org/repos/asf/camel/blob/587f43e3/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/DummyInterceptor.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/DummyInterceptor.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/DummyInterceptor.java new file mode 100644 index 0000000..df15e01 --- /dev/null +++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/DummyInterceptor.java @@ -0,0 +1,30 @@ +/** + * 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.itest.issues; + +import org.apache.camel.CamelContext; +import org.apache.camel.Processor; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.spi.InterceptStrategy; + +public class DummyInterceptor implements InterceptStrategy { + + // Just simply return the target processor + public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, Processor target, Processor nextTarget) throws Exception { + return target; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/587f43e3/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/IntercepFromAndStrategyTest.java ---------------------------------------------------------------------- diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/IntercepFromAndStrategyTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/IntercepFromAndStrategyTest.java new file mode 100644 index 0000000..d93de76 --- /dev/null +++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/issues/IntercepFromAndStrategyTest.java @@ -0,0 +1,62 @@ +/** + * 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.itest.issues; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + + +public class IntercepFromAndStrategyTest extends CamelTestSupport { + + @EndpointInject(uri = "mock:result") + protected MockEndpoint resultEndpoint; + + @Produce(uri = "direct:start") + protected ProducerTemplate template; + + @Test + public void strategyTest() throws Exception { + resultEndpoint.expectedBodiesReceived("Bla Bla Bla"); + template.sendBody("direct:start", "Bla Bla Bla"); + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + + @Override + public void configure() throws Exception { + // add a dummy strategy + // removing this line the test works + context.addInterceptStrategy(new DummyInterceptor()); + // intercet from + interceptFrom("direct:start").log("Intercepted"); + + from("direct:start").to("mock:result"); + } + }; + } + + + +}