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 2024/12/27 10:49:11 UTC
(camel) 01/01: CAMEL-21484: mp-config should not be loadable as it has overhead at runtime
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch mploc
in repository https://gitbox.apache.org/repos/asf/camel.git
commit bb56f275d19f5dac8f03836fcf3b20599ecd7628
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Dec 27 11:48:53 2024 +0100
CAMEL-21484: mp-config should not be loadable as it has overhead at runtime
---
.../config/CamelMicroProfilePropertiesSource.java | 85 +---------------------
.../CamelMicroProfilePropertiesSourceTest.java | 29 --------
2 files changed, 2 insertions(+), 112 deletions(-)
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 40e0b0f1936..3eaadc78f79 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,42 +16,16 @@
*/
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.PropertiesSource;
import org.apache.camel.spi.annotations.JdkService;
-import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
/**
* The microprofile-config component is used for bridging the Eclipse MicroProfile Config with the Properties Component.
* This allows using configuration management from MicroProfile with Camel.
*/
@JdkService("properties-source-factory")
-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);
- }
- }
- }
+public class CamelMicroProfilePropertiesSource implements PropertiesSource {
@Override
public String getName() {
@@ -63,64 +37,9 @@ public class CamelMicroProfilePropertiesSource implements LoadablePropertiesSour
return ConfigProvider.getConfig().getOptionalValue(name, String.class).orElse(null);
}
- @Override
- public Properties loadProperties() {
- final Properties answer = new Properties();
- final Config config = ConfigProvider.getConfig();
- for (String name : config.getPropertyNames()) {
- try {
- 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());
- }
- }
- }
-
- return answer;
- }
-
- @Override
- public Properties loadProperties(Predicate<String> filter) {
- final Properties answer = new Properties();
- final Config config = ConfigProvider.getConfig();
-
- for (String name : config.getPropertyNames()) {
- if (isValidForActiveProfiles(name) && filter.test(name)) {
- try {
- config.getOptionalValue(name, String.class).ifPresent(value -> answer.put(name, value));
- } catch (NoSuchElementException e) {
- if (LOG.isDebugEnabled()) {
- LOG.debug("Failed to resolve property {} due to {}", name, e.getMessage());
- }
- }
- }
- }
-
- return answer;
- }
-
- @Override
- public void reloadProperties(String location) {
- // noop
- }
-
@Override
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 6290f71d9b0..2b78548705d 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
@@ -24,11 +24,9 @@ import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.PropertiesSource;
import org.apache.camel.spi.Registry;
import org.apache.camel.test.junit5.CamelTestSupport;
-import org.assertj.core.api.Assertions;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.Test;
@@ -87,33 +85,6 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport {
});
}
- @Test
- public void testLoadAll() {
- PropertiesComponent pc = context.getPropertiesComponent();
- Properties properties = pc.loadProperties();
-
- Assertions.assertThat(properties.get("start")).isEqualTo("direct:start");
- 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.matches("^start$|.*mock$|.*-profile.*"));
-
- 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
public void testMicroProfileConfig() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("Hello World from Camel");