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


##########
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");

Review Comment:
   why is it needed?



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