gnodet-bot commented on code in PR #30592:
URL: https://github.com/apache/camel/pull/30592#discussion_r4657513224


##########
dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderKamelet.java:
##########
@@ -109,6 +134,131 @@ public void loadRouteTemplate(Resource resource) {
         }
     }
 
+    @Override
+    public void onAddRouteDefinition(RouteDefinition routeDefinition) {
+        // the Kamelets of the route are created after all the routes, when 
the CamelContext is initialized: their
+        // dependencies are downloaded now, so the route can use their classes 
(CAMEL-27364)
+        if (downloader == null) {
+            return;
+        }
+        List<String> uris = new ArrayList<>();
+        if (routeDefinition.getInput() != null) {
+            uris.add(routeDefinition.getInput().getEndpointUri());
+        }
+        for (EndpointRequiredDefinition erd : 
ProcessorDefinitionHelper.filterTypeInOutputs(routeDefinition.getOutputs(),
+                EndpointRequiredDefinition.class)) {
+            uris.add(erd.getEndpointUri());
+        }
+        for (String uri : uris) {
+            String name = kameletName(uri);
+            if (name != null && kamelets.add(name)) {
+                try {
+                    Resource resource = resolveKamelet(name, 
kameletLocation(uri));
+                    if (resource != null) {
+                        LOG.debug("Downloading the dependencies of Kamelet: {} 
used by route: {}", name,
+                                routeDefinition.getRouteId());
+                        loadRouteTemplate(resource);
+                    }
+                } catch (Exception e) {
+                    // the dependencies are then downloaded when the Kamelet 
is loaded, which reports the error
+                    LOG.debug("Cannot download the dependencies of Kamelet: {} 
due to: {}", name, e.getMessage(), e);
+                }
+            }
+        }
+    }
+
+    @Override
+    public void onRemoveRouteDefinition(RouteDefinition routeDefinition) {
+        // noop
+    }
+
+    @Override
+    public void onAddRouteTemplateDefinition(RouteTemplateDefinition 
routeTemplateDefinition) {
+        // noop
+    }
+
+    @Override
+    public void onRemoveRouteTemplateDefinition(RouteTemplateDefinition 
routeTemplateDefinition) {
+        // noop
+    }
+
+    /**
+     * The name of the Kamelet of a kamelet: endpoint uri, or null when it is 
not one, it is kamelet:source or
+     * kamelet:sink (the template of a Kamelet), or the name is a placeholder 
or an expression.
+     */
+    static String kameletName(String uri) {
+        if (uri == null || !uri.startsWith("kamelet:")) {
+            return null;
+        }
+        String name = 
FileUtil.stripLeadingSeparator(uri.substring("kamelet:".length()));
+        int q = name.indexOf('?');
+        if (q >= 0) {
+            name = name.substring(0, q);
+        }
+        int slash = name.indexOf('/');
+        if (slash >= 0) {
+            // kamelet:name/routeId
+            name = name.substring(0, slash);
+        }
+        if (name.isEmpty() || Kamelet.SOURCE_ID.equals(name) || 
Kamelet.SINK_ID.equals(name)
+                || name.contains("{{") || name.contains("${")) {
+            return null;
+        }
+        return name;
+    }
+
+    /**
+     * The location of the Kamelets: the location option of the endpoint, 
otherwise the one of the kamelet component.
+     */
+    private String kameletLocation(String uri) {
+        int q = uri.indexOf('?');
+        if (q >= 0) {
+            try {
+                Map<String, Object> parameters = 
URISupport.parseQuery(uri.substring(q + 1));
+                Object loc = parameters.get(Kamelet.PARAM_LOCATION);
+                if (loc != null) {
+                    return loc.toString();
+                }
+            } catch (Exception e) {
+                // use the location of the component
+            }

Review Comment:
   💡 **Consistency:** The `onAddRouteDefinition()` catch block (around line 
107) logs at DEBUG when resolution fails. This `catch` in `kameletLocation()` 
silently swallows `URISupport.parseQuery` exceptions with only a comment. 
Consider adding a DEBUG log here too for consistency and debuggability — it 
would help trace query-string parsing failures when kamelet location resolution 
falls back to the component location.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to