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/01/12 17:05:06 UTC

[camel] branch camel-3.14.x updated: CAMEL-17479: configuring beans with {{?foo}} supported.

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

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


The following commit(s) were added to refs/heads/camel-3.14.x by this push:
     new 155437d  CAMEL-17479: configuring beans with {{?foo}} supported.
155437d is described below

commit 155437d7b6d08a1880136fa8aa25bf94d386f70f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 12 18:02:41 2022 +0100

    CAMEL-17479: configuring beans with {{?foo}} supported.
---
 core/camel-main/src/main/docs/main.adoc            | 25 +++++++
 .../PropertyBindingSupportOptionalValueTest.java   | 86 ++++++++++++++++++++++
 2 files changed, 111 insertions(+)

diff --git a/core/camel-main/src/main/docs/main.adoc b/core/camel-main/src/main/docs/main.adoc
index ffc7eee..3b3c318 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -408,6 +408,31 @@ camel.beans.foo.?company = Acme
 Then the company parameter is only set if `MyBean` has this option (silent ignore if no option present).
 Otherwise, if a parameter is set, and the bean does not have such a parameter, then an exception is thrown by Camel.
 
+=== Optional parameter values on beans
+
+If a parameter value on a bean is configured using xref:manual:ROOT:using-propertyplaceholder.adoc[Property Placeholder]
+and the placeholder is optional, then the placeholder can be marked as optional using `?` syntax, as shown:
+
+[source,properties]
+----
+camel.beans.foo = #class:com.foo.MyBean("Hello World", true, 123)
+camel.beans.foo.company = {{?companyName}}
+----
+
+Then the company parameter is only set if there is a property placeholder with the key _companyName_ (silent ignore if no option present).
+
+==== Default parameter values on beans
+
+It is possible to supply a default value (using `:defaultValue`) if the placeholder does not exist as shown:
+
+[source,properties]
+----
+camel.beans.foo = #class:com.foo.MyBean("Hello World", true, 123)
+camel.beans.foo.company = {{?companyName:Acme}}
+----
+
+Here the default value is _Acme_ that will be used if there is no property placeholder with the key _companyName_.
+
 === Nested parameters on beans
 
 You can configure nested parameters separating them via `.` (dot).
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportOptionalValueTest.java b/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportOptionalValueTest.java
new file mode 100644
index 0000000..c1f42d0
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/PropertyBindingSupportOptionalValueTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.ExtendedCamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.BeanIntrospection;
+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 PropertyBindingSupportOptionalValueTest {
+
+    @Test
+    public void testOptionalValue() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        BeanIntrospection bi = context.adapt(ExtendedCamelContext.class).getBeanIntrospection();
+        bi.setExtendedStatistics(true);
+
+        Properties prop = new Properties();
+        prop.setProperty("barOne", "Jack");
+        prop.setProperty("luckyNumber", "42");
+        prop.setProperty("barTwo", "Murphy");
+        context.getPropertiesComponent().setInitialProperties(prop);
+
+        context.start();
+
+        MySecondFoo target = new MySecondFoo();
+
+        PropertyBindingSupport.build()
+                .withCamelContext(context)
+                .withTarget(target)
+                .withProperty("bars[0]", "#class:" + MySecondBar.class.getName())
+                .withProperty("bars[0].names[0]", "{{?barOne:John}}")
+                .withProperty("bars[0].names[1]", "{{?barTwo}}")
+                .withRemoveParameters(false).bind();
+
+        assertEquals(1, target.getBars().size());
+        assertEquals(2, target.getBars().get(0).getNames().size());
+        assertEquals("Jack", target.getBars().get(0).getNames().get(0));
+        assertEquals("Murphy", target.getBars().get(0).getNames().get(1));
+
+        // now update values optional
+        PropertyBindingSupport.build()
+                .withCamelContext(context)
+                .withTarget(target)
+                .withProperty("bars[0].names[0]", "{{?unknown:John}}")
+                .withProperty("bars[0].names[1]", "{{?unknown}}")
+                .withRemoveParameters(false).bind();
+
+        assertEquals(1, target.getBars().size());
+        assertEquals(2, target.getBars().get(0).getNames().size());
+        // will use default value
+        assertEquals("John", target.getBars().get(0).getNames().get(0));
+        // will not change existing value
+        assertEquals("Murphy", target.getBars().get(0).getNames().get(1));
+
+        // will auto detect generated configurer so no reflection in use
+        assertEquals(0, bi.getInvokedCounter());
+
+        context.stop();
+    }
+
+}