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 2020/09/30 16:57:05 UTC

[camel] 02/02: CAMEL-15602: camel-main - Add support for property placeholders in #class factory method parameters with camel.beans. style.

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

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

commit 002a4b00a633fbbe73c07cd35da3bec85cbab885
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 30 18:56:13 2020 +0200

    CAMEL-15602: camel-main - Add support for property placeholders in #class factory method parameters with camel.beans. style.
---
 .../main/MainBeansClassFactoryMethodTest.java      |  89 ++++++++++++++
 ...opertyBindingSupportClassFactoryMethodTest.java | 133 +++++++++++++++++++++
 .../camel/support/PropertyBindingSupport.java      |   4 +
 3 files changed, 226 insertions(+)

diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainBeansClassFactoryMethodTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansClassFactoryMethodTest.java
new file mode 100644
index 0000000..5622827
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainBeansClassFactoryMethodTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.main;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class MainBeansClassFactoryMethodTest {
+
+    @Test
+    public void testBindBeans() throws Exception {
+        MyFoo myFoo = new MyFoo();
+
+        Main main = new Main();
+        main.configure().addRoutesBuilder(new MyRouteBuilder());
+        main.bind("myFoolish", myFoo);
+
+        // create by class
+        main.addProperty("myUrl", "localhost:2121");
+        main.addProperty("myUsername", "scott");
+        main.addProperty("myPassword", "tiger");
+        main.addProperty("camel.beans.driver",
+                "#class:" + MyDriver.class.getName() + "('{{myUrl}}', '{{myUsername}}', '{{myPassword}}')");
+
+        main.start();
+
+        CamelContext camelContext = main.getCamelContext();
+        assertNotNull(camelContext);
+
+        MyDriver driver = camelContext.getRegistry().lookupByNameAndType("driver", MyDriver.class);
+        assertNotNull(driver);
+        assertEquals("localhost:2121", driver.getUrl());
+        assertEquals("scott", driver.getUsername());
+        assertEquals("tiger", driver.getPassword());
+
+        main.stop();
+    }
+
+    public static class MyRouteBuilder extends RouteBuilder {
+        @Override
+        public void configure() throws Exception {
+            from("direct:start").to("mock:foo");
+        }
+    }
+
+    public static class MyDriver {
+
+        private String url;
+        private String username;
+        private String password;
+
+        public MyDriver(String url, String username, String password) {
+            this.url = url;
+            this.username = username;
+            this.password = password;
+        }
+
+        public String getUrl() {
+            return url;
+        }
+
+        public String getUsername() {
+            return username;
+        }
+
+        public String getPassword() {
+            return password;
+        }
+    }
+
+}
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportClassFactoryMethodTest.java b/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportClassFactoryMethodTest.java
new file mode 100644
index 0000000..f48d9c1
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportClassFactoryMethodTest.java
@@ -0,0 +1,133 @@
+/*
+ * 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.main;
+
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.PropertyBindingSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Unit test for PropertyBindingSupport
+ */
+public class PropertyBindingSupportClassFactoryMethodTest {
+
+    @Test
+    public void testFactory() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        context.start();
+
+        MyApp target = new MyApp();
+
+        PropertyBindingSupport.build()
+                .withCamelContext(context)
+                .withTarget(target)
+                .withProperty("name", "Donald")
+                .withProperty("myDriver", "#class:" + MyDriver.class.getName() + "('localhost:2121', 'scott', 'tiger')")
+                .withRemoveParameters(false).bind();
+
+        assertEquals("Donald", target.getName());
+        assertEquals("localhost:2121", target.getMyDriver().getUrl());
+        assertEquals("scott", target.getMyDriver().getUsername());
+        assertEquals("tiger", target.getMyDriver().getPassword());
+
+        context.stop();
+    }
+
+    @Test
+    public void testFactoryPropertyPlaceholder() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        Properties prop = new Properties();
+        prop.put("myUsername", "scott");
+        prop.put("myPassword", "tiger");
+        prop.put("myUrl", "localhost:2121");
+        context.getPropertiesComponent().setInitialProperties(prop);
+
+        context.start();
+
+        MyApp target = new MyApp();
+
+        PropertyBindingSupport.build()
+                .withCamelContext(context)
+                .withTarget(target)
+                .withProperty("name", "Donald")
+                .withProperty("myDriver",
+                        "#class:" + MyDriver.class.getName() + "('{{myUrl}}', '{{myUsername}}', '{{myPassword}}')")
+                .withRemoveParameters(false).bind();
+
+        assertEquals("Donald", target.getName());
+        assertEquals("localhost:2121", target.getMyDriver().getUrl());
+        assertEquals("scott", target.getMyDriver().getUsername());
+        assertEquals("tiger", target.getMyDriver().getPassword());
+
+        context.stop();
+    }
+
+    public static class MyApp {
+
+        private String name;
+        private MyDriver myDriver;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public MyDriver getMyDriver() {
+            return myDriver;
+        }
+
+        public void setMyDriver(MyDriver myDriver) {
+            this.myDriver = myDriver;
+        }
+    }
+
+    public static class MyDriver {
+
+        private String url;
+        private String username;
+        private String password;
+
+        public MyDriver(String url, String username, String password) {
+            this.url = url;
+            this.username = username;
+            this.password = password;
+        }
+
+        public String getUrl() {
+            return url;
+        }
+
+        public String getUsername() {
+            return username;
+        }
+
+        public String getPassword() {
+            return password;
+        }
+    }
+
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 5fca48b..6796e1c 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -1588,6 +1588,10 @@ public final class PropertyBindingSupport {
      * @throws Exception    is thrown if error resolving the bean, or if the value is invalid.
      */
     public static Object resolveBean(CamelContext camelContext, String name, Object value) throws Exception {
+        // resolve placeholders
+        if (value != null) {
+            value = camelContext.resolvePropertyPlaceholders(value.toString());
+        }
         if (value.toString().startsWith("#class:")) {
             // its a new class to be created
             String className = value.toString().substring(7);