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

clebertsuconic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new ebd6c28999 ARTEMIS-5373 json properties, support key.surround and user 
surrounding of keys with dots or quotes
ebd6c28999 is described below

commit ebd6c289990ac22b9d8e4ec9ef1bced10520ecc6
Author: Gary Tully <[email protected]>
AuthorDate: Tue Apr 1 17:10:01 2025 +0100

    ARTEMIS-5373 json properties, support key.surround and user surrounding of 
keys with dots or quotes
---
 .../core/config/impl/ConfigurationImpl.java        | 37 +++++++++----
 .../core/config/impl/ConfigurationImplTest.java    | 62 +++++++++++++++++++++-
 2 files changed, 89 insertions(+), 10 deletions(-)

diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java
index d7ee1c6d46..76128e6bca 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java
@@ -597,12 +597,13 @@ public class ConfigurationImpl implements Configuration, 
Serializable {
    }
 
    public void parseFileProperties(File file) throws Exception {
+      final ConfigurationImpl configuration = this;
       InsertionOrderedProperties brokerProperties = new 
InsertionOrderedProperties();
       try (FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader)) {
          try {
             if (file.getName().endsWith(".json")) {
-               brokerProperties.loadJson(reader);
+               brokerProperties.loadJson(configuration, reader);
             } else {
                brokerProperties.load(reader);
             }
@@ -3921,26 +3922,24 @@ public class ConfigurationImpl implements 
Configuration, Serializable {
          orderedMap.clear();
       }
 
-      public synchronized boolean loadJson(Reader reader) throws IOException {
+      public synchronized boolean loadJson(ConfigurationImpl configuration, 
Reader reader) throws IOException {
          JsonObject jsonObject = JsonLoader.readObject(reader);
-
-         loadJsonObject("", jsonObject);
+         final String surroundString = determineSurroundString(configuration, 
jsonObject);
+         loadJsonObject(surroundString, "", jsonObject);
 
          return true;
       }
 
-      private void loadJsonObject(String parentKey, JsonObject jsonObject) {
+      private void loadJsonObject(String keySurroundString, String parentKey, 
JsonObject jsonObject) {
          
jsonObject.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(jsonEntry
 -> {
             JsonValue jsonValue = jsonEntry.getValue();
             JsonValue.ValueType jsonValueType = jsonValue.getValueType();
             String jsonKey = jsonEntry.getKey();
-            if (jsonKey.contains(".")) {
-               jsonKey = "\"" + jsonKey + "\"";
-            }
+            jsonKey = autoSurroundIfNecessary(jsonKey, keySurroundString);
             String propertyKey = parentKey + jsonKey;
             switch (jsonValueType) {
                case OBJECT:
-                  loadJsonObject(propertyKey + ".", jsonValue.asJsonObject());
+                  loadJsonObject(keySurroundString, propertyKey + ".", 
jsonValue.asJsonObject());
                   break;
                case STRING:
                   put(propertyKey, jsonObject.getString(jsonKey));
@@ -3955,5 +3954,25 @@ public class ConfigurationImpl implements Configuration, 
Serializable {
             }
          });
       }
+
+      private String autoSurroundIfNecessary(String jsonKey, String 
keySurroundString) {
+         String result = jsonKey;
+         if (keyNeedsAutoSurround(jsonKey, keySurroundString)) {
+            result = keySurroundString + jsonKey + keySurroundString;
+         }
+         return result;
+      }
+
+      private boolean keyNeedsAutoSurround(String jsonKey, String 
keySurroundString) {
+         return jsonKey.contains(".") && !jsonKey.startsWith("key.") && 
!(jsonKey.startsWith(keySurroundString) && jsonKey.endsWith(keySurroundString));
+      }
+
+      private String determineSurroundString(ConfigurationImpl configuration, 
JsonObject jsonObject) {
+         String surroundString = 
jsonObject.getString(ActiveMQDefaultConfiguration.BROKER_PROPERTIES_KEY_SURROUND_PROPERTY,
 null);
+         if (surroundString == null) {
+            surroundString = 
jsonObject.getString("brokerPropertiesKeySurround", 
configuration.getBrokerPropertiesKeySurround());
+         }
+         return surroundString;
+      }
    }
 }
diff --git 
a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
index c1980c86ba..84b8214af7 100644
--- 
a/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
+++ 
b/artemis-server/src/test/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImplTest.java
@@ -2029,9 +2029,10 @@ public class ConfigurationImplTest extends 
AbstractConfigurationTestBase {
       ConfigurationImpl.InsertionOrderedProperties properties =
          new ConfigurationImpl.InsertionOrderedProperties();
 
+      ConfigurationImpl configuration = new ConfigurationImpl();
       JsonObject configJsonObject = buildSimpleConfigJsonObject();
       try (StringReader stringReader = new 
StringReader(configJsonObject.toString())) {
-         properties.loadJson(stringReader);
+         properties.loadJson(configuration, stringReader);
       }
 
       List<String> keys = new ArrayList<>();
@@ -2076,6 +2077,65 @@ public class ConfigurationImplTest extends 
AbstractConfigurationTestBase {
       testSimpleConfig(configuration);
    }
 
+   @Test
+   public void testJsonPropertiesKeySurround() throws Exception {
+
+      File tmpFile = File.createTempFile("json-surround-props-test", ".json", 
temporaryFolder);
+      try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
+           PrintWriter printWriter = new PrintWriter(fileOutputStream)) {
+         printWriter.write("{\n" +
+                              "\"key.surround\": \"$$\",\n" +
+                              "\"addressSettings\": {\n" +
+                              "\"$$a.\\\"with_quote\\\".b$$\": {\n" +
+                                 "\"maxDeliveryAttempts\": 0\n" +
+                              "}\n}\n}");
+      }
+
+      ConfigurationImpl configuration = new ConfigurationImpl();
+      configuration.parseProperties(tmpFile.getAbsolutePath());
+
+      assertEquals(0, 
configuration.getAddressSettings().get("a.\"with_quote\".b").getMaxDeliveryAttempts());
+   }
+
+   @Test
+   public void testJsonPropertiesGlobalKeySurround() throws Exception {
+
+      File tmpFile = File.createTempFile("json-global-surround-props-test", 
".json", temporaryFolder);
+      try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
+           PrintWriter printWriter = new PrintWriter(fileOutputStream)) {
+         printWriter.write("{\n" +
+                              "\"addressSettings\": {\n" +
+                              "\"$$a.\\\"with_quote\\\".b$$\": {\n" +
+                              "\"maxDeliveryAttempts\": 0\n" +
+                              "}\n}\n}");
+      }
+
+      ConfigurationImpl configuration = new ConfigurationImpl();
+      configuration.setBrokerPropertiesKeySurround("$$");
+      configuration.parseProperties(tmpFile.getAbsolutePath());
+
+      assertEquals(0, 
configuration.getAddressSettings().get("a.\"with_quote\".b").getMaxDeliveryAttempts());
+   }
+
+   @Test
+   public void testJsonPropertiesUserQuoted() throws Exception {
+
+      File tmpFile = File.createTempFile("json-user-quote-props-test", 
".json", temporaryFolder);
+      try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
+           PrintWriter printWriter = new PrintWriter(fileOutputStream)) {
+         printWriter.write("{\n" +
+                              "\"addressSettings\": {\n" +
+                              "\"a.b.c\": {\n" +
+                              "\"maxDeliveryAttempts\": 2\n" +
+                              "}\n}\n}");
+      }
+
+      ConfigurationImpl configuration = new ConfigurationImpl();
+      configuration.parseProperties(tmpFile.getAbsolutePath());
+
+      assertEquals(2, 
configuration.getAddressSettings().get("a.b.c").getMaxDeliveryAttempts());
+   }
+
    @Test
    public void testInvalidPropertiesReaderFromFile() throws Exception {
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact


Reply via email to