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 autoCreated;
public QueueConfiguration() {
}
/**
* Instantiate this object and invoke {@link #setName(SimpleString)}
*

View File

@ -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<>();

View File

@ -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 {

View File

@ -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();