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 35afd1b51e25a70d35e9c5668cd4b0f8abf975d7
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Jul 4 11:48:58 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.
---
 .../camel/blueprint/BlueprintPropertiesSource.java |  3 +-
 .../config/CamelMicroProfilePropertiesSource.java  |  2 +-
 .../src/main/docs/properties-component.adoc        | 29 ++++++++++++++++++++
 .../AbstractLocationPropertiesSource.java          |  1 +
 .../properties/DefaultPropertiesLookup.java        |  2 ++
 .../properties/LocationPropertiesSource.java       |  2 ++
 .../component/properties/PropertiesComponent.java  | 15 +++++-----
 .../spi/BridgePropertyPlaceholderConfigurer.java   |  2 +-
 .../camel/spi}/LoadablePropertiesSource.java       |  2 +-
 .../org/apache/camel/spi/PropertiesComponent.java  | 32 ++++++++++++++++++----
 .../org/apache/camel/spi}/PropertiesSource.java    |  2 +-
 .../org/apache/camel/builder/RouteBuilder.java     |  9 +++---
 .../camel/support/builder/ExpressionBuilder.java   |  2 +-
 13 files changed, 80 insertions(+), 23 deletions(-)

diff --git 
a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesSource.java
 
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesSource.java
index 12b0fcd..29c85b6 100644
--- 
a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesSource.java
+++ 
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesSource.java
@@ -17,14 +17,13 @@
 package org.apache.camel.blueprint;
 
 import java.lang.reflect.Method;
-import java.util.Arrays;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import org.apache.aries.blueprint.ext.PropertyPlaceholderExt;
-import org.apache.camel.component.properties.PropertiesSource;
+import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.support.ObjectHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.ReflectionHelper;
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 71c7541..9c6b21c 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
@@ -18,7 +18,7 @@ package org.apache.camel.component.microprofile.config;
 
 import java.util.Optional;
 
-import org.apache.camel.component.properties.PropertiesSource;
+import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.support.service.ServiceSupport;
 import org.eclipse.microprofile.config.Config;
 import org.eclipse.microprofile.config.ConfigProvider;
diff --git 
a/components/camel-properties/src/main/docs/properties-component.adoc 
b/components/camel-properties/src/main/docs/properties-component.adoc
index a6711b1..3fb7cd3 100644
--- a/components/camel-properties/src/main/docs/properties-component.adoc
+++ b/components/camel-properties/src/main/docs/properties-component.adoc
@@ -925,6 +925,35 @@ pc.addFunction(new MyBeerFunction());
 ----
  
 
+=== Using 3rd-party properties sources
+
+The properties component allows to plugin 3rd party sources to load and lookup 
properties via the `PropertySource`
+API from camel-api. For example the `camel-microprofile-config` component is 
implemented using this.
+The 3rd-party `PropertySource` can automatic be discoverd from classpath when 
Camel is starting up.
+This is done by include the file 
`META-INF/services/org/apache/camel/property-source-factory` file
+which refers to the fully qualified class name of the `PropertySource` 
implementation.
+See the `camel-microprofile-config` for an example.
+
+You can also register 3rd-part property sources via Java API
+
+[source,java]
+----
+PropertiesComponent pc = ...
+pc.addPropertySource(myPropertySource);
+----
+
+==== LoadablePropertySource
+
+A `PropertySource` can define that it supports loading all its properties from 
the source at once,
+for example from file system. This allows Camel properties component to load 
these properties at once
+during startup.
+
+==== PropertySource
+
+The regular `PropertySource` will lookup the property on-demand, for example 
to lookup
+values from a backend source such as a database or HashiCorp Vault etc.
+
+
 === See Also
 
 * xref:properties-component.adoc[Properties] component
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
index abb08df..37e2575 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
+++ 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.properties;
 import java.util.Map;
 import java.util.Properties;
 
+import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.OrderedProperties;
 
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 45737fb..1e4a2b5 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
@@ -18,6 +18,8 @@ package org.apache.camel.component.properties;
 
 import java.util.Iterator;
 
+import org.apache.camel.spi.PropertiesSource;
+
 /**
  * Default {@link PropertiesLookup} which lookup properties from a {@link 
java.util.Properties} with all existing properties.
  */
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/LocationPropertiesSource.java
 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/LocationPropertiesSource.java
index bcfddb0..d924665 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/LocationPropertiesSource.java
+++ 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/LocationPropertiesSource.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.properties;
 
+import org.apache.camel.spi.PropertiesSource;
+
 /**
  * A {@link PropertiesSource} which was created from a {@link 
PropertiesLocation}.
  */
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 be22fc8..1886a4d 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
@@ -22,6 +22,7 @@ import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Properties;
 
 import org.apache.camel.CamelContextAware;
@@ -32,7 +33,9 @@ import org.apache.camel.StaticService;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.spi.FactoryFinder;
+import org.apache.camel.spi.LoadablePropertiesSource;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
 import org.apache.camel.support.OrderedComparator;
@@ -51,10 +54,6 @@ import org.slf4j.LoggerFactory;
 @ManagedResource(description = "Managed PropertiesComponent")
 public class PropertiesComponent extends DefaultComponent implements 
org.apache.camel.spi.PropertiesComponent, StaticService {
 
-    // TODO: PropertySource / LoadablePropertySource to camel-api
-    // TODO: API on PropertiesComponent in SPI to Optional<String> 
lookupProperty(String name);
-    // TODO: Add docs about `PropertiesSource`
-
     /**
      *  Never check system properties.
      */
@@ -171,6 +170,11 @@ public class PropertiesComponent extends DefaultComponent 
implements org.apache.
         return parseUri(uri, propertiesLookup);
     }
 
+    public Optional<String> resolveProperty(String key) {
+        String value = parseUri(key, propertiesLookup);
+        return Optional.of(value);
+    }
+
     public Properties loadProperties() {
         Properties prop = new OrderedProperties();
 
@@ -457,9 +461,6 @@ public class PropertiesComponent extends DefaultComponent 
implements org.apache.
         this.environmentVariableMode = environmentVariableMode;
     }
 
-    /**
-     * Adds a custom {@link PropertiesSource}
-     */
     public void addPropertiesSource(PropertiesSource propertiesSource) {
         if (propertiesSource instanceof CamelContextAware) {
             ((CamelContextAware) 
propertiesSource).setCamelContext(getCamelContext());
diff --git 
a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
 
b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
index 6e74905..4f2229a 100644
--- 
a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
+++ 
b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/BridgePropertyPlaceholderConfigurer.java
@@ -20,7 +20,7 @@ import java.util.Properties;
 
 import org.apache.camel.component.properties.PropertiesLookup;
 import org.apache.camel.component.properties.PropertiesParser;
-import org.apache.camel.component.properties.PropertiesSource;
+import org.apache.camel.spi.PropertiesSource;
 import org.springframework.beans.BeansException;
 import 
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java
 
b/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
similarity index 96%
rename from 
components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java
rename to 
core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
index 0cda7e2..32bf08f 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/LoadablePropertiesSource.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/spi/LoadablePropertiesSource.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.properties;
+package org.apache.camel.spi;
 
 import java.util.Properties;
 
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
index 07a7c8e..10014f3 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
@@ -16,16 +16,18 @@
  */
 package org.apache.camel.spi;
 
-import java.io.IOError;
+import java.util.Optional;
 import java.util.Properties;
 
 import org.apache.camel.Component;
 import org.apache.camel.StaticService;
 
+/**
+ * Component for property placeholders and loading properties from sources
+ * (such as .properties file from classpath or file system)
+ */
 public interface PropertiesComponent extends Component, StaticService {
 
-    // TODO: addPropertiesSource to make it easier to add custom sources
-
     /**
      * The default prefix token.
      */
@@ -41,12 +43,20 @@ public interface PropertiesComponent extends Component, 
StaticService {
      */
     String DEFAULT_CREATED = "PropertiesComponentDefaultCreated";
 
+    /**
+     * The value of the prefix token used to identify properties to replace.
+     * Is default {@link #DEFAULT_PREFIX_TOKEN}
+     */
     String getPrefixToken();
 
+    /**
+     * The value of the suffix token used to identify properties to replace.
+     * Is default {@link #DEFAULT_SUFFIX_TOKEN}
+     */
     String getSuffixToken();
 
     /**
-     * Parses the input text and resolve all property placeholders.
+     * Parses the input text and resolve all property placeholders from within 
the text.
      *
      * @param uri  input text
      * @return text with resolved property placeholders
@@ -55,10 +65,17 @@ public interface PropertiesComponent extends Component, 
StaticService {
     String parseUri(String uri);
 
     /**
+     * Looks up the property with the given key
+     *
+     * @param key  the name of the property
+     * @return the property value if present
+     */
+    Optional<String> resolveProperty(String key);
+
+    /**
      * Loads the properties from the default locations.
      *
      * @return the properties loaded.
-     * @throws IOError is thrown if error loading properties
      */
     Properties loadProperties();
 
@@ -75,6 +92,11 @@ public interface PropertiesComponent extends Component, 
StaticService {
     void addLocation(String location);
 
     /**
+     * Adds a custom {@link PropertiesSource} to use as source for loading 
and/or looking up property values.
+     */
+    void addPropertiesSource(PropertiesSource propertiesSource);
+
+    /**
      * Whether to silently ignore if a location cannot be located, such as a 
properties file not found.
      */
     void setIgnoreMissingLocation(boolean ignoreMissingLocation);
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java
 b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSource.java
similarity index 96%
rename from 
components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java
rename to 
core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSource.java
index 685b754..00116b2 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesSource.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesSource.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.component.properties;
+package org.apache.camel.spi;
 
 import org.apache.camel.Ordered;
 
diff --git 
a/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java 
b/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
index 974316b..200d251 100644
--- a/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
+++ b/core/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.CamelContext;
@@ -274,11 +275,11 @@ public abstract class RouteBuilder extends BuilderSupport 
implements RoutesBuild
 
         // the properties component is mandatory
         PropertiesComponent pc = getContext().getPropertiesComponent();
-        // enclose key with {{ }} to force parsing
-        Object value = pc.parseUri(pc.getPrefixToken() + key + 
pc.getSuffixToken());
+        // resolve property
+        Optional<String> value = pc.resolveProperty(key);
 
-        if (value != null) {
-            return getContext().getTypeConverter().mandatoryConvertTo(type, 
value);
+        if (value.isPresent()) {
+            return getContext().getTypeConverter().mandatoryConvertTo(type, 
value.get());
         } else {
             return null;
         }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
index e38eb61..4965e31 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java
@@ -1557,7 +1557,7 @@ public class ExpressionBuilder {
                         throw new 
IllegalArgumentException("PropertiesComponent with name properties must be 
defined"
                             + " in CamelContext to support property 
placeholders in expressions");
                     }
-                    // enclose key with {{ }} to force parsing
+                    // enclose key with {{ }} to force parsing as key can be a 
nested expression too
                     return pc.parseUri(pc.getPrefixToken() + text + 
pc.getSuffixToken());
                 } catch (Exception e) {
                     // property with key not found, use default value if 
provided

Reply via email to