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/02/15 09:58:59 UTC
[camel] 02/05: CAMEL-17647: camel-core - Properties component should support pluggable functions
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch pf
in repository https://gitbox.apache.org/repos/asf/camel.git
commit b8ea09e05cdd0790b0e3c05fd075504f15b57344
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Feb 15 10:23:22 2022 +0100
CAMEL-17647: camel-core - Properties component should support pluggable functions
---
.../properties/DefaultPropertiesParser.java | 33 ++++----
.../component/properties/PropertiesComponent.java | 24 ++++--
.../properties/PropertiesFunctionResolver.java | 98 ++++++++++++++++++++++
3 files changed, 133 insertions(+), 22 deletions(-)
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 73870e1..2ad3775 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
@@ -237,24 +237,23 @@ public class DefaultPropertiesParser implements PropertiesParser {
// the key may be a function, so lets check this first
if (propertiesComponent != null) {
- for (PropertiesFunction function : propertiesComponent.getFunctions().values()) {
- String token = function.getName() + ":";
- if (key.startsWith(token)) {
- String remainder = key.substring(token.length());
- log.debug("Property with key [{}] is applied by function [{}]", key, function.getName());
- String value = function.apply(remainder);
- if (value == null) {
- throw new IllegalArgumentException(
- "Property with key [" + key + "] using function [" + function.getName() + "]"
- + " returned null value which is not allowed, from input: "
- + input);
- } else {
- if (log.isDebugEnabled()) {
- log.debug("Property with key [{}] applied by function [{}] -> {}", key, function.getName(),
- value);
- }
- return value;
+ String prefix = StringHelper.before(key, ":");
+ PropertiesFunction function = propertiesComponent.getPropertiesFunction(prefix);
+ if (function != null) {
+ String remainder = StringHelper.after(key, ":");
+ log.debug("Property with key [{}] is applied by function [{}]", key, function.getName());
+ String value = function.apply(remainder);
+ if (value == null) {
+ throw new IllegalArgumentException(
+ "Property with key [" + key + "] using function [" + function.getName() + "]"
+ + " returned null value which is not allowed, from input: "
+ + input);
+ } else {
+ if (log.isDebugEnabled()) {
+ log.debug("Property with key [{}] applied by function [{}] -> {}", key, function.getName(),
+ value);
}
+ return value;
}
}
}
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 79b31af..bc11a82 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
@@ -19,7 +19,6 @@ package org.apache.camel.component.properties;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -103,7 +102,7 @@ public class PropertiesComponent extends ServiceSupport
private static final String NEGATE_PREFIX = PREFIX_TOKEN + "!";
private CamelContext camelContext;
- private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>();
+ private final PropertiesFunctionResolver functionResolver = new PropertiesFunctionResolver();
private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
private final PropertiesLookup propertiesLookup = new DefaultPropertiesLookup(this);
private final List<PropertiesSource> sources = new ArrayList<>();
@@ -508,22 +507,36 @@ public class PropertiesComponent extends ServiceSupport
/**
* Gets the functions registered in this properties component.
*/
+ @Deprecated
public Map<String, PropertiesFunction> getFunctions() {
- return functions;
+ return functionResolver.getFunctions();
+ }
+
+ /**
+ * Gets the function by the given name
+ *
+ * @param name the function name
+ * @return the function or null if no function exists
+ */
+ public PropertiesFunction getPropertiesFunction(String name) {
+ if (name == null) {
+ return null;
+ }
+ return functionResolver.resolvePropertiesFunction(name);
}
/**
* Registers the {@link PropertiesFunction} as a function to this component.
*/
public void addPropertiesFunction(PropertiesFunction function) {
- this.functions.put(function.getName(), function);
+ functionResolver.addPropertiesFunction(function);
}
/**
* Is there a {@link PropertiesFunction} with the given name?
*/
public boolean hasFunction(String name) {
- return functions.containsKey(name);
+ return functionResolver.hasFunction(name);
}
@ManagedAttribute(description = "System properties mode")
@@ -627,6 +640,7 @@ public class PropertiesComponent extends ServiceSupport
super.doInit();
ObjectHelper.notNull(camelContext, "CamelContext", this);
+ CamelContextAware.trySetCamelContext(functionResolver, camelContext);
if (systemPropertiesMode != SYSTEM_PROPERTIES_MODE_NEVER
&& systemPropertiesMode != SYSTEM_PROPERTIES_MODE_FALLBACK
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
new file mode 100644
index 0000000..41ca53e
--- /dev/null
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
@@ -0,0 +1,98 @@
+/*
+ * 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 java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.PropertiesFunction;
+import org.apache.camel.spi.PropertiesFunctionFactory;
+import org.apache.camel.support.ResolverHelper;
+
+/**
+ * Resolver for built-in and custom {@link PropertiesFunction}.
+ */
+public final class PropertiesFunctionResolver implements CamelContextAware {
+
+ private CamelContext camelContext;
+ private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>();
+
+ public PropertiesFunctionResolver() {
+ // include out of the box functions
+ addPropertiesFunction(new EnvPropertiesFunction());
+ addPropertiesFunction(new SysPropertiesFunction());
+ addPropertiesFunction(new ServicePropertiesFunction());
+ addPropertiesFunction(new ServiceHostPropertiesFunction());
+ addPropertiesFunction(new ServicePortPropertiesFunction());
+ // TODO: Move AWSSecretsManagerPropertiesFunction to camel-aws-secrets-manager
+ addPropertiesFunction(new AWSSecretsManagerPropertiesFunction());
+ }
+
+ @Override
+ public CamelContext getCamelContext() {
+ return camelContext;
+ }
+
+ @Override
+ public void setCamelContext(CamelContext camelContext) {
+ this.camelContext = camelContext;
+ }
+
+ /**
+ * Registers the {@link PropertiesFunction} as a function to this component.
+ */
+ public void addPropertiesFunction(PropertiesFunction function) {
+ this.functions.put(function.getName(), function);
+ }
+
+ /**
+ * Gets the functions registered in this properties component.
+ */
+ public Map<String, PropertiesFunction> getFunctions() {
+ return functions;
+ }
+
+ /**
+ * Is there a {@link PropertiesFunction} with the given name?
+ */
+ public boolean hasFunction(String name) {
+ return functions.containsKey(name);
+ }
+
+ public PropertiesFunction resolvePropertiesFunction(String name) {
+ PropertiesFunction answer = functions.get(name);
+ if (answer == null) {
+ ExtendedCamelContext ecc = camelContext.adapt(ExtendedCamelContext.class);
+ FactoryFinder ff = ecc.getBootstrapFactoryFinder(PropertiesFunctionFactory.FACTORY);
+
+ PropertiesFunctionFactory factory
+ = ResolverHelper.resolveService(ecc, ff, name, PropertiesFunctionFactory.class).orElse(null);
+ if (factory != null) {
+ answer = factory.createPropertiesFunction();
+ if (answer != null) {
+ functions.put(name, answer);
+ }
+ }
+ }
+ return answer;
+ }
+
+}