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/08/21 11:55:44 UTC

[camel] 05/14: CAMEL-19765: camel-core - SPI for DumpRouteStrategy.

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

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

commit b9f443d3401ed2480ea9981655bfa9e6383a37cf
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Aug 19 16:46:35 2023 +0200

    CAMEL-19765: camel-core - SPI for DumpRouteStrategy.
---
 .../camel/model/OptionalIdentifiedDefinition.java  |  2 +-
 .../apache/camel/model/RouteDefinitionHelper.java  |  3 +-
 .../java/org/apache/camel/xml/jaxb/JaxbHelper.java | 34 ++++++++++++++++++++++
 .../camel/xml/jaxb/JaxbModelToXMLDumper.java       |  3 ++
 4 files changed, 39 insertions(+), 3 deletions(-)

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 cbc6f1a6e9e..eef35ec6d1c 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
@@ -68,7 +68,7 @@ public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedD
     @Metadata(description = "The id of this node")
     public void setId(String id) {
         this.id = id;
-        customId = true;
+        customId = id != null ? true : null;
     }
 
     @Override
diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
index 4200fbd4e01..d803efc03ad 100644
--- a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
+++ b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteDefinitionHelper.java
@@ -189,8 +189,7 @@ public final class RouteDefinitionHelper {
                 if (!done) {
                     throw new IllegalArgumentException("Cannot auto assign id to route: " + route);
                 }
-                route.setId(id);
-                route.setCustomId(false);
+                route.setGeneratedId(id);
                 customIds.add(route.getId());
             }
             RestDefinition rest = route.getRestDefinition();
diff --git a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
index 5ca7e650c3f..07d77c7a422 100644
--- a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
+++ b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbHelper.java
@@ -496,4 +496,38 @@ public final class JaxbHelper {
         }
     }
 
+    public static void removeAutoAssignedIds(Element element) {
+        final NamedNodeMap attrs = element.getAttributes();
+
+        Attr id = null;
+        Attr customId = null;
+        for (int index = 0; index < attrs.getLength(); index++) {
+            final Attr attr = (Attr) attrs.item(index);
+            final String attName = attr.getName();
+
+            if (attName.equals("id")) {
+                id = attr;
+            } else if (attName.equals("customId")) {
+                customId = attr;
+            }
+        }
+
+        // remove auto-assigned id
+        if (id != null && customId == null) {
+            attrs.removeNamedItem("id");
+        }
+        // remove customId as its noisy
+        if (customId != null) {
+            attrs.removeNamedItem("customId");
+        }
+
+        final NodeList children = element.getChildNodes();
+        for (int index = 0; index < children.getLength(); index++) {
+            final Node child = children.item(index);
+            if (child.getNodeType() == Node.ELEMENT_NODE) {
+                removeAutoAssignedIds((Element) child);
+            }
+        }
+    }
+
 }
diff --git a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
index 4495e74b37a..3bf298e862e 100644
--- a/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
+++ b/core/camel-xml-jaxb/src/main/java/org/apache/camel/xml/jaxb/JaxbModelToXMLDumper.java
@@ -60,6 +60,7 @@ import static org.apache.camel.xml.jaxb.JaxbHelper.extractSourceLocations;
 import static org.apache.camel.xml.jaxb.JaxbHelper.getJAXBContext;
 import static org.apache.camel.xml.jaxb.JaxbHelper.modelToXml;
 import static org.apache.camel.xml.jaxb.JaxbHelper.newXmlConverter;
+import static org.apache.camel.xml.jaxb.JaxbHelper.removeAutoAssignedIds;
 import static org.apache.camel.xml.jaxb.JaxbHelper.resolveEndpointDslUris;
 
 /**
@@ -204,6 +205,8 @@ public class JaxbModelToXMLDumper implements ModelToXMLDumper {
             // okay there were some property placeholder or delegate endpoints
             // replaced so re-create the model
             if (changed.get()) {
+                // remove all generated ID from dom, as we do not want to copy those over
+                removeAutoAssignedIds(dom.getDocumentElement());
                 xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
                 NamedNode copy = modelToXml(context, xml, NamedNode.class);
                 xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context, copy);