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 0fa323f CAMEL-17292: Add support for modeline in camel-dsl 0fa323f is described below commit 0fa323f742082b86856b6bde9f2bcd8e0450e738 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Feb 15 15:28:18 2022 +0100 CAMEL-17292: Add support for modeline in camel-dsl --- .../src/main/docs/dsl-modeline.adoc | 1 - .../camel/dsl/modeline/BuildPropertyTrait.java | 26 ------------------ .../apache/camel/dsl/modeline/ModelineParser.java | 5 ---- .../apache/camel/dsl/modeline/PropertyTrait.java | 31 +++++++++++++++++++--- .../camel/dsl/modeline/ModelineParserTest.java | 13 +++++++++ .../src/test/resources/myapp.properties | 19 +++++++++++++ 6 files changed, 60 insertions(+), 35 deletions(-) diff --git a/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc b/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc index 596d666..840d0b5 100644 --- a/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc +++ b/dsl/camel-dsl-modeline/src/main/docs/dsl-modeline.adoc @@ -14,7 +14,6 @@ Support for Camel K style modeline when running Camel standalone such as with Ca The following traits is supported: -- build-property - dependency - env - name diff --git a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/BuildPropertyTrait.java b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/BuildPropertyTrait.java deleted file mode 100644 index 2025fbe..0000000 --- a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/BuildPropertyTrait.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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.modeline; - -public class BuildPropertyTrait extends PropertyTrait { - - @Override - public String getName() { - return "build-property"; - } - -} diff --git a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/ModelineParser.java b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/ModelineParser.java index 9c299d4..f089070 100644 --- a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/ModelineParser.java +++ b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/ModelineParser.java @@ -31,9 +31,6 @@ import org.apache.camel.util.StringQuoteHelper; public class ModelineParser { - // TODO: log which traits was parsed and used and which is skipped - // TODO: add some more docs in adoc file - public static final String MODELINE_START = "camel-k:"; private final CamelContext camelContext; @@ -47,8 +44,6 @@ public class ModelineParser { this.traits.put(trait.getName(), trait); trait = new PropertyTrait(); this.traits.put(trait.getName(), trait); - trait = new BuildPropertyTrait(); - this.traits.put(trait.getName(), trait); trait = new NameTrait(); this.traits.put(trait.getName(), trait); trait = new EnvTrait(); diff --git a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/PropertyTrait.java b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/PropertyTrait.java index 93513fb..50fd0f5 100644 --- a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/PropertyTrait.java +++ b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/PropertyTrait.java @@ -16,9 +16,13 @@ */ package org.apache.camel.dsl.modeline; +import java.io.InputStream; +import java.util.Properties; + import org.apache.camel.CamelContext; import org.apache.camel.spi.CamelContextCustomizer; import org.apache.camel.spi.PropertiesComponent; +import org.apache.camel.support.ResourceHelper; import org.apache.camel.util.StringHelper; public class PropertyTrait implements Trait { @@ -30,13 +34,34 @@ public class PropertyTrait implements Trait { @Override public CamelContextCustomizer parseTrait(String trait) { - String key = StringHelper.before(trait, "=").trim(); - String value = StringHelper.after(trait, "=").trim(); + String key; + String value; + if (trait.contains("=")) { + key = StringHelper.before(trait, "=").trim(); + value = StringHelper.after(trait, "=").trim(); + } else { + key = null; + value = trait; + } return new CamelContextCustomizer() { @Override public void configure(CamelContext camelContext) { PropertiesComponent pc = camelContext.getPropertiesComponent(); - pc.addInitialProperty(key, value); + if (ResourceHelper.hasScheme(value)) { + // it is a properties file so load resource + try (InputStream is = ResourceHelper.resolveResourceAsInputStream(camelContext, value)) { + Properties prop = new Properties(); + prop.load(is); + for (String k : prop.stringPropertyNames()) { + String v = prop.getProperty(k); + pc.addInitialProperty(k, v); + } + } catch (Exception e) { + // ignore + } + } else { + pc.addInitialProperty(key, value); + } } }; } diff --git a/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java b/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java index be0cf65..4b47385 100644 --- a/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java +++ b/dsl/camel-dsl-modeline/src/test/java/org/apache/camel/dsl/modeline/ModelineParserTest.java @@ -171,4 +171,17 @@ public class ModelineParserTest extends CamelTestSupport { Assertions.assertEquals("Hello", context.getPropertiesComponent().parseUri("{{hi}}")); } + @Test + public void testModelinePropertiesFile() throws Exception { + context.start(); + + String line = "// camel-k: property=classpath:myapp.properties"; + ModelineParser parser = new ModelineParser(context); + List<CamelContextCustomizer> customizers = parser.parse(line); + customizers.forEach(c -> c.configure(context)); + + Assertions.assertEquals("Hej", context.getPropertiesComponent().parseUri("{{hi}}")); + Assertions.assertEquals("bar", context.getPropertiesComponent().parseUri("{{foo}}")); + } + } diff --git a/dsl/camel-dsl-modeline/src/test/resources/myapp.properties b/dsl/camel-dsl-modeline/src/test/resources/myapp.properties new file mode 100644 index 0000000..f37f1bd --- /dev/null +++ b/dsl/camel-dsl-modeline/src/test/resources/myapp.properties @@ -0,0 +1,19 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- + +foo=bar +hi=Hej \ No newline at end of file