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 2023/04/04 17:11:02 UTC

[camel] 01/01: CAMEL-19234: Lookup beans by name+type first, and fallback to name-and-convert, when resolving endpoint URI beans.

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

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

commit 23b6f02288d7a3302aefc9e0c38b611bf21b32c7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Apr 4 19:09:40 2023 +0200

    CAMEL-19234: Lookup beans by name+type first, and fallback to name-and-convert, when resolving endpoint URI beans.
---
 .../org/apache/camel/support/EndpointHelper.java   | 44 ++++++++++++----------
 1 file changed, 24 insertions(+), 20 deletions(-)

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 280022165aa..c37820caa3f 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
@@ -310,23 +310,17 @@ public final class EndpointHelper {
      * @throws NoSuchBeanException if object was not found in registry and <code>mandatory</code> is <code>true</code>.
      */
     public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type, boolean mandatory) {
+        Object answer = null;
         if (value.startsWith("#class:")) {
-            Object answer;
             try {
                 answer = createBean(context, value, type);
             } catch (Exception e) {
                 throw new NoSuchBeanException(value, e);
             }
-            if (mandatory && answer == null) {
-                throw new NoSuchBeanException(value);
-            }
-            return type.cast(answer);
         } else if (value.startsWith("#type:")) {
             try {
-                Object answer = null;
-
-                String valueNoHash = value.substring(6);
-                Class<?> clazz = context.getClassResolver().resolveMandatoryClass(valueNoHash);
+                value = value.substring(6);
+                Class<?> clazz = context.getClassResolver().resolveMandatoryClass(value);
                 Set<?> set = context.getRegistry().findByType(clazz);
                 if (set.size() == 1) {
                     answer = set.iterator().next();
@@ -334,25 +328,35 @@ public final class EndpointHelper {
                     throw new NoSuchBeanException(
                             value, "Found " + set.size() + " beans of type: " + clazz + ". Only 1 bean instance is supported.");
                 }
-                if (mandatory && answer == null) {
-                    throw new NoSuchBeanException(value);
-                }
-                if (answer != null) {
-                    answer = context.getTypeConverter().convertTo(type, answer);
-                }
-                return type.cast(answer);
             } catch (ClassNotFoundException e) {
                 throw new NoSuchBeanException(value, e);
             }
         } else {
-            String valueNoHash = value.replace("#bean:", "");
-            valueNoHash = valueNoHash.replace("#", "");
+            value = value.replace("#bean:", "");
+            value = value.replace("#", "");
+            // lookup first with type
+            answer = CamelContextHelper.lookup(context, value, type);
+            if (answer == null) {
+                // fallback to lookup by name
+                answer = CamelContextHelper.lookup(context, value);
+            }
+        }
+
+        if (mandatory && answer == null) {
+            if (type != null) {
+                throw new NoSuchBeanException(value, type.getTypeName());
+            } else {
+                throw new NoSuchBeanException(value);
+            }
+        }
+        if (answer != null) {
             if (mandatory) {
-                return CamelContextHelper.mandatoryLookupAndConvert(context, valueNoHash, type);
+                answer = CamelContextHelper.convertTo(context, type, answer);
             } else {
-                return CamelContextHelper.lookupAndConvert(context, valueNoHash, type);
+                answer = CamelContextHelper.tryConvertTo(context, type, answer);
             }
         }
+        return (T) answer;
     }
 
     private static <T> T createBean(CamelContext camelContext, String name, Class<T> type) throws Exception {