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 2023/04/11 17:53:25 UTC

[camel-quarkus] branch main updated: Ref #4749 - java-joor-dsl - Add templated route support to native mode

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

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


The following commit(s) were added to refs/heads/main by this push:
     new dff1099366 Ref #4749 - java-joor-dsl - Add templated route support to native mode
dff1099366 is described below

commit dff10993667b04fdcda000f130198bb1519b0519
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Fri Apr 7 15:48:05 2023 +0200

    Ref #4749 - java-joor-dsl - Add templated route support to native mode
---
 .../java/joor/deployment/JavaJoorDslProcessor.java | 10 +++++++-
 .../dsl/java/joor/runtime/JavaJoorDslRecorder.java | 11 +++++++-
 .../resources/routes/MyRoutesWithTemplate.java     | 27 +++++++++++++++++++
 .../src/main/resources/routes/MyTemplate.java      | 30 ++++++++++++++++++++++
 .../quarkus/dsl/java/joor/JavaJoorDslTest.java     |  4 +--
 5 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/extensions/java-joor-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/java/joor/deployment/JavaJoorDslProcessor.java b/extensions/java-joor-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/java/joor/deployment/JavaJoorDslProcessor.java
index b149b67cbe..a34f0d8e87 100644
--- a/extensions/java-joor-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/java/joor/deployment/JavaJoorDslProcessor.java
+++ b/extensions/java-joor-dsl/deployment/src/main/java/org/apache/camel/quarkus/dsl/java/joor/deployment/JavaJoorDslProcessor.java
@@ -19,6 +19,7 @@ package org.apache.camel.quarkus.dsl.java.joor.deployment;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -40,6 +41,7 @@ import io.quarkus.paths.PathCollection;
 import io.quarkus.runtime.RuntimeValue;
 import io.quarkus.runtime.annotations.RegisterForReflection;
 import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
 import org.apache.camel.dsl.java.joor.CompilationUnit;
 import org.apache.camel.dsl.java.joor.Helper;
 import org.apache.camel.dsl.java.joor.MultiCompile;
@@ -204,8 +206,14 @@ public class JavaJoorDslProcessor {
             CamelContextBuildItem context,
             JavaJoorDslRecorder recorder) throws Exception {
         RuntimeValue<CamelContext> camelContext = context.getCamelContext();
+        List<RoutesBuilder> builders = new ArrayList<>(classes.size());
+        // Register routes first
         for (JavaJoorGeneratedClassBuildItem clazz : classes) {
-            recorder.registerRoutesBuilder(camelContext, clazz.getName(), clazz.getLocation());
+            builders.add(recorder.registerRoutes(camelContext, clazz.getName(), clazz.getLocation()));
+        }
+        // Then register templated routes
+        for (RoutesBuilder builder : builders) {
+            recorder.registerTemplatedRoutes(camelContext, builder);
         }
     }
 }
diff --git a/extensions/java-joor-dsl/runtime/src/main/java/org/apache/camel/quarkus/dsl/java/joor/runtime/JavaJoorDslRecorder.java b/extensions/java-joor-dsl/runtime/src/main/java/org/apache/camel/quarkus/dsl/java/joor/runtime/JavaJoorDslRecorder.java
index 246627e7d6..e94de5bcae 100644
--- a/extensions/java-joor-dsl/runtime/src/main/java/org/apache/camel/quarkus/dsl/java/joor/runtime/JavaJoorDslRecorder.java
+++ b/extensions/java-joor-dsl/runtime/src/main/java/org/apache/camel/quarkus/dsl/java/joor/runtime/JavaJoorDslRecorder.java
@@ -35,7 +35,8 @@ public class JavaJoorDslRecorder {
 
     private static final Logger LOG = LoggerFactory.getLogger(JavaJoorDslRecorder.class);
 
-    public void registerRoutesBuilder(RuntimeValue<CamelContext> context, String className, String location) throws Exception {
+    public RoutesBuilder registerRoutes(RuntimeValue<CamelContext> context, String className, String location)
+            throws Exception {
         Class<?> clazz = Class.forName(className);
         boolean skip = clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())
                 || Modifier.isPrivate(clazz.getModifiers());
@@ -50,6 +51,7 @@ public class JavaJoorDslRecorder {
                     CamelContextAware.trySetCamelContext(obj, context.getValue());
                     ResourceAware.trySetResource(obj, ResourceHelper.fromString(location, ""));
                     context.getValue().addRoutes(builder);
+                    return builder;
                 } else {
                     LOG.warn("Ignoring the class {} as it is not of type RoutesBuilder", className);
                 }
@@ -59,5 +61,12 @@ public class JavaJoorDslRecorder {
         } else {
             LOG.warn("Ignoring the class {} as it cannot be instantiated with the default constructor", className);
         }
+        return null;
+    }
+
+    public void registerTemplatedRoutes(RuntimeValue<CamelContext> camelContext, RoutesBuilder builder) throws Exception {
+        if (builder != null) {
+            camelContext.getValue().addTemplatedRoutes(builder);
+        }
     }
 }
diff --git a/integration-tests/java-joor-dsl/src/main/resources/routes/MyRoutesWithTemplate.java b/integration-tests/java-joor-dsl/src/main/resources/routes/MyRoutesWithTemplate.java
new file mode 100644
index 0000000000..4b836f0190
--- /dev/null
+++ b/integration-tests/java-joor-dsl/src/main/resources/routes/MyRoutesWithTemplate.java
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyRoutesWithTemplate extends RouteBuilder {
+    @Override
+    public void configure() throws Exception {
+        templatedRoute("someTemplate")
+                .routeId("routes-with-template")
+                .parameter("id", "routes-with-template")
+                .parameter("body", "Hi PK");
+    }
+}
diff --git a/integration-tests/java-joor-dsl/src/main/resources/routes/MyTemplate.java b/integration-tests/java-joor-dsl/src/main/resources/routes/MyTemplate.java
new file mode 100644
index 0000000000..0e4fe84f66
--- /dev/null
+++ b/integration-tests/java-joor-dsl/src/main/resources/routes/MyTemplate.java
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyTemplate extends RouteBuilder {
+    @Override
+    public void configure() throws Exception {
+        routeTemplate("someTemplate")
+                .templateParameter("id")
+                .templateParameter("body")
+                .route()
+                .from("direct:{{id}}")
+                .setBody().constant("{{body}}")
+                .setBody().simple("${body.endsWith('PK')}");
+    }
+}
diff --git a/integration-tests/java-joor-dsl/src/test/java/org/apache/camel/quarkus/dsl/java/joor/JavaJoorDslTest.java b/integration-tests/java-joor-dsl/src/test/java/org/apache/camel/quarkus/dsl/java/joor/JavaJoorDslTest.java
index 38998a3cb5..767196aeaf 100644
--- a/integration-tests/java-joor-dsl/src/test/java/org/apache/camel/quarkus/dsl/java/joor/JavaJoorDslTest.java
+++ b/integration-tests/java-joor-dsl/src/test/java/org/apache/camel/quarkus/dsl/java/joor/JavaJoorDslTest.java
@@ -76,13 +76,13 @@ class JavaJoorDslTest {
                 .then()
                 .statusCode(200)
                 .body(CoreMatchers.is(
-                        "inner-classes-route,my-java-route,reflection-route,routes-with-bean,routes-with-inner-bean,routes-with-nested-class,routes-with-rest,routes-with-rest-get"));
+                        "inner-classes-route,my-java-route,reflection-route,routes-with-bean,routes-with-inner-bean,routes-with-nested-class,routes-with-rest,routes-with-rest-get,routes-with-template"));
 
         RestAssured.given()
                 .get("/java-joor-dsl/main/successful/routes")
                 .then()
                 .statusCode(200)
-                .body(CoreMatchers.is("3"));
+                .body(CoreMatchers.is("4"));
     }
 
     @Test