Skip to content

Commit

Permalink
ARTEMIS-4244 Set web config using system properties
Browse files Browse the repository at this point in the history
  • Loading branch information
brusdev committed Apr 19, 2023
1 parent 05f5af6 commit b8905a4
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public void deActivate() {
broker.components.add(broker.web);
}

server.getServer().getConfiguration().parsePrefixedProperties(broker.web, "web", System.getProperties(), ActiveMQDefaultConfiguration.getDefaultWebPropertyPrefix());

for (ComponentDTO componentDTO : broker.components) {
Class clazz = this.getClass().getClassLoader().loadClass(componentDTO.componentClassName);
ExternalComponent component = (ExternalComponent) clazz.getDeclaredConstructor(null).newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ public static String getDefaultHapolicyBackupStrategy() {

public static final String DEFAULT_SYSTEM_PROPERTY_PREFIX = "brokerconfig.";

public static final String DEFAULT_WEB_PROPERTY_PREFIX = "webconfig.";

public static final String BROKER_PROPERTIES_SYSTEM_PROPERTY_NAME = "broker.properties";

public static final String BROKER_PROPERTIES_KEY_SURROUND = "\"";
Expand Down Expand Up @@ -1615,6 +1617,10 @@ public static String getDefaultSystemPropertyPrefix() {
return DEFAULT_SYSTEM_PROPERTY_PREFIX;
}

public static String getDefaultWebPropertyPrefix() {
return DEFAULT_WEB_PROPERTY_PREFIX;
}

public static String getDefaultBrokerPropertiesKeySurround() {
return BROKER_PROPERTIES_KEY_SURROUND;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,36 @@
@XmlAccessorType(XmlAccessType.FIELD)
public class AppDTO {

@XmlAttribute
public String name;

@XmlAttribute
public String url;

@XmlAttribute
public String war;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getWar() {
return war;
}

public void setWar(String war) {
this.war = war;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "binding")
@XmlAccessorType(XmlAccessType.FIELD)
public class BindingDTO {

@XmlAttribute
public String name;

@XmlAttribute
public String uri;

Expand Down Expand Up @@ -117,6 +121,66 @@ public void setExcludedCipherSuites(String... cipherSuites) {
excludedCipherSuites = marshalArray(cipherSuites);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}

public Boolean getClientAuth() {
return clientAuth;
}

public void setClientAuth(Boolean clientAuth) {
this.clientAuth = clientAuth;
}

public String getPasswordCodec() {
return passwordCodec;
}

public void setPasswordCodec(String passwordCodec) {
this.passwordCodec = passwordCodec;
}

public String getKeyStorePath() {
return keyStorePath;
}

public void setKeyStorePath(String keyStorePath) {
this.keyStorePath = keyStorePath;
}

public String getTrustStorePath() {
return trustStorePath;
}

public void setTrustStorePath(String trustStorePath) {
this.trustStorePath = trustStorePath;
}

public List<AppDTO> getApps() {
return apps;
}

public void addApp(AppDTO app) {
apps.add(app);
}

public BindingDTO() {
apps = new ArrayList<>();
}

private String[] unmarshalArray(String text) {
if (text == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -94,14 +95,57 @@ public class WebServerDTO extends ComponentDTO {
@XmlAttribute
public Boolean webContentEnabled;

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getCustomizer() {
return customizer;
}

public void setCustomizer(String customizer) {
this.customizer = customizer;
}

public RequestLogDTO getRequestLog() {
return requestLog;
}

public void setRequestLog(RequestLogDTO requestLog) {
this.requestLog = requestLog;
}

public String getRootRedirectLocation() {
return rootRedirectLocation;
}

public void setRootRedirectLocation(String rootRedirectLocation) {
this.rootRedirectLocation = rootRedirectLocation;
}

public Boolean getWebContentEnabled() {
return webContentEnabled;
}

public void setWebContentEnabled(Boolean webContentEnabled) {
this.webContentEnabled = webContentEnabled;
}

public void addBinding(BindingDTO binding) {
bindings.add(binding);
}


public WebServerDTO() {
componentClassName = "org.apache.activemq.artemis.component.WebServerComponent";
bindings = new ArrayList<>();
}

public List<BindingDTO> getBindings() {
if (bindings == null || bindings.isEmpty()) {
return Collections.singletonList(convertToBindingDTO());
}
return bindings;
}

Expand All @@ -127,6 +171,10 @@ private BindingDTO convertToBindingDTO() {
}

public BindingDTO getDefaultBinding() {
return getBindings().get(0);
if (bindings == null || bindings.isEmpty()) {
return convertToBindingDTO();
} else {
return getBindings().get(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testDefault() throws Exception {
WebServerDTO webServer = new WebServerDTO();

Assert.assertNotNull(webServer.getBindings());
Assert.assertEquals(1, webServer.getBindings().size());
Assert.assertEquals(0, webServer.getBindings().size());
Assert.assertNotNull(webServer.getDefaultBinding());

BindingDTO defaultBinding = webServer.getDefaultBinding();
Expand All @@ -55,7 +55,7 @@ public void testWebServerConfig() {
webServer.bind = "http://localhost:0";

Assert.assertNotNull(webServer.getBindings());
Assert.assertEquals(1, webServer.getBindings().size());
Assert.assertEquals(0, webServer.getBindings().size());
Assert.assertNotNull(webServer.getDefaultBinding());
Assert.assertEquals("http://localhost:0", webServer.getDefaultBinding().uri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -89,6 +90,8 @@ public interface Configuration {

Configuration parseProperties(String optionalUrlToPropertiesFile) throws Exception;

void parsePrefixedProperties(Object target, String name, Properties properties, String prefix) throws Exception;

boolean isCriticalAnalyzer();

Configuration setCriticalAnalyzer(boolean CriticalAnalyzer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public boolean accept(File file, String s) {
BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.clear();
brokerProperties.load(reader);
parsePrefixedProperties(fileName, brokerProperties, null);
parsePrefixedProperties(this, fileName, brokerProperties, null);
}
}
}
Expand All @@ -536,7 +536,7 @@ public boolean accept(File file, String s) {
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream reader = new BufferedInputStream(fileInputStream)) {
brokerProperties.load(reader);
parsePrefixedProperties(file.getName(), brokerProperties, null);
parsePrefixedProperties(this, file.getName(), brokerProperties, null);
}
}
}
Expand All @@ -546,10 +546,10 @@ public boolean accept(File file, String s) {
}

public void parsePrefixedProperties(Properties properties, String prefix) throws Exception {
parsePrefixedProperties("system", properties, prefix);
parsePrefixedProperties(this, "system", properties, prefix);
}

public void parsePrefixedProperties(String name, Properties properties, String prefix) throws Exception {
public void parsePrefixedProperties(Object target, String name, Properties properties, String prefix) throws Exception {
Map<String, Object> beanProperties = new LinkedHashMap<>();
long alder32Hash = 0;
synchronized (properties) {
Expand Down Expand Up @@ -582,11 +582,11 @@ public void parsePrefixedProperties(String name, Properties properties, String p
updateReadPropertiesStatus(name, alder32Hash);

if (!beanProperties.isEmpty()) {
populateWithProperties(name, beanProperties);
populateWithProperties(target, name, beanProperties);
}
}

public void populateWithProperties(final String propsId, Map<String, Object> beanProperties) throws InvocationTargetException, IllegalAccessException {
public void populateWithProperties(final Object target, final String propsId, Map<String, Object> beanProperties) throws InvocationTargetException, IllegalAccessException {
CollectionAutoFillPropertiesUtil autoFillCollections = new CollectionAutoFillPropertiesUtil();
BeanUtilsBean beanUtils = new BeanUtilsBean(new ConvertUtilsBean(), autoFillCollections) {
// override to treat missing properties as errors, not skip as the default impl does
Expand Down Expand Up @@ -770,7 +770,7 @@ public <T> T convert(Class<T> type, Object value) {
final String name = entry.getKey();
try {
// Perform the assignment for this property
beanUtils.setProperty(this, name, entry.getValue());
beanUtils.setProperty(target, name, entry.getValue());
} catch (InvocationTargetException invocationTargetException) {
logger.trace("failed to populate property with key: {}", name, invocationTargetException);
Throwable toLog = invocationTargetException;
Expand Down
Loading

0 comments on commit b8905a4

Please sign in to comment.