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


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java:
##########
@@ -386,4 +403,333 @@ 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;
+        }
+
+        this.endpoint = updateEndpointWithConnectionDetails(endpoint, 
infraService, connectionDetails);
+
+        return doCallLocal();
+    }
+
+    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 = StringHelper.before(originalEndpoint, ":");
+        String path = StringHelper.after(originalEndpoint, ":");
+
+        String pathBasedEndpoint = buildPathBasedEndpoint(infraService, 
scheme, path, connectionDetails);
+        if (pathBasedEndpoint != null) {
+            return pathBasedEndpoint;
+        }
+
+        Map<String, String> properties = buildPropertiesMap(infraService, 
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);
+    }
+
+    // Build properties map from infrastructure connection details.
+    private Map<String, String> buildPropertiesMap(String infraService, 
JsonObject connectionDetails) {
+        Map<String, String> properties = new LinkedHashMap<>();
+
+        addServiceSpecificProperties(infraService, connectionDetails, 
properties);
+
+        for (Map.Entry<String, Object> entry : connectionDetails.entrySet()) {
+            String key = entry.getKey();
+            Object value = entry.getValue();
+
+            if (key.startsWith("get") || key.startsWith("is")) {
+                continue;
+            }
+
+            if (value == null || value instanceof Map) {
+                continue;
+            }
+
+            if (properties.containsKey(key)) {
+                continue;
+            }
+
+            properties.put(key, String.valueOf(value));
+        }
+
+        if (properties.isEmpty()) {
+            for (Map.Entry<String, Object> entry : 
connectionDetails.entrySet()) {
+                String key = entry.getKey();
+                Object value = entry.getValue();
+
+                if (value == null || value instanceof Map) {
+                    continue;
+                }
+
+                String mappedName = mapDeprecatedPropertyName(key);
+                if (mappedName != null) {
+                    properties.put(mappedName, String.valueOf(value));
+                }
+            }
+        }
+
+        return properties;
+    }
+
+    // Add service-specific property mappings that require special handling.
+    private void addServiceSpecificProperties(
+            String infraService, JsonObject connectionDetails, Map<String, 
String> properties) {
+        switch (infraService) {
+            case "mosquitto" -> {

Review Comment:
   Would it be acceptable to include an integration test in this PR that uses 
Testcontainers to spin up real services and actually test the working of the 
command ? We can skip it by default in the CI but it can be run locally during 
development / making changes. What do you think ? 



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