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 2023/10/11 07:56:23 UTC

[camel] branch main updated: CAMEL-19945: camel-core - Add bean as property placeholder function (#11674)

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


The following commit(s) were added to refs/heads/main by this push:
     new d2ade752768 CAMEL-19945: camel-core - Add bean as property placeholder function (#11674)
d2ade752768 is described below

commit d2ade752768f38fdb1f64df8255b0d1768caa099
Author: TUCJVXCB <tt...@hotmail.com>
AuthorDate: Wed Oct 11 15:56:16 2023 +0800

    CAMEL-19945: camel-core - Add bean as property placeholder function (#11674)
    
    * CAMEL-19945: camel-core - Add bean as property placeholder function
    
    * CAMEL-19945: camel-core - Add bean as property placeholder function
    
    * CAMEL-19945: camel-core - Add bean as property placeholder function
    
    * CAMEL-19945: add unit test
    
    * CAMEL-19945: update docs
---
 .../org/apache/camel/properties-function/bean      |  2 +
 .../component/bean/BeanPropertiesFunction.java     | 66 ++++++++++++++++++++++
 .../component/bean/BeanPropertiesFunctionTest.java | 63 +++++++++++++++++++++
 .../ROOT/pages/using-propertyplaceholder.adoc      | 15 +++++
 4 files changed, 146 insertions(+)

diff --git a/components/camel-bean/src/generated/resources/META-INF/services/org/apache/camel/properties-function/bean b/components/camel-bean/src/generated/resources/META-INF/services/org/apache/camel/properties-function/bean
new file mode 100644
index 00000000000..f45cf126cf1
--- /dev/null
+++ b/components/camel-bean/src/generated/resources/META-INF/services/org/apache/camel/properties-function/bean
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.component.bean.BeanPropertiesFunction
diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanPropertiesFunction.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanPropertiesFunction.java
new file mode 100644
index 00000000000..823676307b6
--- /dev/null
+++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanPropertiesFunction.java
@@ -0,0 +1,66 @@
+/*
+ * 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.bean;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.spi.PropertiesFunction;
+import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.ObjectHelper;
+import org.apache.camel.util.StringHelper;
+
+@org.apache.camel.spi.annotations.PropertiesFunction("bean")
+public class BeanPropertiesFunction implements PropertiesFunction, CamelContextAware {
+    private CamelContext camelContext;
+
+    @Override
+    public String getName() {
+        return "bean";
+    }
+
+    @Override
+    public String apply(String remainder) {
+        if (StringHelper.countChar(remainder, '.') != 1 || remainder.startsWith(".") || remainder.endsWith(".")) {
+            throw new IllegalArgumentException("BeanName and methodName should be separated by a dot.");
+        }
+        String[] beanNameAndMethodName = remainder.split("\\.");
+        String beanName = beanNameAndMethodName[0];
+        String methodName = beanNameAndMethodName[1];
+
+        Object bean = CamelContextHelper.mandatoryLookup(camelContext, beanName);
+
+        String answer = "";
+        try {
+            answer += camelContext.getTypeConverter().convertTo(String.class, ObjectHelper.invokeMethodSafe(methodName, bean));
+        } catch (Exception e) {
+            throw RuntimeCamelException.wrapRuntimeCamelException(e);
+        }
+        return answer;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanPropertiesFunctionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanPropertiesFunctionTest.java
new file mode 100644
index 00000000000..b9e4fc6e6f2
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanPropertiesFunctionTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.bean;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.junit.jupiter.api.Test;
+
+public class BeanPropertiesFunctionTest extends ContextTestSupport {
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        Registry registry = super.createRegistry();
+        registry.bind("fooBean", new BeanPropertiesFunctionTest.FooBean());
+        registry.bind("barBean", new BeanPropertiesFunctionTest.BarBean());
+        return registry;
+    }
+
+    @Test
+    public void testParseEndpoint() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:{{bean:fooBean.foo}}").to("mock:{{bean:barBean.bar}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:foo", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+    public static class FooBean {
+
+        public String foo() {
+            return "foo";
+        }
+    }
+
+    public static class BarBean {
+
+        public String bar() {
+            return "bar";
+        }
+    }
+}
diff --git a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
index 9dc6e8a0fa7..c2ac92e5f0d 100644
--- a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
+++ b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
@@ -438,6 +438,7 @@ The xref:components::properties-component.adoc[Properties] component includes th
 
 * `env` - A function to lookup the property from OS environment variables
 * `sys` - A function to lookup the property from Java JVM system properties
+* `bean` - A function to lookup the property from the return value of bean's method
 * `service` - A function to lookup the property from OS environment variables using the service naming idiom
 * `service.name` - A function to lookup the property from OS environment variables using the service naming idiom returning the hostname part only
 * `service.port` - A function to lookup the property from OS environment variables using the service naming idiom returning the port part only
@@ -506,6 +507,20 @@ And we can use default values if the service has not been defined, for example t
 </camelContext>
 ----
 
+The bean function is for looking up the property from the return value of bean's method.
+
+Assuming we have registered a bean named 'foo' that has a method called 'bar' that returns a directory name, then we can refer to the bean's method in the camel endpoint url, and use the FILE component to poll a directory:
+
+[source,xml]
+----
+<camelContext>
+<route>
+    <from uri="file:{{bean:foo.bar}}"/>
+    <to uri="direct:result"/>
+</route>
+</camelContext>
+----
+
 === Using Kubernetes property placeholder functions
 
 The `camel-kubernetes` component include the following functions: