You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2020/09/28 15:39:33 UTC

[camel-quarkus] 02/03: Work around camel main inability to load REST DSL configs from XML for #1852

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

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

commit daab7141ac2679c74986e604c7cb91ebedf13080
Author: James Netherton <ja...@gmail.com>
AuthorDate: Mon Sep 28 10:32:53 2020 +0100

    Work around camel main inability to load REST DSL configs from XML for #1852
---
 .../quarkus/main/CamelMainRoutesCollector.java     | 53 ++++++++++++++++++++++
 integration-tests/main-xml-io/pom.xml              | 17 +++++++
 .../camel/quarkus/main/CoreMainXmlIoResource.java  |  7 +++
 .../src/main/resources/application.properties      |  3 ++
 .../src/main/resources/rests/my-rests.xml          | 30 ++++++++++++
 .../src/main/resources/templates/my-templates.xml  | 29 ++++++++++++
 .../camel/quarkus/main/CoreMainXmlIoTest.java      | 10 +++-
 integration-tests/main-xml-jaxb/pom.xml            |  4 ++
 .../quarkus/main/CoreMainXmlJaxbResource.java      |  7 +++
 .../src/main/resources/application.properties      |  3 ++
 .../src/main/resources/rests/my-rests.xml          | 30 ++++++++++++
 .../src/main/resources/templates/my-templates.xml  | 29 ++++++++++++
 .../camel/quarkus/main/CoreMainXmlJaxbTest.java    | 10 +++-
 13 files changed, 230 insertions(+), 2 deletions(-)

diff --git a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRoutesCollector.java b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRoutesCollector.java
index 8595f53..db56da9 100644
--- a/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRoutesCollector.java
+++ b/extensions-core/main/runtime/src/main/java/org/apache/camel/quarkus/main/CamelMainRoutesCollector.java
@@ -16,12 +16,22 @@
  */
 package org.apache.camel.quarkus.main;
 
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.main.DefaultRoutesCollector;
+import org.apache.camel.model.rest.RestsDefinition;
 import org.apache.camel.quarkus.core.RegistryRoutesLoader;
+import org.apache.camel.spi.PackageScanResourceResolver;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.StopWatch;
 
 public class CamelMainRoutesCollector extends DefaultRoutesCollector {
     private final RegistryRoutesLoader registryRoutesLoader;
@@ -42,4 +52,47 @@ public class CamelMainRoutesCollector extends DefaultRoutesCollector {
 
         return registryRoutesLoader.collectRoutesFromRegistry(camelContext, excludePattern, includePattern);
     }
+
+    /**
+     * TODO: Remove this when upgrading to Camel > 3.5.0.
+     *
+     * https://github.com/apache/camel-quarkus/issues/1852
+     */
+    @Override
+    public List<RestsDefinition> collectXmlRestsFromDirectory(CamelContext camelContext, String directory) {
+        ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
+        PackageScanResourceResolver resolver = camelContext.adapt(ExtendedCamelContext.class).getPackageScanResourceResolver();
+
+        List<RestsDefinition> answer = new ArrayList<>();
+
+        StopWatch watch = new StopWatch();
+        int count = 0;
+        String[] parts = directory.split(",");
+        for (String part : parts) {
+            log.debug("Loading additional Camel XML rests from: {}", part);
+            try {
+                Set<InputStream> set = resolver.findResources(part);
+                for (InputStream is : set) {
+                    log.debug("Found XML rest from location: {}", part);
+                    RestsDefinition rests = (RestsDefinition) ecc.getXMLRoutesDefinitionLoader().loadRestsDefinition(ecc, is);
+                    if (rests != null) {
+                        answer.add(rests);
+                        IOHelper.close(is);
+                        count += rests.getRests().size();
+                    }
+                }
+            } catch (FileNotFoundException e) {
+                log.debug("No XML rests found in {}. Skipping XML rests detection.", part);
+            } catch (Exception e) {
+                throw RuntimeCamelException.wrapRuntimeException(e);
+            }
+            if (count > 0) {
+                log.info("Loaded {} ({} millis) additional Camel XML rests from: {}", count, watch.taken(), directory);
+            } else {
+                log.info("No additional Camel XML rests discovered from: {}", directory);
+            }
+        }
+
+        return answer;
+    }
 }
diff --git a/integration-tests/main-xml-io/pom.xml b/integration-tests/main-xml-io/pom.xml
index aaa595a..38b6274 100644
--- a/integration-tests/main-xml-io/pom.xml
+++ b/integration-tests/main-xml-io/pom.xml
@@ -48,6 +48,10 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-rest</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-timer</artifactId>
         </dependency>
 
@@ -123,6 +127,19 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-platform-http-deployment</artifactId>
+            <version>${project.version}</version>
+            <type>pom</type>
+            <scope>test</scope>
+            <exclusions>
+                <exclusion>
+                    <groupId>*</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-timer-deployment</artifactId>
             <version>${project.version}</version>
             <type>pom</type>
diff --git a/integration-tests/main-xml-io/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlIoResource.java b/integration-tests/main-xml-io/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlIoResource.java
index 03b225f..d69e170 100644
--- a/integration-tests/main-xml-io/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlIoResource.java
+++ b/integration-tests/main-xml-io/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlIoResource.java
@@ -31,6 +31,7 @@ import javax.ws.rs.core.MediaType;
 
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.TemplatedRouteBuilder;
 
 @Path("/test")
 @ApplicationScoped
@@ -53,6 +54,12 @@ public class CoreMainXmlIoResource {
         JsonArrayBuilder routeBuilders = Json.createArrayBuilder();
         main.configure().getRoutesBuilders().forEach(builder -> routeBuilders.add(builder.getClass().getName()));
 
+        TemplatedRouteBuilder.builder(main.getCamelContext(), "myTemplate")
+                .parameter("name", "Camel Quarkus")
+                .parameter("greeting", "Hello")
+                .routeId("templated-route")
+                .add();
+
         JsonArrayBuilder routes = Json.createArrayBuilder();
         main.getCamelContext().getRoutes().forEach(route -> routes.add(route.getId()));
 
diff --git a/integration-tests/main-xml-io/src/main/resources/application.properties b/integration-tests/main-xml-io/src/main/resources/application.properties
index 3f0170a..de06ddb 100644
--- a/integration-tests/main-xml-io/src/main/resources/application.properties
+++ b/integration-tests/main-xml-io/src/main/resources/application.properties
@@ -23,8 +23,11 @@
 # Camel
 #
 camel.context.name=quarkus-camel-example
+camel.rest.component = platform-http
 
 #
 # Main
 #
 camel.main.xml-routes = classpath:routes/my-routes.xml
+camel.main.xml-rests = classpath:rests/my-rests.xml
+camel.main.xml-route-templates = classpath:templates/my-templates.xml
diff --git a/integration-tests/main-xml-io/src/main/resources/rests/my-rests.xml b/integration-tests/main-xml-io/src/main/resources/rests/my-rests.xml
new file mode 100644
index 0000000..fcbd834
--- /dev/null
+++ b/integration-tests/main-xml-io/src/main/resources/rests/my-rests.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<rests xmlns="http://camel.apache.org/schema/spring">
+    <rest id="greet" path="/greeting">
+        <get uri="/hello">
+            <route id="rest-route">
+                <setBody>
+                    <constant>Hello World!</constant>
+                </setBody>
+            </route>
+        </get>
+    </rest>
+</rests>
diff --git a/integration-tests/main-xml-io/src/main/resources/templates/my-templates.xml b/integration-tests/main-xml-io/src/main/resources/templates/my-templates.xml
new file mode 100644
index 0000000..10416ab
--- /dev/null
+++ b/integration-tests/main-xml-io/src/main/resources/templates/my-templates.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routeTemplates xmlns="http://camel.apache.org/schema/spring">
+    <routeTemplate id="myTemplate">
+        <templateParameter name="name"/>
+        <templateParameter name="greeting"/>
+        <route>
+            <from uri="direct:template"/>
+            <setBody><simple>{{greeting}} ${body}</simple></setBody>
+        </route>
+    </routeTemplate>
+</routeTemplates>
diff --git a/integration-tests/main-xml-io/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlIoTest.java b/integration-tests/main-xml-io/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlIoTest.java
index e773e6c..617c17b 100644
--- a/integration-tests/main-xml-io/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlIoTest.java
+++ b/integration-tests/main-xml-io/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlIoTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.quarkus.main;
 
+import java.util.List;
+
 import javax.ws.rs.core.MediaType;
 
 import io.quarkus.test.junit.QuarkusTest;
@@ -49,8 +51,14 @@ public class CoreMainXmlIoTest {
 
         assertThat(p.getList("routeBuilders", String.class))
                 .isEmpty();
-        assertThat(p.getList("routes", String.class))
+
+        List<String> routes = p.getList("routes", String.class);
+        assertThat(routes)
                 .contains("my-xml-route");
+        assertThat(routes)
+                .contains("templated-route");
+        assertThat(routes)
+                .contains("rest-route");
     }
 
     @Test
diff --git a/integration-tests/main-xml-jaxb/pom.xml b/integration-tests/main-xml-jaxb/pom.xml
index 7bdd38b..344ed86 100644
--- a/integration-tests/main-xml-jaxb/pom.xml
+++ b/integration-tests/main-xml-jaxb/pom.xml
@@ -52,6 +52,10 @@
         </dependency>
         <dependency>
             <groupId>org.apache.camel.quarkus</groupId>
+            <artifactId>camel-quarkus-rest</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-timer</artifactId>
         </dependency>
 
diff --git a/integration-tests/main-xml-jaxb/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbResource.java b/integration-tests/main-xml-jaxb/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbResource.java
index 8b93d78..dab9cf1 100644
--- a/integration-tests/main-xml-jaxb/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbResource.java
+++ b/integration-tests/main-xml-jaxb/src/main/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbResource.java
@@ -28,6 +28,7 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.builder.TemplatedRouteBuilder;
 
 @Path("/test")
 @ApplicationScoped
@@ -47,6 +48,12 @@ public class CoreMainXmlJaxbResource {
         JsonArrayBuilder routeBuilders = Json.createArrayBuilder();
         main.configure().getRoutesBuilders().forEach(builder -> routeBuilders.add(builder.getClass().getName()));
 
+        TemplatedRouteBuilder.builder(main.getCamelContext(), "myTemplate")
+                .parameter("name", "Camel Quarkus")
+                .parameter("greeting", "Hello")
+                .routeId("templated-route")
+                .add();
+
         JsonArrayBuilder routes = Json.createArrayBuilder();
         main.getCamelContext().getRoutes().forEach(route -> routes.add(route.getId()));
 
diff --git a/integration-tests/main-xml-jaxb/src/main/resources/application.properties b/integration-tests/main-xml-jaxb/src/main/resources/application.properties
index 3f0170a..de06ddb 100644
--- a/integration-tests/main-xml-jaxb/src/main/resources/application.properties
+++ b/integration-tests/main-xml-jaxb/src/main/resources/application.properties
@@ -23,8 +23,11 @@
 # Camel
 #
 camel.context.name=quarkus-camel-example
+camel.rest.component = platform-http
 
 #
 # Main
 #
 camel.main.xml-routes = classpath:routes/my-routes.xml
+camel.main.xml-rests = classpath:rests/my-rests.xml
+camel.main.xml-route-templates = classpath:templates/my-templates.xml
diff --git a/integration-tests/main-xml-jaxb/src/main/resources/rests/my-rests.xml b/integration-tests/main-xml-jaxb/src/main/resources/rests/my-rests.xml
new file mode 100644
index 0000000..fcbd834
--- /dev/null
+++ b/integration-tests/main-xml-jaxb/src/main/resources/rests/my-rests.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<rests xmlns="http://camel.apache.org/schema/spring">
+    <rest id="greet" path="/greeting">
+        <get uri="/hello">
+            <route id="rest-route">
+                <setBody>
+                    <constant>Hello World!</constant>
+                </setBody>
+            </route>
+        </get>
+    </rest>
+</rests>
diff --git a/integration-tests/main-xml-jaxb/src/main/resources/templates/my-templates.xml b/integration-tests/main-xml-jaxb/src/main/resources/templates/my-templates.xml
new file mode 100644
index 0000000..10416ab
--- /dev/null
+++ b/integration-tests/main-xml-jaxb/src/main/resources/templates/my-templates.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<routeTemplates xmlns="http://camel.apache.org/schema/spring">
+    <routeTemplate id="myTemplate">
+        <templateParameter name="name"/>
+        <templateParameter name="greeting"/>
+        <route>
+            <from uri="direct:template"/>
+            <setBody><simple>{{greeting}} ${body}</simple></setBody>
+        </route>
+    </routeTemplate>
+</routeTemplates>
diff --git a/integration-tests/main-xml-jaxb/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbTest.java b/integration-tests/main-xml-jaxb/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbTest.java
index 116c0e7..f5f6b24 100644
--- a/integration-tests/main-xml-jaxb/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbTest.java
+++ b/integration-tests/main-xml-jaxb/src/test/java/org/apache/camel/quarkus/main/CoreMainXmlJaxbTest.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.quarkus.main;
 
+import java.util.List;
+
 import javax.ws.rs.core.MediaType;
 
 import io.quarkus.test.junit.QuarkusTest;
@@ -46,7 +48,13 @@ public class CoreMainXmlJaxbTest {
 
         assertThat(p.getList("routeBuilders", String.class))
                 .isEmpty();
-        assertThat(p.getList("routes", String.class))
+
+        List<String> routes = p.getList("routes", String.class);
+        assertThat(routes)
                 .contains("my-xml-route");
+        assertThat(routes)
+                .contains("templated-route");
+        assertThat(routes)
+                .contains("rest-route");
     }
 }