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/02 12:28:04 UTC
[camel] branch main updated: CAMEL-19937: camel-core - Properties component - Add option to ignore unresolved property (#11622)
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 ddc95bbeaae CAMEL-19937: camel-core - Properties component - Add option to ignore unresolved property (#11622)
ddc95bbeaae is described below
commit ddc95bbeaae5633a7737db227ab2d4b4dec2a781
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 2 14:27:54 2023 +0200
CAMEL-19937: camel-core - Properties component - Add option to ignore unresolved property (#11622)
---
.../org/apache/camel/spi/PropertiesComponent.java | 6 +++
.../properties/PropertiesComponentConfigurer.java | 6 +++
.../properties/DefaultPropertiesParser.java | 4 ++
.../component/properties/PropertiesComponent.java | 16 +++++-
...opertiesComponentIgnoreMissingPropertyTest.java | 59 ++++++++++++++++++++++
5 files changed, 90 insertions(+), 1 deletion(-)
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
index 0a201077fd1..00edeab3834 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
@@ -209,6 +209,12 @@ public interface PropertiesComponent extends StaticService {
*/
void setIgnoreMissingLocation(boolean ignoreMissingLocation);
+ /**
+ * Whether to silently ignore if a property cannot be resolved (i.e. all properties is marked as optional), and
+ * return the value as-is.
+ */
+ void setIgnoreMissingProperty(boolean ignoreMissingProperty);
+
/**
* Whether to support nested property placeholders. A nested placeholder, means that a placeholder, has also a
* placeholder, that should be resolved (recursively).
diff --git a/core/camel-base/src/generated/java/org/apache/camel/component/properties/PropertiesComponentConfigurer.java b/core/camel-base/src/generated/java/org/apache/camel/component/properties/PropertiesComponentConfigurer.java
index a41836deb51..7aefabe7df8 100644
--- a/core/camel-base/src/generated/java/org/apache/camel/component/properties/PropertiesComponentConfigurer.java
+++ b/core/camel-base/src/generated/java/org/apache/camel/component/properties/PropertiesComponentConfigurer.java
@@ -33,6 +33,8 @@ public class PropertiesComponentConfigurer extends org.apache.camel.support.comp
case "EnvironmentVariableMode": target.setEnvironmentVariableMode(property(camelContext, int.class, value)); return true;
case "ignoremissinglocation":
case "IgnoreMissingLocation": target.setIgnoreMissingLocation(property(camelContext, boolean.class, value)); return true;
+ case "ignoremissingproperty":
+ case "IgnoreMissingProperty": target.setIgnoreMissingProperty(property(camelContext, boolean.class, value)); return true;
case "initialproperties":
case "InitialProperties": target.setInitialProperties(property(camelContext, java.util.Properties.class, value)); return true;
case "localproperties":
@@ -70,6 +72,8 @@ public class PropertiesComponentConfigurer extends org.apache.camel.support.comp
case "EnvironmentVariableMode": return int.class;
case "ignoremissinglocation":
case "IgnoreMissingLocation": return boolean.class;
+ case "ignoremissingproperty":
+ case "IgnoreMissingProperty": return boolean.class;
case "initialproperties":
case "InitialProperties": return java.util.Properties.class;
case "localproperties":
@@ -108,6 +112,8 @@ public class PropertiesComponentConfigurer extends org.apache.camel.support.comp
case "EnvironmentVariableMode": return target.getEnvironmentVariableMode();
case "ignoremissinglocation":
case "IgnoreMissingLocation": return target.isIgnoreMissingLocation();
+ case "ignoremissingproperty":
+ case "IgnoreMissingProperty": return target.isIgnoreMissingProperty();
case "initialproperties":
case "InitialProperties": return target.getInitialProperties();
case "localproperties":
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
index 112ce64678e..0053cc09de9 100644
--- a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
@@ -362,6 +362,10 @@ public class DefaultPropertiesParser implements PropertiesParser {
}
if (value == null) {
+ if (!optional && propertiesComponent != null && propertiesComponent.isIgnoreMissingProperty()) {
+ // property is missing, but we should ignore this and return the placeholder unresolved
+ return UNRESOLVED_PREFIX_TOKEN + key + UNRESOLVED_SUFFIX_TOKEN;
+ }
if (!optional) {
StringBuilder esb = new StringBuilder();
esb.append("Property with key [").append(key).append("] ");
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 0b470a51013..4a13c4e0e4a 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
@@ -114,6 +114,7 @@ public class PropertiesComponent extends ServiceSupport
private List<PropertiesLocation> locations = new ArrayList<>();
private String location;
private boolean ignoreMissingLocation;
+ private boolean ignoreMissingProperty;
private boolean nestedPlaceholder = true;
private String encoding;
private boolean defaultFallbackEnabled = true;
@@ -173,7 +174,11 @@ public class PropertiesComponent extends ServiceSupport
@Override
public Optional<String> resolveProperty(String key) {
try {
- String value = parseUri(key, propertiesLookup, false);
+ boolean keep = isIgnoreMissingProperty();
+ String value = parseUri(key, propertiesLookup, keep);
+ if (value == null) {
+ return Optional.empty();
+ }
return Optional.of(value);
} catch (IllegalArgumentException e) {
// property not found
@@ -468,6 +473,15 @@ public class PropertiesComponent extends ServiceSupport
this.ignoreMissingLocation = ignoreMissingLocation;
}
+ @ManagedAttribute(description = "Ignore missing location")
+ public boolean isIgnoreMissingProperty() {
+ return ignoreMissingProperty;
+ }
+
+ public void setIgnoreMissingProperty(boolean ignoreMissingProperty) {
+ this.ignoreMissingProperty = ignoreMissingProperty;
+ }
+
@ManagedAttribute(description = "Nested placeholder")
public boolean isNestedPlaceholder() {
return nestedPlaceholder;
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentIgnoreMissingPropertyTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentIgnoreMissingPropertyTest.java
new file mode 100644
index 00000000000..b2ed1928b44
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentIgnoreMissingPropertyTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.support.EndpointHelper;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class PropertiesComponentIgnoreMissingPropertyTest extends ContextTestSupport {
+
+ @Test
+ public void testIgnoreMissingProperty() throws Exception {
+ var o = context.getPropertiesComponent().resolveProperty("{{?foo}}");
+ Assertions.assertFalse(o.isEmpty());
+ Assertions.assertEquals("{{?foo}}", o.get());
+
+ o = context.getPropertiesComponent().resolveProperty("{{foo}}");
+ Assertions.assertFalse(o.isEmpty());
+ Assertions.assertEquals("{{foo}}", o.get());
+
+ o = context.getPropertiesComponent().resolveProperty("{{myQueueSize}}");
+ Assertions.assertFalse(o.isEmpty());
+ Assertions.assertEquals("10", o.get());
+
+ String out = EndpointHelper.resolveEndpointUriPropertyPlaceholders(context, "{{?foo}}");
+ Assertions.assertEquals("{{?foo}}", out);
+
+ out = EndpointHelper.resolveEndpointUriPropertyPlaceholders(context, "{{foo}}");
+ Assertions.assertEquals("{{foo}}", out);
+
+ out = EndpointHelper.resolveEndpointUriPropertyPlaceholders(context, "foo:dummy?a=1&b={{foo}}&c={{myQueueSize}}");
+ Assertions.assertEquals("foo:dummy?a=1&b={{foo}}&c=10", out);
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.getPropertiesComponent().setLocation("classpath:org/apache/camel/component/properties/myproperties.properties");
+ context.getPropertiesComponent().setIgnoreMissingProperty(true);
+ return context;
+ }
+
+}