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/06/22 07:42:22 UTC

[camel] branch main updated: camel-jbang - Support reading properties in camel or kebab case.

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 518e1866c76 camel-jbang - Support reading properties in camel or kebab case.
518e1866c76 is described below

commit 518e1866c7629515e566ccaa17e1dc5bd5f2d589
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jun 22 09:42:11 2022 +0200

    camel-jbang - Support reading properties in camel or kebab case.
---
 .../apache/camel/util/BaseOrderedProperties.java   |  6 +-
 .../camel/util/CamelCaseOrderedProperties.java     | 39 +++++++++++++
 .../camel/util/CamelCaseOrderedPropertiesTest.java | 66 ++++++++++++++++++++++
 .../camel/dsl/jbang/core/commands/Export.java      |  5 +-
 .../dsl/jbang/core/commands/ExportBaseCommand.java | 11 ++--
 .../dsl/jbang/core/commands/ExportCamelMain.java   |  5 +-
 .../dsl/jbang/core/commands/ExportQuarkus.java     |  5 +-
 .../dsl/jbang/core/commands/ExportSpringBoot.java  |  5 +-
 .../apache/camel/dsl/jbang/core/commands/Run.java  |  8 +--
 9 files changed, 131 insertions(+), 19 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/BaseOrderedProperties.java b/core/camel-util/src/main/java/org/apache/camel/util/BaseOrderedProperties.java
index 7ad820cdd28..c85c868941f 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/BaseOrderedProperties.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/BaseOrderedProperties.java
@@ -39,7 +39,11 @@ abstract class BaseOrderedProperties extends Properties {
 
     @Override
     public synchronized Object put(Object key, Object value) {
-        return map.put(key.toString(), value.toString());
+        return doPut(key.toString(), value.toString());
+    }
+
+    protected Object doPut(String key, String value) {
+        return map.put(key, value);
     }
 
     @Override
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java b/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java
new file mode 100644
index 00000000000..f011c86ddd8
--- /dev/null
+++ b/core/camel-util/src/main/java/org/apache/camel/util/CamelCaseOrderedProperties.java
@@ -0,0 +1,39 @@
+/*
+ * 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.util;
+
+import java.util.Properties;
+
+/**
+ * This class is a camelCase ordered {@link Properties} where the key/values are stored in the order they are added or
+ * loaded.
+ * <p/>
+ * The keys are stored in camelCase style, for example a key of <code>camel.main.stream-caching-enabled</code> is stored
+ * as <code>camel.main.streamCachingEnabled</code>
+ * <p/>
+ * Note: This implementation is only intended as implementation detail for the Camel properties component, and has only
+ * been designed to provide the needed functionality. The complex logic for loading properties has been kept from the
+ * JDK {@link Properties} class.
+ */
+public final class CamelCaseOrderedProperties extends BaseOrderedProperties {
+
+    @Override
+    protected Object doPut(String key, String value) {
+        key = StringHelper.dashToCamelCase(key);
+        return super.doPut(key, value);
+    }
+}
diff --git a/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java b/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java
new file mode 100644
index 00000000000..d67df549e85
--- /dev/null
+++ b/core/camel-util/src/test/java/org/apache/camel/util/CamelCaseOrderedPropertiesTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.util;
+
+import java.util.Iterator;
+import java.util.Properties;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CamelCaseOrderedPropertiesTest {
+
+    @Test
+    public void testOrdered() throws Exception {
+        Properties prop = new CamelCaseOrderedProperties();
+        prop.setProperty("hello-world", "Hi Camel");
+        prop.setProperty("camel.main.stream-caching-enabled", "true");
+
+        assertEquals(2, prop.size());
+
+        Iterator it = prop.keySet().iterator();
+        assertEquals("helloWorld", it.next());
+        assertEquals("camel.main.streamCachingEnabled", it.next());
+
+        it = prop.values().iterator();
+        assertEquals("Hi Camel", it.next());
+        assertEquals("true", it.next());
+    }
+
+    @Test
+    public void testOrderedLoad() throws Exception {
+        Properties prop = new CamelCaseOrderedProperties();
+        prop.load(CamelCaseOrderedPropertiesTest.class.getResourceAsStream("/application.properties"));
+
+        assertEquals(4, prop.size());
+
+        Iterator it = prop.keySet().iterator();
+        assertEquals("hello", it.next());
+        assertEquals("camel.component.seda.concurrentConsumers", it.next());
+        assertEquals("camel.component.seda.queueSize", it.next());
+        assertEquals("camel.component.direct.timeout", it.next());
+
+        // should be ordered values
+        it = prop.values().iterator();
+        assertEquals("World", it.next());
+        assertEquals("2", it.next());
+        assertEquals("500", it.next());
+        assertEquals("1234", it.next());
+    }
+
+}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
index 8f420784e6a..2042c024143 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Export.java
@@ -18,8 +18,9 @@ package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.util.Properties;
 
-import org.apache.camel.util.OrderedProperties;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import picocli.CommandLine.Command;
 
 @Command(name = "export",
@@ -35,7 +36,7 @@ class Export extends ExportBaseCommand {
         // read runtime and gav from profile if not configured
         File profile = new File(getProfile() + ".properties");
         if (profile.exists()) {
-            OrderedProperties prop = new OrderedProperties();
+            Properties prop = new CamelCaseOrderedProperties();
             prop.load(new FileInputStream(profile));
             if (this.runtime == null) {
                 this.runtime = prop.getProperty("camel.jbang.runtime");
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
index a55ba670c80..eb0bb305294 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java
@@ -14,7 +14,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.dsl.jbang.core.commands;
 
 import java.io.File;
@@ -37,9 +36,9 @@ import java.util.stream.Collectors;
 
 import org.apache.camel.dsl.jbang.core.common.RuntimeUtil;
 import org.apache.camel.main.download.MavenGav;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.camel.util.StringHelper;
 import picocli.CommandLine;
 
@@ -208,7 +207,7 @@ abstract class ExportBaseCommand extends CamelCommand {
 
         // include custom dependencies defined in profile
         if (profile != null && profile.exists()) {
-            OrderedProperties prop = new OrderedProperties();
+            Properties prop = new CamelCaseOrderedProperties();
             prop.load(new FileInputStream(profile));
             String deps = prop.getProperty("camel.jbang.dependencies");
             if (deps != null) {
@@ -240,7 +239,7 @@ abstract class ExportBaseCommand extends CamelCommand {
             String packageName)
             throws Exception {
         // read the settings file and find the files to copy
-        OrderedProperties prop = new OrderedProperties();
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(settings));
 
         for (String k : SETTINGS_PROP_SOURCE_KEYS) {
@@ -289,9 +288,9 @@ abstract class ExportBaseCommand extends CamelCommand {
             File settings, File profile, File targetDir,
             Function<Properties, Object> customize)
             throws Exception {
-        OrderedProperties prop = new OrderedProperties();
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(settings));
-        OrderedProperties prop2 = new OrderedProperties();
+        Properties prop2 = new CamelCaseOrderedProperties();
         if (profile.exists()) {
             prop2.load(new FileInputStream(profile));
         }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
index 6da1a3b94ed..2ce12401558 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportCamelMain.java
@@ -20,14 +20,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.camel.catalog.CamelCatalog;
 import org.apache.camel.catalog.DefaultCamelCatalog;
 import org.apache.camel.main.download.MavenGav;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 
 class ExportCamelMain extends Export {
@@ -128,7 +129,7 @@ class ExportCamelMain extends Export {
         context = context.replaceAll("\\{\\{ \\.CamelVersion }}", camelVersion);
         context = context.replaceAll("\\{\\{ \\.MainClassname }}", packageName + "." + mainClassname);
 
-        OrderedProperties prop = new OrderedProperties();
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(settings));
         String repos = prop.getProperty("camel.jbang.repos");
         if (repos == null) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
index 0aa570ab99f..68ce820e7f2 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportQuarkus.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
+import java.util.Properties;
 import java.util.Set;
 
 import javax.xml.parsers.DocumentBuilder;
@@ -38,9 +39,9 @@ import org.apache.camel.main.download.MavenArtifact;
 import org.apache.camel.main.download.MavenDependencyDownloader;
 import org.apache.camel.main.download.MavenGav;
 import org.apache.camel.tooling.model.ArtifactModel;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 
 class ExportQuarkus extends Export {
@@ -164,7 +165,7 @@ class ExportQuarkus extends Export {
         context = context.replaceFirst("\\{\\{ \\.JavaVersion }}", javaVersion);
         context = context.replaceFirst("\\{\\{ \\.CamelVersion }}", camelVersion);
 
-        OrderedProperties prop = new OrderedProperties();
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(settings));
         String repos = prop.getProperty("camel.jbang.repos");
         if (repos == null) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
index 2d68d7a6e27..f46bcb8d752 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportSpringBoot.java
@@ -21,6 +21,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.Properties;
 import java.util.Set;
 
 import org.apache.camel.catalog.CamelCatalog;
@@ -30,9 +31,9 @@ import org.apache.camel.main.KameletMain;
 import org.apache.camel.main.download.MavenDependencyDownloader;
 import org.apache.camel.main.download.MavenGav;
 import org.apache.camel.tooling.model.ArtifactModel;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.commons.io.FileUtils;
 
 class ExportSpringBoot extends Export {
@@ -131,7 +132,7 @@ class ExportSpringBoot extends Export {
         context = context.replaceFirst("\\{\\{ \\.JavaVersion }}", javaVersion);
         context = context.replaceFirst("\\{\\{ \\.CamelVersion }}", camelVersion);
 
-        OrderedProperties prop = new OrderedProperties();
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(settings));
         String repos = prop.getProperty("camel.jbang.repos");
         if (repos == null) {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index bf040f80404..a82635414ed 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -54,10 +54,10 @@ import org.apache.camel.impl.lw.LightweightCamelContext;
 import org.apache.camel.main.KameletMain;
 import org.apache.camel.main.download.DownloadListener;
 import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.util.CamelCaseOrderedProperties;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.OrderedProperties;
 import org.apache.camel.util.StringHelper;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.config.Configurator;
@@ -247,8 +247,8 @@ class Run extends CamelCommand {
         return 0;
     }
 
-    private OrderedProperties loadProfileProperties(File source) throws Exception {
-        OrderedProperties prop = new OrderedProperties();
+    private Properties loadProfileProperties(File source) throws Exception {
+        Properties prop = new CamelCaseOrderedProperties();
         prop.load(new FileInputStream(source));
 
         // special for routes include pattern that we need to "fix" after reading from properties
@@ -280,7 +280,7 @@ class Run extends CamelCommand {
             generateOpenApi();
         }
 
-        OrderedProperties profileProperties = null;
+        Properties profileProperties = null;
         File profilePropertiesFile = new File(getProfile() + ".properties");
         if (profilePropertiesFile.exists()) {
             profileProperties = loadProfileProperties(profilePropertiesFile);