Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAMEL-20361: camel-jbang - Make jolokia pluggable for camel-platform-http-main #12985

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/camel-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions catalog/camel-allcomponents/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
95 changes: 95 additions & 0 deletions components/camel-platform-http-jolokia/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<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>
<groupId>org.apache.camel</groupId>
<artifactId>components</artifactId>
<version>4.5.0-SNAPSHOT</version>
</parent>

<artifactId>camel-platform-http-jolokia</artifactId>
<packaging>jar</packaging>

<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>

<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>${hamcrest-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-version}</version>
<scope>test</scope>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by camel build tools - do NOT edit this file!
class=org.apache.camel.component.platform.http.plugin.DefaultJolokiaPlatformHttpPlugin
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,17 +37,20 @@
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;

private HttpRequestHandler requestHandler;

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)));
Expand All @@ -58,21 +62,24 @@ public JolokiaHttpRequestHandlerSupport() {
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;
}

Expand All @@ -88,12 +95,27 @@ private Restrictor createRestrictor(String pLocation) {
}
} 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
Expand All @@ -116,4 +138,5 @@ public boolean isDebug() {
return log.isDebugEnabled();
}
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 0 additions & 7 deletions components/camel-platform-http-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading
Loading