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

davsclaus pushed a commit to branch filter-kamelet2
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 6c6ffe0172388583b9437a1ee74517bc681ed9fb
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Feb 24 10:45:00 2024 +0100

    CAMEL-18858: camel-core - Mark route as created by Kamelet so we know this, 
so we can filter out in tooling and whereelse (kamelet is a blackbox)
---
 components/camel-kamelet/pom.xml                   |  5 ++
 .../kamelet/ManagedKameletRouteDisabledTest.java   | 94 ++++++++++++++++++++++
 .../component/kamelet/ManagedKameletRouteTest.java | 85 +++++++++++++++++++
 .../management/JmxManagementLifecycleStrategy.java |  9 +++
 4 files changed, 193 insertions(+)

diff --git a/components/camel-kamelet/pom.xml b/components/camel-kamelet/pom.xml
index 1fdb9f82918..57b70f9155a 100644
--- a/components/camel-kamelet/pom.xml
+++ b/components/camel-kamelet/pom.xml
@@ -44,6 +44,11 @@
         </dependency>
 
         <!-- TESTS  -->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-management</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-xml-jaxb</artifactId>
diff --git 
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteDisabledTest.java
 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteDisabledTest.java
new file mode 100644
index 00000000000..908c93248d4
--- /dev/null
+++ 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteDisabledTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.kamelet;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StringHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ManagedKameletRouteDisabledTest extends CamelTestSupport {
+
+    @Override
+    protected boolean useJmx() {
+        return true;
+    }
+
+    protected MBeanServer getMBeanServer() {
+        return 
context.getManagementStrategy().getManagementAgent().getMBeanServer();
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        
context.getManagementStrategy().getManagementAgent().setRegisterRoutesCreateByKamelet(false);
+        return context;
+    }
+
+    @Test
+    public void testKameletRouteMBeanDisabled() throws Exception {
+        String body = UUID.randomUUID().toString();
+
+        assertThat(
+                
fluentTemplate.toF("direct:single").withBody(body).request(String.class)).isEqualTo("a-"
 + body);
+
+        MBeanServer mbeanServer = getMBeanServer();
+
+        Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), null);
+        assertEquals(1, set.size());
+
+        Set<String> ids = new HashSet<>();
+        for (ObjectName on : set) {
+            String uri = (String) mbeanServer.getAttribute(on, "EndpointUri");
+            String name = StringHelper.before(uri, ":");
+            ids.add(name);
+        }
+        assertTrue(ids.contains("direct"));
+        assertFalse(ids.contains("kamelet"));
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("echo")
+                        .templateParameter("prefix")
+                        .from("kamelet:source")
+                        .setBody().simple("{{prefix}}-${body}");
+
+                from("direct:single").routeId("test")
+                        .to("kamelet:echo?prefix=a")
+                        .log("${body}");
+            }
+        };
+    }
+}
diff --git 
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteTest.java
 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteTest.java
new file mode 100644
index 00000000000..5c7a676c545
--- /dev/null
+++ 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/ManagedKameletRouteTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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.kamelet;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StringHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ManagedKameletRouteTest extends CamelTestSupport {
+
+    @Override
+    protected boolean useJmx() {
+        return true;
+    }
+
+    protected MBeanServer getMBeanServer() {
+        return 
context.getManagementStrategy().getManagementAgent().getMBeanServer();
+    }
+
+    @Test
+    public void testKameletRouteMBean() throws Exception {
+        String body = UUID.randomUUID().toString();
+
+        assertThat(
+                
fluentTemplate.toF("direct:single").withBody(body).request(String.class)).isEqualTo("a-"
 + body);
+
+        MBeanServer mbeanServer = getMBeanServer();
+
+        Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), null);
+        assertEquals(2, set.size());
+
+        Set<String> ids = new HashSet<>();
+        for (ObjectName on : set) {
+            String uri = (String) mbeanServer.getAttribute(on, "EndpointUri");
+            String name = StringHelper.before(uri, ":");
+            ids.add(name);
+        }
+        assertTrue(ids.contains("direct"));
+        assertTrue(ids.contains("kamelet"));
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("echo")
+                        .templateParameter("prefix")
+                        .from("kamelet:source")
+                        .setBody().simple("{{prefix}}-${body}");
+
+                from("direct:single").routeId("test")
+                        .to("kamelet:echo?prefix=a")
+                        .log("${body}");
+            }
+        };
+    }
+}
diff --git 
a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
 
b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
index 426ce12f53b..16d230ce87a 100644
--- 
a/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
+++ 
b/core/camel-management/src/main/java/org/apache/camel/management/JmxManagementLifecycleStrategy.java
@@ -931,6 +931,15 @@ public class JmxManagementLifecycleStrategy extends 
ServiceSupport implements Li
             return false;
         }
 
+        if (route != null && route.isCreatedByKamelet() && 
!agent.getRegisterRoutesCreateByKamelet()) {
+            // skip routes created from kamelets
+            return false;
+        }
+        if (route != null && route.isCreatedByRouteTemplate() && 
!agent.getRegisterRoutesCreateByTemplate()) {
+            // skip routes created from route templates
+            return false;
+        }
+
         // always register if we are starting CamelContext
         if (getCamelContext().getStatus().isStarting()
                 || getCamelContext().getStatus().isInitializing()) {

Reply via email to