davsclaus commented on code in PR #29747:
URL: https://github.com/apache/camel/pull/29747#discussion_r4555822602
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java:
##########
@@ -402,6 +403,45 @@ static void restoreProperties(Map<String, String>
replacedProperties) {
});
}
+ /**
+ * Adds the name and id of the container a service runs in, so that {@code
docker exec}, {@code docker logs} and
+ * {@code camel infra ps} can tell which container it is. A service that
does not run in a container, such as an
+ * embedded broker, has none.
+ *
+ * @param service the started service
+ * @param properties the service data to add the container details to
+ */
+ static void addContainerDetails(Object service, Map<String, Object>
properties) {
+ Object container;
+ try {
+ container =
service.getClass().getMethod("getContainer").invoke(service);
+ } catch (Exception e) {
+ // not a container service
+ return;
+ }
+ if (container == null) {
+ return;
+ }
+ String name = (String) invokeQuietly(container, "getContainerName");
+ if (name != null && !name.isBlank()) {
+ // Docker answers the name with a leading slash
+ properties.put("containerName", name.startsWith("/") ?
name.substring(1) : name);
+ }
+ String id = (String) invokeQuietly(container, "getContainerId");
+ if (id != null && !id.isBlank()) {
+ // the short id, as docker ps shows it
+ properties.put("containerId", id.length() > 12 ? id.substring(0,
12) : id);
+ }
+ }
+
+ private static Object invokeQuietly(Object target, String methodName) {
+ try {
+ return target.getClass().getMethod(methodName).invoke(target);
+ } catch (Exception e) {
+ return null;
+ }
+ }
Review Comment:
Done in bbc724db99b3: `InvocationTargetException` (the service has the
method but it failed, such as when docker cannot be reached) is now logged at
DEBUG with its cause, and any other exception still means "not a container
service" and returns null quietly. I used imports and a `LOG` field rather than
the fully qualified name in the suggestion, as the project conventions ask.
_Claude Code on behalf of davsclaus_
--
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]