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

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


The following commit(s) were added to refs/heads/2.x by this push:
     new ee81084da2 ARTEMIS-3998 Fix broker properties for connectionRouters
ee81084da2 is described below

commit ee81084da27c5e6d328fe1c1d42e689d1d7a579f
Author: Domenico Francesco Bruscino <[email protected]>
AuthorDate: Thu Sep 15 20:30:56 2022 +0200

    ARTEMIS-3998 Fix broker properties for connectionRouters
    
    (cherry picked from commit fda7f6ba379ec2f05be7d26524cc18f41b7e56fb)
---
 .../core/config/impl/ConfigurationImpl.java        | 57 +++++++++++-----------
 .../core/config/impl/ConfigurationImplTest.java    |  9 +++-
 2 files changed, 37 insertions(+), 29 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 e69e0eed0a..749993fce8 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
@@ -31,7 +31,6 @@ import java.io.StringWriter;
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
 import java.net.URI;
 import java.net.URL;
 import java.security.AccessController;
@@ -2961,41 +2960,43 @@ public class ConfigurationImpl implements 
Configuration, Serializable {
       private Object newNamedInstanceForCollection(String 
collectionPropertyName, Object hostingBean, String name) {
          // find the add X and init an instance of the type with name=name
 
-         String addPropertyName = "add";
+         StringBuilder addPropertyNameBuilder = new StringBuilder("add");
          // expect an add... without the plural for named accessors
          if (collectionPropertyName != null && collectionPropertyName.length() 
> 0) {
-            addPropertyName += 
Character.toUpperCase(collectionPropertyName.charAt(0)) + 
collectionPropertyName.substring(1, collectionPropertyName.length() - 1);
+            
addPropertyNameBuilder.append(Character.toUpperCase(collectionPropertyName.charAt(0)));
+            addPropertyNameBuilder.append(collectionPropertyName, 1, 
collectionPropertyName.length() - 1);
          }
 
          // we don't know the type, infer from add method add(X x) or 
add(String key, X x)
-         final Method[] methods = hostingBean.getClass().getDeclaredMethods();
-         for (Method candidate : methods) {
-            if (Modifier.isPublic(candidate.getModifiers()) && 
candidate.getName().equals(addPropertyName) &&
-               (candidate.getParameterCount() == 1 ||
-                  (candidate.getParameterCount() == 2
-                     // has a String key
-                     && String.class.equals(candidate.getParameterTypes()[0])
-                     // but not initialised from a String form (eg: uri)
-                     && 
!String.class.equals(candidate.getParameterTypes()[1])))) {
-
-               // create one and initialise with name
-               try {
-                  Object instance = 
candidate.getParameterTypes()[candidate.getParameterCount() - 
1].getDeclaredConstructor().newInstance();
-                  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) {
-                     beanUtilsBean.setProperty(instance, "factoryClassName", 
"invm".equals(name) ? InVMConnectorFactory.class.getName() : 
NettyConnectorFactory.class.getName());
-                  }
-                  return instance;
+         final String addPropertyName = addPropertyNameBuilder.toString();
+         final Method[] methods = hostingBean.getClass().getMethods();
+         final Method candidate = Arrays.stream(methods).filter(method -> 
method.getName().equals(addPropertyName) &&
+            ((method.getParameterCount() == 1) || (method.getParameterCount() 
== 2
+               // has a String key
+               && String.class.equals(method.getParameterTypes()[0])
+               // but not initialised from a String form (eg: uri)
+               && !String.class.equals(method.getParameterTypes()[1]))))
+            .sorted((method1, method2) -> method2.getParameterCount() - 
method1.getParameterCount()).findFirst().orElse(null);
+
+         if (candidate == null) {
+            throw new IllegalArgumentException("failed to locate add method 
for collection property " + addPropertyName);
+         }
 
-               } catch (Exception e) {
-                  logger.debug("Failed to add entry for " + name + " with 
method: " + candidate, e);
-                  throw new IllegalArgumentException("failed to add entry for 
collection key " + name, e);
-               }
+         // create one and initialise with name
+         try {
+            Object instance = 
candidate.getParameterTypes()[candidate.getParameterCount() - 
1].getDeclaredConstructor().newInstance();
+            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) {
+               beanUtilsBean.setProperty(instance, "factoryClassName", 
"invm".equals(name) ? InVMConnectorFactory.class.getName() : 
NettyConnectorFactory.class.getName());
             }
+            return instance;
+
+         } catch (Exception e) {
+            logger.debug("Failed to add entry for " + name + " with method: " 
+ candidate, e);
+            throw new IllegalArgumentException("failed to add entry for 
collection key " + name, e);
          }
-         throw new IllegalArgumentException("failed to locate add method for 
collection property " + addPropertyName);
       }
 
       public void setBeanUtilsBean(BeanUtilsBean beanUtilsBean) {
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 5213cbbea9..a85714d4e8 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
@@ -673,8 +673,15 @@ public class ConfigurationImplTest extends 
ActiveMQTestBase {
 
    @Test
    public void testSetConnectionRoutersPolicyConfiguration() throws Throwable {
-      ConfigurationImpl configuration = new ConfigurationImpl();
+      testSetConnectionRoutersPolicyConfiguration(new ConfigurationImpl());
+   }
+
+   @Test
+   public void testSetConnectionRoutersPolicyFileConfiguration() throws 
Throwable {
+      testSetConnectionRoutersPolicyConfiguration(new FileConfiguration());
+   }
 
+   private void testSetConnectionRoutersPolicyConfiguration(ConfigurationImpl 
configuration) throws Throwable {
       Properties insertionOrderedProperties = new 
ConfigurationImpl.InsertionOrderedProperties();
       
insertionOrderedProperties.put("connectionRouters.autoShard.localTargetFilter", 
"NULL|$STATEFUL_SET_ORDINAL");
       insertionOrderedProperties.put("connectionRouters.autoShard.keyType", 
KeyType.CLIENT_ID);

Reply via email to