You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2021/02/11 12:55:28 UTC

[camel] branch master updated: core: tidy up PropertyBindingSupport::resolveBean

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

lburgazzoli 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 c7b64ff  core: tidy up PropertyBindingSupport::resolveBean
c7b64ff is described below

commit c7b64ff477c3a931f4fca5d74f40893c7f46df67
Author: Luca Burgazzoli <lb...@gmail.com>
AuthorDate: Thu Feb 11 11:58:37 2021 +0100

    core: tidy up PropertyBindingSupport::resolveBean
    
    - remove the name parameter as it was not used
    - ensure the value is a string instead of rely to .toString() as it may
      produce false results if the .toString() method is crafted to generate
      a value starting with #bean:, #class:, #type:
---
 .../org/apache/camel/main/BaseMainSupport.java     |  2 +-
 .../camel/support/PropertyBindingSupport.java      | 47 +++++++++++++---------
 2 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index afe03d9..21768f2 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -1013,7 +1013,7 @@ public abstract class BaseMainSupport extends BaseService {
             if (key.indexOf('.') == -1) {
                 String name = key;
                 Object value = properties.remove(key);
-                Object bean = PropertyBindingSupport.resolveBean(camelContext, name, value);
+                Object bean = PropertyBindingSupport.resolveBean(camelContext, value);
                 if (bean == null) {
                     throw new IllegalArgumentException(
                             "Cannot create/resolve bean with name " + name + " from value: " + value);
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 5401b7e..aeee7e3 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
@@ -794,7 +794,7 @@ public final class PropertyBindingSupport {
                 value = resolveAutowired(context, target, name, value, ignoreCase, fluentBuilder, allowPrivateSetter,
                         reflection, configurer);
             } else {
-                value = resolveBean(context, name, value);
+                value = resolveBean(context, value);
             }
         }
         return value;
@@ -1442,19 +1442,26 @@ public final class PropertyBindingSupport {
      * Resolves the value as either a class, type or bean.
      *
      * @param  camelContext the camel context
-     * @param  name         the name of the bean
      * @param  value        how to resolve the bean with a prefix of either #class:, #type: or #bean:
      * @return              the resolve bean
      * @throws Exception    is thrown if error resolving the bean, or if the value is invalid.
      */
-    public static Object resolveBean(CamelContext camelContext, String name, Object value) throws Exception {
+    public static Object resolveBean(CamelContext camelContext, Object value) throws Exception {
+        if (!(value instanceof String)) {
+            return value;
+        }
+
+        String strval = (String) value;
+        Object answer = value;
+
         // resolve placeholders
-        if (value != null) {
-            value = camelContext.resolvePropertyPlaceholders(value.toString());
+        if (strval != null) {
+            strval = camelContext.resolvePropertyPlaceholders(strval);
         }
-        if (value.toString().startsWith("#class:")) {
+
+        if (strval.startsWith("#class:")) {
             // its a new class to be created
-            String className = value.toString().substring(7);
+            String className = strval.substring(7);
             String factoryMethod = null;
             String parameters = null;
             if (className.endsWith(")") && className.indexOf('(') != -1) {
@@ -1470,30 +1477,30 @@ public final class PropertyBindingSupport {
             if (factoryMethod != null) {
                 if (parameters != null) {
                     // special to support factory method parameters
-                    value = newInstanceFactoryParameters(camelContext, type, factoryMethod, parameters);
+                    answer = newInstanceFactoryParameters(camelContext, type, factoryMethod, parameters);
                 } else {
-                    value = camelContext.getInjector().newInstance(type, factoryMethod);
+                    answer = camelContext.getInjector().newInstance(type, factoryMethod);
                 }
-                if (value == null) {
+                if (answer == null) {
                     throw new IllegalStateException(
                             "Cannot create bean instance using factory method: " + className + "#" + factoryMethod);
                 }
             } else if (parameters != null) {
                 // special to support constructor parameters
-                value = newInstanceConstructorParameters(camelContext, type, parameters);
+                answer = newInstanceConstructorParameters(camelContext, type, parameters);
             } else {
-                value = camelContext.getInjector().newInstance(type);
+                answer = camelContext.getInjector().newInstance(type);
             }
-            if (value == null) {
+            if (answer == null) {
                 throw new IllegalStateException("Cannot create instance of class: " + className);
             }
-        } else if (value.toString().startsWith("#type:")) {
+        } else if (strval.startsWith("#type:")) {
             // its reference by type, so lookup the actual value and use it if there is only one instance in the registry
-            String typeName = value.toString().substring(6);
+            String typeName = strval.substring(6);
             Class<?> type = camelContext.getClassResolver().resolveMandatoryClass(typeName);
             Set<?> types = camelContext.getRegistry().findByType(type);
             if (types.size() == 1) {
-                value = types.iterator().next();
+                answer = types.iterator().next();
             } else if (types.size() > 1) {
                 throw new IllegalStateException(
                         "Cannot select single type: " + typeName + " as there are " + types.size()
@@ -1502,12 +1509,12 @@ public final class PropertyBindingSupport {
                 throw new IllegalStateException(
                         "Cannot select single type: " + typeName + " as there are no beans in the registry with this type");
             }
-        } else if (value.toString().startsWith("#bean:")) {
-            String key = value.toString().substring(6);
-            value = camelContext.getRegistry().lookupByName(key);
+        } else if (strval.startsWith("#bean:")) {
+            String key = strval.substring(6);
+            answer = camelContext.getRegistry().lookupByName(key);
         }
 
-        return value;
+        return answer;
     }
 
     private static String undashKey(String key) {