svn commit: r1031258 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/BeanInfo.java test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java test/java/org/apache/camel/component/bean/MyDummyBean.java

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1031258 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/bean/BeanInfo.java test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java test/java/org/apache/camel/component/bean/MyDummyBean.java

Christian Müller
Author: cmueller
Date: Thu Nov  4 21:26:48 2010
New Revision: 1031258

URL: http://svn.apache.org/viewvc?rev=1031258&view=rev
Log:
CAMEL-3311: bean parameter binding - Add support for type option to force Camel to pick method with compatible type
improve the ability to choose the right method (work in progress)

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyDummyBean.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=1031258&r1=1031257&r2=1031258&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Thu Nov  4 21:26:48 2010
@@ -469,23 +469,26 @@ public class BeanInfo {
             // lets try converting
             Object newBody = null;
             MethodInfo matched = null;
+            int matchCounter = 0;
             for (MethodInfo methodInfo : operationList) {
+                if (methodInfo.getBodyParameterType().isInstance(body)) {
+                    return methodInfo;
+                }
+                
                 Object value = convertToType(exchange, methodInfo.getBodyParameterType(), body);
                 if (value != null) {
                     if (LOG.isTraceEnabled()) {
                         LOG.trace("Converted body from: " + body.getClass().getCanonicalName()
                                 + "to: " + methodInfo.getBodyParameterType().getCanonicalName());
                     }
-                    if (newBody != null) {
-                        // we already have found one new body that could be converted so now we have 2 methods
-                        // and then its ambiguous
-                        throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, methodInfo));
-                    } else {
-                        newBody = value;
-                        matched = methodInfo;
-                    }
+                    matchCounter++;
+                    newBody = value;
+                    matched = methodInfo;
                 }
             }
+            if (matchCounter > 1) {
+                throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched));
+            }
             if (matched != null) {
                 if (LOG.isTraceEnabled()) {
                     LOG.trace("Setting converted body: " + body);

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java?rev=1031258&r1=1031257&r2=1031258&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExplicitMethodAmbiguousTest.java Thu Nov  4 21:26:48 2010
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.bean;
 
+import java.io.ByteArrayInputStream;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.impl.JndiRegistry;
@@ -46,6 +48,16 @@ public class BeanExplicitMethodAmbiguous
         String out = template.requestBody("direct:bye", "Camel", String.class);
         assertEquals("Bye Camel", out);
     }
+    
+    public void testBeanExplicitMethodInvocationStringBody() throws Exception {
+        String out = template.requestBody("direct:foo", "Camel", String.class);
+        assertEquals("String", out);
+    }
+    
+    public void testBeanExplicitMethodInvocationInputStreamBody() throws Exception {
+        String out = template.requestBody("direct:foo", new ByteArrayInputStream("Camel".getBytes()), String.class);
+        assertEquals("InputStream", out);
+    }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -55,6 +67,8 @@ public class BeanExplicitMethodAmbiguous
                 from("direct:hello").beanRef("dummy", "hello");
 
                 from("direct:bye").beanRef("dummy");
+                
+                from("direct:foo").beanRef("dummy", "bar");
             }
         };
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyDummyBean.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyDummyBean.java?rev=1031258&r1=1031257&r2=1031258&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyDummyBean.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyDummyBean.java Thu Nov  4 21:26:48 2010
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.bean;
 
+import java.io.InputStream;
+import java.io.Reader;
+
 import org.apache.camel.Handler;
 
 /**
@@ -35,4 +38,16 @@ public class MyDummyBean {
     public String bye(String s) {
         return "Bye " + s;
     }
+    
+    public String bar(String s) {
+        return "String";
+    }
+    
+    public String bar(Reader s) {
+        return "Reader";
+    }
+    
+    public String bar(InputStream s) {
+        return "InputStream";
+    }
 }