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 958321b58ceb2de3ff1c31197fd1655fa1d5ba92
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Thu Jul 4 05:52:31 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.
---
 .../src/main/docs/properties-component.adoc        |  2 +-
 .../AbstractLocationPropertiesSource.java          |  8 +++----
 .../properties/ClasspathPropertiesSource.java      |  8 +++----
 .../component/properties/FilePropertiesSource.java |  8 +++----
 .../component/properties/PropertiesComponent.java  |  8 +++----
 .../component/properties/RefPropertiesSource.java  | 28 +++++++++++-----------
 .../camel/spring/CamelContextFactoryBean.java      |  8 ++++---
 .../spi/BridgePropertyPlaceholderConfigurer.java   | 15 ++++++++++--
 8 files changed, 48 insertions(+), 37 deletions(-)

diff --git 
a/components/camel-properties/src/main/docs/properties-component.adoc 
b/components/camel-properties/src/main/docs/properties-component.adoc
index f80a0b2..ae8154c 100644
--- a/components/camel-properties/src/main/docs/properties-component.adoc
+++ b/components/camel-properties/src/main/docs/properties-component.adoc
@@ -112,10 +112,10 @@ The component supports 17 options, which are listed below.
 | *camel.component.properties.override-properties* | Sets a special list of 
override properties that take precedence and will use first, if a property 
exist. The option is a java.util.Properties type. |  | String
 | *camel.component.properties.prefix-token* | Sets the value of the prefix 
token used to identify properties to replace. Setting a value of null restores 
the default token (link DEFAULT_PREFIX_TOKEN). | {{ | String
 | *camel.component.properties.properties-parser* | To use a custom 
PropertiesParser. The option is a 
org.apache.camel.component.properties.PropertiesParser type. |  | String
-| *camel.component.properties.properties-resolver* | To use a custom 
PropertiesResolver. The option is a 
org.apache.camel.component.properties.PropertiesResolver type. |  | String
 | *camel.component.properties.resolve-property-placeholders* | Whether the 
component should resolve property placeholders on itself when starting. Only 
properties which are of String type can use property placeholders. | true | 
Boolean
 | *camel.component.properties.suffix-token* | Sets the value of the suffix 
token used to identify properties to replace. Setting a value of null restores 
the default token (link DEFAULT_SUFFIX_TOKEN). | }} | String
 | *camel.component.properties.system-properties-mode* | Sets the system 
property mode. The default mode (override) is to use system properties if 
present, and override any existing properties. | 2 | Integer
+| *camel.component.properties.properties-resolver* | *Deprecated* To use a 
custom PropertiesResolver. The option is a 
org.apache.camel.component.properties.PropertiesResolver type. |  | String
 |===
 // spring-boot-auto-configure options: END
 
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 0a7a010..94a10c4c 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
@@ -30,15 +30,13 @@ public abstract class AbstractLocationPropertiesSource 
extends ServiceSupport im
     private final Properties properties = new OrderedProperties();
     private final PropertiesComponent propertiesComponent;
     private final PropertiesLocation location;
-    private final boolean ignoreMissingLocation;
 
-    protected AbstractLocationPropertiesSource(PropertiesComponent 
propertiesComponent, PropertiesLocation location, boolean 
ignoreMissingLocation) {
+    protected AbstractLocationPropertiesSource(PropertiesComponent 
propertiesComponent, PropertiesLocation location) {
         this.propertiesComponent = propertiesComponent;
         this.location = location;
-        this.ignoreMissingLocation = ignoreMissingLocation;
     }
 
-    abstract Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation 
location);
+    abstract Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, PropertiesLocation location);
 
     public PropertiesLocation getLocation() {
         return location;
@@ -58,7 +56,7 @@ public abstract class AbstractLocationPropertiesSource 
extends ServiceSupport im
     protected void doInit() throws Exception {
         super.doInit();
 
-        Properties prop = loadPropertiesFromLocation(propertiesComponent, 
ignoreMissingLocation, location);
+        Properties prop = loadPropertiesFromLocation(propertiesComponent, 
location);
         prop = prepareLoadedProperties(prop);
         properties.putAll(prop);
     }
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
index 2971411..8817873 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
+++ 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
@@ -30,8 +30,8 @@ import org.apache.camel.util.OrderedProperties;
 
 public class ClasspathPropertiesSource extends 
AbstractLocationPropertiesSource {
 
-    public ClasspathPropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location, boolean ignoreMissingLocation) {
-        super(propertiesComponent, location, ignoreMissingLocation);
+    public ClasspathPropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location) {
+        super(propertiesComponent, location);
     }
 
     @Override
@@ -40,14 +40,14 @@ public class ClasspathPropertiesSource extends 
AbstractLocationPropertiesSource
     }
 
     @Override
-    protected Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation 
location) {
+    protected Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, PropertiesLocation location) {
         Properties answer = new OrderedProperties();
         String path = location.getPath();
 
         InputStream is = 
propertiesComponent.getCamelContext().getClassResolver().loadResourceAsStream(path);
         Reader reader = null;
         if (is == null) {
-            if (!ignoreMissingLocation && !location.isOptional()) {
+            if (!propertiesComponent.isIgnoreMissingLocation() && 
!location.isOptional()) {
                 throw RuntimeCamelException.wrapRuntimeCamelException(new 
FileNotFoundException("Properties file " + path + " not found in classpath"));
             }
         } else {
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
index ea74da1..ee94acc 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
+++ 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
@@ -31,8 +31,8 @@ import org.apache.camel.util.OrderedProperties;
 
 public class FilePropertiesSource extends AbstractLocationPropertiesSource {
 
-    protected FilePropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location, boolean ignoreMissingLocation) {
-        super(propertiesComponent, location, ignoreMissingLocation);
+    protected FilePropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location) {
+        super(propertiesComponent, location);
     }
 
     @Override
@@ -40,7 +40,7 @@ public class FilePropertiesSource extends 
AbstractLocationPropertiesSource {
         return "FilePropertiesSource[" + getLocation().getPath() + "]";
     }
 
-    protected Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation 
location) {
+    protected Properties loadPropertiesFromLocation(PropertiesComponent 
propertiesComponent, PropertiesLocation location) {
         Properties answer = new OrderedProperties();
         String path = location.getPath();
 
@@ -55,7 +55,7 @@ public class FilePropertiesSource extends 
AbstractLocationPropertiesSource {
                 answer.load(is);
             }
         } catch (FileNotFoundException e) {
-            if (!ignoreMissingLocation && !location.isOptional()) {
+            if (!propertiesComponent.isIgnoreMissingLocation() && 
!location.isOptional()) {
                 throw RuntimeCamelException.wrapRuntimeCamelException(e);
             }
         } catch (IOException e) {
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 95ed85f..af122fe 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
@@ -612,14 +612,14 @@ public class PropertiesComponent extends DefaultComponent 
implements org.apache.
 
     private void addPropertiesLocationsAsPropertiesSource(PropertiesLocation 
location) {
         if ("ref".equals(location.getResolver())) {
-            addPropertiesSource(new RefPropertiesSource(this, location, 
ignoreMissingLocation));
+            addPropertiesSource(new RefPropertiesSource(this, location));
         } else if ("file".equals(location.getResolver())) {
-            addPropertiesSource(new FilePropertiesSource(this, location, 
ignoreMissingLocation));
+            addPropertiesSource(new FilePropertiesSource(this, location));
         } else if ("classpath".equals(location.getResolver())) {
-            addPropertiesSource(new ClasspathPropertiesSource(this, location, 
ignoreMissingLocation));
+            addPropertiesSource(new ClasspathPropertiesSource(this, location));
         } else {
             // classpath is also default
-            addPropertiesSource(new ClasspathPropertiesSource(this, location, 
ignoreMissingLocation));
+            addPropertiesSource(new ClasspathPropertiesSource(this, location));
         }
     }
     private List<PropertiesLocation> parseLocations(List<PropertiesLocation> 
locations) {
diff --git 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java
 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java
index 946ab45..ad99eef 100644
--- 
a/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java
+++ 
b/components/camel-properties/src/main/java/org/apache/camel/component/properties/RefPropertiesSource.java
@@ -27,12 +27,10 @@ public class RefPropertiesSource implements 
LocationPropertiesSource  {
 
     private final PropertiesComponent propertiesComponent;
     private final PropertiesLocation location;
-    private final boolean ignoreMissingLocation;
 
-    public RefPropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location, boolean ignoreMissingLocation) {
+    public RefPropertiesSource(PropertiesComponent propertiesComponent, 
PropertiesLocation location) {
         this.propertiesComponent = propertiesComponent;
         this.location = location;
-        this.ignoreMissingLocation = ignoreMissingLocation;
     }
 
     @Override
@@ -48,7 +46,7 @@ public class RefPropertiesSource implements 
LocationPropertiesSource  {
     @Override
     public String getProperty(String name) {
         // this will lookup the property on-demand
-        Properties properties = 
lookupPropertiesInRegistry(propertiesComponent, ignoreMissingLocation, 
location);
+        Properties properties = 
lookupPropertiesInRegistry(propertiesComponent, location);
         if (properties != null) {
             return properties.getProperty(name);
         } else {
@@ -56,20 +54,22 @@ public class RefPropertiesSource implements 
LocationPropertiesSource  {
         }
     }
 
-    protected Properties lookupPropertiesInRegistry(PropertiesComponent 
propertiesComponent, boolean ignoreMissingLocation, PropertiesLocation 
location) {
+    protected Properties lookupPropertiesInRegistry(PropertiesComponent 
propertiesComponent, PropertiesLocation location) {
         String path = location.getPath();
-        Properties answer;
-        try {
-            answer = 
propertiesComponent.getCamelContext().getRegistry().lookupByNameAndType(path, 
Properties.class);
-        } catch (Exception ex) {
-            // just look up the Map as a fault back
-            Map map = 
propertiesComponent.getCamelContext().getRegistry().lookupByNameAndType(path, 
Map.class);
+        Properties answer = null;
+
+        Object obj = 
propertiesComponent.getCamelContext().getRegistry().lookupByName(path);
+        if (obj instanceof Properties) {
+            answer = (Properties) obj;
+        } else if (obj instanceof Map) {
             answer = new OrderedProperties();
-            answer.putAll(map);
-        }
-        if (answer == null && (!ignoreMissingLocation && 
!location.isOptional())) {
+            answer.putAll((Map<?, ?>) obj);
+        } else if (obj instanceof PropertiesResolver) {
+            // ignore
+        } else if (!propertiesComponent.isIgnoreMissingLocation() && 
!location.isOptional()) {
             throw RuntimeCamelException.wrapRuntimeCamelException(new 
FileNotFoundException("Properties " + path + " not found in registry"));
         }
+
         return answer;
     }
 }
diff --git 
a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
 
b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
index cdce2c9..0cbc647 100644
--- 
a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
+++ 
b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java
@@ -341,11 +341,13 @@ public class CamelContextFactoryBean extends 
AbstractCamelContextFactoryBean<Spr
             pc.setPropertiesResolver(configurer);
             pc.setPropertiesParser(configurer);
 
+            pc.addPropertiesSource(configurer);
+
             // and update locations to have our as ref first
-            List<PropertiesLocation> locations = new 
ArrayList<>(pc.getLocations());
-            locations.add(0, new PropertiesLocation("ref", id));
+//            List<PropertiesLocation> locations = new 
ArrayList<>(pc.getLocations());
+//            locations.add(0, new PropertiesLocation("ref", id));
 
-            pc.setLocations(locations);
+            //pc.setLocations(locations);
         } else if (beans.size() > 1) {
             LOG.warn("Cannot bridge Camel and Spring property placeholders, as 
exact only 1 bean of type BridgePropertyPlaceholderConfigurer"
                     + " must be defined, was {} beans defined.", beans.size());
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 22407f2..b5e2f1d 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
@@ -26,6 +26,7 @@ import 
org.apache.camel.component.properties.PropertiesLocation;
 import org.apache.camel.component.properties.PropertiesLookup;
 import org.apache.camel.component.properties.PropertiesParser;
 import org.apache.camel.component.properties.PropertiesResolver;
+import org.apache.camel.component.properties.PropertiesSource;
 import org.springframework.beans.BeansException;
 import 
org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
@@ -36,7 +37,7 @@ import org.springframework.util.PropertyPlaceholderHelper;
  * A {@link PropertyPlaceholderConfigurer} that bridges Camel's <a 
href="http://camel.apache.org/using-propertyplaceholder.html";>
  * property placeholder</a> with the Spring property placeholder mechanism.
  */
-public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConfigurer implements PropertiesResolver, PropertiesParser {
+public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConfigurer implements PropertiesResolver, PropertiesParser, 
PropertiesSource {
 
     // NOTE: this class must be in the spi package as if its in the root 
package, then Spring fails to parse the XML
     // files due some weird spring issue. But that is okay as having this 
class in the spi package is fine anyway.
@@ -126,7 +127,7 @@ public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConf
         String value = props.getProperty(placeholder);
         if (parser != null) {
             // Just apply the parser to the place holder value to avoid 
configuring the other placeholder configure twice for the inside and outside 
camel context
-            return parser.parseProperty(placeholder, value, new 
DefaultPropertiesLookup(props));
+            return parser.parseProperty(placeholder, value, 
props::getProperty);
         } else {
             return value;
         }
@@ -204,6 +205,16 @@ public class BridgePropertyPlaceholderConfigurer extends 
PropertyPlaceholderConf
         }
     }
 
+    @Override
+    public String getName() {
+        return "BridgePropertyPlaceholderConfigurer";
+    }
+
+    @Override
+    public String getProperty(String name) {
+        return properties.getProperty(name);
+    }
+
     private class BridgePropertyPlaceholderResolver implements 
PropertyPlaceholderHelper.PlaceholderResolver {
 
         private final PropertiesLookup properties;

Reply via email to