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 2022/02/01 17:39:04 UTC

[camel] 04/13: CAMEL-17569: camel-jbang and java-dsl: Support loading camel configuration classes, and servicies (BindToRegistry). WIP.

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.git

commit b665754f544f4a9f7a9655b506d4383d37bb92fd
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Feb 1 10:46:34 2022 +0100

    CAMEL-17569: camel-jbang and java-dsl: Support loading camel configuration classes, and servicies (BindToRegistry). WIP.
---
 .../dsl/java/joor/JavaRoutesBuilderLoader.java     | 15 +++++---
 .../camel/dsl/java/joor/JavaMainConverterTest.java | 45 ++++++++++++++++++++++
 .../src/test/resources/routes/MyConverter.java     | 35 +++++++++++++++++
 .../test/resources/routes/MyConverterRoute.java    | 27 +++++++++++++
 4 files changed, 116 insertions(+), 6 deletions(-)

diff --git a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
index a5d1474..6617a3c 100644
--- a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
+++ b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
@@ -55,15 +55,18 @@ public class JavaRoutesBuilderLoader extends RouteBuilderLoaderSupport {
             final String content = IOHelper.loadText(is);
             final String name = determineName(resource, content);
 
-            Object obj = Reflect.compile(name, content).create().get();
+            Reflect ref = Reflect.compile(name, content).create();
+            Class<?> clazz = ref.type();
+
+            if (clazz.getAnnotation(Converter.class) != null) {
+                getCamelContext().getTypeConverterRegistry().addTypeConverters(clazz);
+                return null;
+            }
+
+            Object obj = ref.get();
             if (obj instanceof RouteBuilder) {
                 return (RouteBuilder) obj;
             } else if (obj != null) {
-                Converter tc = obj.getClass().getAnnotation(Converter.class);
-                if (tc != null) {
-                    getCamelContext().getTypeConverterRegistry().addTypeConverters(obj);
-                    return null;
-                }
                 // is the bean a custom bean
                 BindToRegistry bir = obj.getClass().getAnnotation(BindToRegistry.class);
                 if (bir != null) {
diff --git a/dsl/camel-java-joor-dsl/src/test/java/org/apache/camel/dsl/java/joor/JavaMainConverterTest.java b/dsl/camel-java-joor-dsl/src/test/java/org/apache/camel/dsl/java/joor/JavaMainConverterTest.java
new file mode 100644
index 0000000..38beeec
--- /dev/null
+++ b/dsl/camel-java-joor-dsl/src/test/java/org/apache/camel/dsl/java/joor/JavaMainConverterTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.dsl.java.joor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.dsl.java.joor.support.MyUser;
+import org.apache.camel.main.Main;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class JavaMainConverterTest {
+
+    @Test
+    public void testMainConverter() throws Exception {
+        Main main = new Main();
+        main.configure().withRoutesIncludePattern("routes/MyConverterRoute.java,routes/MyConverter.java");
+        main.start();
+
+        CamelContext camelContext = main.getCamelContext();
+        assertNotNull(camelContext);
+
+        MyUser out = main.getCamelTemplate().requestBody("direct:start", "Donald", MyUser.class);
+        assertEquals("Donald", out.getName());
+        assertEquals(42, out.getAge());
+
+        main.stop();
+    }
+
+}
diff --git a/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverter.java b/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverter.java
new file mode 100644
index 0000000..7c8921b
--- /dev/null
+++ b/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverter.java
@@ -0,0 +1,35 @@
+/*
+ * 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.Converter;
+
+import org.apache.camel.dsl.java.joor.support.MyUser;
+
+@Converter
+public final class MyConverter {
+
+    private MyConverter() {
+    }
+
+    @Converter
+    public static MyUser toMyUser(String s) {
+        MyUser user = new MyUser();
+        user.setName(s);
+        user.setAge(42);
+        return user;
+    }
+
+}
\ No newline at end of file
diff --git a/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverterRoute.java b/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverterRoute.java
new file mode 100644
index 0000000..637e488
--- /dev/null
+++ b/dsl/camel-java-joor-dsl/src/test/resources/routes/MyConverterRoute.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;
+import org.apache.camel.dsl.java.joor.support.MyUser;
+
+public class MyConverterRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        from("direct:start")
+                .convertBodyTo(MyUser.class);
+    }
+}
\ No newline at end of file