Skip to content
Permalink
Browse files
ARTEMIS-3826 - allow address/queue configuration via properties
  • Loading branch information
gtully authored and brusdev committed May 18, 2022
1 parent f632e81 commit 168b61ec38f8e17c5b3c7695cef2b9d57bed19ac
Showing 4 changed files with 55 additions and 21 deletions.
@@ -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)}
*
@@ -76,6 +76,10 @@ public CoreAddressConfiguration addQueueConfiguration(QueueConfiguration queueCo
return this;
}

public CoreAddressConfiguration addQueueConfig(QueueConfiguration queueConfiguration) {
return addQueueConfiguration(queueConfiguration);
}

@Deprecated
public List<CoreQueueConfiguration> getQueueConfigurations() {
List<CoreQueueConfiguration> result = new ArrayList<>();
@@ -519,7 +519,9 @@ public void parsePrefixedProperties(Properties properties, String prefix) throws
}

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 @@ private static class CollectionAutoFillPropertiesUtil extends PropertyUtilsBean

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 Object getProperty(final Object bean,
}
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 Object getProperty(final Object bean,
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 Object getMappedProperty(final Object bean,
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 @@ private Object newNamedInstanceForCollection(String collectionPropertyName, Obje
// 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 @@ private Object newNamedInstanceForCollection(String collectionPropertyName, Obje
}
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 {
@@ -722,6 +722,25 @@ public void testSetNestedPropertyOnExistingCollectionEntryViaMappedNotation() th
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();

0 comments on commit 168b61e

Please sign in to comment.