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 a7b6081 CAMEL-16695: camel-core - Properties placeholder - Add
support for negate boolean answer
a7b6081 is described below
commit a7b6081236e586fc0ced7501f518ac4d06a3263e
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jun 8 07:28:14 2021 +0200
CAMEL-16695: camel-core - Properties placeholder - Add support for negate
boolean answer
---
.../camel/catalog/docs/properties-component.adoc | 24 +++++++++++--
.../src/main/docs/properties-component.adoc | 24 +++++++++++--
.../component/properties/PropertiesComponent.java | 33 ++++++++++++++----
.../PropertiesComponentLoadPropertiesTest.java | 3 +-
...est.java => PropertiesComponentNegateTest.java} | 40 +++++++++++-----------
.../component/properties/myproperties.properties | 2 ++
.../modules/ROOT/pages/properties-component.adoc | 24 +++++++++++--
7 files changed, 116 insertions(+), 34 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/properties-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/properties-component.adoc
index 36ff2f7..4a7eb02 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/properties-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/properties-component.adoc
@@ -308,6 +308,26 @@ from("direct:start")
.transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");
----
+== Negate boolean value
+
+If a property placeholder is a boolean value, then it is possible to negate
(reverse) the value by using `!` as prefix in the key.
+
+[source,java]
+----
+// properties
+integration.ftpEnabled=true
+
+// route
+from("ftp:....").autoStartup("{{integration.ftpEnabled}}")
+ .to("kafka:cheese")
+
+from("jms:....").autoStartup("{{!integration.ftpEnabled}}")
+ .to("kafka:cheese")
+----
+
+In the example above then the FTP route or the JMS route should only be
started. So if the FTP is enabled then JMS should be disable, and vise-versa.
+We can do this be negating the `autoStartup` in the JMS route, by using
`!integration.ftpEnabled` as the key.
+
== Additional property placeholder supported in Spring XML
The property placeholders is also supported in many of the Camel Spring
@@ -358,10 +378,10 @@ as values which can either be used as override or
fallback values.
The default mode is that both of them are in override mode, and they are check
in the following order:
1. OS environment variable (override mode)
-2. JVM system property (override mode)
+2. JVM system property (override mode)
3. Property files and other locations
4. OS environment variable (fallback mode)
-5. JVM system property (fallback mode)
+5. JVM system property (fallback mode)
The check stops at first found property value for the key.
diff --git a/core/camel-base/src/main/docs/properties-component.adoc
b/core/camel-base/src/main/docs/properties-component.adoc
index 36ff2f7..4a7eb02 100644
--- a/core/camel-base/src/main/docs/properties-component.adoc
+++ b/core/camel-base/src/main/docs/properties-component.adoc
@@ -308,6 +308,26 @@ from("direct:start")
.transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");
----
+== Negate boolean value
+
+If a property placeholder is a boolean value, then it is possible to negate
(reverse) the value by using `!` as prefix in the key.
+
+[source,java]
+----
+// properties
+integration.ftpEnabled=true
+
+// route
+from("ftp:....").autoStartup("{{integration.ftpEnabled}}")
+ .to("kafka:cheese")
+
+from("jms:....").autoStartup("{{!integration.ftpEnabled}}")
+ .to("kafka:cheese")
+----
+
+In the example above then the FTP route or the JMS route should only be
started. So if the FTP is enabled then JMS should be disable, and vise-versa.
+We can do this be negating the `autoStartup` in the JMS route, by using
`!integration.ftpEnabled` as the key.
+
== Additional property placeholder supported in Spring XML
The property placeholders is also supported in many of the Camel Spring
@@ -358,10 +378,10 @@ as values which can either be used as override or
fallback values.
The default mode is that both of them are in override mode, and they are check
in the following order:
1. OS environment variable (override mode)
-2. JVM system property (override mode)
+2. JVM system property (override mode)
3. Property files and other locations
4. OS environment variable (fallback mode)
-5. JVM system property (fallback mode)
+5. JVM system property (fallback mode)
The check stops at first found property value for the key.
diff --git
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 8e2191a..3eb6563 100644
---
a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++
b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -98,6 +98,8 @@ public class PropertiesComponent extends ServiceSupport
private static final Logger LOG =
LoggerFactory.getLogger(PropertiesComponent.class);
+ private static final String NEGATE_PREFIX = PREFIX_TOKEN + "!";
+
private CamelContext camelContext;
private final Map<String, PropertiesFunction> functions = new
LinkedHashMap<>();
private PropertiesParser propertiesParser = new
DefaultPropertiesParser(this);
@@ -249,17 +251,34 @@ public class PropertiesComponent extends ServiceSupport
return prop;
}
- protected String parseUri(String uri, PropertiesLookup properties, boolean
keepUnresolvedOptional) {
+ protected String parseUri(final String uri, PropertiesLookup properties,
boolean keepUnresolvedOptional) {
+ LOG.trace("Parsing uri {}", uri);
+
+ String key = uri;
// enclose tokens if missing
- if (!uri.contains(PREFIX_TOKEN) && !uri.startsWith(PREFIX_TOKEN)) {
- uri = PREFIX_TOKEN + uri;
+ if (!key.contains(PREFIX_TOKEN) && !key.startsWith(PREFIX_TOKEN)) {
+ key = PREFIX_TOKEN + key;
}
- if (!uri.contains(SUFFIX_TOKEN) && !uri.endsWith(SUFFIX_TOKEN)) {
- uri = uri + SUFFIX_TOKEN;
+ if (!key.contains(SUFFIX_TOKEN) && !key.endsWith(SUFFIX_TOKEN)) {
+ key = key + SUFFIX_TOKEN;
}
- LOG.trace("Parsing uri {}", uri);
- return propertiesParser.parseUri(uri, properties,
defaultFallbackEnabled, keepUnresolvedOptional);
+ // if key starts with a ! then negate a boolean response
+ boolean negate = key.startsWith(NEGATE_PREFIX);
+ if (negate) {
+ key = PREFIX_TOKEN + key.substring(NEGATE_PREFIX.length());
+ }
+
+ String answer = propertiesParser.parseUri(key, properties,
defaultFallbackEnabled, keepUnresolvedOptional);
+ if (negate) {
+ if ("true".equalsIgnoreCase(answer)) {
+ answer = "false";
+ } else if ("false".equalsIgnoreCase(answer)) {
+ answer = "true";
+ }
+ }
+ LOG.trace("Parsed uri {} -> {}", uri, answer);
+ return answer;
}
@Override
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
index 0191306..211bec4 100644
---
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
@@ -40,10 +40,11 @@ public class PropertiesComponentLoadPropertiesTest extends
ContextTestSupport {
Properties prop = pc.loadProperties();
assertNotNull(prop);
- assertEquals(19, prop.size());
+ assertEquals(20, prop.size());
assertEquals("{{cool.b}}", prop.getProperty("cool.a"));
assertEquals("10", prop.getProperty("myQueueSize"));
+ assertEquals("true", prop.getProperty("integration.ftpEnabled"));
}
@Override
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNegateTest.java
similarity index 59%
copy from
core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
copy to
core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNegateTest.java
index 0191306..a7548ae 100644
---
a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentLoadPropertiesTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentNegateTest.java
@@ -16,34 +16,34 @@
*/
package org.apache.camel.component.properties;
-import java.util.Properties;
-
import org.apache.camel.CamelContext;
import org.apache.camel.ContextTestSupport;
+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 PropertiesComponentLoadPropertiesTest extends ContextTestSupport {
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
- @Override
- public boolean isUseRouteBuilder() {
- return false;
- }
+public class PropertiesComponentNegateTest extends ContextTestSupport {
@Test
- public void testLoadProperties() throws Exception {
- context.start();
-
- org.apache.camel.spi.PropertiesComponent pc =
context.getPropertiesComponent();
- Properties prop = pc.loadProperties();
-
- assertNotNull(prop);
- assertEquals(19, prop.size());
+ public void testNegate() throws Exception {
+ assertTrue(context.getRoute("ftp").isAutoStartup());
+ assertFalse(context.getRoute("jms").isAutoStartup());
+ }
- assertEquals("{{cool.b}}", prop.getProperty("cool.a"));
- assertEquals("10", prop.getProperty("myQueueSize"));
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+
from("direct:ftp").routeId("ftp").autoStartup("{{integration.ftpEnabled}}")
+ .to("mock:ftp");
+
+
from("direct:jms").routeId("jms").autoStartup("{{!integration.ftpEnabled}}")
+ .to("mock:jms");
+ }
+ };
}
@Override
diff --git
a/core/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
b/core/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
index 1bd9df3..fd499be 100644
---
a/core/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
+++
b/core/camel-core/src/test/resources/org/apache/camel/component/properties/myproperties.properties
@@ -41,3 +41,5 @@ myDelayPattern=3:10;5:30;10:50;20:100
stop=true
onlytwo=2
+
+integration.ftpEnabled = true
\ No newline at end of file
diff --git a/docs/components/modules/ROOT/pages/properties-component.adoc
b/docs/components/modules/ROOT/pages/properties-component.adoc
index a4ac5f2..c277f588 100644
--- a/docs/components/modules/ROOT/pages/properties-component.adoc
+++ b/docs/components/modules/ROOT/pages/properties-component.adoc
@@ -310,6 +310,26 @@ from("direct:start")
.transform().simple("Hi ${body} do you think ${properties:cheese.quote}?");
----
+== Negate boolean value
+
+If a property placeholder is a boolean value, then it is possible to negate
(reverse) the value by using `!` as prefix in the key.
+
+[source,java]
+----
+// properties
+integration.ftpEnabled=true
+
+// route
+from("ftp:....").autoStartup("{{integration.ftpEnabled}}")
+ .to("kafka:cheese")
+
+from("jms:....").autoStartup("{{!integration.ftpEnabled}}")
+ .to("kafka:cheese")
+----
+
+In the example above then the FTP route or the JMS route should only be
started. So if the FTP is enabled then JMS should be disable, and vise-versa.
+We can do this be negating the `autoStartup` in the JMS route, by using
`!integration.ftpEnabled` as the key.
+
== Additional property placeholder supported in Spring XML
The property placeholders is also supported in many of the Camel Spring
@@ -360,10 +380,10 @@ as values which can either be used as override or
fallback values.
The default mode is that both of them are in override mode, and they are check
in the following order:
1. OS environment variable (override mode)
-2. JVM system property (override mode)
+2. JVM system property (override mode)
3. Property files and other locations
4. OS environment variable (fallback mode)
-5. JVM system property (fallback mode)
+5. JVM system property (fallback mode)
The check stops at first found property value for the key.