Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARTEMIS-4020 Fixing Management DTO Parsing with custom ETC #4277

Merged
merged 1 commit into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,8 +26,6 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* <p>
Expand All @@ -37,8 +35,6 @@
*/
public class Artemis {

private static final Logger logger = Logger.getLogger(Artemis.class.getName());

public static void main(String[] args) throws Throwable {
String home = System.getProperty("artemis.home");

Expand All @@ -47,7 +43,22 @@ public static void main(String[] args) throws Throwable {
String instance = System.getProperty("artemis.instance");
File fileInstance = instance != null ? new File(instance) : null;

Object result = execute(fileHome, fileInstance, true, args);

String brokerEtc = System.getProperty("artemis.instance.etc");
File fileBrokerETC = null;
if (brokerEtc != null) {
brokerEtc = brokerEtc.replace("\\", "/");
fileBrokerETC = new File(brokerEtc);
} else {
if (instance != null) {
brokerEtc = instance + "/etc";
fileBrokerETC = new File(brokerEtc);
}
}



Object result = execute(fileHome, fileInstance, fileBrokerETC, true, args);
if (result instanceof Exception) {
// Set a nonzero status code for the exceptions caught and printed by org.apache.activemq.artemis.cli.Artemis.execute
System.exit(1);
Expand All @@ -57,14 +68,14 @@ public static void main(String[] args) throws Throwable {
/**
* This is a good method for booting an embedded command
*/
public static Object execute(File artemisHome, File artemisInstance, boolean useSystemOut, List<String> args) throws Throwable {
return execute(artemisHome, artemisInstance, useSystemOut, args.toArray(new String[args.size()]));
public static Object execute(File artemisHome, File artemisInstance, File fileBrokerETC, boolean useSystemOut, List<String> args) throws Throwable {
return execute(artemisHome, artemisInstance, fileBrokerETC, useSystemOut, args.toArray(new String[args.size()]));
}

/**
* This is a good method for booting an embedded command
*/
public static Object execute(File fileHome, File fileInstance, boolean useSystemOut, String... args) throws Throwable {
public static Object execute(File fileHome, File fileInstance, File fileBrokerETC, boolean useSystemOut, String... args) throws Throwable {
ArrayList<File> dirs = new ArrayList<>();
if (fileHome != null) {
dirs.add(new File(fileHome, "lib"));
Expand All @@ -76,11 +87,16 @@ public static Object execute(File fileHome, File fileInstance, boolean useSystem
ArrayList<URL> urls = new ArrayList<>();

// Without the etc on the config, things like JGroups configuration wouldn't be loaded
if (fileInstance != null) {
File etcFile = new File(fileInstance, "etc");
if (fileBrokerETC == null && fileInstance != null) {
// the fileBrokerETC could be null if execute is called directly from an external module
fileBrokerETC = new File(fileInstance, "etc");
}

if (fileBrokerETC != null) {
// Adding etc to the classLoader so modules can lookup for their configs
urls.add(etcFile.toURI().toURL());
urls.add(fileBrokerETC.toURI().toURL());
}

if (fileHome != null) {
File etcFile = new File(fileHome, "etc");
// Adding etc to the classLoader so modules can lookup for their configs
Expand Down Expand Up @@ -122,10 +138,10 @@ public int compare(File file, File file1) {
URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
Thread.currentThread().setContextClassLoader(loader);
Class<?> clazz = loader.loadClass("org.apache.activemq.artemis.cli.Artemis");
Method method = clazz.getMethod("execute", Boolean.TYPE, Boolean.TYPE, File.class, File.class, args.getClass());
Method method = clazz.getMethod("execute", Boolean.TYPE, Boolean.TYPE, File.class, File.class, File.class, args.getClass());

try {
return method.invoke(null, useSystemOut, useSystemOut, fileHome, fileInstance, args);
return method.invoke(null, useSystemOut, useSystemOut, fileHome, fileInstance, fileBrokerETC, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} finally {
Expand All @@ -138,7 +154,7 @@ private static void add(ArrayList<URL> urls, File file) {
try {
urls.add(file.toURI().toURL());
} catch (MalformedURLException e) {
logger.log(Level.WARNING, e.getMessage(), e);
e.printStackTrace();
}
}

Expand Down
Expand Up @@ -93,18 +93,26 @@ public static void main(String... args) throws Exception {
String instance = System.getProperty("artemis.instance");
File fileInstance = instance != null ? new File(instance) : null;

verifyManagementDTO(fileInstance);

execute(true, true, fileHome, fileInstance, args);
String brokerEtc = System.getProperty("artemis.instance.etc");
if (brokerEtc != null) {
brokerEtc = brokerEtc.replace("\\", "/");
} else {
brokerEtc = instance + "/etc";
}

File fileBrokerETC = new File(brokerEtc);

verifyManagementDTO(fileBrokerETC);

execute(true, true, fileHome, fileInstance, fileBrokerETC, args);
}


// Notice this has to happen before any Log4j is used.
// otherwise Log4j's JMX will start the JMX before this property was able to tbe set
public static void verifyManagementDTO(File fileInstance) {
if (fileInstance != null) {

File etc = new File(fileInstance, "etc");
public static void verifyManagementDTO(File etc) {
if (etc != null) {
File management = new File(etc, "management.xml");

try {
Expand All @@ -119,16 +127,21 @@ public static void verifyManagementDTO(File fileInstance) {
}

public static Object internalExecute(String... args) throws Exception {
return internalExecute(null, null, args);
return internalExecute(null, null, null, args);
}

public static Object execute(File artemisHome, File artemisInstance, List<String> args) throws Exception {
return execute(false, false, artemisHome, artemisInstance, args.toArray(new String[args.size()]));
public static Object execute(File artemisHome, File artemisInstance, File etcFolder, List<String> args) throws Exception {
return execute(false, false, artemisHome, artemisInstance, etcFolder, args.toArray(new String[args.size()]));
}

public static Object execute(boolean inputEnabled, boolean useSystemOut, File artemisHome, File artemisInstance, String... args) throws Exception {
public static Object execute(boolean inputEnabled, boolean useSystemOut, File artemisHome, File artemisInstance, File etcFolder, String... args) throws Exception {

// using a default etc in case that is not set in the variables
if (etcFolder == null && artemisInstance != null) {
etcFolder = new File(artemisInstance, "etc");
}

verifyManagementDTO(artemisInstance);
verifyManagementDTO(etcFolder);

if (inputEnabled) {
InputAbstract.enableInput();
Expand All @@ -145,7 +158,7 @@ public static Object execute(boolean inputEnabled, boolean useSystemOut, File ar
ActionContext.setSystem(context);

try {
return internalExecute(artemisHome, artemisInstance, args, context);
return internalExecute(artemisHome, artemisInstance, etcFolder, args, context);
} catch (ConfigurationException configException) {
context.err.println(configException.getMessage());
context.out.println();
Expand Down Expand Up @@ -177,13 +190,13 @@ public static Object execute(boolean inputEnabled, boolean useSystemOut, File ar
* This method is used to validate exception returns.
* Useful on test cases
*/
private static Object internalExecute(File artemisHome, File artemisInstance, String[] args) throws Exception {
return internalExecute(artemisHome, artemisInstance, args, ActionContext.system());
private static Object internalExecute(File artemisHome, File artemisInstance, File etcFolder, String[] args) throws Exception {
return internalExecute(artemisHome, artemisInstance, etcFolder, args, ActionContext.system());
}

public static Object internalExecute(File artemisHome, File artemisInstance, String[] args, ActionContext context) throws Exception {
public static Object internalExecute(File artemisHome, File artemisInstance, File etcFolder, String[] args, ActionContext context) throws Exception {
Action action = builder(artemisInstance).build().parse(args);
action.setHomeValues(artemisHome, artemisInstance);
action.setHomeValues(artemisHome, artemisInstance, etcFolder);

if (action.isVerbose()) {
context.out.print("Executing " + action.getClass().getName() + " ");
Expand Down
Expand Up @@ -22,7 +22,7 @@ public interface Action {

boolean isVerbose();

void setHomeValues(File brokerHome, File brokerInstance);
void setHomeValues(File brokerHome, File brokerInstance, File etcFolder);

Object execute(ActionContext context) throws Exception;

Expand Down
Expand Up @@ -63,13 +63,16 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etcFolder) {
if (brokerHome != null) {
this.brokerHome = brokerHome.getAbsolutePath();
}
if (brokerInstance != null) {
this.brokerInstance = brokerInstance.getAbsolutePath();
}
if (etcFolder != null) {
this.brokerEtc = etcFolder.getAbsolutePath();
}
}

@Override
Expand Down
Expand Up @@ -28,7 +28,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etcFolder) {

}

Expand Down
Expand Up @@ -35,7 +35,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etc) {
}

@Override
Expand Down
Expand Up @@ -35,7 +35,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etc) {
}

@Override
Expand Down
Expand Up @@ -35,7 +35,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etc) {
}

@Override
Expand Down
Expand Up @@ -35,7 +35,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etc) {

}

Expand Down
Expand Up @@ -34,7 +34,7 @@ public boolean isVerbose() {
}

@Override
public void setHomeValues(File brokerHome, File brokerInstance) {
public void setHomeValues(File brokerHome, File brokerInstance, File etcFolder) {
}

@Override
Expand Down
Expand Up @@ -34,7 +34,7 @@ public void testDefaultAcceptor() throws Exception {

System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile());
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);

connectionAbstract.execute(new TestActionContext());

Expand All @@ -54,7 +54,7 @@ public void testAMQPAcceptor() throws Exception {

System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile());
connectionAbstract.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);
connectionAbstract.setAcceptor("amqp");

connectionAbstract.execute(new TestActionContext());
Expand Down
Expand Up @@ -35,7 +35,7 @@ public void testDefaultSourceAcceptor() {

System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile());
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);

try {
transfer.execute(new TestActionContext());
Expand All @@ -58,7 +58,7 @@ public void testAMQPSourceAcceptor() {

System.setProperty("artemis.instance.etc", brokerInstanceEtc.getAbsolutePath());
try {
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile());
transfer.setHomeValues(null, brokerInstanceEtc.getParentFile(), null);
transfer.setSourceAcceptor("amqp");

try {
Expand Down
Expand Up @@ -1287,14 +1287,14 @@ public void testProducerRetry() throws Exception {
* it will read from the InputStream in the ActionContext. It can't read the password since it's using
* System.console.readPassword() for that.
*/
assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin"}, context));
assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin"}, context));

/*
* This is the same as above except it will prompt the user to re-enter both the URL and the username.
*/
in = new ByteArrayInputStream("tcp://localhost:61616\nadmin\n".getBytes());
context = new ActionContext(in, System.out, System.err);
assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin", "--url", "tcp://badhost:11111"}, context));
assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin", "--url", "tcp://badhost:11111"}, context));
} finally {
stopServer();
}
Expand All @@ -1318,14 +1318,14 @@ public void testQueueStatRetry() throws Exception {
* it will read from the InputStream in the ActionContext. It can't read the password since it's using
* System.console.readPassword() for that.
*/
assertTrue((int) Artemis.internalExecute(null, null, new String[] {"queue", "stat", "--password", "admin"}, context) > 0);
assertTrue((int) Artemis.internalExecute(null, null, null, new String[] {"queue", "stat", "--password", "admin"}, context) > 0);

/*
* This is the same as above except it will prompt the user to re-enter both the URL and the username.
*/
in = new ByteArrayInputStream("tcp://localhost:61616\nadmin\n".getBytes());
context = new ActionContext(in, System.out, System.err);
assertTrue((int) Artemis.internalExecute(null, null, new String[] {"queue", "stat", "--password", "admin", "--url", "tcp://badhost:11111"}, context) > 0);
assertTrue((int) Artemis.internalExecute(null, null, null, new String[] {"queue", "stat", "--password", "admin", "--url", "tcp://badhost:11111"}, context) > 0);
} finally {
stopServer();
}
Expand Down
Expand Up @@ -154,7 +154,7 @@ public void testNodeCheckTopology() throws Exception {
"--replicated", "--host", "127.0.0.1", "--default-port", "61626", "--silent", "--no-autotune", "--no-web", "--require-login", "--slave");

System.setProperty("artemis.instance", masterInstance.getAbsolutePath());
Object master = Artemis.execute(false, false, null, masterInstance, "run");
Object master = Artemis.execute(false, false, null, masterInstance, null, "run");
ActiveMQServerImpl masterServer = (ActiveMQServerImpl)((Pair)master).getB();

try {
Expand All @@ -181,7 +181,7 @@ public void testNodeCheckTopology() throws Exception {
}

LockAbstract.unlock();
Object slave = Artemis.execute(false, false, null, slaveInstance, "run");
Object slave = Artemis.execute(false, false, null, slaveInstance, null, "run");
ActiveMQServerImpl slaveServer = (ActiveMQServerImpl)((Pair)slave).getB();

Wait.assertTrue("Backup isn't announced", () -> slaveServer.getBackupManager() != null &&
Expand All @@ -197,7 +197,7 @@ public void testNodeCheckTopology() throws Exception {
nodeCheck.setPeers(2);
Assert.assertEquals(3, nodeCheck.execute(context));
} finally {
Artemis.internalExecute(null, slaveInstance, new String[] {"stop"}, ActionContext.system());
Artemis.internalExecute(null, slaveInstance, null, new String[] {"stop"}, ActionContext.system());
}
} finally {
stopServer();
Expand Down
Expand Up @@ -103,7 +103,7 @@ public void testCommand() throws Exception {
invalidArgs = new String[] {group, command, "--blahblah-" + command, "--rubbish-" + command + "=" + "more-rubbish", "--input=blahblah"};
}
try {
Artemis.internalExecute(null, needInstance ? this.artemisInstance : null, invalidArgs, context);
Artemis.internalExecute(null, needInstance ? this.artemisInstance : null, null, invalidArgs, context);
fail("cannot detect invalid options");
} catch (InvalidOptionsError e) {
assertTrue(e.getMessage().contains("Found unexpected parameters"));
Expand Down
Expand Up @@ -50,6 +50,10 @@ public class ArtemisCLIPlugin extends ArtemisAbstractPlugin {
@Parameter(defaultValue = "${basedir}/target/server0", required = true)
private File location;


@Parameter
private File etc;

@Parameter
private String[] args;

Expand Down Expand Up @@ -127,7 +131,7 @@ public void run() {
}
}
} else {
Artemis.execute(home, location, useSystemOutput, args);
Artemis.execute(home, location, etc, useSystemOutput, args);
}

Thread.sleep(600);
Expand Down