Repository: camel Updated Branches: refs/heads/master bc6b0f694 -> d62b0c7e6
CAMEL-10450: bean/class component should be able to call groovy function directly Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d62b0c7e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d62b0c7e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d62b0c7e Branch: refs/heads/master Commit: d62b0c7e685f9576cb1a49492205e68a224c9326 Parents: bc6b0f6 Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Nov 7 23:44:51 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Nov 7 23:44:51 2016 +0100 ---------------------------------------------------------------------- .../apache/camel/component/bean/MethodInfo.java | 13 ++++ .../bean/BeanReturnCallableReturnNullTest.java | 74 ++++++++++++++++++++ .../component/bean/BeanReturnCallableTest.java | 68 ++++++++++++++++++ 3 files changed, 155 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d62b0c7e/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java index e4b0e82..5ee4b46 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutorService; @@ -288,6 +289,18 @@ public class MethodInfo { } Object result = invoke(method, pojo, arguments, exchange); + // the method may be a closure or chained method returning a callable which should be called + if (result instanceof Callable) { + LOG.trace("Method returned Callback which will be called: {}", result); + Object callableResult = ((Callable) result).call(); + if (callableResult != null) { + result = callableResult; + } else { + // if callable returned null we should not change the body + result = Void.TYPE; + } + } + if (recipientList != null) { // ensure its started if (!recipientList.isStarted()) { http://git-wip-us.apache.org/repos/asf/camel/blob/d62b0c7e/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableReturnNullTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableReturnNullTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableReturnNullTest.java new file mode 100644 index 0000000..5c24474 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableReturnNullTest.java @@ -0,0 +1,74 @@ +/** + * 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.component.bean; + +import java.util.concurrent.Callable; +import javax.naming.Context; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.util.jndi.JndiContext; + +public class BeanReturnCallableReturnNullTest extends ContextTestSupport { + + public void testBeanReturnCallable() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("StartMyBean"); + mock.expectedHeaderReceived("user", "admin"); + mock.expectedHeaderReceived("foo", "bar"); + + template.requestBody("direct:in", "Start"); + + mock.assertIsSatisfied(); + } + + protected Context createJndiContext() throws Exception { + JndiContext answer = new JndiContext(); + answer.bind("myBean", new MyBean()); + return answer; + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("direct:in") + .setHeader("foo", constant("bar")) + .to("bean:myBean") + .to("mock:result"); + } + }; + } + + public static class MyBean { + + public Callable doSomething(final Exchange exchange) { + return new Callable() { + @Override + public Object call() throws Exception { + String body = exchange.getIn().getBody(String.class); + exchange.getIn().setHeader("user", "admin"); + exchange.getIn().setBody(body + "MyBean"); + // return null as we have set changes already + return null; + } + }; + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d62b0c7e/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableTest.java new file mode 100644 index 0000000..54bdda5 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanReturnCallableTest.java @@ -0,0 +1,68 @@ +/** + * 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.component.bean; + +import java.util.concurrent.Callable; +import javax.naming.Context; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.util.jndi.JndiContext; + +public class BeanReturnCallableTest extends ContextTestSupport { + + public void testBeanReturnCallable() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("I was called"); + mock.expectedHeaderReceived("foo", "bar"); + + template.requestBody("direct:in", "Start"); + + mock.assertIsSatisfied(); + } + + protected Context createJndiContext() throws Exception { + JndiContext answer = new JndiContext(); + answer.bind("myBean", new MyBean()); + return answer; + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("direct:in") + .setHeader("foo", constant("bar")) + .to("bean:myBean") + .to("mock:result"); + } + }; + } + + public static class MyBean { + + public Callable doSomething() { + return new Callable() { + @Override + public Object call() throws Exception { + return "I was called"; + } + }; + } + } + +}