This is an automated email from the ASF dual-hosted git repository.

Croway pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new a0e02f2385d5 camel-jbang: Fix Jolokia command when running from 
camel-launcher nested JAR
a0e02f2385d5 is described below

commit a0e02f2385d5890795eab29095a2634783d0e211
Author: Salvatore Mongiardo <[email protected]>
AuthorDate: Thu May 7 14:14:56 2026 +0200

    camel-jbang: Fix Jolokia command when running from camel-launcher nested JAR
    
    When camel-launcher is packaged as a Spring Boot executable JAR,
    OptionsAndArgs.lookupJarFile() resolves the fat JAR instead of the
    Jolokia agent JAR. VirtualMachine.loadAgent() then fails because the
    fat JAR is not a valid Java agent.
    
    Ship the Jolokia agent JAR alongside the launcher in the distribution
    and look for it in the same directory at runtime.
---
 .../dsl/jbang/core/commands/process/Jolokia.java   | 65 +++++++++++++++++++++-
 dsl/camel-jbang/camel-launcher/pom.xml             |  7 +++
 .../camel-launcher/src/main/assembly/bin.xml       |  9 +++
 3 files changed, 79 insertions(+), 2 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
index 277f340e0092..b542cabd65c6 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/Jolokia.java
@@ -16,7 +16,15 @@
  */
 package org.apache.camel.dsl.jbang.core.commands.process;
 
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.List;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
 
 import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
 import org.jolokia.jvmagent.client.command.CommandDispatcher;
@@ -67,12 +75,26 @@ public class Jolokia extends ProcessBaseCommand {
             } else {
                 long p = port;
                 if (p <= 0) {
-                    // find a new free port to use when starting a new 
connection
                     p = AvailablePortFinder.getNextAvailable(8778, 10000);
                 }
                 options = new OptionsAndArgs(
                         null, "--port", Long.toString(p), 
"--discoveryEnabled", "true", "start", Long.toString(pid));
             }
+
+            // When running from a Spring Boot nested JAR (e.g. 
camel-launcher),
+            // OptionsAndArgs resolves the fat JAR instead of the Jolokia 
agent JAR.
+            // The camel-launcher distribution ships the Jolokia agent JAR 
alongside
+            // the launcher in the same directory, so look for it there.
+            File resolvedJar = OptionsAndArgs.lookupJarFile();
+            if (!isJolokiaAgentJar(resolvedJar)) {
+                File agentJar = findJolokiaAgentJar(resolvedJar);
+                if (agentJar != null) {
+                    Field jarFileField = 
OptionsAndArgs.class.getDeclaredField("jarFile");
+                    jarFileField.setAccessible(true);
+                    jarFileField.set(options, agentJar);
+                }
+            }
+
             VirtualMachineHandlerOperations vmHandler = 
PlatformUtils.createVMAccess(options);
             CommandDispatcher dispatcher = new CommandDispatcher(options);
 
@@ -93,8 +115,47 @@ public class Jolokia extends ProcessBaseCommand {
     }
 
     /**
-     * The pid of the running Camel integration that was discovered and used
+     * Checks whether the given JAR file is a Jolokia agent JAR by inspecting 
its manifest for the Agent-Class
+     * attribute.
      */
+    private boolean isJolokiaAgentJar(File jar) {
+        if (jar == null || !jar.isFile()) {
+            return false;
+        }
+        try (JarFile jf = new JarFile(jar)) {
+            Manifest manifest = jf.getManifest();
+            if (manifest != null) {
+                String agentClass = 
manifest.getMainAttributes().getValue("Agent-Class");
+                return agentClass != null && agentClass.contains("jolokia");
+            }
+        } catch (IOException e) {
+            // not a valid JAR or unreadable
+        }
+        return false;
+    }
+
+    /**
+     * Finds the Jolokia agent JAR in the same directory as the given JAR 
file. The camel-launcher distribution ships
+     * the Jolokia agent JAR alongside the launcher JAR in the bin/ directory.
+     */
+    private File findJolokiaAgentJar(File launcherJar) {
+        if (launcherJar == null || !launcherJar.isFile()) {
+            return null;
+        }
+        Path dir = launcherJar.toPath().getParent();
+        if (dir == null) {
+            return null;
+        }
+        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, 
"jolokia-*-javaagent.jar")) {
+            for (Path entry : stream) {
+                return entry.toFile();
+            }
+        } catch (IOException e) {
+            // directory not readable
+        }
+        return null;
+    }
+
     long getPid() {
         return pid;
     }
diff --git a/dsl/camel-jbang/camel-launcher/pom.xml 
b/dsl/camel-jbang/camel-launcher/pom.xml
index 6bfc9171b708..cbe9ca1ad16a 100644
--- a/dsl/camel-jbang/camel-launcher/pom.xml
+++ b/dsl/camel-jbang/camel-launcher/pom.xml
@@ -235,6 +235,13 @@
             <artifactId>jcl-over-slf4j</artifactId>
             <version>${jcl-over-slf4j-version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.jolokia</groupId>
+            <artifactId>jolokia-agent-jvm</artifactId>
+            <version>${jolokia-version}</version>
+            <classifier>javaagent</classifier>
+            <scope>provided</scope>
+        </dependency>
 
         <!-- Test dependencies -->
         <dependency>
diff --git a/dsl/camel-jbang/camel-launcher/src/main/assembly/bin.xml 
b/dsl/camel-jbang/camel-launcher/src/main/assembly/bin.xml
index 93ba54a68134..32432cac0ccb 100644
--- a/dsl/camel-jbang/camel-launcher/src/main/assembly/bin.xml
+++ b/dsl/camel-jbang/camel-launcher/src/main/assembly/bin.xml
@@ -25,6 +25,15 @@
         <format>zip</format>
         <format>tar.gz</format>
     </formats>
+    <dependencySets>
+        <dependencySet>
+            <outputDirectory>bin</outputDirectory>
+            <includes>
+                <include>org.jolokia:jolokia-agent-jvm:jar:javaagent</include>
+            </includes>
+            <useProjectArtifact>false</useProjectArtifact>
+        </dependencySet>
+    </dependencySets>
     <fileSets>
         <fileSet>
             <directory>${project.basedir}</directory>

Reply via email to