ARTEMIS-4348 Parse connectors on broker.xml for default URI

This commit is contained in:
Clebert Suconic 2023-07-05 16:34:26 -04:00 committed by clebertsuconic
parent 44b1027b38
commit 36a20dbf76
7 changed files with 189 additions and 23 deletions

View File

@ -19,6 +19,7 @@ package org.apache.activemq.artemis.cli.commands;
import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.util.Collection;
import java.util.Map;
import com.github.rvesse.airline.annotations.Option;
@ -39,6 +40,9 @@ public abstract class ActionAbstract implements Action {
@Option(name = "--verbose", description = "Print additional information.")
public boolean verbose;
// this could be changed by a test accessor for testing purposes.
String brokerConfigurationFileName = "broker.xml";
private String brokerInstance;
private String brokerHome;
@ -90,17 +94,38 @@ public abstract class ActionAbstract implements Action {
return brokerInstance;
}
public String getBrokerURLInstance(String acceptor) {
public String getBrokerURLInstance(String name) {
String uri = null;
if (getBrokerInstance() != null) {
try {
Configuration brokerConfiguration = getBrokerConfiguration();
if (acceptor == null) {
acceptor = DEFAULT_BROKER_ACCEPTOR;
if (name == null) {
name = DEFAULT_BROKER_ACCEPTOR;
}
for (TransportConfiguration acceptorConfiguration: brokerConfiguration.getAcceptorConfigurations()) {
if (acceptorConfiguration.getName().equals(acceptor)) {
uri = findTransportURI(name, brokerConfiguration.getAcceptorConfigurations());
if (uri == null) {
uri = findTransportURI(name, brokerConfiguration.getConnectorConfigurations().values());
}
return uri;
} catch (Exception e) {
getActionContext().out.print("Can not get the broker url instance: " + e.getMessage());
e.printStackTrace(getActionContext().out);
}
}
if (uri == null) {
uri = DEFAULT_BROKER_URL;
}
return uri;
}
private String findTransportURI(String name, Collection<TransportConfiguration> configurationis) throws Exception {
for (TransportConfiguration acceptorConfiguration: configurationis) {
if (acceptorConfiguration.getName().equals(name)) {
Map<String, Object> acceptorParams = acceptorConfiguration.getParams();
String scheme = ConfigurationHelper.getStringProperty(TransportConstants.SCHEME_PROP_NAME, SchemaConstants.TCP, acceptorParams);
String host = ConfigurationHelper.getStringProperty(TransportConstants.HOST_PROP_NAME, "localhost", acceptorParams);
@ -113,20 +138,14 @@ public abstract class ActionAbstract implements Action {
return new URI(scheme, null, host, port, null, null, null).toString();
}
}
} catch (Exception e) {
if (isVerbose()) {
getActionContext().out.print("Can not get the broker url instance: " + e.toString());
}
}
}
return null;
}
protected Configuration getBrokerConfiguration() throws Exception {
FileConfiguration fileConfiguration = new FileConfiguration();
String brokerConfiguration = new File(new File(getBrokerEtc()), "broker.xml").toURI().toASCIIString();
String brokerConfiguration = new File(new File(getBrokerEtc()), this.brokerConfigurationFileName).toURI().toASCIIString();
FileDeploymentManager fileDeploymentManager = new FileDeploymentManager(brokerConfiguration);
fileDeploymentManager.addDeployable(fileConfiguration);
fileDeploymentManager.readConfiguration();

View File

@ -32,7 +32,7 @@ public class ConnectionAbstract extends InputAbstract {
@Option(name = "--url", description = "Connection URL. Default: build URL from the 'artemis' acceptor defined in the broker.xml or tcp://localhost:61616 if the acceptor cannot be parsed.")
protected String brokerURL = DEFAULT_BROKER_URL;
@Option(name = "--acceptor", description = "Acceptor used to build the connection URL.")
@Option(name = "--acceptor", description = "Name used to find the default connection URL on the acceptor list. If an acceptor with that name cannot be found the CLI will look for a connector with the same name.")
protected String acceptor;
@Option(name = "--user", description = "User used to connect.")

View File

@ -43,7 +43,7 @@ public class Transfer extends InputAbstract {
protected String sourceURL = DEFAULT_BROKER_URL;
@Option(name = "--source-acceptor", description = "Acceptor used to build URL towards the broker. Default: 'artemis'.")
protected String sourceAcceptor;
protected String sourceAcceptor = DEFAULT_BROKER_ACCEPTOR;
@Option(name = "--source-user", description = "User used to connect to source broker.")
protected String sourceUser;

View File

@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.cli.commands;
public class ActionAbstractAccessor {
public static void setBrokerConfig(ActionAbstract actionAbstract, String brokerFile) {
actionAbstract.brokerConfigurationFileName = brokerFile;
}
}

View File

@ -16,6 +16,9 @@
*/
package org.apache.activemq.artemis.cli.commands.messages;
import javax.jms.JMSException;
import org.apache.activemq.artemis.cli.commands.ActionAbstractAccessor;
import org.apache.activemq.cli.test.TestActionContext;
import org.junit.Assert;
import org.junit.Test;
@ -43,6 +46,30 @@ public class ConnectionAbstractTest {
}
}
@Test
public void testDefaultSourceAcceptorNoArtemis() {
ConnectionAbstract connectionAbstract = new ConnectionAbstract();
File brokerInstanceEtc = new File(this.getClass().getClassLoader().getResource("broker.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);
ActionAbstractAccessor.setBrokerConfig(connectionAbstract, "broker-with-connector.xml");
try {
connectionAbstract.execute(new TestActionContext());
} catch (Exception e) {
e.printStackTrace();
Assert.assertEquals(JMSException.class, e.getClass());
}
Assert.assertEquals("tcp://localhost:3344", connectionAbstract.getBrokerURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
@Test
public void testAMQPAcceptor() throws Exception {
ConnectionAbstract connectionAbstract = new ConnectionAbstract();

View File

@ -16,6 +16,7 @@
*/
package org.apache.activemq.artemis.cli.commands.messages;
import org.apache.activemq.artemis.cli.commands.ActionAbstractAccessor;
import org.apache.activemq.cli.test.TestActionContext;
import org.junit.Assert;
import org.junit.Test;
@ -48,6 +49,33 @@ public class TransferTest {
}
}
@Test
public void testDefaultSourceAcceptorNoArtemis() {
Transfer transfer = new Transfer();
File brokerInstanceEtc = new File(this.getClass().getClassLoader()
.getResource("broker-with-connector.xml").getFile()).getParentFile();
System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);
ActionAbstractAccessor.setBrokerConfig(transfer, "broker-with-connector.xml");
try {
transfer.execute(new TestActionContext());
} catch (Exception e) {
e.printStackTrace();
Assert.assertEquals(JMSException.class, e.getClass());
}
Assert.assertEquals("tcp://localhost:3344", transfer.getSourceURL());
} finally {
System.clearProperty("artemis.instance.etc");
}
}
@Test
public void testAMQPSourceAcceptor() {
Transfer transfer = new Transfer();

View File

@ -0,0 +1,66 @@
<?xml version='1.0'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<configuration xmlns="urn:activemq">
<jms xmlns="urn:activemq:jms">
<queue name="DLQ"/>
<queue name="ExpiryQueue"/>
</jms>
<core xmlns="urn:activemq:core">
<paging-directory>./target/paging</paging-directory>
<bindings-directory>./target/bindings</bindings-directory>
<journal-directory>./target/journal</journal-directory>
<journal-min-files>2</journal-min-files>
<large-messages-directory>./target/large-messages</large-messages-directory>
<connectors>
<connector name="artemis">tcp://localhost:3344</connector>
</connectors>
<acceptors>
<acceptor name="hello1">tcp://localhost:3333?protocols=AMQP</acceptor>
</acceptors>
<security-settings>
<security-setting match="#">
<permission type="createNonDurableQueue" roles="guest"/>
<permission type="deleteNonDurableQueue" roles="guest"/>
<permission type="consume" roles="guest"/>
<permission type="send" roles="guest"/>
</security-setting>
</security-settings>
<address-settings>
<!--default for catch all-->
<address-setting match="#">
<dead-letter-address>DLQ</dead-letter-address>
<expiry-address>ExpiryQueue</expiry-address>
<redelivery-delay>0</redelivery-delay>
<max-size-bytes>10Mb</max-size-bytes>
<message-counter-history-day-limit>10</message-counter-history-day-limit>
<address-full-policy>BLOCK</address-full-policy>
</address-setting>
</address-settings>
</core>
</configuration>