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 15:20:25 UTC
[camel] 02/02: 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 main
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 9d2ffe2d3fc815deb2a06af346d1ee666b2e5f5b
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 12 16:19:39 2022 +0100
CAMEL-17479: configuring beans with {{?foo}} supported.
---
.../OptionalPropertyPlaceholderBeanTest.java | 5 -----
.../apache/camel/support/PropertyBindingSupport.java | 19 ++++++++++++++++++-
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
index f388097..c281670 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertyPlaceholderBeanTest.java
@@ -20,7 +20,6 @@ import org.apache.camel.CamelContext;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.component.pojo.SayService;
import org.apache.camel.support.PropertyBindingSupport;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -38,20 +37,17 @@ public class OptionalPropertyPlaceholderBeanTest extends ContextTestSupport {
assertEquals("Hello", say.getMessage());
PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?cool.name}}")
- .withMandatory(false)
.withCamelContext(context)
.bind();
assertEquals("Camel", say.getMessage());
}
@Test
- @Disabled("TODO: https://issues.apache.org/jira/browse/CAMEL-17479")
public void testQueryOptionalNotPresent() throws Exception {
SayService say = new SayService();
assertEquals("Hello", say.getMessage());
PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown}}")
- .withMandatory(false)
.withCamelContext(context)
.bind();
assertEquals("Hello", say.getMessage());
@@ -63,7 +59,6 @@ public class OptionalPropertyPlaceholderBeanTest extends ContextTestSupport {
assertEquals("Hello", say.getMessage());
PropertyBindingSupport.build().withTarget(say).withProperty("message", "{{?unknown:Bye}}")
- .withMandatory(false)
.withCamelContext(context)
.bind();
assertEquals("Bye", say.getMessage());
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 ee379b9..789b13a 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
@@ -38,6 +38,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.PropertyBindingException;
import org.apache.camel.spi.BeanIntrospection;
+import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.PropertyConfigurer;
import org.apache.camel.spi.PropertyConfigurerGetter;
import org.apache.camel.util.StringHelper;
@@ -84,6 +85,17 @@ import static org.apache.camel.util.StringHelper.startsWithIgnoreCase;
* </pre>
* <p>
* Where foo is mandatory, and bar is optional.
+ *
+ * <p>
+ * Values can be marked as optional property placeholder if the values name starts with a question mark, such as:
+ *
+ * <pre>
+ * username={{?clientUserName}}
+ * </pre>
+ * <p>
+ * Where the username property will only be set if the property placeholder <tt>clientUserName</tt> exists, otherwise
+ * the username is not affected.
+ * </p>
*/
public final class PropertyBindingSupport {
@@ -420,7 +432,12 @@ public final class PropertyBindingSupport {
key = camelContext.resolvePropertyPlaceholders(key);
if (text instanceof String) {
// resolve property placeholders
- text = camelContext.resolvePropertyPlaceholders(text.toString());
+ String s = text.toString();
+ text = camelContext.resolvePropertyPlaceholders(s);
+ if (text == null && s.startsWith(PropertiesComponent.PREFIX_TOKEN + "?")) {
+ // it was an optional value, so we should not try to set the property but regard it as a "hit"
+ return true;
+ }
}
}