ARTEMIS-3826 - allow address/queue configuration via properties

This commit is contained in:
Gary Tully 2022-05-12 12:37:10 +01:00 committed by Bruscino Domenico Francesco
parent f632e8104b
commit 168b61ec38
4 changed files with 55 additions and 21 deletions

View File

@ -110,6 +110,9 @@ public class QueueConfiguration implements Serializable {
private Boolean _transient; private Boolean _transient;
private Boolean autoCreated; private Boolean autoCreated;
public QueueConfiguration() {
}
/** /**
* Instantiate this object and invoke {@link #setName(SimpleString)} * Instantiate this object and invoke {@link #setName(SimpleString)}
* *

View File

@ -76,6 +76,10 @@ public class CoreAddressConfiguration implements Serializable {
return this; return this;
} }
public CoreAddressConfiguration addQueueConfig(QueueConfiguration queueConfiguration) {
return addQueueConfiguration(queueConfiguration);
}
@Deprecated @Deprecated
public List<CoreQueueConfiguration> getQueueConfigurations() { public List<CoreQueueConfiguration> getQueueConfigurations() {
List<CoreQueueConfiguration> result = new ArrayList<>(); List<CoreQueueConfiguration> result = new ArrayList<>();

View File

@ -519,7 +519,9 @@ public class ConfigurationImpl implements Configuration, Serializable {
} }
public void populateWithProperties(Map<String, Object> beanProperties) throws InvocationTargetException, IllegalAccessException { 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 // nested property keys delimited by . and enclosed by '"' if they key's themselves contain dots
beanUtils.getPropertyUtils().setResolver(new SurroundResolver(getBrokerPropertiesKeySurround(beanProperties))); beanUtils.getPropertyUtils().setResolver(new SurroundResolver(getBrokerPropertiesKeySurround(beanProperties)));
beanUtils.getConvertUtils().register(new Converter() { beanUtils.getConvertUtils().register(new Converter() {
@ -2763,6 +2765,7 @@ public class ConfigurationImpl implements Configuration, Serializable {
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[]{}; private static final Object[] EMPTY_OBJECT_ARRAY = new Object[]{};
final Stack<Pair<String, Object>> collections = new Stack<>(); final Stack<Pair<String, Object>> collections = new Stack<>();
private BeanUtilsBean beanUtilsBean;
@Override @Override
public void setProperty(final Object bean, final String name, final Object value) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException { 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); return map.get(key);
} else { // collection } else { // collection
// locate on name property Object value = findByNameProperty(key, (Collection)bean);
for (Object candidate : (Collection) bean) { if (value == null) {
if (key.equals(getProperty(candidate, "name"))) { // create it
return candidate; value = newNamedInstanceForCollection(collectionInfo.getA(), collectionInfo.getB(), key);
} ((Collection) bean).add(value);
} }
// or create it return value;
Object created = newNamedInstanceForCollection(collectionInfo.getA(), collectionInfo.getB(), key);
((Collection) bean).add(created);
return created;
} }
} }
@ -2809,6 +2809,17 @@ public class ConfigurationImpl implements Configuration, Serializable {
return resolved; 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) // allow finding beans in collections via name() such that a mapped key (key)
// can be used to access and *not* auto create entries // can be used to access and *not* auto create entries
@Override @Override
@ -2858,12 +2869,7 @@ public class ConfigurationImpl implements Configuration, Serializable {
if (invokeResult instanceof Map) { if (invokeResult instanceof Map) {
result = ((Map<?, ?>)invokeResult).get(key); result = ((Map<?, ?>)invokeResult).get(key);
} else if (invokeResult instanceof Collection) { } else if (invokeResult instanceof Collection) {
// locate on name property result = findByNameProperty(key, (Collection) invokeResult);
for (Object candidate : (Collection) invokeResult) {
if (key.equals(getProperty(candidate, "name"))) {
return candidate;
}
}
} }
} else { } else {
throw new NoSuchMethodException("Property '" + name + throw new NoSuchMethodException("Property '" + name +
@ -2894,14 +2900,11 @@ public class ConfigurationImpl implements Configuration, Serializable {
// create one and initialise with name // create one and initialise with name
try { try {
Object instance = candidate.getParameterTypes()[candidate.getParameterCount() - 1].getDeclaredConstructor().newInstance(null); Object instance = candidate.getParameterTypes()[candidate.getParameterCount() - 1].getDeclaredConstructor().newInstance(null);
try { beanUtilsBean.setProperty(instance, "name", name);
setProperty(instance, "name", name);
} catch (NoSuchMethodException okIgnore) {
}
// this is always going to be a little hacky b/c our config is not natively property friendly // this is always going to be a little hacky b/c our config is not natively property friendly
if (instance instanceof TransportConfiguration) { 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; return instance;
@ -2913,6 +2916,11 @@ public class ConfigurationImpl implements Configuration, Serializable {
} }
throw new IllegalArgumentException("failed to locate add method for collection property " + addPropertyName); 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 { private static class SurroundResolver extends DefaultResolver {

View File

@ -722,6 +722,25 @@ public class ConfigurationImplTest extends ActiveMQTestBase {
Assert.assertEquals("TF", configuration.getConnectionRouters().get(0).getKeyFilter()); 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 @Test
public void testAddressSettingsViaProperties() throws Throwable { public void testAddressSettingsViaProperties() throws Throwable {
ConfigurationImpl configuration = new ConfigurationImpl(); ConfigurationImpl configuration = new ConfigurationImpl();