This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch properties in repository https://gitbox.apache.org/repos/asf/camel.git
commit c8ac7a018bdc6a86d8f3f3ccbd417439fe96d0fa Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jul 3 14:51:52 2019 +0200 CAMEL-13708: Properties component should have API to make it easier to lookup a property on-demand or from all pre-loaded properties. --- .../org/apache/camel/component/bean/BeanInfo.java | 2 +- .../config/CamelMicroProfilePropertiesSource.java | 37 +++++----------------- .../properties/DefaultPropertiesLookup.java | 32 ++++++++++++------- .../component/properties/PropertiesComponent.java | 12 +++++-- .../properties/PropertiesComponentRefTest.java | 17 ---------- 5 files changed, 39 insertions(+), 61 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 0e54632..ae8818d 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -985,7 +985,7 @@ public class BeanInfo { return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class)); } else if (annotation instanceof PropertyInject) { PropertyInject propertyAnnotation = (PropertyInject) annotation; - Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), null, propertyAnnotation.defaultValue()); + Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), propertyAnnotation.defaultValue()); return ExpressionBuilder.convertToExpression(inject, parameterType); } else { LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class); diff --git a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java index ce9ca88..71c7541 100644 --- a/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java +++ b/components/camel-microprofile-config/src/main/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSource.java @@ -17,27 +17,19 @@ package org.apache.camel.component.microprofile.config; import java.util.Optional; -import java.util.Properties; -import org.apache.camel.component.properties.LoadablePropertiesSource; +import org.apache.camel.component.properties.PropertiesSource; import org.apache.camel.support.service.ServiceSupport; -import org.apache.camel.util.OrderedProperties; 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 Camels * properties component. This allows to use configuration management from Eclipse MicroProfile with Camel. */ -public class CamelMicroProfilePropertiesSource extends ServiceSupport implements LoadablePropertiesSource { +public class CamelMicroProfilePropertiesSource extends ServiceSupport implements PropertiesSource { - // TODO: Should not be loadable but regular source so we lookup on demand - - private static final Logger LOG = LoggerFactory.getLogger(CamelMicroProfilePropertiesSource.class); - - private final Properties properties = new OrderedProperties(); + private Config config; @Override public String getName() { @@ -46,29 +38,16 @@ public class CamelMicroProfilePropertiesSource extends ServiceSupport implements @Override public String getProperty(String name) { - return properties.getProperty(name); - } - - @Override - public Properties loadProperties() { - return properties; - } - - @Override - protected void doInit() throws Exception { - Config config = ConfigProvider.getConfig(); - - for (String name : config.getPropertyNames()) { - Optional<String> value = config.getOptionalValue(name, String.class); - value.ifPresent(s -> properties.put(name, s)); + if (config == null) { + config = ConfigProvider.getConfig(); } - - LOG.info("Initialized CamelMicroProfilePropertiesSource with {} properties loaded", properties.size()); + Optional<String> value = config.getOptionalValue(name, String.class); + return value.orElse(null); } @Override protected void doStart() throws Exception { - // noop + config = ConfigProvider.getConfig(); } @Override diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java index c1793cb..c2debeb 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java @@ -17,36 +17,44 @@ package org.apache.camel.component.properties; import java.util.Iterator; -import java.util.List; /** * Default {@link PropertiesLookup} which lookup properties from a {@link java.util.Properties} with all existing properties. */ public class DefaultPropertiesLookup implements PropertiesLookup { - private final List<LocationPropertiesSource> locationSources; - private final List<PropertiesSource> sources; + private final PropertiesComponent component; - public DefaultPropertiesLookup(List<LocationPropertiesSource> locationSources, List<PropertiesSource> sources) { - this.locationSources = locationSources; - this.sources = sources; + public DefaultPropertiesLookup(PropertiesComponent component) { + this.component = component; } @Override public String lookup(String name) { String answer = null; - // try till first found source - Iterator<LocationPropertiesSource> it = locationSources.iterator(); - while (answer == null && it.hasNext()) { - answer = it.next().getProperty(name); + + // override takes precedence + if (component.getOverrideProperties() != null) { + answer = component.getOverrideProperties().getProperty(name); } - if (answer == null && sources != null) { + if (answer == null) { // try till first found source - Iterator<PropertiesSource> it2 = sources.iterator(); + Iterator<PropertiesSource> it2 = component.getSources().iterator(); while (answer == null && it2.hasNext()) { answer = it2.next().getProperty(name); } } + if (answer == null) { + // try till first found location source + Iterator<LocationPropertiesSource> it = component.getLocationSources().iterator(); + while (answer == null && it.hasNext()) { + answer = it.next().getProperty(name); + } + } + if (answer == null && component.getInitialProperties() != null) { + answer = component.getInitialProperties().getProperty(name); + } + return answer; } } 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 9e2e030..74af64f 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 @@ -105,12 +105,12 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>(); private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver(this); private PropertiesParser propertiesParser = new DefaultPropertiesParser(this); - private List<PropertiesLocation> locations = Collections.emptyList(); + private final PropertiesLookup propertiesLookup = new DefaultPropertiesLookup(this); private final List<PropertiesSource> sources = new ArrayList<>(); private final List<LocationPropertiesSource> locationSources = new ArrayList<>(); + private List<PropertiesLocation> locations = Collections.emptyList(); private transient Properties cachedLoadedProperties; - private final PropertiesLookup propertiesLookup = new DefaultPropertiesLookup(locationSources, sources); @Metadata private boolean ignoreMissingLocation; @@ -524,6 +524,14 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. } } + public List<PropertiesSource> getSources() { + return sources; + } + + public List<LocationPropertiesSource> getLocationSources() { + return locationSources; + } + @Override protected void doInit() throws Exception { super.doInit(); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java index 375c38f..53244d0 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentRefTest.java @@ -48,23 +48,6 @@ public class PropertiesComponentRefTest extends ContextTestSupport { assertMockEndpointsSatisfied(); } - @Test - public void testPropertiesComponentLocationsOverride() throws Exception { - context.addRoutes(new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:start").to("properties:{{bar.end}}?locations=ref:coolBar"); - } - }); - context.start(); - - getMockEndpoint("mock:bar").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - @Override protected JndiRegistry createRegistry() throws Exception { JndiRegistry jndi = super.createRegistry();