pkalsi97 commented on code in PR #20767:
URL: https://github.com/apache/camel/pull/20767#discussion_r2719888358


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java:
##########
@@ -386,4 +403,245 @@ private String getStatus(JsonObject r) {
         }
     }
 
+    private Integer doCallInfra(String infraService) throws Exception {
+        Map<Long, Path> infraPids = findInfraPids(infraService);
+
+        if (infraPids.isEmpty()) {
+            printer().println("No running infrastructure service found for: " 
+ infraService);
+            return 1;
+        }
+
+        if (infraPids.size() > 1) {
+            printer().println("Multiple running infrastructure services found 
for: " + infraService +
+                              ". Found " + infraPids.size() + " services.");
+            return 1;
+        }
+
+        Map.Entry<Long, Path> entry = infraPids.entrySet().iterator().next();
+        Path jsonFile = entry.getValue();
+
+        JsonObject connectionDetails = readConnectionDetails(jsonFile);
+
+        if (connectionDetails == null) {
+            printer().println("Could not read connection details from: " + 
jsonFile);
+            return 1;
+        }
+
+        String scheme = extractScheme(endpoint, infraService);
+
+        this.endpoint = updateEndpointWithConnectionDetails(endpoint, 
infraService, connectionDetails);
+
+        Map<String, String> componentProps = buildComponentProperties(scheme, 
connectionDetails);
+
+        Map<String, String> beanProps = buildBeanProperties(connectionDetails);
+
+        int additionalCount = componentProps.size() + beanProps.size();
+        if (additionalCount > 0) {
+            int existingCount = (property != null) ? property.length : 0;
+            String[] merged = new String[existingCount + additionalCount];
+            if (property != null) {
+                System.arraycopy(property, 0, merged, 0, existingCount);
+            }
+            int i = existingCount;
+            for (Map.Entry<String, String> compEntry : 
componentProps.entrySet()) {
+                merged[i++] = "camel.component." + scheme + "." + 
compEntry.getKey() + "=" + compEntry.getValue();
+            }
+            for (Map.Entry<String, String> beanEntry : beanProps.entrySet()) {
+                merged[i++] = beanEntry.getKey() + "=" + beanEntry.getValue();
+            }
+            property = merged;
+        }
+
+        return doCallLocal();
+    }
+
+    private String extractScheme(String endpoint, String infraService) {
+        if (endpoint != null && endpoint.contains(":")) {
+            return StringHelper.before(endpoint, ":");
+        }
+        return infraService;
+    }
+
+    private Map<Long, Path> findInfraPids(String serviceName) throws Exception 
{
+        Map<Long, Path> pids = new HashMap<>();
+
+        Path camelDir = CommandLineHelper.getCamelDir();
+
+        try (Stream<Path> files = Files.list(camelDir)) {
+            List<Path> pidFiles = files
+                    .filter(p -> {
+                        String fileName = p.getFileName().toString();
+                        return fileName.startsWith("infra-") && 
fileName.endsWith(".json");
+                    })
+                    .collect(Collectors.toList());
+
+            for (Path pidFile : pidFiles) {
+                String fileName = pidFile.getFileName().toString();
+                // Format: infra-{service}-{pid}.json
+                String withoutPrefix = fileName.substring("infra-".length());
+                String withoutExtension = withoutPrefix.substring(0, 
withoutPrefix.lastIndexOf(".json"));
+
+                int lastDashIndex = withoutExtension.lastIndexOf('-');
+                if (lastDashIndex > 0) {
+                    String service = withoutExtension.substring(0, 
lastDashIndex);
+                    String pidStr = withoutExtension.substring(lastDashIndex + 
1);
+
+                    if (service.equals(serviceName)) {
+                        try {
+                            long pid = Long.parseLong(pidStr);
+                            pids.put(pid, pidFile);
+                        } catch (NumberFormatException e) {
+                            // Skip invalid PID
+                        }
+                    }
+                }
+            }
+        }
+
+        return pids;
+    }
+
+    private JsonObject readConnectionDetails(Path jsonFile) throws Exception {
+        String content = Files.readString(jsonFile);
+        return (JsonObject) Jsoner.deserialize(content);
+    }
+
+    String updateEndpointWithConnectionDetails(
+            String originalEndpoint, String infraService, JsonObject 
connectionDetails) {
+
+        if (originalEndpoint == null) {
+            originalEndpoint = infraService + ":default";
+        }
+
+        if (originalEndpoint.contains("?")) {
+            return originalEndpoint;
+        }
+
+        String scheme;
+        String path;
+        if (originalEndpoint.contains(":")) {
+            scheme = StringHelper.before(originalEndpoint, ":");
+            path = StringHelper.after(originalEndpoint, ":");
+        } else {
+            scheme = originalEndpoint;
+            path = null;
+        }
+
+        String pathBasedEndpoint = buildPathBasedEndpoint(path, 
connectionDetails);
+        if (pathBasedEndpoint != null) {
+            return pathBasedEndpoint;
+        }
+
+        Map<String, String> properties = buildPropertiesMap(scheme, 
connectionDetails);
+
+        try {
+            String uri = catalog.asEndpointUri(scheme, properties, true);
+            if (uri != null && !uri.isEmpty()) {
+                String queryPart = "";
+                if (uri.contains("?")) {
+                    queryPart = "?" + StringHelper.after(uri, "?");
+                }
+
+                if (path != null && !path.isEmpty()) {
+                    return scheme + ":" + path + queryPart;
+                } else {
+                    return uri;
+                }
+            }
+        } catch (URISyntaxException e) {
+            // Fall back to manual construction if catalog fails
+        }
+
+        return buildEndpointManually(originalEndpoint, properties);
+    }
+
+    private Map<String, String> buildBeanProperties(JsonObject 
connectionDetails) {
+        Map<String, String> properties = new LinkedHashMap<>();
+
+        Object beanProps = connectionDetails.get("beanProperties");
+        if (beanProps instanceof Map<?, ?> beanMap) {
+            for (Map.Entry<?, ?> entry : beanMap.entrySet()) {
+                if (entry.getKey() != null && entry.getValue() != null) {
+                    properties.put(String.valueOf(entry.getKey()), 
String.valueOf(entry.getValue()));
+                }
+            }
+        }
+
+        return properties;
+    }
+
+    private Map<String, String> buildComponentProperties(String scheme, 
JsonObject connectionDetails) {
+        Map<String, String> properties = new LinkedHashMap<>();
+
+        var componentModel = catalog.componentModel(scheme);
+        if (componentModel != null) {
+            componentModel.getComponentOptions().stream()
+                    .map(opt -> opt.getName())
+                    .filter(name -> isValidConnectionProperty(name, 
connectionDetails))
+                    .forEach(name -> properties.put(name, 
String.valueOf(connectionDetails.get(name))));
+        }
+
+        return properties;
+    }
+
+    private Map<String, String> buildPropertiesMap(String scheme, JsonObject 
connectionDetails) {
+        Map<String, String> properties = new LinkedHashMap<>();
+
+        var componentModel = catalog.componentModel(scheme);
+        if (componentModel != null) {
+            componentModel.getEndpointOptions().stream()
+                    .map(opt -> opt.getName())
+                    .filter(name -> isValidConnectionProperty(name, 
connectionDetails))
+                    .forEach(name -> properties.put(name, 
String.valueOf(connectionDetails.get(name))));
+        }
+
+        return properties;
+    }
+
+    private boolean isValidConnectionProperty(String name, JsonObject 
connectionDetails) {
+        if (!connectionDetails.containsKey(name)) {
+            return false;
+        }
+        Object value = connectionDetails.get(name);
+        return value != null && !(value instanceof Map);
+    }
+
+    private String buildPathBasedEndpoint(String path, JsonObject 
connectionDetails) {
+
+        Object endpointUri = connectionDetails.get("endpointUri");

Review Comment:
   I implemented this method to handle infra like FTP, SMB, ZooKeeper etc where 
the connection details (host:port) and the target resource file path are fused 
into a single URI eg `ftp://localhost:2121/myDir`
   
   If a user tries to override the target path, i came to an understanding that 
we would have no clean way to retain the host/port, we would loose the 
connection. This method safely appends the user's requested path and ensures we 
can switch targets dynamically without loosing the correct infra connection 
settings.



-- 
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