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 2023/11/27 13:14:42 UTC

(camel) 03/05: CAMEL-20155: camel-core - MBean should use nodePrefixId in their ObjectName (used for avoding duplicate ids and be able to assign fixed ids)

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

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

commit 31a51d36a1ffd56d61125af6b01bb8e84dc7304f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 27 13:54:37 2023 +0100

    CAMEL-20155: camel-core - MBean should use nodePrefixId in their ObjectName (used for avoding duplicate ids and be able to assign fixed ids)
---
 .../src/main/java/org/apache/camel/NamedNode.java  |  5 ++
 .../src/main/java/org/apache/camel/NamedRoute.java |  5 ++
 .../java/org/apache/camel/impl/DefaultModel.java   | 11 ++-
 .../camel/model/OptionalIdentifiedDefinition.java  | 39 ++++++----
 .../DefaultManagementObjectNameStrategy.java       | 14 +++-
 .../management/ManagedRouteNodePrefixIdTest.java   | 88 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_3.adoc     |  5 ++
 7 files changed, 149 insertions(+), 18 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/NamedNode.java b/core/camel-api/src/main/java/org/apache/camel/NamedNode.java
index 9e40b6ba6c5..aba445e31d1 100644
--- a/core/camel-api/src/main/java/org/apache/camel/NamedNode.java
+++ b/core/camel-api/src/main/java/org/apache/camel/NamedNode.java
@@ -26,6 +26,11 @@ public interface NamedNode extends LineNumberAware {
      */
     String getId();
 
+    /**
+     * Gets the node prefix id.
+     */
+    String getNodePrefixId();
+
     /**
      * Returns a short name for this node which can be useful for ID generation or referring to related resources like
      * images
diff --git a/core/camel-api/src/main/java/org/apache/camel/NamedRoute.java b/core/camel-api/src/main/java/org/apache/camel/NamedRoute.java
index 37ff8e86561..047cd99be2d 100644
--- a/core/camel-api/src/main/java/org/apache/camel/NamedRoute.java
+++ b/core/camel-api/src/main/java/org/apache/camel/NamedRoute.java
@@ -26,6 +26,11 @@ public interface NamedRoute {
      */
     String getRouteId();
 
+    /**
+     * Gets the node prefix id.
+     */
+    String getNodePrefixId();
+
     /**
      * Gets the route endpoint url.
      */
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
index e49e4f95459..1a3a0d55e65 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -858,9 +858,18 @@ public class DefaultModel implements Model {
             Collection<ProcessorDefinition> col
                     = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), ProcessorDefinition.class);
             for (ProcessorDefinition proc : col) {
-                if (id.equals(proc.getId())) {
+                String pid = proc.getId();
+                // match direct by ids
+                if (id.equals(pid)) {
                     return proc;
                 }
+                // try to match via node prefix id
+                if (proc.getNodePrefixId() != null) {
+                    pid = proc.getNodePrefixId() + pid;
+                    if (id.equals(pid)) {
+                        return proc;
+                    }
+                }
             }
         }
         return null;
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java
index eef35ec6d1c..f8c42b3e246 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java
@@ -61,6 +61,21 @@ public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedD
         return id;
     }
 
+    @Override
+    public String getNodePrefixId() {
+        // prefix is only for nodes in the route (not the route id)
+        String prefix = null;
+        boolean iAmRoute = this instanceof RouteDefinition;
+        boolean allowPrefix = !iAmRoute && this instanceof ProcessorDefinition;
+        if (allowPrefix) {
+            RouteDefinition route = ProcessorDefinitionHelper.getRoute(this);
+            if (route != null) {
+                prefix = route.getNodePrefixId();
+            }
+        }
+        return prefix;
+    }
+
     /**
      * Sets the id of this node
      */
@@ -153,26 +168,20 @@ public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedD
      * Gets the node id, creating one if not already set.
      */
     public String idOrCreate(NodeIdFactory factory) {
-        // prefix is only for nodes in the route (not the route id)
-        String prefix = null;
-        boolean iAmRoute = this instanceof RouteDefinition;
-        boolean allowPrefix = !iAmRoute && this instanceof ProcessorDefinition;
-        if (allowPrefix) {
-            RouteDefinition route = ProcessorDefinitionHelper.getRoute(this);
-            if (route != null) {
-                prefix = route.getNodePrefixId();
-            }
-        }
         if (id == null) {
             setGeneratedId(factory.createId(this));
         }
 
-        // return with prefix
-        if (prefix != null) {
-            return prefix + id;
-        } else {
-            return id;
+        // return with prefix if configured
+        boolean iAmRoute = this instanceof RouteDefinition;
+        boolean allowPrefix = !iAmRoute && this instanceof ProcessorDefinition;
+        if (allowPrefix) {
+            String prefix = getNodePrefixId();
+            if (prefix != null) {
+                return prefix + id;
+            }
         }
+        return id;
     }
 
     public Boolean getCustomId() {
diff --git a/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java b/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java
index 7815c2f0b19..55b0a3cec69 100644
--- a/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java
+++ b/core/camel-management/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java
@@ -289,7 +289,12 @@ public class DefaultManagementObjectNameStrategy implements ManagementObjectName
         buffer.append(domainName).append(":");
         buffer.append(KEY_CONTEXT + "=").append(getContextId(context)).append(",");
         buffer.append(KEY_TYPE + "=").append(TYPE_PROCESSOR).append(",");
-        buffer.append(KEY_NAME + "=").append(ObjectName.quote(definition.getId()));
+        String id = definition.getId();
+        String prefix = definition.getNodePrefixId();
+        if (prefix != null) {
+            id = prefix + id;
+        }
+        buffer.append(KEY_NAME + "=").append(ObjectName.quote(id));
         return createObjectName(buffer);
     }
 
@@ -300,7 +305,12 @@ public class DefaultManagementObjectNameStrategy implements ManagementObjectName
         buffer.append(domainName).append(":");
         buffer.append(KEY_CONTEXT + "=").append(getContextId(context)).append(",");
         buffer.append(KEY_TYPE + "=").append(TYPE_STEP).append(",");
-        buffer.append(KEY_NAME + "=").append(ObjectName.quote(definition.getId()));
+        String id = definition.getId();
+        String prefix = definition.getNodePrefixId();
+        if (prefix != null) {
+            id = prefix + id;
+        }
+        buffer.append(KEY_NAME + "=").append(ObjectName.quote(id));
         return createObjectName(buffer);
     }
 
diff --git a/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteNodePrefixIdTest.java b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteNodePrefixIdTest.java
new file mode 100644
index 00000000000..2b65f1afd99
--- /dev/null
+++ b/core/camel-management/src/test/java/org/apache/camel/management/ManagedRouteNodePrefixIdTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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.management;
+
+import java.util.Set;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.camel.api.management.ManagedCamelContext;
+import org.apache.camel.api.management.mbean.ManagedLogMBean;
+import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.OS;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@DisabledOnOs(OS.AIX)
+public class ManagedRouteNodePrefixIdTest extends ManagementTestSupport {
+
+    @Test
+    public void testNodeIdPrefix() throws Exception {
+        // get the stats for the route
+        MBeanServer mbeanServer = getMBeanServer();
+
+        Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null);
+        assertEquals(4, set.size());
+
+        System.out.println(set);
+
+        // hardcoded ids should also be prefixed
+        ManagedProcessorMBean mb
+                = context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class)
+                        .getManagedProcessor("aaamyMock");
+        assertEquals("aaamyMock", mb.getProcessorId());
+        assertEquals("foo", mb.getRouteId());
+        assertEquals("aaa", mb.getNodePrefixId());
+        mb = context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class).getManagedProcessor("bbbmyMock");
+        assertEquals("bbbmyMock", mb.getProcessorId());
+        assertEquals("bar", mb.getRouteId());
+        assertEquals("bbb", mb.getNodePrefixId());
+
+        // auto assigned ids should be prefixed
+        mb = context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class).getManagedProcessor("aaalog2",
+                ManagedLogMBean.class);
+        assertEquals("aaalog2", mb.getProcessorId());
+        assertEquals("foo", mb.getRouteId());
+        assertEquals("aaa", mb.getNodePrefixId());
+        mb = context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class).getManagedProcessor("bbblog4",
+                ManagedLogMBean.class);
+        assertEquals("bbblog4", mb.getProcessorId());
+        assertEquals("bar", mb.getRouteId());
+        assertEquals("bbb", mb.getNodePrefixId());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:foo").routeId("foo").nodePrefixId("aaa")
+                        .to("mock:foo").id("myMock")
+                        .log("Hello foo");
+
+                from("direct:bar").nodePrefixId("bbb").routeId("bar")
+                        .to("mock:bar").id("myMock")
+                        .log("Hello bar");
+            }
+        };
+    }
+
+}
diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_3.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_3.adoc
index 29e4cb334bf..87d278e8df1 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_3.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_3.adoc
@@ -45,6 +45,11 @@ from("seda:c")
   .to("seda:d")
 ----
 
+=== camel-management
+
+If the `nodeIdPrefix` has been configured on routes, then the MBeans for the processors will now use the prefix
+in their `ObjectName` also.
+
 === camel-jbang
 
 The `camel transform` command has been renamed to `camel transform route` as this command is used for transforming