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 2020/02/04 12:02:25 UTC

[camel] 02/02: CAMEL-14484: Make XML routes loader using JAXB into its own module

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

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

commit 6666db12ba149d668ead00c99b245ae719cd387c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Feb 4 12:46:17 2020 +0100

    CAMEL-14484: Make XML routes loader using JAXB into its own module
---
 core/camel-core-engine/pom.xml                     |  8 +--
 .../java/org/apache/camel/model/ModelHelper.java   | 80 ++--------------------
 core/camel-core/pom.xml                            |  4 ++
 core/camel-xml-jaxb/pom.xml                        | 11 ---
 .../karaf/features/src/main/resources/features.xml |  1 +
 5 files changed, 14 insertions(+), 90 deletions(-)

diff --git a/core/camel-core-engine/pom.xml b/core/camel-core-engine/pom.xml
index aeca054..ac2ba06 100644
--- a/core/camel-core-engine/pom.xml
+++ b/core/camel-core-engine/pom.xml
@@ -68,10 +68,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-base</artifactId>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.apache.camel</groupId>-->
-<!--            <artifactId>camel-jaxp</artifactId>-->
-<!--        </dependency>-->
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jaxp</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-management-api</artifactId>
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/model/ModelHelper.java b/core/camel-core-engine/src/main/java/org/apache/camel/model/ModelHelper.java
index 9e36bd4..da3a878 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/model/ModelHelper.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/model/ModelHelper.java
@@ -17,7 +17,6 @@
 package org.apache.camel.model;
 
 import java.io.ByteArrayInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
@@ -32,7 +31,6 @@ import javax.xml.bind.Binder;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
-import javax.xml.bind.Unmarshaller;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.TransformerException;
 
@@ -50,12 +48,10 @@ import org.apache.camel.NamedNode;
 import org.apache.camel.TypeConversionException;
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.model.language.ExpressionDefinition;
-import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.RestsDefinition;
 import org.apache.camel.spi.ModelJAXBContextFactory;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.spi.TypeConverterRegistry;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.xml.XmlLineNumberParser;
 
 import static org.apache.camel.model.ProcessorDefinitionHelper.filterTypeInOutputs;
@@ -257,83 +253,17 @@ public final class ModelHelper {
      */
     @Deprecated
     public static RoutesDefinition loadRoutesDefinition(CamelContext context, InputStream inputStream) throws Exception {
-        XmlConverter xmlConverter = newXmlConverter(context);
-        Document dom = xmlConverter.toDOMDocument(inputStream, null);
-        return loadRoutesDefinition(context, dom);
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        return (RoutesDefinition) ecc.getXMLRoutesDefinitionLoader().loadRoutesDefinition(context, inputStream);
     }
 
     /**
-     * Marshal the xml to the model definition
-     *
-     * @param context the CamelContext, if <tt>null</tt> then
-     *            {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in
-     *            use
-     * @param node the xml node
-     * @throws Exception is thrown if an error is encountered unmarshalling from
-     *             xml to model
      * @deprecated use {@link org.apache.camel.spi.XMLRoutesDefinitionLoader} from {@link ExtendedCamelContext}
      */
     @Deprecated
-    public static RoutesDefinition loadRoutesDefinition(CamelContext context, Node node) throws Exception {
-        JAXBContext jaxbContext = getJAXBContext(context);
-
-        Map<String, String> namespaces = new LinkedHashMap<>();
-
-        Document dom = node instanceof Document ? (Document)node : node.getOwnerDocument();
-        extractNamespaces(dom, namespaces);
-
-        Binder<Node> binder = jaxbContext.createBinder();
-        Object result = binder.unmarshal(node);
-
-        if (result == null) {
-            throw new JAXBException("Cannot unmarshal to RoutesDefinition using JAXB");
-        }
-
-        // can either be routes or a single route
-        RoutesDefinition answer;
-        if (result instanceof RouteDefinition) {
-            RouteDefinition route = (RouteDefinition)result;
-            answer = new RoutesDefinition();
-            applyNamespaces(route, namespaces);
-            answer.getRoutes().add(route);
-        } else if (result instanceof RoutesDefinition) {
-            answer = (RoutesDefinition)result;
-            for (RouteDefinition route : answer.getRoutes()) {
-                applyNamespaces(route, namespaces);
-            }
-        } else {
-            throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
-        }
-
-        return answer;
-    }
-
-    /**
-     * @deprecated use {@link org.apache.camel.spi.XMLRoutesDefinitionLoader} from {@link ExtendedCamelContext}
-     */
-    @Deprecated
-    public static RestsDefinition loadRestsDefinition(CamelContext context, InputStream is) throws Exception {
-        // load routes using JAXB
-        Unmarshaller unmarshaller = getJAXBContext(context).createUnmarshaller();
-        Object result = unmarshaller.unmarshal(is);
-
-        if (result == null) {
-            throw new IOException("Cannot unmarshal to rests using JAXB from input stream: " + is);
-        }
-
-        // can either be routes or a single route
-        RestsDefinition answer;
-        if (result instanceof RestDefinition) {
-            RestDefinition rest = (RestDefinition)result;
-            answer = new RestsDefinition();
-            answer.getRests().add(rest);
-        } else if (result instanceof RestsDefinition) {
-            answer = (RestsDefinition)result;
-        } else {
-            throw new IllegalArgumentException("Unmarshalled object is an unsupported type: " + ObjectHelper.className(result) + " -> " + result);
-        }
-
-        return answer;
+    public static RestsDefinition loadRestsDefinition(CamelContext context, InputStream inputStream) throws Exception {
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        return (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRestsDefinition(context, inputStream);
     }
 
     private static <T extends NamedNode> T modelToXml(CamelContext context, InputStream is, String xml, Class<T> type) throws JAXBException {
diff --git a/core/camel-core/pom.xml b/core/camel-core/pom.xml
index 915c28f..a62cd52 100644
--- a/core/camel-core/pom.xml
+++ b/core/camel-core/pom.xml
@@ -133,6 +133,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-xslt</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-xml-jaxb</artifactId>
+        </dependency>
 
         <!-- required logging api dependency by camel-core -->
         <dependency>
diff --git a/core/camel-xml-jaxb/pom.xml b/core/camel-xml-jaxb/pom.xml
index b7eaaf5..0c3f919 100644
--- a/core/camel-xml-jaxb/pom.xml
+++ b/core/camel-xml-jaxb/pom.xml
@@ -42,17 +42,6 @@
             <artifactId>camel-jaxp</artifactId>
         </dependency>
 
-        <!-- testing -->
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-simple</artifactId>
-            <scope>test</scope>
-        </dependency>
     </dependencies>
 
 </project>
diff --git a/platforms/karaf/features/src/main/resources/features.xml b/platforms/karaf/features/src/main/resources/features.xml
index c806b5e..a183476 100644
--- a/platforms/karaf/features/src/main/resources/features.xml
+++ b/platforms/karaf/features/src/main/resources/features.xml
@@ -60,6 +60,7 @@
     <bundle>mvn:org.apache.camel/camel-core-osgi/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-cloud/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-jaxp/${project.version}</bundle>
+    <bundle>mvn:org.apache.camel/camel-xml-jaxb/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-main/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-caffeine-lrucache/${project.version}</bundle>
     <bundle>mvn:org.apache.camel/camel-tooling-model/${project.version}</bundle>