You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by br...@apache.org on 2022/05/18 06:08:03 UTC

[activemq-artemis] branch main updated: ARTEMIS-3826 - allow address/queue configuration via properties

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

brusdev 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 168b61ec38 ARTEMIS-3826 - allow address/queue configuration via properties
168b61ec38 is described below

commit 168b61ec38f8e17c5b3c7695cef2b9d57bed19ac
Author: Gary Tully <ga...@gmail.com>
AuthorDate: Thu May 12 12:37:10 2022 +0100

    ARTEMIS-3826 - allow address/queue configuration via properties
---
 .../artemis/api/core/QueueConfiguration.java       |  3 ++
 .../core/config/CoreAddressConfiguration.java      |  4 ++
 .../core/config/impl/ConfigurationImpl.java        | 50 +++++++++++++---------
 .../core/config/impl/ConfigurationImplTest.java    | 19 ++++++++
 4 files changed, 55 insertions(+), 21 deletions(-)

diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/QueueConfiguration.java b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/QueueConfiguration.java
index f6b630e642..75ea6bf1b6 100644
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/QueueConfiguration.java
+++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/QueueConfiguration.java
@@ -110,6 +110,9 @@ public class QueueConfiguration implements Serializable {
    private Boolean _transient;
    private Boolean autoCreated;
 
+   public QueueConfiguration() {
+   }
+
    /**
     * Instantiate this object and invoke {@link #setName(SimpleString)}
     *
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/CoreAddressConfiguration.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/CoreAddressConfiguration.java
index 948d1ce252..0dad2b4a40 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/CoreAddressConfiguration.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/config/CoreAddressConfiguration.java
@@ -76,6 +76,10 @@ public class CoreAddressConfiguration implements Serializable {
       return this;
    }
 
+   public CoreAddressConfiguration addQueueConfig(QueueConfiguration queueConfiguration) {
+      return addQueueConfiguration(queueConfiguration);
+   }
+
    @Deprecated
    public List<CoreQueueConfiguration> getQueueConfigurations() {
       List<CoreQueueConfiguration> result = new ArrayList<>();
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 6a0e86e3e3..aeb5b829f4 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
@@ -519,7 +519,9 @@ public class ConfigurationImpl implements Configuration, Serializable {
    }
 
    public void populateWithProperties(Map<String, Object> beanProperties) throws InvocationTargetException, IllegalAccessException {
-      BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), new CollectionAutoFillPropertiesUtil());
+      CollectionAutoFillPropertiesUtil autoFillCollections = new CollectionAutoFillPropertiesUtil();
+      BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), autoFillCollections);
+      autoFillCollections.setBeanUtilsBean(beanUtils);
       // nested property keys delimited by . and enclosed by '"' if they key's themselves contain dots
       beanUtils.getPropertyUtils().setResolver(new SurroundResolver(getBrokerPropertiesKeySurround(beanProperties)));
       beanUtils.getConvertUtils().register(new Converter() {
@@ -2763,6 +2765,7 @@ public class ConfigurationImpl implements Configuration, Serializable {
 
       private static final Object[] EMPTY_OBJECT_ARRAY = new Object[]{};
       final Stack<Pair<String, Object>> collections = new Stack<>();
+      private BeanUtilsBean beanUtilsBean;
 
       @Override
       public void setProperty(final Object bean, final String name, final Object value) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
@@ -2788,16 +2791,13 @@ public class ConfigurationImpl implements Configuration, Serializable {
                }
                return map.get(key);
             } else { // collection
-               // locate on name property
-               for (Object candidate : (Collection) bean) {
-                  if (key.equals(getProperty(candidate, "name"))) {
-                     return candidate;
-                  }
+               Object value = findByNameProperty(key, (Collection)bean);
+               if (value == null) {
+                  // create it
+                  value = newNamedInstanceForCollection(collectionInfo.getA(), collectionInfo.getB(), key);
+                  ((Collection) bean).add(value);
                }
-               // or create it
-               Object created = newNamedInstanceForCollection(collectionInfo.getA(), collectionInfo.getB(), key);
-               ((Collection) bean).add(created);
-               return created;
+               return value;
             }
          }
 
@@ -2809,6 +2809,17 @@ public class ConfigurationImpl implements Configuration, Serializable {
          return resolved;
       }
 
+      private Object findByNameProperty(String key, Collection collection) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
+         // locate on name property, may be a SimpleString
+         for (Object candidate : collection) {
+            Object candidateName = getProperty(candidate, "name");
+            if (candidateName != null && key.equals(candidateName.toString())) {
+               return candidate;
+            }
+         }
+         return null;
+      }
+
       // allow finding beans in collections via name() such that a mapped key (key)
       // can be used to access and *not* auto create entries
       @Override
@@ -2858,12 +2869,7 @@ public class ConfigurationImpl implements Configuration, Serializable {
                if (invokeResult instanceof Map) {
                   result = ((Map<?, ?>)invokeResult).get(key);
                } else if (invokeResult instanceof Collection) {
-                  // locate on name property
-                  for (Object candidate : (Collection) invokeResult) {
-                     if (key.equals(getProperty(candidate, "name"))) {
-                        return candidate;
-                     }
-                  }
+                  result = findByNameProperty(key, (Collection) invokeResult);
                }
             } else {
                throw new NoSuchMethodException("Property '" + name +
@@ -2894,14 +2900,11 @@ public class ConfigurationImpl implements Configuration, Serializable {
                // create one and initialise with name
                try {
                   Object instance = candidate.getParameterTypes()[candidate.getParameterCount() - 1].getDeclaredConstructor().newInstance(null);
-                  try {
-                     setProperty(instance, "name", name);
-                  } catch (NoSuchMethodException okIgnore) {
-                  }
+                  beanUtilsBean.setProperty(instance, "name", name);
 
                   // this is always going to be a little hacky b/c our config is not natively property friendly
                   if (instance instanceof TransportConfiguration) {
-                     setProperty(instance, "factoryClassName", "invm".equals(name) ? InVMConnectorFactory.class.getName() : NettyConnectorFactory.class.getName());
+                     beanUtilsBean.setProperty(instance, "factoryClassName", "invm".equals(name) ? InVMConnectorFactory.class.getName() : NettyConnectorFactory.class.getName());
                   }
                   return instance;
 
@@ -2913,6 +2916,11 @@ public class ConfigurationImpl implements Configuration, Serializable {
          }
          throw new IllegalArgumentException("failed to locate add method for collection property " + addPropertyName);
       }
+
+      public void setBeanUtilsBean(BeanUtilsBean beanUtilsBean) {
+         // we want type conversion
+         this.beanUtilsBean = beanUtilsBean;
+      }
    }
 
    private static class SurroundResolver extends DefaultResolver {
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 76c478f620..8f7128446d 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
@@ -722,6 +722,25 @@ public class ConfigurationImplTest extends ActiveMQTestBase {
       Assert.assertEquals("TF", configuration.getConnectionRouters().get(0).getKeyFilter());
    }
 
+
+   @Test
+   public void testAddressViaProperties() throws Throwable {
+      ConfigurationImpl configuration = new ConfigurationImpl();
+
+      Properties properties = new Properties();
+
+      properties.put("addressConfigurations.\"LB.TEST\".queueConfigs.\"LB.TEST\".routingType", "ANYCAST");
+      properties.put("addressConfigurations.\"LB.TEST\".queueConfigs.\"LB.TEST\".durable", "false");
+
+      configuration.parsePrefixedProperties(properties, null);
+
+      Assert.assertEquals(1, configuration.getAddressConfigurations().size());
+      Assert.assertEquals(1, configuration.getAddressConfigurations().get(0).getQueueConfigs().size());
+      Assert.assertEquals(SimpleString.toSimpleString("LB.TEST"), configuration.getAddressConfigurations().get(0).getQueueConfigs().get(0).getAddress());
+      Assert.assertEquals(false, configuration.getAddressConfigurations().get(0).getQueueConfigs().get(0).isDurable());
+   }
+
+
    @Test
    public void testAddressSettingsViaProperties() throws Throwable {
       ConfigurationImpl configuration = new ConfigurationImpl();