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

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

commit 23b48f10d5e5b2f642c92ef6fab980c95e0145d4
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Mar 24 10:31:59 2023 +0100

    CAMEL-19156: XML routes loader (jaxb) should load routes-configurations
---
 .../java/org/apache/camel/xml/jaxb/JaxbHelper.java | 62 ++++++++++++++++++++++
 .../dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java   | 21 +++++++-
 2 files changed, 82 insertions(+), 1 deletion(-)

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 460c5df1bbe..9e22aec53f9 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
@@ -18,6 +18,7 @@ package org.apache.camel.xml.jaxb;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -42,6 +43,9 @@ import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.model.ExpressionNode;
 import org.apache.camel.model.FromDefinition;
 import org.apache.camel.model.OptionalIdentifiedDefinition;
+import org.apache.camel.model.OutputDefinition;
+import org.apache.camel.model.RouteConfigurationDefinition;
+import org.apache.camel.model.RouteConfigurationsDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RouteTemplateDefinition;
 import org.apache.camel.model.RouteTemplatesDefinition;
@@ -199,6 +203,24 @@ public final class JaxbHelper {
         }
     }
 
+    public static void applyNamespaces(RouteConfigurationDefinition config, 
Map<String, String> namespaces) {
+        List<OutputDefinition<?>> defs = new ArrayList<>();
+        defs.addAll(config.getIntercepts());
+        defs.addAll(config.getInterceptFroms());
+        defs.addAll(config.getInterceptSendTos());
+        defs.addAll(config.getOnCompletions());
+        defs.addAll(config.getOnExceptions());
+        for (OutputDefinition<?> def : defs) {
+            Collection<ExpressionNode> col = 
filterTypeInOutputs(def.getOutputs(), ExpressionNode.class);
+            for (ExpressionNode en : col) {
+                NamespaceAware na = getNamespaceAwareFromExpression(en);
+                if (na != null) {
+                    na.setNamespaces(namespaces);
+                }
+            }
+        }
+    }
+
     public static <T extends NamedNode> T modelToXml(CamelContext context, 
String xml, Class<T> type) throws Exception {
         JAXBContext jaxbContext = getJAXBContext(context);
 
@@ -284,6 +306,46 @@ public final class JaxbHelper {
         return answer;
     }
 
+    public static RouteConfigurationsDefinition 
loadRouteConfigurationsDefinition(CamelContext context, InputStream inputStream)
+            throws Exception {
+        XmlConverter xmlConverter = newXmlConverter(context);
+        Document dom = xmlConverter.toDOMDocument(inputStream, null);
+
+        JAXBContext jaxbContext = getJAXBContext(context);
+
+        Map<String, String> namespaces = new LinkedHashMap<>();
+        extractNamespaces(dom, namespaces);
+        if (!namespaces.containsValue(CAMEL_NS)) {
+            addNamespaceToDom(dom);
+        }
+
+        Binder<Node> binder = jaxbContext.createBinder();
+        Object result = binder.unmarshal(dom);
+
+        if (result == null) {
+            throw new JAXBException("Cannot unmarshal to 
RouteConfigurationsDefinition using JAXB");
+        }
+
+        // can either be routes or a single route
+        RouteConfigurationsDefinition answer;
+        if (result instanceof RouteConfigurationDefinition) {
+            RouteConfigurationDefinition config = 
(RouteConfigurationDefinition) result;
+            answer = new RouteConfigurationsDefinition();
+            applyNamespaces(config, namespaces);
+            answer.getRouteConfigurations().add(config);
+        } else if (result instanceof RouteConfigurationsDefinition) {
+            answer = (RouteConfigurationsDefinition) result;
+            for (RouteConfigurationDefinition config : 
answer.getRouteConfigurations()) {
+                applyNamespaces(config, namespaces);
+            }
+        } else {
+            // ignore not supported type
+            return null;
+        }
+
+        return answer;
+    }
+
     public static RouteTemplatesDefinition 
loadRouteTemplatesDefinition(CamelContext context, InputStream inputStream)
             throws Exception {
         XmlConverter xmlConverter = newXmlConverter(context);
diff --git 
a/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
 
b/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
index 48e3453d84d..544c7b179ac 100644
--- 
a/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
+++ 
b/dsl/camel-xml-jaxb-dsl/src/main/java/org/apache/camel/dsl/xml/jaxb/JaxbXmlRoutesBuilderLoader.java
@@ -21,7 +21,11 @@ import java.io.InputStream;
 import org.apache.camel.CamelContextAware;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteConfigurationBuilder;
 import org.apache.camel.dsl.support.RouteBuilderLoaderSupport;
+import org.apache.camel.model.ModelCamelContext;
+import org.apache.camel.model.RouteConfigurationDefinition;
+import org.apache.camel.model.RouteConfigurationsDefinition;
 import org.apache.camel.model.RouteDefinition;
 import org.apache.camel.model.RouteTemplatesDefinition;
 import org.apache.camel.model.RoutesDefinition;
@@ -31,6 +35,7 @@ import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.annotations.RoutesLoader;
 
 import static org.apache.camel.xml.jaxb.JaxbHelper.loadRestsDefinition;
+import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadRouteConfigurationsDefinition;
 import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadRouteTemplatesDefinition;
 import static org.apache.camel.xml.jaxb.JaxbHelper.loadRoutesDefinition;
 import static 
org.apache.camel.xml.jaxb.JaxbHelper.loadTemplatedRoutesDefinition;
@@ -46,7 +51,7 @@ public class JaxbXmlRoutesBuilderLoader extends 
RouteBuilderLoaderSupport {
 
     @Override
     public RouteBuilder doLoadRouteBuilder(Resource resource) throws Exception 
{
-        return new RouteBuilder() {
+        return new RouteConfigurationBuilder() {
             @Override
             public void configure() throws Exception {
                 // we use configure to load the routes
@@ -83,6 +88,20 @@ public class JaxbXmlRoutesBuilderLoader extends 
RouteBuilderLoaderSupport {
                     }
                 }
             }
+
+            @Override
+            public void configuration() throws Exception {
+                try (InputStream is = resourceInputStream(resource)) {
+                    RouteConfigurationsDefinition configurations = 
loadRouteConfigurationsDefinition(getCamelContext(), is);
+                    if (configurations != null) {
+                        for (RouteConfigurationDefinition config : 
configurations.getRouteConfigurations()) {
+                            CamelContextAware.trySetCamelContext(config, 
getCamelContext());
+                            ((ModelCamelContext) 
getCamelContext()).addRouteConfiguration(config);
+                        }
+                    }
+                }
+            }
+
         };
     }
 }

Reply via email to