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/06/24 05:29:02 UTC

[camel] 01/02: CAMEL-18224: properties component - Allow to plugin custom properties function resolver.

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 a507d769687e4a22d075d23388824eb46f981789
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Jun 24 07:11:29 2022 +0200

    CAMEL-18224: properties component - Allow to plugin custom properties function resolver.
---
 ...java => DefaultPropertiesFunctionResolver.java} |  25 ++---
 .../component/properties/PropertiesComponent.java  |  15 ++-
 .../properties/PropertiesFunctionResolver.java     | 122 ++-------------------
 3 files changed, 34 insertions(+), 128 deletions(-)

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/DefaultPropertiesFunctionResolver.java
similarity index 86%
copy from core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesFunctionResolver.java
copy to core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesFunctionResolver.java
index c977508ae44..c9e6a7f0c04 100644
--- 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/DefaultPropertiesFunctionResolver.java
@@ -32,20 +32,18 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Resolver for built-in and custom {@link PropertiesFunction}.
+ * Default {@link PropertiesFunctionResolver}.
  */
-public final class PropertiesFunctionResolver extends ServiceSupport
-        implements CamelContextAware, NonManagedService, StaticService {
+public final class DefaultPropertiesFunctionResolver extends ServiceSupport
+        implements PropertiesFunctionResolver, CamelContextAware, NonManagedService, StaticService {
 
-    public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/properties-function/";
-
-    private static final Logger LOG = LoggerFactory.getLogger(PropertiesFunctionResolver.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultPropertiesFunctionResolver.class);
 
     private CamelContext camelContext;
     private FactoryFinder factoryFinder;
     private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>();
 
-    public PropertiesFunctionResolver() {
+    public DefaultPropertiesFunctionResolver() {
         // include out of the box functions
         addPropertiesFunction(new EnvPropertiesFunction());
         addPropertiesFunction(new SysPropertiesFunction());
@@ -64,27 +62,22 @@ public final class PropertiesFunctionResolver extends ServiceSupport
         this.camelContext = camelContext;
     }
 
-    /**
-     * Registers the {@link PropertiesFunction} as a function to this component.
-     */
+    @Override
     public void addPropertiesFunction(PropertiesFunction function) {
         this.functions.put(function.getName(), function);
     }
 
-    /**
-     * Gets the functions registered in this properties component.
-     */
+    @Override
     public Map<String, PropertiesFunction> getFunctions() {
         return functions;
     }
 
-    /**
-     * Is there a {@link PropertiesFunction} with the given name?
-     */
+    @Override
     public boolean hasFunction(String name) {
         return functions.containsKey(name);
     }
 
+    @Override
     public PropertiesFunction resolvePropertiesFunction(String name) {
         PropertiesFunction answer = functions.get(name);
         if (answer == null) {
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 0c5c4707e77..07dc7c4dea5 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
@@ -105,10 +105,10 @@ public class PropertiesComponent extends ServiceSupport
     private static final String NEGATE_PREFIX = PREFIX_TOKEN + "!";
 
     private CamelContext camelContext;
-    private final PropertiesFunctionResolver functionResolver = new PropertiesFunctionResolver();
+    private PropertiesFunctionResolver functionResolver = new DefaultPropertiesFunctionResolver();
     private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
     private final PropertiesLookup propertiesLookup = new DefaultPropertiesLookup(this);
-    private List<PropertiesLookupListener> propertiesLookupListeners = new ArrayList<>();
+    private final List<PropertiesLookupListener> propertiesLookupListeners = new ArrayList<>();
     private final List<PropertiesSource> sources = new ArrayList<>();
     private List<PropertiesLocation> locations = new ArrayList<>();
     private String location;
@@ -439,6 +439,17 @@ public class PropertiesComponent extends ServiceSupport
         this.propertiesParser = propertiesParser;
     }
 
+    public PropertiesFunctionResolver getFunctionResolver() {
+        return functionResolver;
+    }
+
+    /**
+     * To use a custom PropertiesFunctionResolver
+     */
+    public void setFunctionResolver(PropertiesFunctionResolver functionResolver) {
+        this.functionResolver = functionResolver;
+    }
+
     @ManagedAttribute(description = "Whether to support using fallback values if a property cannot be found")
     public boolean isDefaultFallbackEnabled() {
         return defaultFallbackEnabled;
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
index c977508ae44..06fe61a240a 100644
--- 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
@@ -16,136 +16,38 @@
  */
 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.NonManagedService;
-import org.apache.camel.StaticService;
-import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.PropertiesFunction;
-import org.apache.camel.support.service.ServiceHelper;
-import org.apache.camel.support.service.ServiceSupport;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Resolver for built-in and custom {@link PropertiesFunction}.
  */
-public final class PropertiesFunctionResolver extends ServiceSupport
-        implements CamelContextAware, NonManagedService, StaticService {
+public interface PropertiesFunctionResolver {
 
-    public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/properties-function/";
-
-    private static final Logger LOG = LoggerFactory.getLogger(PropertiesFunctionResolver.class);
-
-    private CamelContext camelContext;
-    private FactoryFinder factoryFinder;
-    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());
-    }
-
-    @Override
-    public CamelContext getCamelContext() {
-        return camelContext;
-    }
-
-    @Override
-    public void setCamelContext(CamelContext camelContext) {
-        this.camelContext = camelContext;
-    }
+    String RESOURCE_PATH = "META-INF/services/org/apache/camel/properties-function/";
 
     /**
      * Registers the {@link PropertiesFunction} as a function to this component.
      */
-    public void addPropertiesFunction(PropertiesFunction function) {
-        this.functions.put(function.getName(), function);
-    }
+    void addPropertiesFunction(PropertiesFunction function);
 
     /**
      * Gets the functions registered in this properties component.
      */
-    public Map<String, PropertiesFunction> getFunctions() {
-        return functions;
-    }
+    Map<String, PropertiesFunction> getFunctions();
 
     /**
      * 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) {
-            answer = resolve(camelContext, name);
-            if (answer != null) {
-                functions.put(name, answer);
-            }
-        }
-        return answer;
-    }
-
-    private PropertiesFunction resolve(CamelContext context, String name) {
-        // use factory finder to find a custom implementations
-        Class<?> type = null;
-        try {
-            type = findFactory(name, context);
-        } catch (Exception e) {
-            // ignore
-        }
+    boolean hasFunction(String name);
 
-        if (type != null) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Found PropertiesFunction: {} via: {}{}", type.getName(), factoryFinder.getResourcePath(), name);
-            }
-            if (PropertiesFunction.class.isAssignableFrom(type)) {
-                PropertiesFunction answer = (PropertiesFunction) context.getInjector().newInstance(type, false);
-                CamelContextAware.trySetCamelContext(answer, camelContext);
-                ServiceHelper.startService(answer);
-                return answer;
-            } else {
-                throw new IllegalArgumentException("Type is not a PropertiesFunction implementation. Found: " + type.getName());
-            }
-        }
-
-        return null;
-    }
-
-    private Class<?> findFactory(String name, CamelContext context) {
-        if (factoryFinder == null) {
-            factoryFinder = context.adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH);
-        }
-        return factoryFinder.findClass(name).orElse(null);
-    }
-
-    @Override
-    protected void doInit() throws Exception {
-        ServiceHelper.initService(functions.values());
-    }
-
-    @Override
-    protected void doStart() throws Exception {
-        ServiceHelper.startService(functions.values());
-    }
-
-    @Override
-    protected void doStop() throws Exception {
-        ServiceHelper.stopService(functions.values());
-    }
+    /**
+     * Resolves the properties function with the given name
+     *
+     * @param  name the name of the properties function
+     * @return      the function or <tt>null</tt> if not found
+     */
+    PropertiesFunction resolvePropertiesFunction(String name);
 
-    @Override
-    protected void doShutdown() throws Exception {
-        ServiceHelper.stopAndShutdownService(functions.values());
-    }
 }