gnodet-bot commented on code in PR #29747:
URL: https://github.com/apache/camel/pull/29747#discussion_r4554927379
##########
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:
💡 **Nit:** `invokeQuietly` silently returns `null` for all exceptions,
including `InvocationTargetException` — which means if `getContainerName()` or
`getContainerId()` throws at runtime (e.g. Docker socket not available), the
CONTAINER column goes blank with no hint why.
Consider distinguishing `InvocationTargetException` (the method was found
and invoked, but it threw) from `NoSuchMethodException` (not a container
service), and logging the former at TRACE/DEBUG:
```suggestion
private static Object invokeQuietly(Object target, String methodName) {
try {
return target.getClass().getMethod(methodName).invoke(target);
} catch (java.lang.reflect.InvocationTargetException e) {
LOG.debug("{}#{} threw unexpectedly",
target.getClass().getSimpleName(), methodName, e.getCause());
return null;
} catch (Exception e) {
return null;
}
}
```
Not blocking — silent null is safe degradation, and this is a debug aid only.
--
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]