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


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelSendAction.java:
##########
@@ -386,4 +397,122 @@ 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();
+        this.pid = entry.getKey();
+        Path jsonFile = entry.getValue();
+
+        JsonObject connectionDetails = readConnectionDetails(jsonFile);
+
+        if (connectionDetails == null) {
+            printer().println("Could not read connection details from: " + 
jsonFile);
+            return 1;
+        }
+
+        String updatedEndpoint = updateEndpointWithServerInfo(endpoint, 
infraService, connectionDetails);
+
+        String originalEndpoint = this.endpoint;
+        this.endpoint = updatedEndpoint;
+
+        try {
+            Path outputFile = writeSendData();
+            showStatus(outputFile);
+            return 0;
+        } finally {
+            this.endpoint = originalEndpoint;
+        }
+    }
+
+    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);
+    }
+
+    private String updateEndpointWithServerInfo(String originalEndpoint, 
String infraService, JsonObject connectionDetails) {
+        if (originalEndpoint == null) {
+            if ("nats".equals(infraService)) {
+                return infraService + ":subject:test";
+            } else {
+                return infraService + ":default";
+            }
+        }
+
+        if (originalEndpoint.startsWith(infraService + ":")) {
+            String remainingPart = 
originalEndpoint.substring(infraService.length() + 1);
+
+            if (remainingPart.contains("?")) {
+                return originalEndpoint;
+            } else {
+                String serverAddress = (String) 
connectionDetails.get("getServiceAddress");
+                if (serverAddress != null) {
+                    return originalEndpoint + "?servers=" + serverAddress;

Review Comment:
   servers is not used for all the components, this should be dynamic, the json 
property should be used. For example, when running kafka
   ```
   {
     "brokers" : "localhost:9092",
     "getBootstrapServers" : "localhost:9092"
   }
   ```
   
   There is not `server` property but the `brokers` property can be used as is.
   
   `getBootstrapServers` is deprecated, and I think we can remove the 
deprecated ones for the next release.
   
   The `camel infra run ?` json should contain only properties that can be used 
in the corresponding Camel component.
   
   



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