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 2019/05/23 19:30:59 UTC

[camel] 13/18: CAMEL-13557: Add property binding support to make it convenient to configure components and whatnot.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch 13557
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 05f40f2d6c24e3b9ffbfb75ded02eb90ffd88421
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 23 14:05:31 2019 +0200

    CAMEL-13557: Add property binding support to make it convenient to configure components and whatnot.
---
 .../file/FileProducerChmodOptionTest.java          |  9 +++++---
 .../DefaultComponentReferencePropertiesTest.java   |  4 ++--
 .../org/apache/camel/support/EndpointHelper.java   |  4 +++-
 .../apache/camel/support/IntrospectionSupport.java | 16 ++++++++++++--
 .../camel/support/PropertyBindingSupport.java      | 25 +++++++++++++++-------
 5 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
index 3a018d6..31c8ceb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerChmodOptionTest.java
@@ -26,6 +26,8 @@ import java.util.Set;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.PropertyBindingException;
+import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.Before;
@@ -99,9 +101,10 @@ public class FileProducerChmodOptionTest extends ContextTestSupport {
                 }
             });
             fail("Expected FailedToCreateRouteException");
-        } catch (Exception e) {
-            assertTrue("Expected FailedToCreateRouteException, was " + e.getClass().getCanonicalName(), e instanceof FailedToCreateRouteException);
-            assertTrue("Message was [" + e.getMessage() + "]", e.getMessage().endsWith("conversion possible: chmod option [abc] is not valid"));
+        } catch (FailedToCreateRouteException e) {
+            assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            PropertyBindingException pbe = assertIsInstanceOf(PropertyBindingException.class, e.getCause().getCause());
+            assertEquals("chmod", pbe.getPropertyName());
         }
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
index ecd1b6b..0ab463e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentReferencePropertiesTest.java
@@ -199,8 +199,8 @@ public class DefaultComponentReferencePropertiesTest extends ContextTestSupport
         MyComponent component = new MyComponent(context);
         try {
             component.createEndpoint("foo://?special=#dummy");
-            fail("Should have throw a IllegalArgumentException");
-        } catch (IllegalArgumentException e) {
+            fail("Should have throw a ResolveEndpointFailedException");
+        } catch (ResolveEndpointFailedException e) {
             // ok
         }
     }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
index d690bdd..adc82cd 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
@@ -157,7 +157,9 @@ public final class EndpointHelper {
      * @throws Exception is thrown if setting property fails
      */
     public static void setProperties(CamelContext context, Object bean, Map<String, Object> parameters) throws Exception {
-        IntrospectionSupport.setProperties(context.getTypeConverter(), bean, parameters);
+         // TODO: Use more advanced bindingDefaultComponentReferencePropertiesTes
+         PropertyBindingSupport.bindProperties(context, bean, parameters);
+         //IntrospectionSupport.setProperties(context.getTypeConverter(), bean, parameters);
     }
 
     /**
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java
index 3585940..01f8eee 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/IntrospectionSupport.java
@@ -539,8 +539,14 @@ public final class IntrospectionSupport {
 
         // loop and execute the best setter method
         Exception typeConversionFailed = null;
-        for (Method setter : setters) {
+        Method stringSetterMethod = null;
+        Iterator<Method> it = setters.iterator();
+        while (it.hasNext()) {
+            Method setter = it.next();
             Class<?> parameterType = setter.getParameterTypes()[0];
+            if (parameterType.getName().equals("java.lang.String")) {
+                stringSetterMethod = setter;
+            }
             Object ref = value;
             // try and lookup the reference based on the method
             if (context != null && refName != null && ref == null) {
@@ -548,7 +554,13 @@ public final class IntrospectionSupport {
                 ref = CamelContextHelper.lookup(context, s);
                 if (ref == null) {
                     // try the next method if nothing was found
-                    continue;
+                    // if we did not found a good candidate then fallback to use the string setter (if possible) with the actual ref name value as-is
+                    if (!it.hasNext() && stringSetterMethod != null) {
+                        setter = stringSetterMethod;
+                        ref = refName;
+                    } else {
+                        continue;
+                    }
                 } else {
                     // setter method has not the correct type
                     // (must use ObjectHelper.isAssignableFrom which takes primitive types into account)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index f336d75..02c24c1 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -17,6 +17,7 @@
 package org.apache.camel.support;
 
 import java.lang.reflect.Method;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
@@ -132,20 +133,28 @@ public final class PropertyBindingSupport {
     }
 
     /**
-     * Binds the properties to the target object.
+     * Binds the properties to the target object, and removes the property that was bound from properties.
      *
      * @param camelContext  the camel context
      * @param target        the target object
-     * @param properties    the properties
-     * @return              true if all the properties was bound, false otherwise
+     * @param properties    the properties where the bound properties will be removed from
+     * @return              true if one or more properties was bound
      */
     public static boolean bindProperties(CamelContext camelContext, Object target, Map<String, Object> properties) {
-        boolean answer = true;
-        for (Map.Entry<String, Object> entry : properties.entrySet()) {
-            answer &= bindProperty(camelContext, target, entry.getKey(), entry.getValue());
+        org.apache.camel.util.ObjectHelper.notNull(target, "target");
+        org.apache.camel.util.ObjectHelper.notNull(properties, "properties");
+        boolean rc = false;
+
+        for (Iterator<Map.Entry<String, Object>> iter = properties.entrySet().iterator(); iter.hasNext();) {
+            Map.Entry<String, Object> entry = iter.next();
+            if (bindProperty(camelContext, target, entry.getKey(), entry.getValue())) {
+                iter.remove();
+                rc = true;
+            }
         }
-        return answer;
-    }
+
+        return rc;
+   }
 
     /**
      * Binds the property to the target object.