You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/06/17 10:42:31 UTC

svn commit: r1136809 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/ test/java/org/apache/camel/component/bean/

Author: davsclaus
Date: Fri Jun 17 08:42:30 2011
New Revision: 1136809

URL: http://svn.apache.org/viewvc?rev=1136809&view=rev
Log:
CAMEL-4115: Bean component should not propagate CamelBeanMethodName header as its intended as override method to use for this particular bean endpoint only.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodNameHeaderIssueTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java?rev=1136809&r1=1136808&r2=1136809&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanProcessor.java Fri Jun 17 08:42:30 2011
@@ -75,8 +75,8 @@ public class BeanProcessor extends Servi
     }
 
     public boolean process(Exchange exchange, AsyncCallback callback) {
-        // do we have an explicit method name we always should invoke
-        boolean isExplicitMethod = ObjectHelper.isNotEmpty(method);
+        // do we have an explicit method name we always should invoke (either configured on endpoint or as a header)
+        String explicitMethodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, method, String.class);
 
         Object bean;
         BeanInfo beanInfo;
@@ -92,7 +92,7 @@ public class BeanProcessor extends Servi
         // do we have a custom adapter for this POJO to a Processor
         // should not be invoked if an explicit method has been set
         Processor processor = getProcessor();
-        if (!isExplicitMethod && processor != null) {
+        if (explicitMethodName == null && processor != null) {
             LOG.trace("Using a custom adapter as bean invocation: {}", processor);
             try {
                 processor.process(exchange);
@@ -139,15 +139,13 @@ public class BeanProcessor extends Servi
             in.setHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY, isMultiParameterArray());
         }
 
-        String prevMethod = null;
         MethodInvocation invocation;
         if (methodObject != null) {
             invocation = beanInfo.createInvocation(methodObject, bean, exchange);
         } else {
-            // we just override the bean's invocation method name here
-            if (isExplicitMethod) {
-                prevMethod = in.getHeader(Exchange.BEAN_METHOD_NAME, String.class);
-                in.setHeader(Exchange.BEAN_METHOD_NAME, method);
+            // set explicit method name to invoke as a header, which is how BeanInfo can detect it
+            if (explicitMethodName != null) {
+                in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
             }
             try {
                 invocation = beanInfo.createInvocation(bean, exchange);
@@ -161,8 +159,9 @@ public class BeanProcessor extends Servi
             throw new IllegalStateException("No method invocation could be created, no matching method could be found on: " + bean);
         }
 
-        // remove temporary header
+        // remove headers as they should not be propagated
         in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
+        in.removeHeader(Exchange.BEAN_METHOD_NAME);
 
         Object value = null;
         try {
@@ -185,10 +184,6 @@ public class BeanProcessor extends Servi
             exchange.setException(e);
             callback.done(true);
             return true;
-        } finally {
-            if (isExplicitMethod) {
-                in.setHeader(Exchange.BEAN_METHOD_NAME, prevMethod);
-            }
         }
 
         // if the method returns something then set the value returned on the Exchange

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodNameHeaderIssueTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodNameHeaderIssueTest.java?rev=1136809&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodNameHeaderIssueTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodNameHeaderIssueTest.java Fri Jun 17 08:42:30 2011
@@ -0,0 +1,80 @@
+/**
+ * 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 org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Handler;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class BeanMethodNameHeaderIssueTest extends ContextTestSupport {
+
+    public void testBeanMethodNameHeaderIssue() throws Exception {
+        getMockEndpoint("mock:a").expectedBodiesReceived("foo");
+        getMockEndpoint("mock:a").message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
+        getMockEndpoint("mock:b").expectedBodiesReceived("bar");
+        getMockEndpoint("mock:b").message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
+        getMockEndpoint("mock:c").expectedBodiesReceived("Bye bar");
+        getMockEndpoint("mock:c").message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
+        getMockEndpoint("mock:d").expectedBodiesReceived("Bye bar Bye bar");
+        getMockEndpoint("mock:d").message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
+
+        template.sendBodyAndHeader("direct:start", "Hello World", Exchange.BEAN_METHOD_NAME, "foo");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .bean(BeanMethodNameHeaderIssueTest.class)
+                    .to("mock:a")
+                    .bean(BeanMethodNameHeaderIssueTest.class, "bar")
+                    .to("mock:b")
+                    .bean(BeanMethodNameHeaderIssueTest.class)
+                    .to("mock:c")
+                    .setHeader(Exchange.BEAN_METHOD_NAME, constant("echo"))
+                    .bean(BeanMethodNameHeaderIssueTest.class)
+                    .to("mock:d");
+            }
+        };
+    }
+
+    public String foo() {
+        return "foo";
+    }
+
+    public String bar() {
+        return "bar";
+    }
+
+    public String echo(String body) {
+        return body + " " + body;
+    }
+
+    @Handler
+    public String doSomething(String body) {
+        return "Bye " + body;
+    }
+
+}

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java?rev=1136809&r1=1136808&r2=1136809&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanWithMethodHeaderTest.java Fri Jun 17 08:42:30 2011
@@ -47,9 +47,11 @@ public class BeanWithMethodHeaderTest ex
     
     public void testEchoWithMethodHeaderHi() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("echo Hello World");
-        mock.expectedHeaderReceived(Exchange.BEAN_METHOD_NAME, "hi");
+        mock.expectedBodiesReceived("hi Hello World");
+        // header should be removed after usage
+        mock.message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
 
+        // header overrule endpoint configuration, so we should invoke the hi method
         template.sendBodyAndHeader("direct:echo", ExchangePattern.InOut, "Hello World", Exchange.BEAN_METHOD_NAME, "hi");
 
         assertMockEndpointsSatisfied();
@@ -57,9 +59,11 @@ public class BeanWithMethodHeaderTest ex
     
     public void testMixedBeanEndpoints() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("hi echo Hello World");
-        mock.expectedHeaderReceived(Exchange.BEAN_METHOD_NAME, "hi");
+        mock.expectedBodiesReceived("hi hi Hello World");
+        // header should be removed after usage
+        mock.message(0).header(Exchange.BEAN_METHOD_NAME).isNull();
 
+        // header overrule endpoint configuration, so we should invoke the hi method
         template.sendBodyAndHeader("direct:mixed", ExchangePattern.InOut, "Hello World", Exchange.BEAN_METHOD_NAME, "hi");
 
         assertMockEndpointsSatisfied();