You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2024/06/06 18:36:22 UTC

(camel) branch main updated: CAMEL-20847: camel-jbang - Run with jolokia enabled does not work

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

davsclaus 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 0c3a28572a6 CAMEL-20847: camel-jbang - Run with jolokia enabled does not work
0c3a28572a6 is described below

commit 0c3a28572a6932ec36a1014debfdcafea582eb4d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Jun 6 20:35:33 2024 +0200

    CAMEL-20847: camel-jbang - Run with jolokia enabled does not work
---
 components/camel-platform-http-jolokia/pom.xml     | 12 +++-
 .../plugin/DefaultJolokiaPlatformHttpPlugin.java   | 72 +++++++++++++++-------
 2 files changed, 61 insertions(+), 23 deletions(-)

diff --git a/components/camel-platform-http-jolokia/pom.xml b/components/camel-platform-http-jolokia/pom.xml
index 59ad8e17e43..9826eb2d31d 100644
--- a/components/camel-platform-http-jolokia/pom.xml
+++ b/components/camel-platform-http-jolokia/pom.xml
@@ -56,7 +56,17 @@
         <!-- jolokia -->
         <dependency>
             <groupId>org.jolokia</groupId>
-            <artifactId>jolokia-agent-jvm</artifactId>
+            <artifactId>jolokia-server-core</artifactId>
+            <version>${jolokia-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jolokia</groupId>
+            <artifactId>jolokia-service-serializer</artifactId>
+            <version>${jolokia-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jolokia</groupId>
+            <artifactId>jolokia-service-jmx</artifactId>
             <version>${jolokia-version}</version>
         </dependency>
 
diff --git a/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
index 5275ad7f8c4..7ea95eb8623 100644
--- a/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
+++ b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.platform.http.plugin;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
@@ -35,7 +36,9 @@ import io.vertx.ext.web.RoutingContext;
 import io.vertx.ext.web.impl.Utils;
 import org.apache.camel.CamelContext;
 import org.apache.camel.spi.annotations.JdkService;
+import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.ReflectionHelper;
 import org.jolokia.server.core.config.ConfigKey;
 import org.jolokia.server.core.config.StaticConfiguration;
 import org.jolokia.server.core.http.HttpRequestHandler;
@@ -49,8 +52,6 @@ import org.jolokia.server.core.service.api.Restrictor;
 import org.jolokia.server.core.util.NetworkUtil;
 import org.jolokia.service.jmx.LocalRequestHandler;
 import org.jolokia.service.serializer.JolokiaSerializer;
-import org.json.simple.JSONAware;
-import org.json.simple.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -159,34 +160,68 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
         }
     }
 
+    /**
+     * This method uses reflection to invoke the Jolokia HttpRequestHandler because its shading
+     * JSon library which at runtime causes NoSuchMethodError due to JSONObject has been shaded
+     * into a different package.
+     */
     private Handler<RoutingContext> createVertxHandler() {
         return routingContext -> {
             HttpServerRequest req = routingContext.request();
             String remainingPath = Utils.pathOffset(req.path(), routingContext);
 
-            JSONAware json = null;
+            Object json = null;
+            int status = 200;
             try {
-                requestHandler.checkAccess(req.scheme(), req.remoteAddress().host(), req.remoteAddress().host(),
-                        getOriginOrReferer(req));
+                ObjectHelper.invokeMethodSafe("checkAccess", requestHandler, req.scheme(), req.remoteAddress().host(),
+                        req.remoteAddress().host(), getOriginOrReferer(req));
                 if (req.method() == HttpMethod.GET) {
-                    json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params()));
+                    Method m = ReflectionHelper.findMethod(requestHandler.getClass(), "handleGetRequest", String.class,
+                            String.class, Map.class);
+                    if (m != null) {
+                        json = ObjectHelper.invokeMethodSafe(m, requestHandler, req.uri(), remainingPath,
+                                getParams(req.params()));
+                    }
                 } else {
                     Arguments.require(routingContext.body() != null, "Missing body");
                     InputStream inputStream = new ByteBufInputStream(routingContext.body().buffer().getByteBuf());
-                    json = requestHandler.handlePostRequest(req.uri(), inputStream, StandardCharsets.UTF_8.name(),
-                            getParams(req.params()));
+                    Method m = ReflectionHelper.findMethod(requestHandler.getClass(), "handlePostRequest", String.class,
+                            InputStream.class, String.class, Map.class);
+                    if (m != null) {
+                        json = ObjectHelper.invokeMethodSafe(m, requestHandler, req.uri(), inputStream,
+                                StandardCharsets.UTF_8.name(), getParams(req.params()));
+                    }
                 }
             } catch (Throwable exp) {
-                json = requestHandler.handleThrowable(
-                        exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp);
+                status = 500;
+                try {
+                    Method m = ReflectionHelper.findMethod(requestHandler.getClass(), "handleThrowable", Throwable.class);
+                    if (m != null) {
+                        json = ObjectHelper.invokeMethodSafe(m, requestHandler, exp instanceof RuntimeMBeanException
+                                ? ((RuntimeMBeanException) exp).getTargetException() : exp);
+                    }
+                } catch (Exception e) {
+                    // ignore
+                }
             } finally {
-                if (json == null)
-                    json = requestHandler.handleThrowable(new Exception("Internal error while handling an exception"));
+                if (json == null) {
+                    try {
+                        Method m = ReflectionHelper.findMethod(requestHandler.getClass(), "handleThrowable", Throwable.class);
+                        if (m != null) {
+                            json = ObjectHelper.invokeMethodSafe(m, requestHandler,
+                                    new Exception("Internal error while handling an exception"));
+                        }
+                    } catch (Exception e) {
+                        // ignore
+                    }
+                }
+
+                String data = json != null ? json.toString() : "";
 
                 routingContext.response()
-                        .setStatusCode(getStatusCode(json))
+                        .setStatusCode(status)
                         .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
-                        .end(json.toJSONString());
+                        .end(data);
             }
         };
     }
@@ -204,14 +239,7 @@ public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements
         if (origin == null) {
             origin = req.getHeader(HttpHeaders.REFERER);
         }
-        return origin != null ? origin.replaceAll("[\\n\\r]*", "") : null;
-    }
-
-    private int getStatusCode(JSONAware json) {
-        if (json instanceof JSONObject && ((JSONObject) json).get("status") instanceof Integer) {
-            return (Integer) ((JSONObject) json).get("status");
-        }
-        return 200;
+        return origin != null ? origin.replaceAll("[\\n\\r]*", "") : "";
     }
 
 }