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


The following commit(s) were added to refs/heads/master by this push:
     new 5273249  CAMEL-13557: Add property binding support to make it 
convenient to configure components and whatnot.
5273249 is described below

commit 52732499f9bb828838bb4010e2b38a2e593b38e5
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu May 30 15:40:18 2019 +0200

    CAMEL-13557: Add property binding support to make it convenient to 
configure components and whatnot.
---
 .../camel/reifier/rest/RestBindingReifier.java     |  6 +--
 .../org/apache/camel/support/DefaultComponent.java |  4 --
 .../org/apache/camel/support/DefaultEndpoint.java  |  3 --
 .../org/apache/camel/support/EndpointHelper.java   | 42 -------------------
 .../apache/camel/support/IntrospectionSupport.java |  6 ++-
 .../camel/support/PropertyBindingSupport.java      | 49 ++++++++++++++++++----
 6 files changed, 48 insertions(+), 62 deletions(-)

diff --git 
a/core/camel-core/src/main/java/org/apache/camel/reifier/rest/RestBindingReifier.java
 
b/core/camel-core/src/main/java/org/apache/camel/reifier/rest/RestBindingReifier.java
index b088f4f..e2788d0 100644
--- 
a/core/camel-core/src/main/java/org/apache/camel/reifier/rest/RestBindingReifier.java
+++ 
b/core/camel-core/src/main/java/org/apache/camel/reifier/rest/RestBindingReifier.java
@@ -27,8 +27,8 @@ import org.apache.camel.processor.RestBindingAdvice;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spi.RouteContext;
-import org.apache.camel.support.EndpointHelper;
 import org.apache.camel.support.IntrospectionSupport;
+import org.apache.camel.support.PropertyBindingSupport;
 
 public class RestBindingReifier {
 
@@ -200,9 +200,7 @@ public class RestBindingReifier {
                 }
             }
 
-            // set reference properties first as they use # syntax that fools 
the regular properties setter
-            EndpointHelper.setReferenceProperties(context, dataFormat, copy);
-            EndpointHelper.setProperties(context, dataFormat, copy);
+            PropertyBindingSupport.bindProperties(context, dataFormat, copy);
         }
     }
 
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
index d6f1583..00819bc 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
@@ -311,10 +311,6 @@ public abstract class DefaultComponent extends 
ServiceSupport implements Compone
      * @param parameters    properties to set
      */
     protected void setProperties(CamelContext camelContext, Object bean, 
Map<String, Object> parameters) throws Exception {
-        // set reference properties first as they use # syntax that fools the 
regular properties setter
-        // TODO: We should find out the weird camel-cxf error where we need to 
do this, as we can put this logic into PropertyBindingSupport
-        EndpointHelper.setReferenceProperties(camelContext, bean, parameters);
-
         if (basicPropertyBinding) {
             // use basic binding
             PropertyBindingSupport.build()
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
index aadc56b..cb11ab0 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
@@ -379,9 +379,6 @@ public abstract class DefaultEndpoint extends 
ServiceSupport implements Endpoint
      * @param parameters  properties to set
      */
     protected void setProperties(Object bean, Map<String, Object> parameters) 
throws Exception {
-        // set reference properties first as they use # syntax that fools the 
regular properties setter
-        EndpointHelper.setReferenceProperties(camelContext, bean, parameters);
-
         if (basicPropertyBinding) {
             // use basic binding
             PropertyBindingSupport.build()
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 c19c240..f4b973c 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
@@ -29,13 +29,10 @@ import org.apache.camel.DelegateEndpoint;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
-import org.apache.camel.Message;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Processor;
 import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.Route;
-import org.apache.camel.spi.BrowsableEndpoint;
-
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.util.StringHelper;
 import org.apache.camel.util.URISupport;
@@ -358,45 +355,6 @@ public final class EndpointHelper {
     }
 
     /**
-     * Browses the {@link BrowsableEndpoint} within the given range, and 
returns the messages as a XML payload.
-     *
-     * @param endpoint    the browsable endpoint
-     * @param fromIndex   from range
-     * @param toIndex     to range
-     * @param includeBody whether to include the message body in the XML 
payload
-     * @return XML payload with the messages
-     * @throws IllegalArgumentException if the from and to range is invalid
-     * @see MessageHelper#dumpAsXml(org.apache.camel.Message)
-     */
-    public static String browseRangeMessagesAsXml(BrowsableEndpoint endpoint, 
Integer fromIndex, Integer toIndex, Boolean includeBody) {
-        if (fromIndex == null) {
-            fromIndex = 0;
-        }
-        if (toIndex == null) {
-            toIndex = Integer.MAX_VALUE;
-        }
-        if (fromIndex > toIndex) {
-            throw new IllegalArgumentException("From index cannot be larger 
than to index, was: " + fromIndex + " > " + toIndex);
-        }
-
-        List<Exchange> exchanges = endpoint.getExchanges();
-        if (exchanges.size() == 0) {
-            return null;
-        }
-
-        StringBuilder sb = new StringBuilder();
-        sb.append("<messages>");
-        for (int i = fromIndex; i < exchanges.size() && i <= toIndex; i++) {
-            Exchange exchange = exchanges.get(i);
-            Message msg = exchange.hasOut() ? exchange.getOut() : 
exchange.getIn();
-            String xml = MessageHelper.dumpAsXml(msg, includeBody);
-            sb.append("\n").append(xml);
-        }
-        sb.append("\n</messages>");
-        return sb.toString();
-    }
-
-    /**
      * Attempts to resolve if the url has an <tt>exchangePattern</tt> option 
configured
      *
      * @param url the url
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 05893a7..c2770e3 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
@@ -570,14 +570,16 @@ public final class IntrospectionSupport {
             if (obj instanceof Map) {
                 Map map = (Map) obj;
                 if (context != null && refName != null && value == null) {
-                    value = CamelContextHelper.lookup(context, refName);
+                    String s = StringHelper.replaceAll(refName, "#", "");
+                    value = CamelContextHelper.lookup(context, s);
                 }
                 map.put(lookupKey, value);
                 return true;
             } else if (obj instanceof List) {
                 List list = (List) obj;
                 if (context != null && refName != null && value == null) {
-                    value = CamelContextHelper.lookup(context, refName);
+                    String s = StringHelper.replaceAll(refName, "#", "");
+                    value = CamelContextHelper.lookup(context, s);
                 }
                 if (isNotEmpty(lookupKey)) {
                     int idx = Integer.valueOf(lookupKey);
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 9741b80..468ef3e 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
@@ -23,15 +23,12 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.PropertyBindingException;
 
 import static org.apache.camel.support.IntrospectionSupport.findSetterMethods;
 import static org.apache.camel.util.ObjectHelper.isNotEmpty;
-import static org.apache.camel.util.StringHelper.notEmpty;
 
 /**
  * A convenient support class for binding String valued properties to an 
instance which
@@ -42,7 +39,7 @@ import static org.apache.camel.util.StringHelper.notEmpty;
  *     <li>map</li> - Properties can lookup in Map's using map syntax, eg 
foo[bar] where foo is the name of the property that is a Map instance, and bar 
is the name of the key.</li>
  *     <li>list</li> - Properties can refer or add to in List's using list 
syntax, eg foo[0] where foo is the name of the property that is a
  *                     List instance, and 0 is the index. To refer to the last 
element, then use last as key.</li>
- *     <li>reference by bean id - Values can refer to other beans in the 
registry by prefixing with #nean: eg #bean:myBean</li>
+ *     <li>reference by bean id - Values can refer to other beans in the 
registry by prefixing with with # or #bean: eg #myBean or #bean: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>autowire by type - Values can refer to singleton beans by auto 
wiring by setting the value to #autowired</li>
  *     <li>reference new class - Values can refer to creating new beans by 
their class name by prefixing with #class, eg #class:com.foo.MyClassType</li>
@@ -51,8 +48,6 @@ import static org.apache.camel.util.StringHelper.notEmpty;
  */
 public final class PropertyBindingSupport {
 
-    // TODO: Add support for List in keys
-
     /**
      * To use a fluent builder style to configure this property binding 
support.
      */
@@ -287,6 +282,9 @@ public final class PropertyBindingSupport {
         org.apache.camel.util.ObjectHelper.notNull(properties, "properties");
         boolean rc = false;
 
+        // must set reference parameters first before the other bindings
+        setReferenceProperties(camelContext, target, properties);
+
         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(), nesting, deepNesting, fluentBuilder, reference, placeholder)) 
{
@@ -439,7 +437,7 @@ public final class PropertyBindingSupport {
                 }
             } else if (value.toString().startsWith("#bean:")) {
                 // okay its a reference so swap to lookup this which is 
already supported in IntrospectionSupport
-                refName = ((String) value).substring(6);
+                refName = "#" + ((String) value).substring(6);
                 value = null;
             }
         }
@@ -514,4 +512,41 @@ public final class PropertyBindingSupport {
         return type != null && !type.isPrimitive() && 
!type.getName().startsWith("java");
     }
 
+    private static void setReferenceProperties(CamelContext context, Object 
target, Map<String, Object> parameters) {
+        Iterator<Map.Entry<String, Object>> it = 
parameters.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry<String, Object> entry = it.next();
+            String name = entry.getKey();
+
+            // we only support basic keys
+            if (name.contains(".") || name.contains("[") || 
name.contains("]")) {
+                continue;
+            }
+
+            Object v = entry.getValue();
+            String value = v != null ? v.toString() : null;
+            if (isReferenceParameter(value)) {
+                try {
+                    boolean hit = IntrospectionSupport.setProperty(context, 
context.getTypeConverter(), target, name, null, value, true);
+                    if (hit) {
+                        // must remove as its a valid option and we could 
configure it
+                        it.remove();
+                    }
+                } catch (Exception e) {
+                    throw new PropertyBindingException(target, e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Is the given parameter a reference parameter (starting with a # char)
+     *
+     * @param parameter the parameter
+     * @return <tt>true</tt> if its a reference parameter
+     */
+    private static boolean isReferenceParameter(String parameter) {
+        return parameter != null && parameter.trim().startsWith("#");
+    }
+
 }

Reply via email to