You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/04/11 19:45:54 UTC
[camel-quarkus] 06/06: Ref #4749 - java-joor-dsl - Add templated route support to native mode
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a commit to branch java-joor-dsl-backports
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit dc9d60c56873750f41d9dadbd4c6eefe82543a60
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 | 14 ++++++++--
.../resources/routes/MyRoutesWithTemplate.java | 27 +++++++++++++++++++
.../src/main/resources/routes/MyTemplate.java | 30 ++++++++++++++++++++++
.../quarkus/dsl/java/joor/JavaJoorDslTest.java | 4 +--
5 files changed, 80 insertions(+), 5 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 b52818dc46..c2d2cf3bb0 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;
@@ -42,6 +43,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.MultiCompile;
import org.apache.camel.quarkus.core.deployment.main.CamelMainHelper;
@@ -208,8 +210,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 4a309eceb4..315e6e4cfe 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());
@@ -49,7 +50,9 @@ public class JavaJoorDslRecorder {
// inject context and resource
CamelContextAware.trySetCamelContext(obj, context.getValue());
ResourceAware.trySetResource(obj, ResourceHelper.fromString(location, ""));
- context.getValue().addRoutes((RoutesBuilder) obj);
+ RoutesBuilder builder = (RoutesBuilder) obj;
+ context.getValue().addRoutes(builder);
+ return builder;
} else {
LOG.warn("Ignoring the class {} as it is not of type RoutesBuilder", className);
}
@@ -59,5 +62,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