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


The following commit(s) were added to refs/heads/main by this push:
     new 1f2a0ff  CAMEL-17293: camel-jbang/camel-yaml-dsl - Add support for 
camel-k spec/configuration
1f2a0ff is described below

commit 1f2a0ff41cd66ea338dedc9b99b6dc399fe02aec
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Feb 27 16:10:15 2022 +0100

    CAMEL-17293: camel-jbang/camel-yaml-dsl - Add support for camel-k 
spec/configuration
---
 .../IntegrationConfigurationPropertiesSource.java  | 85 ++++++++++++++++++++++
 .../camel/dsl/yaml/YamlRoutesBuilderLoader.java    | 43 +++++++++++
 .../camel/dsl/yaml/IntegrationLoaderTest.groovy    | 56 ++++++++++++++
 3 files changed, 184 insertions(+)

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
new file mode 100644
index 0000000..9c326ed
--- /dev/null
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/main/java/org/apache/camel/dsl/yaml/IntegrationConfigurationPropertiesSource.java
@@ -0,0 +1,85 @@
+/*
+ * 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.dsl.yaml;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelContextAware;
+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 {
+
+    private final Properties properties = new OrderedProperties();
+    private CamelContext camelContext;
+
+    @Override
+    public CamelContext getCamelContext() {
+        return camelContext;
+    }
+
+    @Override
+    public void setCamelContext(CamelContext camelContext) {
+        this.camelContext = camelContext;
+    }
+
+    @Override
+    public String getName() {
+        return "integration-configuration";
+    }
+
+    @Override
+    public String getProperty(String name) {
+        return properties.getProperty(name);
+    }
+
+    public void parseConfigurationValue(String line) {
+        if (line.contains("=")) {
+            String key = StringHelper.before(line, "=").trim();
+            String value = StringHelper.after(line, "=").trim();
+            properties.setProperty(key, value);
+        } else {
+            if (ResourceHelper.hasScheme(line)) {
+                // it is a properties file so load resource
+                try (InputStream is = 
ResourceHelper.resolveResourceAsInputStream(camelContext, line)) {
+                    Properties prop = new Properties();
+                    prop.load(is);
+                    for (String k : prop.stringPropertyNames()) {
+                        String v = prop.getProperty(k);
+                        String key = k.trim();
+                        String value = v.trim();
+                        properties.setProperty(key, value);
+                    }
+                } catch (Exception e) {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    @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 23a5b53..9040a16 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
@@ -52,6 +52,7 @@ import org.apache.camel.model.rest.RestDefinition;
 import org.apache.camel.model.rest.VerbDefinition;
 import org.apache.camel.spi.CamelContextCustomizer;
 import org.apache.camel.spi.DependencyStrategy;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.Resource;
 import org.apache.camel.spi.annotations.RoutesLoader;
 import org.apache.camel.support.ObjectHelper;
@@ -268,6 +269,12 @@ public class YamlRoutesBuilderLoader extends 
YamlRoutesBuilderLoaderSupport {
             var dep = preConfigureDependencies(deps);
             answer.add(dep);
         }
+        // if there are configurations then include them early
+        Node configuration = nodeAt(root, "/spec/configuration");
+        if (configuration != null) {
+            var list = preConfigureConfiguration(configuration);
+            answer.addAll(list);
+        }
         // if there are sources then include them before routes
         Node sources = nodeAt(root, "/spec/sources");
         if (sources != null) {
@@ -304,6 +311,42 @@ public class YamlRoutesBuilderLoader extends 
YamlRoutesBuilderLoaderSupport {
         };
     }
 
+    private List<CamelContextCustomizer> preConfigureConfiguration(Node node) {
+        List<CamelContextCustomizer> answer = new ArrayList<>();
+
+        final List<String> lines = new ArrayList<>();
+        SequenceNode seq = asSequenceNode(node);
+        for (Node n : seq.getValue()) {
+            MappingNode content = asMappingNode(n);
+            Map<String, Object> params = asMap(content);
+            Object type = params.get("type");
+            Object value = params.get("value");
+            if ("property".equals(type) && value != null) {
+                String line = value.toString();
+                lines.add(line);
+            }
+        }
+        answer.add(new CamelContextCustomizer() {
+            @Override
+            public void configure(CamelContext camelContext) {
+                try {
+                    PropertiesComponent pc = 
camelContext.getPropertiesComponent();
+                    IntegrationConfigurationPropertiesSource ps
+                            = (IntegrationConfigurationPropertiesSource) 
pc.getPropertiesSource("integration-configuration");
+                    if (ps == null) {
+                        ps = new IntegrationConfigurationPropertiesSource();
+                        pc.addPropertiesSource(ps);
+                    }
+                    lines.forEach(ps::parseConfigurationValue);
+                } catch (Exception e) {
+                    throw new RuntimeCamelException("Error adding properties 
from spec/configuration", e);
+                }
+            }
+        });
+
+        return answer;
+    }
+
     private List<CamelContextCustomizer> preConfigureSources(Node node) {
         List<CamelContextCustomizer> answer = new ArrayList<>();
 
diff --git 
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/IntegrationLoaderTest.groovy
 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/IntegrationLoaderTest.groovy
new file mode 100644
index 0000000..75e9904
--- /dev/null
+++ 
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/groovy/org/apache/camel/dsl/yaml/IntegrationLoaderTest.groovy
@@ -0,0 +1,56 @@
+/*
+ * 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.dsl.yaml
+
+import org.apache.camel.component.seda.SedaComponent
+import org.apache.camel.dsl.yaml.support.YamlTestSupport
+import org.apache.camel.spi.PropertiesComponent
+
+class IntegrationLoaderTest extends YamlTestSupport {
+
+    def "integration configuration"() {
+        when:
+            loadIntegrations('''
+                apiVersion: camel.apache.org/v1
+                kind: Integration
+                metadata:
+                  name: foobar
+                spec:
+                  configuration:
+                    - type: property
+                      value: camel.component.seda.queueSize = 123
+                    - type: property
+                      value: camel.component.seda.default-block-when-full = 
true
+                  flows:
+                    - from:
+                        uri: "seda:foo"
+                        steps:    
+                          - log:
+                             logging-level: "INFO"
+                             message: "test"
+                             log-name: "yaml"
+                          - to: "mock:result"   
+                          ''')
+        then:
+            context.routeDefinitions.size() == 1
+
+            PropertiesComponent pc = context.getPropertiesComponent()
+            pc.resolveProperty("camel.component.seda.queueSize").get() == "123"
+            
pc.resolveProperty("camel.component.seda.default-block-when-full").get() == 
"true"
+    }
+
+}

Reply via email to