You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2024/01/23 12:22:20 UTC

(camel) branch main updated: CAMEL-20358: Make CamelMicroProfilePropertiesSource aware of profiles when loading properties

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

jamesnetherton 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 503296b90b9 CAMEL-20358: Make CamelMicroProfilePropertiesSource aware of profiles when loading properties
503296b90b9 is described below

commit 503296b90b9a93dd03630569b2c4d677b3e0ba13
Author: James Netherton <ja...@gmail.com>
AuthorDate: Tue Jan 23 08:01:49 2024 +0000

    CAMEL-20358: Make CamelMicroProfilePropertiesSource aware of profiles when loading properties
---
 .../camel-microprofile-config/pom.xml              | 12 +++----
 .../config/CamelMicroProfilePropertiesSource.java  | 35 +++++++++++++++++--
 .../CamelMicroProfilePropertiesSourceTest.java     | 39 ++++++++++++++++++++--
 3 files changed, 74 insertions(+), 12 deletions(-)

diff --git a/components/camel-microprofile/camel-microprofile-config/pom.xml b/components/camel-microprofile/camel-microprofile-config/pom.xml
index 49807583948..5fbc2dbb294 100644
--- a/components/camel-microprofile/camel-microprofile-config/pom.xml
+++ b/components/camel-microprofile/camel-microprofile-config/pom.xml
@@ -45,6 +45,11 @@
             <version>${project.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>io.smallrye.config</groupId>
+            <artifactId>smallrye-config</artifactId>
+            <version>${smallrye-config-version}</version>
+        </dependency>
         <dependency>
             <groupId>org.eclipse.microprofile.config</groupId>
             <artifactId>microprofile-config-api</artifactId>
@@ -58,13 +63,6 @@
             <scope>test</scope>
         </dependency>
 
-        <dependency>
-            <groupId>io.smallrye.config</groupId>
-            <artifactId>smallrye-config</artifactId>
-            <version>${smallrye-config-version}</version>
-            <scope>test</scope>
-        </dependency>
-
         <dependency>
             <groupId>org.junit.jupiter</groupId>
             <artifactId>junit-jupiter</artifactId>
diff --git a/components/camel-microprofile/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java b/components/camel-microprofile/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
index 6fd85bae4c3..b0f6f391061 100644
--- a/components/camel-microprofile/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
+++ b/components/camel-microprofile/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java
@@ -16,10 +16,13 @@
  */
 package org.apache.camel.component.microprofile.config;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.function.Predicate;
 
+import io.smallrye.config.SmallRyeConfig;
 import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.spi.annotations.JdkService;
 import org.eclipse.microprofile.config.Config;
@@ -35,6 +38,20 @@ import org.slf4j.LoggerFactory;
 public class CamelMicroProfilePropertiesSource implements LoadablePropertiesSource {
 
     private static final Logger LOG = LoggerFactory.getLogger(CamelMicroProfilePropertiesSource.class);
+    private List<String> profiles = Collections.emptyList();
+
+    public CamelMicroProfilePropertiesSource() {
+        try {
+            this.profiles = ConfigProvider.getConfig()
+                    .unwrap(SmallRyeConfig.class)
+                    .getProfiles();
+        } catch (IllegalArgumentException e) {
+            // Handle unlikely event that the config could not be unwrapped
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Failed to discover active configuration profiles", e);
+            }
+        }
+    }
 
     @Override
     public String getName() {
@@ -52,7 +69,9 @@ public class CamelMicroProfilePropertiesSource implements LoadablePropertiesSour
         final Config config = ConfigProvider.getConfig();
         for (String name : config.getPropertyNames()) {
             try {
-                answer.put(name, config.getValue(name, String.class));
+                if (isValidForActiveProfiles(name)) {
+                    answer.put(name, config.getValue(name, String.class));
+                }
             } catch (NoSuchElementException e) {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Failed to resolve property {} due to {}", name, e.getMessage());
@@ -69,7 +88,7 @@ public class CamelMicroProfilePropertiesSource implements LoadablePropertiesSour
         final Config config = ConfigProvider.getConfig();
 
         for (String name : config.getPropertyNames()) {
-            if (filter.test(name)) {
+            if (isValidForActiveProfiles(name) && filter.test(name)) {
                 try {
                     config.getOptionalValue(name, String.class).ifPresent(value -> answer.put(name, value));
                 } catch (NoSuchElementException e) {
@@ -92,4 +111,16 @@ public class CamelMicroProfilePropertiesSource implements LoadablePropertiesSour
     public String toString() {
         return "camel-microprofile-config";
     }
+
+    private boolean isValidForActiveProfiles(String name) {
+        if (!profiles.isEmpty() && name.startsWith("%")) {
+            for (String profile : profiles) {
+                if (name.startsWith(profile + ".", 1)) {
+                    return true;
+                }
+            }
+            return false;
+        }
+        return true;
+    }
 }
diff --git a/components/camel-microprofile/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java b/components/camel-microprofile/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
index b25f4fcc584..67290b96ef9 100644
--- a/components/camel-microprofile/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
+++ b/components/camel-microprofile/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java
@@ -34,6 +34,8 @@ import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
 
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
 public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
 
     private Config config;
@@ -46,10 +48,17 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
         prop.put("hi", "World");
         prop.put("my-mock", "result");
         prop.put("empty", "");
+        prop.put("%non-active-profile.test-non-active-profile", "should not see this");
+        prop.put("%profileA.test-profile-a", "Profile A");
+        prop.put("%profileB.test-profile-b", "Profile B");
 
         // create PMC config source and register it so we can use it for testing
         PropertiesConfigSource pcs = new PropertiesConfigSource(prop, "my-smallrye-config");
-        config = new SmallRyeConfigBuilder().withSources(pcs).build();
+        config = new SmallRyeConfigBuilder()
+                .withProfile("profileA")
+                .withProfile("profileB")
+                .withSources(pcs)
+                .build();
 
         ConfigProviderResolver.instance().registerConfig(config, CamelMicroProfilePropertiesSourceTest.class.getClassLoader());
 
@@ -90,16 +99,22 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
         Assertions.assertThat(properties.get("hi")).isEqualTo("World");
         Assertions.assertThat(properties.get("my-mock")).isEqualTo("result");
         Assertions.assertThat(properties.get("empty")).isNull();
+        Assertions.assertThat(properties.get("test-non-active-profile")).isNull();
+        Assertions.assertThat(properties.get("test-profile-a")).isEqualTo("Profile A");
+        Assertions.assertThat(properties.get("test-profile-b")).isEqualTo("Profile B");
     }
 
     @Test
     public void testLoadFiltered() {
         PropertiesComponent pc = context.getPropertiesComponent();
-        Properties properties = pc.loadProperties(k -> k.length() > 2);
+        Properties properties = pc.loadProperties(k -> k.matches("^start$|.*mock$|.*-profile.*"));
 
-        Assertions.assertThat(properties).hasSize(2);
+        Assertions.assertThat(properties).hasSize(4);
         Assertions.assertThat(properties.get("start")).isEqualTo("direct:start");
         Assertions.assertThat(properties.get("my-mock")).isEqualTo("result");
+        Assertions.assertThat(properties.get("test-non-active-profile")).isNull();
+        Assertions.assertThat(properties.get("test-profile-a")).isEqualTo("Profile A");
+        Assertions.assertThat(properties.get("test-profile-b")).isEqualTo("Profile B");
     }
 
     @Test
@@ -111,6 +126,24 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
         MockEndpoint.assertIsSatisfied(context);
     }
 
+    @Test
+    public void testActiveConfigProfiles() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Profile A :: Profile B");
+
+        template.sendBody("direct:start", context.resolvePropertyPlaceholders("{{test-profile-a}} :: {{test-profile-b}}"));
+
+        MockEndpoint.assertIsSatisfied(context);
+    }
+
+    @Test
+    public void testInactiveConfigProfiles() throws Exception {
+        assertThatThrownBy(() -> {
+            template.sendBody("direct:start", context.resolvePropertyPlaceholders("{{test-non-active-profile}}"));
+        })
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("Property with key [test-non-active-profile] not found");
+    }
+
     @Override
     protected RoutesBuilder createRouteBuilder() {
         return new RouteBuilder() {