Skip to content
Permalink
Browse files
ARTEMIS-3757 - allow system and env var substution of properties conf…
…ig, respect order of file loaded properties and add generic enum converter
  • Loading branch information
gtully authored and brusdev committed Apr 6, 2022
1 parent 4ff5d95 commit a739b9f06874f78f2999b0e15b9f2a106a78a725
Showing 8 changed files with 218 additions and 70 deletions.
@@ -17,23 +17,6 @@

package org.apache.activemq.artemis.utils.critical;

import org.apache.activemq.artemis.utils.uri.BeanSupport;
import org.apache.commons.beanutils.Converter;

public enum CriticalAnalyzerPolicy {
HALT, SHUTDOWN, LOG;

static {
// for URI support on ClusterConnection
BeanSupport.registerConverter(new CriticalAnalyzerPolicyConverter(), CriticalAnalyzerPolicy.class);
}

static class CriticalAnalyzerPolicyConverter implements Converter {

@Override
public <T> T convert(Class<T> type, Object value) {
return type.cast(CriticalAnalyzerPolicy.valueOf(value.toString()));
}
}

}
@@ -30,6 +30,7 @@
import java.util.Set;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

public class BeanSupport {
@@ -40,6 +41,25 @@ public class BeanSupport {
static {
// This is to customize the BeanUtils to use Fluent Properties as well
beanUtils.getPropertyUtils().addBeanIntrospector(new FluentPropertyBeanIntrospectorWithIgnores());

// generic converter from String to enum type via valueOf with no default
registerConverter(new Converter() {
@Override
public <T> T convert(Class<T> type, Object value) {
if (value == null) {
return null;
}

if (String.class.equals(type) || Object.class.equals(type)) {
return type.cast(value.toString());
}
if (type.isEnum()) {
return (T) Enum.valueOf((Class<Enum>)type, value.toString());
}
throw new ConversionException("Can't convert value '" + value
+ "' to type " + type);
}
}, String.class);
}

public static void registerConverter(Converter converter, Class type) {
@@ -52,6 +52,12 @@ private XMLUtil() {
// Utility class
}

public static String CONSIDER_OS_ENV_PROP = "org.apache.activemq.artemis.utils.considerOsEnv";
private static final boolean considerOsEnv;
static {
considerOsEnv = Boolean.parseBoolean(System.getProperty(CONSIDER_OS_ENV_PROP, "true"));
}

public static Element streamToElement(InputStream inputStream) throws Exception {
try (Reader reader = new InputStreamReader(inputStream)) {
return XMLUtil.readerToElement(reader);
@@ -271,7 +277,14 @@ public static String replaceSystemPropsInString(String xml) {
val = parts[1].trim();
}

String sysProp = System.getProperty(prop, val);
String sysProp = System.getProperty(prop);
if (sysProp == null && considerOsEnv) {
sysProp = System.getenv(prop);
}
if (sysProp == null) {
sysProp = val;
}
// interesting choice to replace with val == "" with no match!
logger.debug("replacing " + subString + " with " + sysProp);
xml = xml.replace(subString, sysProp);
}
@@ -99,6 +99,7 @@
import org.apache.activemq.artemis.utils.ByteUtil;
import org.apache.activemq.artemis.utils.Env;
import org.apache.activemq.artemis.utils.ObjectInputStreamWithClassLoader;
import org.apache.activemq.artemis.utils.XMLUtil;
import org.apache.activemq.artemis.utils.critical.CriticalAnalyzerPolicy;
import org.apache.activemq.artemis.utils.uri.BeanSupport;
import org.apache.commons.beanutils.BeanUtilsBean;
@@ -464,21 +465,35 @@ public void setBrokerPropertiesKeySurround(String brokerPropertiesKeySurround) {

@Override
public Configuration parseProperties(String fileUrlToProperties) throws Exception {
// system property overrides
// system property overrides location of file(s)
fileUrlToProperties = System.getProperty(ActiveMQDefaultConfiguration.BROKER_PROPERTIES_SYSTEM_PROPERTY_NAME, fileUrlToProperties);
if (fileUrlToProperties != null) {
Properties brokerProperties = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(fileUrlToProperties); BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.load(reader);
parsePrefixedProperties(brokerProperties, null);
for (String fileUrl : fileUrlToProperties.split(",")) {
Properties brokerProperties = new Properties() {
final LinkedHashMap<Object, Object> orderedMap = new LinkedHashMap<>();

@Override
public Object put(Object key, Object value) {
return orderedMap.put(key.toString(), value.toString());
}

@Override
public Set<Map.Entry<Object, Object>> entrySet() {
return orderedMap.entrySet();
}
};
try (FileInputStream fileInputStream = new FileInputStream(fileUrl); BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.load(reader);
parsePrefixedProperties(brokerProperties, null);
}
}
}
parsePrefixedProperties(System.getProperties(), systemPropertyPrefix);
return this;
}

public void parsePrefixedProperties(Properties properties, String prefix) throws Exception {
Map<String, Object> beanProperties = new HashMap<>();
Map<String, Object> beanProperties = new LinkedHashMap<>();

synchronized (properties) {
String key = null;
@@ -490,8 +505,10 @@ public void parsePrefixedProperties(Properties properties, String prefix) throws
}
key = entry.getKey().toString().substring(prefix.length());
}
logger.debug("Setting up config, " + key + "=" + entry.getValue());
beanProperties.put(key, entry.getValue());
String value = XMLUtil.replaceSystemPropsInString(entry.getValue().toString());
key = XMLUtil.replaceSystemPropsInString(key);
logger.debug("Property config, " + key + "=" + value);
beanProperties.put(key, value);
}
}

@@ -184,7 +184,7 @@ public String getMessageLoadBalancingType() {
}
clearIO();
try {
return configuration.getMessageLoadBalancingType().getType();
return configuration.getMessageLoadBalancingType().toString();
} finally {
blockOnIO();
}
@@ -16,46 +16,6 @@
*/
package org.apache.activemq.artemis.core.server.cluster.impl;

import org.apache.activemq.artemis.utils.uri.BeanSupport;
import org.apache.commons.beanutils.Converter;

public enum MessageLoadBalancingType {
OFF("OFF"), STRICT("STRICT"), ON_DEMAND("ON_DEMAND"), OFF_WITH_REDISTRIBUTION("OFF_WITH_REDISTRIBUTION");

static {
// for URI support on ClusterConnection
BeanSupport.registerConverter(new MessageLoadBalancingTypeConverter(), MessageLoadBalancingType.class);
}

static class MessageLoadBalancingTypeConverter implements Converter {

@Override
public <T> T convert(Class<T> type, Object value) {
return type.cast(MessageLoadBalancingType.getType(value.toString()));
}
}

private String type;

MessageLoadBalancingType(final String type) {
this.type = type;
}

public String getType() {
return type;
}

public static MessageLoadBalancingType getType(String string) {
if (string.equals(OFF.getType())) {
return MessageLoadBalancingType.OFF;
} else if (string.equals(STRICT.getType())) {
return MessageLoadBalancingType.STRICT;
} else if (string.equals(ON_DEMAND.getType())) {
return MessageLoadBalancingType.ON_DEMAND;
} else if (string.equals(OFF_WITH_REDISTRIBUTION.getType())) {
return MessageLoadBalancingType.OFF_WITH_REDISTRIBUTION;
} else {
return null;
}
}
OFF, STRICT, ON_DEMAND, OFF_WITH_REDISTRIBUTION;
}

0 comments on commit a739b9f

Please sign in to comment.