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 2021/12/04 11:13:04 UTC

[camel-karaf] branch main updated: CAMEL-17272: camel-spring-xml - Classic Spring XML add support for external route configuration XML files

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

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


The following commit(s) were added to refs/heads/main by this push:
     new cb013de  CAMEL-17272: camel-spring-xml - Classic Spring XML add support for external route configuration XML files
cb013de is described below

commit cb013de8b900278617eeb2ededb97997e01f62ce
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Dec 4 11:52:34 2021 +0100

    CAMEL-17272: camel-spring-xml - Classic Spring XML add support for external route configuration XML files
---
 .../org/apache/camel/blueprint/jaxb.index          |  1 +
 .../camel/blueprint/CamelContextFactoryBean.java   | 12 +++++
 .../CamelRouteConfigurationContextFactoryBean.java | 39 ++++++++++++++
 .../blueprint/handler/CamelNamespaceHandler.java   | 62 ++++++++++++++++++----
 4 files changed, 105 insertions(+), 9 deletions(-)

diff --git a/components/camel-blueprint/src/generated/resources/org/apache/camel/blueprint/jaxb.index b/components/camel-blueprint/src/generated/resources/org/apache/camel/blueprint/jaxb.index
index bab0bd6..55cce40 100644
--- a/components/camel-blueprint/src/generated/resources/org/apache/camel/blueprint/jaxb.index
+++ b/components/camel-blueprint/src/generated/resources/org/apache/camel/blueprint/jaxb.index
@@ -8,6 +8,7 @@ CamelProducerTemplateFactoryBean
 CamelProxyFactoryBean
 CamelRedeliveryPolicyFactoryBean
 CamelRestContextFactoryBean
+CamelRouteConfigurationContextFactoryBean
 CamelRouteContextFactoryBean
 CamelRouteTemplateContextFactoryBean
 CamelThreadPoolFactoryBean
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
index 34fec02..982e7ef 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java
@@ -62,6 +62,7 @@ import org.apache.camel.model.PackageScanDefinition;
 import org.apache.camel.model.Resilience4jConfigurationDefinition;
 import org.apache.camel.model.RestContextRefDefinition;
 import org.apache.camel.model.RouteBuilderDefinition;
+import org.apache.camel.model.RouteConfigurationContextRefDefinition;
 import org.apache.camel.model.RouteConfigurationDefinition;
 import org.apache.camel.model.RouteContextRefDefinition;
 import org.apache.camel.model.RouteDefinition;
@@ -207,6 +208,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu
     private FaultToleranceConfigurationDefinition defaultFaultToleranceConfiguration;
     @XmlElement(name = "faultToleranceConfiguration", type = FaultToleranceConfigurationDefinition.class)
     private List<FaultToleranceConfigurationDefinition> faultToleranceConfigurations;
+    @XmlElement(name = "routeConfigurationContextRef")
+    private List<RouteConfigurationContextRefDefinition> routeConfigurationRefs = new ArrayList<>();
     @XmlElement(name = "routeTemplateContextRef")
     private List<RouteTemplateContextRefDefinition> routeTemplateRefs = new ArrayList<>();
     @XmlElement(name = "routeBuilder")
@@ -621,6 +624,15 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu
     }
 
     @Override
+    public List<RouteConfigurationContextRefDefinition> getRouteConfigurationRefs() {
+        return routeConfigurationRefs;
+    }
+
+    public void setRouteConfigurationRefs(List<RouteConfigurationContextRefDefinition> routeConfigurationRefs) {
+        this.routeConfigurationRefs = routeConfigurationRefs;
+    }
+
+    @Override
     public List<RouteTemplateContextRefDefinition> getRouteTemplateRefs() {
         return routeTemplateRefs;
     }
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelRouteConfigurationContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelRouteConfigurationContextFactoryBean.java
new file mode 100644
index 0000000..3f44611
--- /dev/null
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelRouteConfigurationContextFactoryBean.java
@@ -0,0 +1,39 @@
+/*
+ * 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.blueprint;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.model.IdentifiedType;
+import org.apache.camel.model.RouteConfigurationDefinition;
+
+@XmlRootElement(name = "routeConfigurationContext")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class CamelRouteConfigurationContextFactoryBean extends IdentifiedType {
+
+    @XmlElement(name = "routeConfiguration", required = true)
+    private List<RouteConfigurationDefinition> routeConfigurations = new ArrayList<>();
+
+    public List<RouteConfigurationDefinition> getRouteConfigurations() {
+        return routeConfigurations;
+    }
+}
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
index 3111b48..b633cb6 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
@@ -34,6 +34,7 @@ import javax.xml.bind.Binder;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 
+import org.apache.camel.blueprint.CamelRouteConfigurationContextFactoryBean;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
@@ -131,6 +132,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
 
     private static final String CAMEL_CONTEXT = "camelContext";
     private static final String ROUTE_CONTEXT = "routeContext";
+    private static final String ROUTE_CONFIGURATION_CONTEXT = "routeConfigurationContext";
     private static final String ROUTE_TEMPLATE_CONTEXT = "routeTemplateContext";
     private static final String REST_CONTEXT = "restContext";
     private static final String ENDPOINT = "endpoint";
@@ -209,6 +211,9 @@ public class CamelNamespaceHandler implements NamespaceHandler {
             if (element.getLocalName().equals(ROUTE_CONTEXT)) {
                 return parseRouteContextNode(element, context);
             }
+            if (element.getLocalName().equals(ROUTE_CONFIGURATION_CONTEXT)) {
+                return parseRouteConfigurationContextNode(element, context);
+            }
             if (element.getLocalName().equals(ROUTE_TEMPLATE_CONTEXT)) {
                 return parseRouteTemplateContextNode(element, context);
             }
@@ -364,8 +369,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof CamelRouteContextFactoryBean)) {
@@ -399,6 +403,47 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         return ctx;
     }
 
+    private Metadata parseRouteConfigurationContextNode(Element element, ParserContext context) {
+        LOG.trace("Parsing RouteConfigurationContext {}", element);
+        // now parse the routes with JAXB
+        Binder<Node> binder;
+        try {
+            binder = getJaxbContext().createBinder();
+        } catch (JAXBException e) {
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
+        }
+        Object value = parseUsingJaxb(element, context, binder);
+        if (!(value instanceof CamelRouteConfigurationContextFactoryBean)) {
+            throw new ComponentDefinitionException("Expected an instance of " + CamelRouteConfigurationContextFactoryBean.class);
+        }
+
+        CamelRouteConfigurationContextFactoryBean rcfb = (CamelRouteConfigurationContextFactoryBean) value;
+        String id = rcfb.getId();
+
+        MutablePassThroughMetadata factory = context.createMetadata(MutablePassThroughMetadata.class);
+        factory.setId(".camelBlueprint.passThrough." + id);
+        factory.setObject(new PassThroughCallable<Object>(rcfb));
+
+        MutableBeanMetadata factory2 = context.createMetadata(MutableBeanMetadata.class);
+        factory2.setId(".camelBlueprint.factory." + id);
+        factory2.setFactoryComponent(factory);
+        factory2.setFactoryMethod("call");
+
+        MutableBeanMetadata ctx = context.createMetadata(MutableBeanMetadata.class);
+        ctx.setId(id);
+        ctx.setRuntimeClass(List.class);
+        ctx.setFactoryComponent(factory2);
+        ctx.setFactoryMethod("getRouteConfigurations");
+        // must be lazy as we want CamelContext to be activated first
+        ctx.setActivation(ACTIVATION_LAZY);
+
+        // lets inject the namespaces into any namespace aware POJOs
+        injectNamespaces(element, binder);
+
+        LOG.trace("Parsing RouteConfigurationContext {} done, returning {}", element, ctx);
+        return ctx;
+    }
+
     private Metadata parseRouteTemplateContextNode(Element element, ParserContext context) {
         LOG.trace("Parsing RouteTemplateContext {}", element);
         // now parse the routes with JAXB
@@ -406,8 +451,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof CamelRouteTemplateContextFactoryBean)) {
@@ -448,7 +492,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof CamelRestContextFactoryBean)) {
@@ -489,7 +533,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof CamelEndpointFactoryBean)) {
@@ -530,7 +574,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof KeyStoreParametersFactoryBean)) {
@@ -571,7 +615,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof SecureRandomParametersFactoryBean)) {
@@ -612,7 +656,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
         try {
             binder = getJaxbContext().createBinder();
         } catch (JAXBException e) {
-            throw new ComponentDefinitionException("Failed to create the JAXB binder : " + e, e);
+            throw new ComponentDefinitionException("Failed to create the JAXB binder: " + e, e);
         }
         Object value = parseUsingJaxb(element, context, binder);
         if (!(value instanceof SSLContextParametersFactoryBean)) {