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

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

commit 08502da172ec3033aa6eb306d9b0c99b1ce2dc38
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu May 13 08:31:22 2021 +0200

    CAMEL-16607: route template / kamelets. Allow to use #type and #class for 
local beans
---
 .../kamelet/KameletLocalBeanClassTest.java         | 69 +++++++++++++++++++
 .../kamelet/KameletLocalBeanClassTwoTest.java      | 69 +++++++++++++++++++
 .../kamelet/KameletLocalBeanTypeTest.java          | 77 ++++++++++++++++++++++
 .../java/org/apache/camel/impl/DefaultModel.java   | 13 ++--
 .../camel/model/RouteTemplateDefinition.java       |  5 +-
 5 files changed, 223 insertions(+), 10 deletions(-)

diff --git 
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTest.java
 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTest.java
new file mode 100644
index 0000000..b692401
--- /dev/null
+++ 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.component.kamelet;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.http.annotation.Obsolete;
+import org.junit.jupiter.api.Test;
+
+public class KameletLocalBeanClassTest extends CamelTestSupport {
+
+    @Test
+    public void testOne() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hi John we are 
going to Murphys");
+
+        template.sendBody("direct:bar", "John");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    // **********************************************
+    //
+    // test set-up
+    //
+    // **********************************************
+
+    @Obsolete
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("whereTo")
+                        .templateBean("myBar", MyBar.class)
+                        .from("kamelet:source")
+                        // must use {{myBar}} to refer to the local bean
+                        .to("bean:{{myBar}}");
+
+                from("direct:bar")
+                        .kamelet("whereTo")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    public static class MyBar {
+
+        private final String bar = "Murphys";
+
+        public String where(String name) {
+            return "Hi " + name + " we are going to " + bar;
+        }
+    }
+
+}
diff --git 
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTwoTest.java
 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTwoTest.java
new file mode 100644
index 0000000..e57760b
--- /dev/null
+++ 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanClassTwoTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.component.kamelet;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.http.annotation.Obsolete;
+import org.junit.jupiter.api.Test;
+
+public class KameletLocalBeanClassTwoTest extends CamelTestSupport {
+
+    @Test
+    public void testOne() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hi John we are 
going to Murphys");
+
+        template.sendBody("direct:bar", "John");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    // **********************************************
+    //
+    // test set-up
+    //
+    // **********************************************
+
+    @Obsolete
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("whereTo")
+                        .templateBean("myBar", 
"#class:org.apache.camel.component.kamelet.KameletLocalBeanClassTwoTest$MyBar")
+                        .from("kamelet:source")
+                        // must use {{myBar}} to refer to the local bean
+                        .to("bean:{{myBar}}");
+
+                from("direct:bar")
+                        .kamelet("whereTo")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    public static class MyBar {
+
+        private final String bar = "Murphys";
+
+        public String where(String name) {
+            return "Hi " + name + " we are going to " + bar;
+        }
+    }
+
+}
diff --git 
a/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanTypeTest.java
 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanTypeTest.java
new file mode 100644
index 0000000..19d70dc
--- /dev/null
+++ 
b/components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletLocalBeanTypeTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.component.kamelet;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.http.annotation.Obsolete;
+import org.junit.jupiter.api.Test;
+
+public class KameletLocalBeanTypeTest extends CamelTestSupport {
+
+    @BindToRegistry("myBar")
+    private KameletLocalBeanClassTest.MyBar bar = new 
KameletLocalBeanClassTest.MyBar();
+
+    @Test
+    public void testOne() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hi John we are 
going to Murphys");
+
+        template.sendBody("direct:bar", "John");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    // **********************************************
+    //
+    // test set-up
+    //
+    // **********************************************
+
+    @Obsolete
+    protected RoutesBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                routeTemplate("whereTo")
+                        .templateBean("myBar", 
"#type:org.apache.camel.component.kamelet.KameletLocalBeanTypeTest$Bar")
+                        .from("kamelet:source")
+                        // must use {{myBar}} to refer to the local bean
+                        .to("bean:{{myBar}}");
+
+                from("direct:bar")
+                        .kamelet("whereTo")
+                        .to("mock:result");
+            }
+        };
+    }
+
+    public interface Bar {
+        String where(String name);
+    }
+
+    public static class MyBar implements Bar {
+
+        private final String bar = "Murphys";
+
+        public String where(String name) {
+            return "Hi " + name + " we are going to " + bar;
+        }
+    }
+
+}
diff --git 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
index d462721..2388fdc 100644
--- 
a/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
+++ 
b/core/camel-core-engine/src/main/java/org/apache/camel/impl/DefaultModel.java
@@ -385,15 +385,10 @@ public class DefaultModel implements Model {
                         () -> camelContext.getInjector().newInstance(clazz));
             } else if (b.getType() != null && 
b.getType().startsWith("#type:")) {
                 Class<?> clazz = 
camelContext.getClassResolver().resolveMandatoryClass(b.getType().substring(6));
-                routeTemplateContext.bind(b.getName(), clazz,
-                        () -> {
-                            Set<?> set = 
camelContext.getRegistry().findByType(clazz);
-                            if (set.size() == 1) {
-                                return set.iterator().next();
-                            } else {
-                                return null;
-                            }
-                        });
+                Set<?> set = camelContext.getRegistry().findByType(clazz);
+                if (set.size() == 1) {
+                    routeTemplateContext.bind(b.getName(), clazz, 
set.iterator().next());
+                }
             }
         }
     }
diff --git 
a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteTemplateDefinition.java
 
b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteTemplateDefinition.java
index 88139fa..26ae044 100644
--- 
a/core/camel-core-model/src/main/java/org/apache/camel/model/RouteTemplateDefinition.java
+++ 
b/core/camel-core-model/src/main/java/org/apache/camel/model/RouteTemplateDefinition.java
@@ -223,7 +223,7 @@ public class RouteTemplateDefinition extends 
OptionalIdentifiedDefinition {
      * Adds a local bean the route template uses
      *
      * @param name the name of the bean
-     * @param bean the bean or a supplier for the bean
+     * @param bean the bean, or reference to bean (#class or #type), or a 
supplier for the bean
      */
     @SuppressWarnings("unchecked")
     public RouteTemplateDefinition templateBean(String name, Object bean) {
@@ -236,6 +236,9 @@ public class RouteTemplateDefinition extends 
OptionalIdentifiedDefinition {
             def.setBeanSupplier((RouteTemplateContext.BeanSupplier<Object>) 
bean);
         } else if (bean instanceof Supplier) {
             def.setBeanSupplier(ctx -> ((Supplier<?>) bean).get());
+        } else if (bean instanceof String) {
+            // its a string type
+            def.setType((String) bean);
         } else {
             def.setBeanSupplier(ctx -> bean);
         }

Reply via email to