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 43643fd73dc9ecdcc25656648aa31f28ef3fce3a Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Sun Aug 11 23:57:38 2019 +0200 CAMEL-13855: camel-microprofile-config: discover properties sources from registry --- .../CamelMicroProfilePropertiesSourceTest.java | 24 +++++++++- .../component/properties/PropertiesComponent.java | 7 ++- .../PropertiesComponentPropertiesSourceTest.java | 54 ++++++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java b/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java index 1487926..16370bc 100644 --- a/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java +++ b/components/camel-microprofile-config/src/test/java/org/apache/camel/component/microprofile/config/CamelMicroProfilePropertiesSourceTest.java @@ -23,6 +23,8 @@ import io.smallrye.config.SmallRyeConfigBuilder; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.PropertiesSource; +import org.apache.camel.spi.Registry; import org.apache.camel.test.junit4.CamelTestSupport; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; @@ -47,11 +49,29 @@ public class CamelMicroProfilePropertiesSourceTest extends CamelTestSupport { return super.createCamelContext(); } + @Override + protected void bindToRegistry(Registry registry) throws Exception { + Properties prop = new Properties(); + prop.put("who", "Camel"); + + registry.bind("ps", new PropertiesSource() { + @Override + public String getName() { + return "ps"; + } + + @Override + public String getProperty(String name) { + return prop.getProperty(name); + } + }); + } + @Test public void testMicroProfileConfig() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World from Camel"); - template.sendBody("direct:start", context.resolvePropertyPlaceholders("Hello {{hi}}")); + template.sendBody("direct:start", context.resolvePropertyPlaceholders("Hello {{hi}} from {{who}}")); assertMockEndpointsSatisfied(); } 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 17db6ba..42ba34c 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 @@ -461,6 +461,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. // 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) { @@ -468,7 +473,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. if (obj instanceof PropertiesSource) { PropertiesSource ps = (PropertiesSource) obj; addPropertiesSource(ps); - LOG.info("PropertiesComponent added custom PropertiesSource: {}", 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()); } 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 new file mode 100644 index 0000000..77aa267 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentPropertiesSourceTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.properties; + +import java.util.Properties; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.spi.PropertiesSource; +import org.junit.Test; + +public class PropertiesComponentPropertiesSourceTest extends ContextTestSupport { + @Test + public void testPropertiesSourceFromRegistry() { + 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}}")); + } + + private static final class PropertiesPropertiesSource extends Properties implements PropertiesSource { + private final String name; + + public PropertiesPropertiesSource(String name, String... kv) { + assert kv.length % 2 == 0; + + this.name = name; + + for (int i = 0; i < kv.length; i += 2) { + super.setProperty(kv[i], kv[i + 1]); + } + } + + @Override + public String getName() { + return name; + } + } +}