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 336c1ba79f43ebdf19e192cbe94a7a88dee15083 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jul 3 06:44:09 2019 +0200 CAMEL-13708: PropertiesSource to resolve a single property --- .../config/CamelMicroProfilePropertiesSource.java | 9 +++++-- ...esSource.java => LoadablePropertiesSource.java} | 11 +++----- .../component/properties/PropertiesComponent.java | 30 +++++++++++----------- .../component/properties/PropertiesSource.java | 21 ++++++++------- 4 files changed, 35 insertions(+), 36 deletions(-) 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 65b0d52..7622889 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 @@ -19,7 +19,7 @@ package org.apache.camel.component.microprofile.config; import java.util.Optional; import java.util.Properties; -import org.apache.camel.component.properties.PropertiesSource; +import org.apache.camel.component.properties.LoadablePropertiesSource; import org.apache.camel.support.service.ServiceSupport; import org.apache.camel.util.OrderedProperties; import org.eclipse.microprofile.config.Config; @@ -31,7 +31,7 @@ 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 PropertiesSource { +public class CamelMicroProfilePropertiesSource extends ServiceSupport implements LoadablePropertiesSource { private static final Logger LOG = LoggerFactory.getLogger(CamelMicroProfilePropertiesSource.class); @@ -43,6 +43,11 @@ public class CamelMicroProfilePropertiesSource extends ServiceSupport implements } @Override + public String getProperty(String name) { + return properties.getProperty(name); + } + + @Override public Properties loadProperties() { return properties; } diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java similarity index 78% copy from components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java copy to components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java index fee8792..f4927f2 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java @@ -19,15 +19,10 @@ package org.apache.camel.component.properties; import java.util.Properties; /** - * A source for 3rd party properties, such as Eclipse MicroProfile Config, a custom implementation - * that loads properties from a database table, or HashiCorp Vault etc. + * A source for properties that can be loaded all at once during initialization, + * such as loading .properties files. */ -public interface PropertiesSource { - - /** - * Name of properties source - */ - String getName(); +public interface LoadablePropertiesSource extends PropertiesSource { /** * Loads the properties from the source 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 28fdad0..861271a 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 @@ -222,8 +222,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. // add 3rd party sources if (!sources.isEmpty()) { for (PropertiesSource ps : sources) { - Properties p = ps.loadProperties(); - prop.putAll(p); + if (ps instanceof LoadablePropertiesSource) { + LoadablePropertiesSource lps = (LoadablePropertiesSource) ps; + Properties p = lps.loadProperties(); + prop.putAll(p); + } } } @@ -609,14 +612,12 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. * Adds a custom {@link PropertiesSource} */ public void addPropertiesSource(PropertiesSource propertiesSource) { - sources.add(propertiesSource); // prepare properties sources which we must do eager - for (PropertiesSource ps : sources) { - if (ps instanceof CamelContextAware) { - ((CamelContextAware) ps).setCamelContext(getCamelContext()); - } + if (propertiesSource instanceof CamelContextAware) { + ((CamelContextAware) propertiesSource).setCamelContext(getCamelContext()); } ServiceHelper.initService(propertiesSource); + sources.add(propertiesSource); } @Override @@ -628,14 +629,13 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. FactoryFinder factoryFinder = getCamelContext().adapt(ExtendedCamelContext.class).getFactoryFinder("META-INF/services/org/apache/camel"); Class<?> type = factoryFinder.findClass("properties-source-factory"); if (type != null) { - Object ps = getCamelContext().getInjector().newInstance(type, false); - if (ps != null) { - if (ps instanceof PropertiesSource) { - LOG.info("PropertiesComponent added custom PropertiesSource: {}", ps); - addPropertiesSource((PropertiesSource) ps); - } else { - LOG.warn("PropertiesComponent cannot add custom PropertiesSource as the type is not a org.apache.camel.component.properties.PropertiesSource but: " + type.getName()); - } + Object obj = getCamelContext().getInjector().newInstance(type, false); + if (obj instanceof PropertiesSource) { + PropertiesSource ps = (PropertiesSource) obj; + addPropertiesSource(ps); + LOG.info("PropertiesComponent added custom PropertiesSource: {}", 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) { diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java index fee8792..dfd72fd 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java @@ -1,13 +1,13 @@ -/* +/** * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -16,11 +16,8 @@ */ package org.apache.camel.component.properties; -import java.util.Properties; - /** - * A source for 3rd party properties, such as Eclipse MicroProfile Config, a custom implementation - * that loads properties from a database table, or HashiCorp Vault etc. + * A source for properties. */ public interface PropertiesSource { @@ -30,9 +27,11 @@ public interface PropertiesSource { String getName(); /** - * Loads the properties from the source + * Gets the property with the name * - * @return the loaded properties + * @param name name of property + * @return the property value, or <tt>null</tt> if no property exists */ - Properties loadProperties(); + String getProperty(String name); + }