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:34 UTC

[camel] 05/07: CAMEL-13858: camel-properties: load properties should iterate through loadable property source in reverse order

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 43389ad12761a8ec1631cee00c1c87d538584df8
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Mon Aug 12 07:35:56 2019 +0200

    CAMEL-13858: camel-properties: load properties should iterate through loadable property source in reverse order
---
 .../component/properties/PropertiesComponent.java  |  6 +++-
 .../PropertiesComponentPropertiesSourceTest.java   | 36 ++++++++++++++++++++--
 2 files changed, 39 insertions(+), 3 deletions(-)

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 260309d..10fd38c 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
@@ -187,7 +187,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
         }
 
         if (!sources.isEmpty()) {
-            for (PropertiesSource ps : sources) {
+            // 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();
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 4ddb75b..38b7714 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
@@ -19,8 +19,9 @@ package org.apache.camel.component.properties;
 import java.util.Properties;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Ordered;
 import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.spi.PropertiesSource;
+import org.apache.camel.spi.LoadablePropertiesSource;
 import org.junit.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -38,6 +39,20 @@ public class PropertiesComponentPropertiesSourceTest {
     }
 
     @Test
+    public void testOrderedPropertiesSources() {
+        CamelContext context = new DefaultCamelContext();
+        context.getRegistry().bind("my-ps-1", new PropertiesPropertiesSource(Ordered.HIGHEST, "ps1", "shared", "v1", "my-key-1", "my-val-1"));
+        context.getRegistry().bind("my-ps-2", new PropertiesPropertiesSource(Ordered.LOWEST,"ps2", "shared", "v2", "my-key-2", "my-val-2"));
+
+        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
+        Properties properties = pc.loadProperties();
+
+        assertThat(properties.get("my-key-1")).isEqualTo("my-val-1");
+        assertThat(properties.get("my-key-2")).isEqualTo("my-val-2");
+        assertThat(properties.get("shared")).isEqualTo("v1");
+    }
+
+    @Test
     public void testDisablePropertiesSourceDiscovery() {
         PropertiesComponent pc = new PropertiesComponent();
         pc.setAutoDiscoverPropertiesSources(false);
@@ -56,13 +71,19 @@ 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 PropertiesSource {
+    private static final class PropertiesPropertiesSource extends Properties implements LoadablePropertiesSource, Ordered {
         private final String name;
+        private final int order;
 
         public PropertiesPropertiesSource(String name, String... kv) {
+            this(Ordered.LOWEST, name, kv);
+        }
+
+        public PropertiesPropertiesSource(int order, String name, String... kv) {
             assert kv.length % 2 == 0;
 
             this.name = name;
+            this.order = order;
 
             for (int i = 0; i < kv.length; i += 2) {
                 super.setProperty(kv[i], kv[i + 1]);
@@ -73,5 +94,16 @@ public class PropertiesComponentPropertiesSourceTest {
         public String getName() {
             return name;
         }
+
+        @Override
+        public int getOrder() {
+            return order;
+        }
+
+        @Override
+        public Properties loadProperties() {
+            return this;
+        }
     }
 }
+