Skip to content

Commit

Permalink
CAMEL-21484: mp-config should not be loadable as it has overhead at r…
Browse files Browse the repository at this point in the history
…untime (#16661)
  • Loading branch information
davsclaus authored Dec 28, 2024
1 parent 6d8ba99 commit 3adff53
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -63,64 +37,9 @@ public String getProperty(String name) {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
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;
Expand Down Expand Up @@ -87,33 +85,6 @@ public String getProperty(String name) {
});
}

@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");
Expand Down

0 comments on commit 3adff53

Please sign in to comment.