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/24 10:12:49 UTC

[camel] 09/27: 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 master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6113385992353e4311c64df11e1ffb1c9430dd1d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 23 09:57:29 2019 +0200

    CAMEL-13557: Add property binding support to make it convenient to configure components and whatnot.
---
 .../camel/support/PropertyBindingSupportTest.java    | 20 +++++++++++++++++++-
 .../apache/camel/support/PropertyBindingSupport.java |  8 ++++++--
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
index b3f32c7..bce0ad0 100644
--- a/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/support/PropertyBindingSupportTest.java
@@ -107,6 +107,24 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
     }
 
     @Test
+    public void testNestedReferenceId() throws Exception {
+        Foo foo = new Foo();
+
+        PropertyBindingSupport.bindProperty(context, foo, "name", "James");
+        PropertyBindingSupport.bindProperty(context, foo, "bar.age", "33");
+        PropertyBindingSupport.bindProperty(context, foo, "bar.gold-customer", "true");
+        PropertyBindingSupport.bindProperty(context, foo, "bar.rider", "true");
+        PropertyBindingSupport.bindProperty(context, foo, "bar.work", "#id:myWork");
+
+        assertEquals("James", foo.getName());
+        assertEquals(33, foo.getBar().getAge());
+        assertTrue(foo.getBar().isRider());
+        assertTrue(foo.getBar().isGoldCustomer());
+        assertEquals(456, foo.getBar().getWork().getId());
+        assertEquals("Acme", foo.getBar().getWork().getName());
+    }
+
+    @Test
     public void testNestedType() throws Exception {
         Foo foo = new Foo();
 
@@ -132,7 +150,7 @@ public class PropertyBindingSupportTest extends ContextTestSupport {
         PropertyBindingSupport.bindProperty(context, foo, "bar.age", "33");
         PropertyBindingSupport.bindProperty(context, foo, "bar.{{committer}}", "true");
         PropertyBindingSupport.bindProperty(context, foo, "bar.gold-customer", "true");
-        PropertyBindingSupport.bindProperty(context, foo, "bar.work", "class:org.apache.camel.support.Company");
+        PropertyBindingSupport.bindProperty(context, foo, "bar.work", "#class:org.apache.camel.support.Company");
 
         assertEquals("James", foo.getName());
         assertEquals(33, foo.getBar().getAge());
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 5d06df4..6765233 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
@@ -32,7 +32,7 @@ import static org.apache.camel.support.IntrospectionSupport.getOrElseProperty;
  * <ul>
  *     <li>property placeholders - Keys and values using Camels property placeholder will be resolved</li>
  *     <li>nested - Properties can be nested using the dot syntax (OGNL and builder pattern using with as prefix), eg foo.bar=123</li>
- *     <li>reference by id - Values can refer to other beans in the registry by prefixing with # syntax, eg #myBean</li>
+ *     <li>reference by id - Values can refer to other beans in the registry by prefixing with #id: or # syntax, eg #id:myBean or #myBean</li>
  *     <li>reference by type - Values can refer to singleton beans by their type in the registry by prefixing with #type: syntax, eg #type:com.foo.MyClassType</li>
  *     <li>new class - Values can refer to creating new beans by their class name syntax, eg class:com.foo.MyClassType</li>
  * </ul>
@@ -150,7 +150,7 @@ public final class PropertyBindingSupport {
         }
 
         if (value instanceof String) {
-            if (value.toString().startsWith("class:")) {
+            if (value.toString().startsWith("#class:")) {
                 // its a new class to be created
                 String className = value.toString().substring(6);
                 Class<?> type = context.getClassResolver().resolveMandatoryClass(className);
@@ -167,6 +167,10 @@ public final class PropertyBindingSupport {
                         value = types.iterator().next();
                     }
                 }
+            } else if (value.toString().startsWith("#id:")) {
+                // okay its a reference so swap to lookup this which is already supported in IntrospectionSupport
+                refName = ((String) value).substring(4);
+                value = null;
             } else if (EndpointHelper.isReferenceParameter(value.toString())) {
                 // okay its a reference so swap to lookup this which is already supported in IntrospectionSupport
                 refName = value.toString();