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 2019/08/12 09:57:35 UTC

[camel] 06/07: CAMEL-13859: came-properties: allow to filter properties by key when loading all

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 08c61928e278c42c35b537b3a1b985b7827566fd
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Mon Aug 12 10:14:02 2019 +0200

    CAMEL-13859: came-properties: allow to filter properties by key when loading all
---
 .../config/CamelMicroProfilePropertiesSource.java  | 18 +++++++++
 .../AbstractLocationPropertiesSource.java          | 14 +++++++
 .../component/properties/PropertiesComponent.java  | 43 ++++++++++++++++++++++
 .../apache/camel/spi/LoadablePropertiesSource.java |  9 +++++
 .../org/apache/camel/spi/PropertiesComponent.java  | 16 +++++++-
 .../PropertiesComponentPropertiesSourceTest.java   | 36 ++++++++++++++++++
 6 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
index a4e0524..88624e6 100644
--- a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
+++ b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.microprofile.config;
 
 import java.util.Properties;
+import java.util.function.Predicate;
 
 import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.support.service.ServiceSupport;
@@ -60,6 +61,23 @@ public class CamelMicroProfilePropertiesSource extends ServiceSupport implements
     }
 
     @Override
+    public Properties loadProperties(Predicate<String> filter) {
+        if (config == null) {
+            config = ConfigProvider.getConfig();
+        }
+
+        Properties answer = new Properties();
+
+        for (String name: answer.stringPropertyNames()) {
+            if (filter.test(name)) {
+                config.getOptionalValue(name, String.class).ifPresent(v -> answer.put(name, v));
+            }
+        }
+
+        return answer;
+    }
+
+    @Override
     protected void doInit() throws Exception {
         config = ConfigProvider.getConfig();
     }
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
index 4a42e54..a019ac9 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.properties;
 
 import java.util.Map;
 import java.util.Properties;
+import java.util.function.Predicate;
 
 import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.support.service.ServiceSupport;
@@ -50,6 +51,19 @@ public abstract class AbstractLocationPropertiesSource extends ServiceSupport im
     }
 
     @Override
+    public Properties loadProperties(Predicate<String> filter) {
+        Properties answer = new Properties();
+
+        for (String name: answer.stringPropertyNames()) {
+            if (filter.test(name)) {
+                answer.put(name, properties.get(name));
+            }
+        }
+
+        return answer;
+    }
+
+    @Override
     public String getProperty(String name) {
         return properties.getProperty(name);
     }
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 10fd38c..29845c6 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -24,6 +24,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.function.Predicate;
 import java.util.stream.Collectors;
 
 import org.apache.camel.CamelContextAware;
@@ -179,6 +180,9 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
 
     @Override
     public Properties loadProperties() {
+        // this method may be replaced by loadProperties(k -> true) but the underlying sources
+        // may have some optimization for bulk load so let's keep it
+
         Properties prop = new OrderedProperties();
 
         // use initial properties
@@ -212,6 +216,45 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
         return prop;
     }
 
+    @Override
+    public Properties loadProperties(Predicate<String> filter) {
+        Properties prop = new OrderedProperties();
+
+        // use initial properties
+        if (initialProperties != null) {
+            for (String name: initialProperties.stringPropertyNames()) {
+                if (filter.test(name)) {
+                    prop.put(name, initialProperties.get(name));
+                }
+            }
+        }
+
+        if (!sources.isEmpty()) {
+            // sources are ordered according to {@link org.apache.camel.support.OrderComparator} so
+            // it is needed to iterate them in reverse order otherwise lower priority sources may
+            // override properties from higher priority ones
+            for (int i = sources.size(); i-- > 0; ) {
+                PropertiesSource ps = sources.get(i);
+                if (ps instanceof LoadablePropertiesSource) {
+                    LoadablePropertiesSource lps = (LoadablePropertiesSource) ps;
+                    Properties p = lps.loadProperties(filter);
+                    prop.putAll(p);
+                }
+            }
+        }
+
+        // use override properties
+        if (overrideProperties != null) {
+            for (String name: overrideProperties.stringPropertyNames()) {
+                if (filter.test(name)) {
+                    prop.put(name, overrideProperties.get(name));
+                }
+            }
+        }
+
+        return prop;
+    }
+
     protected String parseUri(String uri, PropertiesLookup properties) {
         // enclose tokens if missing
         if (!uri.contains(PREFIX_TOKEN) && !uri.startsWith(PREFIX_TOKEN)) {
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java b/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
index 32bf08f..90ab31d 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
@@ -17,6 +17,7 @@
 package org.apache.camel.spi;
 
 import java.util.Properties;
+import java.util.function.Predicate;
 
 import org.apache.camel.Ordered;
 
@@ -35,4 +36,12 @@ public interface LoadablePropertiesSource extends PropertiesSource {
      * @return the loaded properties
      */
     Properties loadProperties();
+
+    /**
+     * Loads the properties from the source filtering them out according to a predicate.
+     *
+     * @param filter the predicate used to filter out properties based on the key.
+     * @return the properties loaded.
+     */
+    Properties loadProperties(Predicate<String> filter);
 }
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 b063778..d92ed2a 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
@@ -19,6 +19,7 @@ package org.apache.camel.spi;
 import java.util.List;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.function.Predicate;
 
 import org.apache.camel.Component;
 import org.apache.camel.StaticService;
@@ -62,13 +63,26 @@ public interface PropertiesComponent extends Component, StaticService {
     Optional<String> resolveProperty(String key);
 
     /**
-     * Loads the properties from the default locations.
+     * Loads the properties from the default locations and sources.
      *
      * @return the properties loaded.
      */
     Properties loadProperties();
 
     /**
+     * Loads the properties from the default locations and sources filtering them out according to a predicate.
+     * </p>
+     * <pre>{@code
+     *     PropertiesComponent pc = getPropertiesComponent();
+     *     Properties props = pc.loadProperties(key -> key.startsWith("camel.component.seda"));
+     * }</pre>
+     *
+     * @param filter the predicate used to filter out properties based on the key.
+     * @return the properties loaded.
+     */
+    Properties loadProperties(Predicate<String> filter);
+
+    /**
      * Gets the configured properties locations.
      * This may be empty if the properties component has only been configured with {@link PropertiesSource}.
      */
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentPropertiesSourceTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentPropertiesSourceTest.java
index 38b7714..2807827 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentPropertiesSourceTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentPropertiesSourceTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.properties;
 
 import java.util.Properties;
+import java.util.function.Predicate;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Ordered;
@@ -53,6 +54,27 @@ public class PropertiesComponentPropertiesSourceTest {
     }
 
     @Test
+    public void testFilteredPropertiesSources() {
+        Properties initial = new Properties();
+        initial.put("initial-1", "initial-val-1");
+        initial.put("initial-2", "initial-val-2");
+        initial.put("my-key-2", "initial-val-2");
+
+        CamelContext context = new DefaultCamelContext();
+        context.getRegistry().bind("my-ps-1", new PropertiesPropertiesSource("ps1", "my-key-1", "my-val-1"));
+        context.getRegistry().bind("my-ps-2", new PropertiesPropertiesSource("ps2", "my-key-2", "my-val-2"));
+
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        pc.setInitialProperties(initial);
+
+        Properties properties = pc.loadProperties(k -> k.endsWith("-2"));
+
+        assertThat(properties).hasSize(2);
+        assertThat(properties.get("initial-2")).isEqualTo("initial-val-2");
+        assertThat(properties.get("my-key-2")).isEqualTo("my-val-2");
+    }
+
+    @Test
     public void testDisablePropertiesSourceDiscovery() {
         PropertiesComponent pc = new PropertiesComponent();
         pc.setAutoDiscoverPropertiesSources(false);
@@ -71,6 +93,7 @@ public class PropertiesComponentPropertiesSourceTest {
             .hasMessage("Property with key [my-key-2] not found in properties from text: {{my-key-2}}");
     }
 
+
     private static final class PropertiesPropertiesSource extends Properties implements LoadablePropertiesSource, Ordered {
         private final String name;
         private final int order;
@@ -104,6 +127,19 @@ public class PropertiesComponentPropertiesSourceTest {
         public Properties loadProperties() {
             return this;
         }
+
+        @Override
+        public Properties loadProperties(Predicate<String> filter) {
+            Properties props = new Properties();
+
+            for (String name: stringPropertyNames()) {
+                if (filter.test(name)) {
+                    props.put(name, get(name));
+                }
+            }
+
+            return props;
+        }
     }
 }