You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/03/30 09:40:42 UTC

[camel] 02/02: CAMEL-17815: Show correct location of configured properties when parsed from yaml-dsl

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit bcbf2a33ec96c832d4488275587551fa6d8ea700
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Mar 30 11:39:46 2022 +0200

    CAMEL-17815: Show correct location of configured properties when parsed from yaml-dsl
---
 .../AbstractLocationPropertiesSource.java          | 12 ++++++++-
 .../properties/ClasspathPropertiesSource.java      |  4 +--
 .../component/properties/FilePropertiesSource.java |  2 +-
 .../IntegrationConfigurationPropertiesSource.java  | 31 ++++++++++++----------
 .../camel/dsl/yaml/YamlRoutesBuilderLoader.java    | 31 +++++++++++++---------
 5 files changed, 50 insertions(+), 30 deletions(-)

diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
index 62d8918..b4bd408 100644
--- a/core/camel-base/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/AbstractLocationPropertiesSource.java
@@ -41,7 +41,7 @@ public abstract class AbstractLocationPropertiesSource extends ServiceSupport
         this.location = location;
     }
 
-    abstract Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location);
+    public abstract Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location);
 
     @Override
     public PropertiesLocation getLocation() {
@@ -88,6 +88,16 @@ public abstract class AbstractLocationPropertiesSource extends ServiceSupport
         return properties.getProperty(name);
     }
 
+    /**
+     * Sets a property
+     *
+     * @param key   the key
+     * @param value the value
+     */
+    public void setProperty(String key, String value) {
+        properties.setProperty(key, value);
+    }
+
     @Override
     protected void doInit() throws Exception {
         super.doInit();
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
index 7aba136..2646289 100644
--- a/core/camel-base/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/ClasspathPropertiesSource.java
@@ -41,7 +41,7 @@ public class ClasspathPropertiesSource extends AbstractLocationPropertiesSource
     }
 
     @Override
-    protected Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location) {
+    public Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location) {
         Properties answer = new OrderedProperties();
         String path = location.getPath();
 
@@ -71,6 +71,6 @@ public class ClasspathPropertiesSource extends AbstractLocationPropertiesSource
 
     @Override
     public int getOrder() {
-        return 200;
+        return 300;
     }
 }
diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
index 7d63576..88cc980 100644
--- a/core/camel-base/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
+++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/FilePropertiesSource.java
@@ -42,7 +42,7 @@ public class FilePropertiesSource extends AbstractLocationPropertiesSource imple
     }
 
     @Override
-    protected Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location) {
+    public Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location) {
         Properties answer = new OrderedProperties();
         String path = location.getPath();
 
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/IntegrationConfigurationPropertiesSource.java b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/IntegrationConfigurationPropertiesSource.java
index 14d270f..61b6c2c 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/IntegrationConfigurationPropertiesSource.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/IntegrationConfigurationPropertiesSource.java
@@ -21,21 +21,26 @@ import java.util.Properties;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.Ordered;
+import org.apache.camel.component.properties.AbstractLocationPropertiesSource;
+import org.apache.camel.component.properties.PropertiesComponent;
+import org.apache.camel.component.properties.PropertiesLocation;
 import org.apache.camel.spi.PropertiesSource;
 import org.apache.camel.support.ResourceHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.camel.util.StringHelper;
 
 /**
  * {@link PropertiesSource} for camel-k integration spec/configuration values.
  */
-public class IntegrationConfigurationPropertiesSource implements PropertiesSource, CamelContextAware {
+public class IntegrationConfigurationPropertiesSource extends AbstractLocationPropertiesSource
+        implements CamelContextAware, Ordered {
 
     private final String name;
-    private final Properties properties = new OrderedProperties();
     private CamelContext camelContext;
 
-    public IntegrationConfigurationPropertiesSource(String name) {
+    public IntegrationConfigurationPropertiesSource(PropertiesComponent propertiesComponent, PropertiesLocation location,
+                                                    String name) {
+        super(propertiesComponent, location);
         this.name = name;
     }
 
@@ -55,8 +60,14 @@ public class IntegrationConfigurationPropertiesSource implements PropertiesSourc
     }
 
     @Override
-    public String getProperty(String name) {
-        return properties.getProperty(name);
+    public Properties loadPropertiesFromLocation(PropertiesComponent propertiesComponent, PropertiesLocation location) {
+        // properties are "loaded" in the parseConfigurationValue
+        return null;
+    }
+
+    @Override
+    public int getOrder() {
+        return 300;
     }
 
     public void parseConfigurationValue(String line) {
@@ -83,14 +94,6 @@ public class IntegrationConfigurationPropertiesSource implements PropertiesSourc
         }
     }
 
-    protected void setProperty(String key, String value) {
-        properties.setProperty(key, value);
-        if (!camelContext.isStarted()) {
-            // if we are bootstrapping then also set as initial property, so it can be used there as well
-            camelContext.getPropertiesComponent().addInitialProperty(key, value);
-        }
-    }
-
     @Override
     public String toString() {
         return "camel-yaml-dsl";
diff --git a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
index 659cc81..3a396ee 100644
--- a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
+++ b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/YamlRoutesBuilderLoader.java
@@ -37,6 +37,7 @@ import org.apache.camel.builder.ErrorHandlerBuilder;
 import org.apache.camel.builder.NoErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.apache.camel.component.properties.PropertiesLocation;
 import org.apache.camel.dsl.yaml.common.YamlDeserializationContext;
 import org.apache.camel.dsl.yaml.common.YamlDeserializerSupport;
 import org.apache.camel.dsl.yaml.deserializers.OutputAwareFromDefinition;
@@ -289,19 +290,19 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
         // if there are configurations then include them early
         Node configuration = nodeAt(root, "/spec/configuration");
         if (configuration != null) {
-            var list = preConfigureConfiguration(configuration);
+            var list = preConfigureConfiguration(ctx.getResource(), configuration);
             answer.addAll(list);
         }
         // if there are trait configuration then include them early
         configuration = nodeAt(root, "/spec/traits/camel");
         if (configuration != null) {
-            var list = preConfigureTraitConfiguration(configuration);
+            var list = preConfigureTraitConfiguration(ctx.getResource(), configuration);
             answer.addAll(list);
         }
         // if there are trait environment then include them early
         configuration = nodeAt(root, "/spec/traits/environment");
         if (configuration != null) {
-            var list = preConfigureTraitEnvironment(configuration);
+            var list = preConfigureTraitEnvironment(ctx.getResource(), configuration);
             answer.addAll(list);
         }
 
@@ -344,7 +345,7 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
         };
     }
 
-    private List<CamelContextCustomizer> preConfigureConfiguration(Node node) {
+    private List<CamelContextCustomizer> preConfigureConfiguration(Resource resource, Node node) {
         List<CamelContextCustomizer> answer = new ArrayList<>();
 
         final List<String> lines = new ArrayList<>();
@@ -363,11 +364,13 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
             @Override
             public void configure(CamelContext camelContext) {
                 try {
-                    PropertiesComponent pc = camelContext.getPropertiesComponent();
+                    org.apache.camel.component.properties.PropertiesComponent pc
+                            = (org.apache.camel.component.properties.PropertiesComponent) camelContext.getPropertiesComponent();
                     IntegrationConfigurationPropertiesSource ps
                             = (IntegrationConfigurationPropertiesSource) pc.getPropertiesSource("integration-configuration");
                     if (ps == null) {
-                        ps = new IntegrationConfigurationPropertiesSource("integration-configuration");
+                        ps = new IntegrationConfigurationPropertiesSource(
+                                pc, new PropertiesLocation(resource.getLocation()), "integration-configuration");
                         pc.addPropertiesSource(ps);
                     }
                     lines.forEach(ps::parseConfigurationValue);
@@ -380,7 +383,7 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
         return answer;
     }
 
-    private List<CamelContextCustomizer> preConfigureTraitConfiguration(Node node) {
+    private List<CamelContextCustomizer> preConfigureTraitConfiguration(Resource resource, Node node) {
         List<CamelContextCustomizer> answer = new ArrayList<>();
 
         Node target = nodeAt(node, "configuration/properties/");
@@ -393,12 +396,14 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
             @Override
             public void configure(CamelContext camelContext) {
                 try {
-                    PropertiesComponent pc = camelContext.getPropertiesComponent();
+                    org.apache.camel.component.properties.PropertiesComponent pc
+                            = (org.apache.camel.component.properties.PropertiesComponent) camelContext.getPropertiesComponent();
                     IntegrationConfigurationPropertiesSource ps
                             = (IntegrationConfigurationPropertiesSource) pc
                                     .getPropertiesSource("integration-trait-configuration");
                     if (ps == null) {
-                        ps = new IntegrationConfigurationPropertiesSource("integration-trait-configuration");
+                        ps = new IntegrationConfigurationPropertiesSource(
+                                pc, new PropertiesLocation(resource.getLocation()), "integration-trait-configuration");
                         pc.addPropertiesSource(ps);
                     }
                     lines.forEach(ps::parseConfigurationValue);
@@ -411,7 +416,7 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
         return answer;
     }
 
-    private List<CamelContextCustomizer> preConfigureTraitEnvironment(Node node) {
+    private List<CamelContextCustomizer> preConfigureTraitEnvironment(Resource resource, Node node) {
         List<CamelContextCustomizer> answer = new ArrayList<>();
 
         Node target = nodeAt(node, "configuration/vars/");
@@ -424,12 +429,14 @@ public class YamlRoutesBuilderLoader extends YamlRoutesBuilderLoaderSupport {
             @Override
             public void configure(CamelContext camelContext) {
                 try {
-                    PropertiesComponent pc = camelContext.getPropertiesComponent();
+                    org.apache.camel.component.properties.PropertiesComponent pc
+                            = (org.apache.camel.component.properties.PropertiesComponent) camelContext.getPropertiesComponent();
                     IntegrationConfigurationPropertiesSource ps
                             = (IntegrationConfigurationPropertiesSource) pc
                                     .getPropertiesSource("environment-trait-configuration");
                     if (ps == null) {
-                        ps = new IntegrationConfigurationPropertiesSource("environment-trait-configuration");
+                        ps = new IntegrationConfigurationPropertiesSource(
+                                pc, new PropertiesLocation(resource.getLocation()), "environment-trait-configuration");
                         pc.addPropertiesSource(ps);
                     }
                     lines.forEach(ps::parseConfigurationValue);