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 d622cefb643 CAMEL-20361: camel-jbang - Make jolokia pluggable for camel-platform-http-main (#12985) d622cefb643 is described below commit d622cefb643e01394cc3bf8a478bb22b8f8e58fd Author: Ivan Kulaga <kulagaivanandreev...@gmail.com> AuthorDate: Sun Feb 18 13:12:14 2024 +0600 CAMEL-20361: camel-jbang - Make jolokia pluggable for camel-platform-http-main (#12985) * CAMEL-20361: camel-jbang - Make jolokia pluggable for camel-platform-http-main PlatformHttpPluginRegistry was made to resolve plugins for camel-platform-http. Jolokia is now camel-platform-http plugin * fix version and null check * up version --- bom/camel-bom/pom.xml | 5 + catalog/camel-allcomponents/pom.xml | 5 + .../pom.xml | 56 ++++++---- .../http/plugin/camel-platform-http-jolokia | 2 + .../services/org/apache/camel/other.properties | 7 ++ .../generated/resources/platform-http-jolokia.json | 15 +++ .../plugin/DefaultJolokiaPlatformHttpPlugin.java} | 45 ++++++-- .../DefaultJolokiaPlatformHttpPluginTest.java | 67 ++++++++++++ components/camel-platform-http-main/pom.xml | 7 -- .../platform/http/main/MainHttpServer.java | 43 ++++++-- .../http/main/MainHttpServerJolokiaTest.java | 75 ------------- components/camel-platform-http/pom.xml | 7 ++ .../platform/http/platform-http-plugin-registry | 2 + .../platform/http/PlatformHttpPluginRegistry.java | 121 +++++++++++++++++++++ .../http/plugin/JolokiaPlatformHttpPlugin.java | 30 +++++ .../platform/http/spi/PlatformHttpPlugin.java | 32 ++++++ .../http/spi/PlatformHttpPluginRegistry.java | 36 ++++++ components/pom.xml | 3 +- parent/pom.xml | 5 + 19 files changed, 435 insertions(+), 128 deletions(-) diff --git a/bom/camel-bom/pom.xml b/bom/camel-bom/pom.xml index 79c7195516d..8326f57cef9 100644 --- a/bom/camel-bom/pom.xml +++ b/bom/camel-bom/pom.xml @@ -1547,6 +1547,11 @@ <artifactId>camel-platform-http</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-platform-http-jolokia</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-platform-http-main</artifactId> diff --git a/catalog/camel-allcomponents/pom.xml b/catalog/camel-allcomponents/pom.xml index 1ad44fb6a60..b4902a65b02 100644 --- a/catalog/camel-allcomponents/pom.xml +++ b/catalog/camel-allcomponents/pom.xml @@ -1342,6 +1342,11 @@ <artifactId>camel-platform-http</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-platform-http-jolokia</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-platform-http-main</artifactId> diff --git a/components/camel-platform-http/pom.xml b/components/camel-platform-http-jolokia/pom.xml similarity index 63% copy from components/camel-platform-http/pom.xml copy to components/camel-platform-http-jolokia/pom.xml index b1b5f3961f3..974b51fc0b2 100644 --- a/components/camel-platform-http/pom.xml +++ b/components/camel-platform-http-jolokia/pom.xml @@ -17,8 +17,7 @@ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> @@ -27,57 +26,70 @@ <version>4.5.0-SNAPSHOT</version> </parent> - <artifactId>camel-platform-http</artifactId> + <artifactId>camel-platform-http-jolokia</artifactId> <packaging>jar</packaging> - <name>Camel :: Platform HTTP</name> - <description>HTTP service leveraging existing runtime platform HTTP server</description> + + <name>Camel :: Platform HTTP :: Jolokia</name> + <description>Jolokia plugin for standalone Camel HTTP Platform</description> + + <properties> + <firstVersion>4.5.0</firstVersion> + <label>http,monitoring</label> + </properties> <dependencies> + <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-platform-http</artifactId> + </dependency> + + <!-- json --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-util-json</artifactId> </dependency> - <!-- test infra --> + <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-test-infra-jetty</artifactId> - <version>${project.version}</version> + <artifactId>camel-core</artifactId> <type>test-jar</type> <scope>test</scope> </dependency> <dependency> - <groupId>org.eclipse.jetty.ee10</groupId> - <artifactId>jetty-ee10-servlet</artifactId> - <version>${jetty-version}</version> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>org.eclipse.jetty.ee10</groupId> - <artifactId>jetty-ee10-servlets</artifactId> - <version>${jetty-version}</version> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest</artifactId> + <version>${hamcrest-version}</version> <scope>test</scope> </dependency> - <dependency> - <groupId>io.rest-assured</groupId> - <artifactId>rest-assured</artifactId> - <version>${rest-assured-version}</version> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj-version}</version> <scope>test</scope> </dependency> + + <!-- logging --> <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-test-junit5</artifactId> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-http</artifactId> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j2-impl</artifactId> <scope>test</scope> </dependency> </dependencies> + </project> diff --git a/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/plugin/camel-platform-http-jolokia b/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/plugin/camel-platform-http-jolokia new file mode 100644 index 00000000000..0a5eddbdad4 --- /dev/null +++ b/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/plugin/camel-platform-http-jolokia @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.platform.http.plugin.DefaultJolokiaPlatformHttpPlugin \ No newline at end of file diff --git a/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/other.properties b/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/other.properties new file mode 100644 index 00000000000..4ec5cad9954 --- /dev/null +++ b/components/camel-platform-http-jolokia/src/generated/resources/META-INF/services/org/apache/camel/other.properties @@ -0,0 +1,7 @@ +# Generated by camel build tools - do NOT edit this file! +name=platform-http-jolokia +groupId=org.apache.camel +artifactId=camel-platform-http-jolokia +version=4.5.0-SNAPSHOT +projectName=Camel :: Platform HTTP :: Jolokia +projectDescription=Jolokia plugin for standalone Camel HTTP Platform diff --git a/components/camel-platform-http-jolokia/src/generated/resources/platform-http-jolokia.json b/components/camel-platform-http-jolokia/src/generated/resources/platform-http-jolokia.json new file mode 100644 index 00000000000..1d6ac0f464c --- /dev/null +++ b/components/camel-platform-http-jolokia/src/generated/resources/platform-http-jolokia.json @@ -0,0 +1,15 @@ +{ + "other": { + "kind": "other", + "name": "platform-http-jolokia", + "title": "Platform Http Jolokia", + "description": "Jolokia plugin for standalone Camel HTTP Platform", + "deprecated": false, + "firstVersion": "4.4.0", + "label": "http,monitoring", + "supportLevel": "Preview", + "groupId": "org.apache.camel", + "artifactId": "camel-platform-http-jolokia", + "version": "4.4.0-SNAPSHOT" + } +} diff --git a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/jolokia/JolokiaHttpRequestHandlerSupport.java b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java similarity index 77% rename from components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/jolokia/JolokiaHttpRequestHandlerSupport.java rename to components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java index 6fd001ed030..55cd5abe6f1 100644 --- a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/jolokia/JolokiaHttpRequestHandlerSupport.java +++ b/components/camel-platform-http-jolokia/src/main/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPlugin.java @@ -14,11 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.component.platform.http.main.jolokia; +package org.apache.camel.component.platform.http.plugin; import java.io.IOException; -import org.apache.camel.StaticService; +import org.apache.camel.CamelContext; +import org.apache.camel.spi.annotations.JdkService; import org.apache.camel.support.service.ServiceSupport; import org.jolokia.server.core.config.ConfigKey; import org.jolokia.server.core.config.StaticConfiguration; @@ -36,9 +37,10 @@ import org.jolokia.service.serializer.JolokiaSerializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class JolokiaHttpRequestHandlerSupport extends ServiceSupport implements StaticService { +@JdkService(DefaultJolokiaPlatformHttpPlugin.NAME) +public class DefaultJolokiaPlatformHttpPlugin extends ServiceSupport implements JolokiaPlatformHttpPlugin { - private static final Logger LOG = LoggerFactory.getLogger(JolokiaHttpRequestHandlerSupport.class); + private static final Logger LOG = LoggerFactory.getLogger(DefaultJolokiaPlatformHttpPlugin.class); private final JolokiaServiceManager serviceManager; @@ -46,7 +48,9 @@ public class JolokiaHttpRequestHandlerSupport extends ServiceSupport implements private final LogHandler jolokiaLogHandler; - public JolokiaHttpRequestHandlerSupport() { + private CamelContext camelContext; + + public DefaultJolokiaPlatformHttpPlugin() { var config = new StaticConfiguration(ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(), "vertx")); jolokiaLogHandler = new JolokiaLogHandler(LOG); var restrictor = createRestrictor(NetworkUtil.replaceExpression(config.getConfig(ConfigKey.POLICY_LOCATION))); @@ -58,21 +62,24 @@ public class JolokiaHttpRequestHandlerSupport extends ServiceSupport implements serviceManager.addService(new JolokiaSerializer()); serviceManager.addService(new LocalRequestHandler(1)); - LOG.info("Creating JolokiaHttpRequestHandlerSupport with restrictor {}", restrictor); + LOG.info("Creating DefaultJolokiaPlatformHttpPlugin with restrictor {}", restrictor); } @Override - public void start() { + public void doStart() { var jolokiaContext = serviceManager.start(); requestHandler = new HttpRequestHandler(jolokiaContext); } @Override - public void stop() { - serviceManager.stop(); + public void doStop() { + if (serviceManager != null) { + serviceManager.stop(); + } } - public HttpRequestHandler getHttpRequestHandler() { + @Override + public HttpRequestHandler getRequestHandler() { return requestHandler; } @@ -88,12 +95,27 @@ public class JolokiaHttpRequestHandlerSupport extends ServiceSupport implements } } catch (IOException e) { jolokiaLogHandler.error("Error while accessing access restrictor at " + pLocation + - ". Denying all access to MBeans for security reasons. Exception: " + e, + ". Denying all access to MBeans for security reasons. Exception: " + e, e); return new DenyAllRestrictor(); } } + @Override + public String getId() { + return NAME; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + private record JolokiaLogHandler(Logger log) implements LogHandler { @Override @@ -116,4 +138,5 @@ public class JolokiaHttpRequestHandlerSupport extends ServiceSupport implements return log.isDebugEnabled(); } } + } diff --git a/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java b/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java new file mode 100644 index 00000000000..0eeaec4639a --- /dev/null +++ b/components/camel-platform-http-jolokia/src/test/java/org/apache/camel/component/platform/http/plugin/DefaultJolokiaPlatformHttpPluginTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.platform.http.plugin; + +import java.util.HashMap; +import java.util.Optional; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry; +import org.apache.camel.support.ResolverHelper; +import org.jolokia.server.core.http.HttpRequestHandler; +import org.json.simple.JSONAware; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DefaultJolokiaPlatformHttpPluginTest extends ContextTestSupport { + + @Test + public void jolokiaPlatformHttpPluginContextTest() throws Exception { + PlatformHttpPluginRegistry registry = resolvePlatformHttpPluginRegistry(); + Assertions.assertNotNull(registry); + DefaultJolokiaPlatformHttpPlugin plugin = (DefaultJolokiaPlatformHttpPlugin) registry + .resolvePluginById(DefaultJolokiaPlatformHttpPlugin.NAME).orElseThrow(); + Assertions.assertNotNull(plugin); + HttpRequestHandler handler = plugin.getRequestHandler(); + + JSONAware json = handler.handleGetRequest("", "/", new HashMap<>()); + + JSONParser parser = new JSONParser(); + JSONObject responseBody = (JSONObject) parser.parse(json.toJSONString()); + + JSONObject value = (JSONObject) responseBody.get("value"); + String agentVersion = (String) value.get("agent"); + + JSONObject request = (JSONObject) responseBody.get("request"); + String type = (String) request.get("type"); + + assertEquals("version", type); + assertEquals("2.0.1", agentVersion); + } + + private PlatformHttpPluginRegistry resolvePlatformHttpPluginRegistry() { + Optional<PlatformHttpPluginRegistry> result = ResolverHelper.resolveService( + context, + PlatformHttpPluginRegistry.FACTORY, + PlatformHttpPluginRegistry.class); + return result.orElse(null); + } +} diff --git a/components/camel-platform-http-main/pom.xml b/components/camel-platform-http-main/pom.xml index 0ee949ec6c4..c7ca3e970f1 100644 --- a/components/camel-platform-http-main/pom.xml +++ b/components/camel-platform-http-main/pom.xml @@ -44,13 +44,6 @@ <artifactId>camel-main</artifactId> </dependency> - <!-- jolokia --> - <dependency> - <groupId>org.jolokia</groupId> - <artifactId>jolokia-agent-jvm</artifactId> - <version>${jolokia-version}</version> - </dependency> - <!-- use vertx as http engine --> <dependency> <groupId>org.apache.camel</groupId> diff --git a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java index b93e12aff0d..882e50ea706 100644 --- a/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java +++ b/components/camel-platform-http-main/src/main/java/org/apache/camel/component/platform/http/main/MainHttpServer.java @@ -18,7 +18,6 @@ package org.apache.camel.component.platform.http.main; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; @@ -30,6 +29,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; @@ -58,7 +58,8 @@ import org.apache.camel.api.management.ManagedAttribute; import org.apache.camel.api.management.ManagedResource; import org.apache.camel.component.platform.http.HttpEndpointModel; import org.apache.camel.component.platform.http.PlatformHttpComponent; -import org.apache.camel.component.platform.http.main.jolokia.JolokiaHttpRequestHandlerSupport; +import org.apache.camel.component.platform.http.plugin.JolokiaPlatformHttpPlugin; +import org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry; import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpRouter; import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer; import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServerConfiguration; @@ -68,6 +69,7 @@ import org.apache.camel.health.HealthCheck; import org.apache.camel.health.HealthCheckHelper; import org.apache.camel.health.HealthCheckRegistry; import org.apache.camel.spi.CamelEvent; +import org.apache.camel.support.ResolverHelper; import org.apache.camel.support.SimpleEventNotifierSupport; import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.support.service.ServiceHelper; @@ -272,6 +274,18 @@ public class MainHttpServer extends ServiceSupport implements CamelContextAware, server = new VertxPlatformHttpServer(configuration); camelContext.addService(server); + + PlatformHttpPluginRegistry pluginRegistry + = getCamelContext().getCamelContextExtension().getContextPlugin(PlatformHttpPluginRegistry.class); + if (pluginRegistry == null && pluginsEnabled()) { + pluginRegistry = resolvePlatformHttpPluginRegistry(); + pluginRegistry.setCamelContext(getCamelContext()); + getCamelContext().getCamelContextExtension().addContextPlugin(PlatformHttpPluginRegistry.class, pluginRegistry); + } + } + + private boolean pluginsEnabled() { + return jolokiaEnabled; } @Override @@ -478,17 +492,22 @@ public class MainHttpServer extends ServiceSupport implements CamelContextAware, platformHttpComponent.addHttpEndpoint("/q/jolokia", null, null); } + protected PlatformHttpPluginRegistry resolvePlatformHttpPluginRegistry() { + Optional<PlatformHttpPluginRegistry> result = ResolverHelper.resolveService( + getCamelContext(), + PlatformHttpPluginRegistry.FACTORY, + PlatformHttpPluginRegistry.class); + return result.orElse(null); + } + private HttpRequestHandler getHttpRequestHandler() { - //TODO: make jolokiaService more pluggable - //JolokiaHttpRequestHandlerSupport jolokiaService = camelContext.getCamelContextExtension().getContextPlugin(JolokiaHttpRequestHandlerSupport.class); - HttpRequestHandler requestHandler; - try (JolokiaHttpRequestHandlerSupport jolokiaService = new JolokiaHttpRequestHandlerSupport()) { - jolokiaService.start(); - requestHandler = jolokiaService.getHttpRequestHandler(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return requestHandler; + PlatformHttpPluginRegistry registry + = camelContext.getCamelContextExtension().getContextPlugin(PlatformHttpPluginRegistry.class); + JolokiaPlatformHttpPlugin jolokia + = (JolokiaPlatformHttpPlugin) registry.resolvePluginById(JolokiaPlatformHttpPlugin.NAME) + .orElseThrow(() -> new RuntimeException( + "JolokiaPlatformHttpPlugin not found. Please add camel-platform-http-jolokia dependency.")); + return jolokia.getRequestHandler(); } private Map<String, String[]> getParams(MultiMap params) { diff --git a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerJolokiaTest.java b/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerJolokiaTest.java deleted file mode 100644 index 8da4c2b179d..00000000000 --- a/components/camel-platform-http-main/src/test/java/org/apache/camel/component/platform/http/main/MainHttpServerJolokiaTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.camel.component.platform.http.main; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; - -import org.apache.camel.CamelContext; -import org.apache.camel.impl.DefaultCamelContext; -import org.apache.camel.test.AvailablePortFinder; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class MainHttpServerJolokiaTest { - - private CamelContext camelContext; - - private final int port = AvailablePortFinder.getNextAvailable(); - - @Test - public void jolokiaIsUp() throws IOException, InterruptedException, ParseException { - MainHttpServer server = new MainHttpServer(); - - camelContext = new DefaultCamelContext(); - server.setCamelContext(camelContext); - - server.setHost("0.0.0.0"); - server.setPort(port); - server.setPath("/"); - - server.setJolokiaEnabled(true); - server.start(); - - HttpRequest httpRequest = HttpRequest.newBuilder() - .uri(URI.create("http://localhost:" + port + "/q/jolokia")) - .build(); - - HttpResponse<String> response = HttpClient.newBuilder().build().send(httpRequest, HttpResponse.BodyHandlers.ofString()); - JSONParser parser = new JSONParser(); - JSONObject responseBody = (JSONObject) parser.parse(response.body()); - - JSONObject value = (JSONObject) responseBody.get("value"); - String agentVersion = (String) value.get("agent"); - - JSONObject request = (JSONObject) responseBody.get("request"); - String type = (String) request.get("type"); - - assertEquals(200, response.statusCode()); - assertEquals("version", type); - assertEquals("2.0.1", agentVersion); - } - -} diff --git a/components/camel-platform-http/pom.xml b/components/camel-platform-http/pom.xml index b1b5f3961f3..5d567f0d69b 100644 --- a/components/camel-platform-http/pom.xml +++ b/components/camel-platform-http/pom.xml @@ -42,6 +42,13 @@ <artifactId>camel-util-json</artifactId> </dependency> + <!-- jolokia --> + <dependency> + <groupId>org.jolokia</groupId> + <artifactId>jolokia-agent-jvm</artifactId> + <version>${jolokia-version}</version> + </dependency> + <!-- test infra --> <dependency> <groupId>org.apache.camel</groupId> diff --git a/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/platform-http-plugin-registry b/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/platform-http-plugin-registry new file mode 100644 index 00000000000..a845295309d --- /dev/null +++ b/components/camel-platform-http/src/generated/resources/META-INF/services/org/apache/camel/component/platform/http/platform-http-plugin-registry @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.component.platform.http.PlatformHttpPluginRegistry diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpPluginRegistry.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpPluginRegistry.java new file mode 100644 index 00000000000..115e2722f4f --- /dev/null +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/PlatformHttpPluginRegistry.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.platform.http; + +import java.util.Comparator; +import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.component.platform.http.spi.PlatformHttpPlugin; +import org.apache.camel.spi.FactoryFinder; +import org.apache.camel.spi.annotations.JdkService; +import org.apache.camel.support.service.ServiceHelper; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.ObjectHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Default {@link org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry}. + */ +@JdkService(org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry.FACTORY) +public class PlatformHttpPluginRegistry extends ServiceSupport + implements org.apache.camel.component.platform.http.spi.PlatformHttpPluginRegistry { + + private static final String PLATFORM_HTTP_PLUGIN_FACTORY_PATH + = "META-INF/services/org/apache/camel/" + PLATFORM_HTTP_FACTORY_PATH + "plugin/"; + private CamelContext camelContext; + + private final Set<PlatformHttpPlugin> plugins = new TreeSet<>(Comparator.comparing(PlatformHttpPlugin::getId)); + + private static final Logger LOG = LoggerFactory.getLogger(PlatformHttpPluginRegistry.class); + + @Override + public Optional<PlatformHttpPlugin> resolvePluginById(String id) { + PlatformHttpPlugin answer = plugins.stream().filter(plugin -> plugin.getId().equals(id)).findFirst() + .orElse(getCamelContext().getRegistry().findByTypeWithName(PlatformHttpPlugin.class).get(id)); + + if (answer == null) { + answer = resolvePluginWithFactoryFinderById(id); + + } + + if (answer != null) { + register(answer); + } + + return Optional.ofNullable(answer); + } + + @Override + public boolean register(PlatformHttpPlugin plugin) { + boolean result; + + if (getPlugin(plugin.getId()).isPresent()) { + return false; + } + + result = plugins.add(plugin); + + if (result) { + CamelContextAware.trySetCamelContext(plugin, camelContext); + ServiceHelper.startService(plugin); + LOG.debug("platform-http-plugin with id {} successfully registered", plugin.getId()); + } + return result; + } + + private Optional<PlatformHttpPlugin> getPlugin(String id) { + return plugins.stream() + .filter(r -> ObjectHelper.equal(r.getId(), id)) + .findFirst(); + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + private PlatformHttpPlugin resolvePluginWithFactoryFinderById(String id) { + PlatformHttpPlugin answer = null; + FactoryFinder factoryFinder + = getCamelContext().getCamelContextExtension().getFactoryFinder(PLATFORM_HTTP_PLUGIN_FACTORY_PATH); + Class<?> type = factoryFinder.findOptionalClass(id).orElse(null); + + if (type != null) { + if (PlatformHttpPlugin.class.isAssignableFrom(type)) { + answer = (PlatformHttpPlugin) camelContext.getInjector().newInstance(type, false); + CamelContextAware.trySetCamelContext(answer, camelContext); + } else { + throw new IllegalArgumentException( + "Resolving platform-http-plugin: " + id + + " detected type conflict: Not a PlatformHttpPlugin implementation. Found: " + + type.getName()); + } + } + + return answer; + } +} diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java new file mode 100644 index 00000000000..12e622fbfa4 --- /dev/null +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/plugin/JolokiaPlatformHttpPlugin.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.platform.http.plugin; + +import org.apache.camel.component.platform.http.spi.PlatformHttpPlugin; +import org.jolokia.server.core.http.HttpRequestHandler; + +/** + * camel-platform-http plugin. + */ +public interface JolokiaPlatformHttpPlugin extends PlatformHttpPlugin { + + String NAME = "camel-platform-http-jolokia"; + + HttpRequestHandler getRequestHandler(); +} diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPlugin.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPlugin.java new file mode 100644 index 00000000000..13e9dfd7a06 --- /dev/null +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPlugin.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.platform.http.spi; + +import org.apache.camel.CamelContextAware; +import org.apache.camel.StaticService; + +/** + * camel-platform-http plugin. + */ +public interface PlatformHttpPlugin extends StaticService, CamelContextAware { + + /** + * The ID of this plugin. + */ + String getId(); + +} diff --git a/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPluginRegistry.java b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPluginRegistry.java new file mode 100644 index 00000000000..e5742a87f1f --- /dev/null +++ b/components/camel-platform-http/src/main/java/org/apache/camel/component/platform/http/spi/PlatformHttpPluginRegistry.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.platform.http.spi; + +import java.util.Optional; + +import org.apache.camel.CamelContextAware; +import org.apache.camel.StaticService; + +/** + * Factory to abstract the creation of the Plugin Registry for camel-platform-http. + */ +public interface PlatformHttpPluginRegistry extends CamelContextAware, StaticService { + + String PLATFORM_HTTP_FACTORY_PATH = "component/platform/http/"; + + String FACTORY = PLATFORM_HTTP_FACTORY_PATH + "platform-http-plugin-registry"; + + Optional<PlatformHttpPlugin> resolvePluginById(String id); + + boolean register(PlatformHttpPlugin plugin); +} diff --git a/components/pom.xml b/components/pom.xml index 0ec53589af5..f9c96cbe9ab 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -234,8 +234,9 @@ <module>camel-pg-replication-slot</module> <module>camel-pgevent</module> <module>camel-platform-http</module> - <module>camel-platform-http-vertx</module> + <module>camel-platform-http-jolokia</module> <module>camel-platform-http-main</module> + <module>camel-platform-http-vertx</module> <module>camel-plc4x</module> <module>camel-printer</module> <module>camel-protobuf</module> diff --git a/parent/pom.xml b/parent/pom.xml index f0ea047763e..acd84cf3bd0 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -1945,6 +1945,11 @@ <artifactId>camel-platform-http</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-platform-http-jolokia</artifactId> + <version>${project.version}</version> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-platform-http-main</artifactId>