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 91c40c941d706f06aeac27fb685205c7900ec8c1 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Mon Aug 12 06:54:08 2019 +0200 CAMEL-13856: camel-microprofile-config: sould have an option to enable/disable automatic lookup of properties sources --- .../src/main/docs/properties-component.adoc | 3 +- .../component/properties/PropertiesComponent.java | 55 ++++++++++++++-------- .../PropertiesComponentPropertiesSourceTest.java | 33 +++++++++++-- .../PropertiesComponentConfiguration.java | 14 ++++++ 4 files changed, 79 insertions(+), 26 deletions(-) diff --git a/components/camel-properties/src/main/docs/properties-component.adoc b/components/camel-properties/src/main/docs/properties-component.adoc index 71bdf2e..83dbb0a 100644 --- a/components/camel-properties/src/main/docs/properties-component.adoc +++ b/components/camel-properties/src/main/docs/properties-component.adoc @@ -15,7 +15,7 @@ Where *key* is the key for the property to lookup == Options // component options: START -The Properties component supports 12 options, which are listed below. +The Properties component supports 13 options, which are listed below. @@ -32,6 +32,7 @@ The Properties component supports 12 options, which are listed below. | *overrideProperties* (advanced) | Sets a special list of override properties that take precedence and will use first, if a property exist. | | Properties | *systemPropertiesMode* (common) | Sets the JVM system property mode (0 = never, 1 = fallback, 2 = override). The default mode (override) is to use system properties if present, and override any existing properties. OS environment variable mode is checked before JVM system property mode | 2 | int | *environmentVariableMode* (common) | Sets the OS environment variables mode (0 = never, 1 = fallback, 2 = override). The default mode (override) is to use OS environment variables if present, and override any existing properties. OS environment variable mode is checked before JVM system property mode | 2 | int +| *autoDiscoverProperties Sources* (common) | Whether to automatically discovery instances of PropertiesSource from registry and service factory. | true | boolean | *resolveProperty Placeholders* (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean |=== 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 42ba34c..260309d 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 @@ -122,6 +122,9 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_OVERRIDE; @Metadata(defaultValue = "" + ENVIRONMENT_VARIABLES_MODE_OVERRIDE, enums = "0,1,2") private int environmentVariableMode = ENVIRONMENT_VARIABLES_MODE_OVERRIDE; + @Metadata(defaultValue = "true") + private boolean autoDiscoverPropertiesSources = true; + public PropertiesComponent() { super(); @@ -438,6 +441,16 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. public void setEnvironmentVariableMode(int environmentVariableMode) { this.environmentVariableMode = environmentVariableMode; } + public boolean isAutoDiscoverPropertiesSources() { + return autoDiscoverPropertiesSources; + } + + /** + * Whether to automatically discovery instances of {@link PropertiesSource} from registry and service factory. + */ + public void setAutoDiscoverPropertiesSources(boolean autoDiscoverPropertiesSources) { + this.autoDiscoverPropertiesSources = autoDiscoverPropertiesSources; + } @Override public void addPropertiesSource(PropertiesSource propertiesSource) { @@ -459,29 +472,31 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. protected void doInit() throws Exception { super.doInit(); - // discover any 3rd party properties sources - try { - for (PropertiesSource source: getCamelContext().getRegistry().findByType(PropertiesSource.class)) { - addPropertiesSource(source); - LOG.info("PropertiesComponent added custom PropertiesSource (registry): {}", source); - } + if (isAutoDiscoverPropertiesSources()) { + // discover any 3rd party properties sources + try { + for (PropertiesSource source : getCamelContext().getRegistry().findByType(PropertiesSource.class)) { + addPropertiesSource(source); + LOG.info("PropertiesComponent added custom PropertiesSource (registry): {}", source); + } - FactoryFinder factoryFinder = getCamelContext().adapt(ExtendedCamelContext.class).getFactoryFinder("META-INF/services/org/apache/camel/"); - Class<?> type = factoryFinder.findClass("properties-source-factory").orElse(null); - if (type != null) { - Object obj = getCamelContext().getInjector().newInstance(type, false); - if (obj instanceof PropertiesSource) { - PropertiesSource ps = (PropertiesSource) obj; - addPropertiesSource(ps); - LOG.info("PropertiesComponent added custom PropertiesSource (factory): {}", ps); - } else if (obj != null) { - LOG.warn("PropertiesComponent cannot add custom PropertiesSource as the type is not a org.apache.camel.component.properties.PropertiesSource but: " + type.getName()); + FactoryFinder factoryFinder = getCamelContext().adapt(ExtendedCamelContext.class).getFactoryFinder("META-INF/services/org/apache/camel/"); + Class<?> type = factoryFinder.findClass("properties-source-factory").orElse(null); + if (type != null) { + Object obj = getCamelContext().getInjector().newInstance(type, false); + if (obj instanceof PropertiesSource) { + PropertiesSource ps = (PropertiesSource) obj; + addPropertiesSource(ps); + LOG.info("PropertiesComponent added custom PropertiesSource (factory): {}", ps); + } else if (obj != null) { + LOG.warn("PropertiesComponent cannot add custom PropertiesSource as the type is not a org.apache.camel.component.properties.PropertiesSource but: " + type.getName()); + } } + } catch (NoFactoryAvailableException e) { + // ignore + } catch (Exception e) { + LOG.debug("Error discovering and using custom PropertiesSource due to " + e.getMessage() + ". This exception is ignored", e); } - } catch (NoFactoryAvailableException e) { - // ignore - } catch (Exception e) { - LOG.debug("Error discovering and using custom PropertiesSource due to " + e.getMessage() + ". This exception is ignored", e); } ServiceHelper.initService(sources); 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 77aa267..4ddb75b 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 @@ -18,19 +18,42 @@ package org.apache.camel.component.properties; import java.util.Properties; -import org.apache.camel.ContextTestSupport; +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.spi.PropertiesSource; import org.junit.Test; -public class PropertiesComponentPropertiesSourceTest extends ContextTestSupport { +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class PropertiesComponentPropertiesSourceTest { @Test public void testPropertiesSourceFromRegistry() { + 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")); - context.start(); - assertEquals("my-val-1", context.resolvePropertyPlaceholders("{{my-key-1}}")); - assertEquals("my-val-2", context.resolvePropertyPlaceholders("{{my-key-2}}")); + assertThat(context.resolvePropertyPlaceholders("{{my-key-1}}")).isEqualTo("my-val-1"); + assertThat(context.resolvePropertyPlaceholders("{{my-key-2}}")).isEqualTo("my-val-2"); + } + + @Test + public void testDisablePropertiesSourceDiscovery() { + PropertiesComponent pc = new PropertiesComponent(); + pc.setAutoDiscoverPropertiesSources(false); + + CamelContext context = new DefaultCamelContext(); + context.addComponent("properties", pc); + 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")); + + assertThatThrownBy(() -> context.resolvePropertyPlaceholders("{{my-key-1}}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Property with key [my-key-1] not found in properties from text: {{my-key-1}}"); + + assertThatThrownBy(() -> context.resolvePropertyPlaceholders("{{my-key-2}}")) + .isInstanceOf(IllegalArgumentException.class) + .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 { diff --git a/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java index 2749c5c..2218e81 100644 --- a/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-properties-starter/src/main/java/org/apache/camel/component/properties/springboot/PropertiesComponentConfiguration.java @@ -98,6 +98,11 @@ public class PropertiesComponentConfiguration */ private Integer environmentVariableMode = 2; /** + * Whether to automatically discovery instances of PropertiesSource from + * registry and service factory. + */ + private Boolean autoDiscoverPropertiesSources = true; + /** * Whether the component should resolve property placeholders on itself when * starting. Only properties which are of String type can use property * placeholders. @@ -189,6 +194,15 @@ public class PropertiesComponentConfiguration this.environmentVariableMode = environmentVariableMode; } + public Boolean getAutoDiscoverPropertiesSources() { + return autoDiscoverPropertiesSources; + } + + public void setAutoDiscoverPropertiesSources( + Boolean autoDiscoverPropertiesSources) { + this.autoDiscoverPropertiesSources = autoDiscoverPropertiesSources; + } + public Boolean getResolvePropertyPlaceholders() { return resolvePropertyPlaceholders; }