ARTEMIS-5168 Improve remoting to brokers from Artemis shell

This commit is contained in:
AntonRoskvist 2024-11-22 09:00:20 +01:00 committed by clebertsuconic
parent 1ba7bec2b6
commit 78c816b772
5 changed files with 60 additions and 3 deletions

View File

@ -275,7 +275,7 @@ public class Artemis implements Runnable {
commandLine.addSubcommand(new QueueGroup(commandLine));
commandLine.addSubcommand(new AddressGroup(commandLine));
if (shellEnabled) {
if (Shell.inShell()) {
commandLine.addSubcommand(new Connect());
commandLine.addSubcommand(new Disconnect());
}

View File

@ -67,6 +67,8 @@ public class Shell implements Runnable {
}
private static ThreadLocal<AtomicBoolean> IN_SHELL = ThreadLocal.withInitial(() -> new AtomicBoolean(false));
private static ThreadLocal<AtomicBoolean> CONNECTED = ThreadLocal.withInitial(() -> new AtomicBoolean(false));
private static ThreadLocal<String> PROMPT = new ThreadLocal<>();
public static boolean inShell() {
return IN_SHELL.get().get();
@ -76,6 +78,14 @@ public class Shell implements Runnable {
IN_SHELL.get().set(true);
}
public static boolean isConnected() {
return CONNECTED.get().get();
}
public static void setConnected(boolean connected) {
CONNECTED.get().set(connected);
}
public static void runShell(boolean printBanner) {
try {
setInShell();
@ -104,7 +114,6 @@ public class Shell implements Runnable {
.build();
factory.setTerminal(terminal);
String prompt = org.apache.activemq.artemis.cli.Terminal.YELLOW_UNICODE + Artemis.getNameFromBanner() + " > " + org.apache.activemq.artemis.cli.Terminal.CLEAR_UNICODE;
String rightPrompt = null;
if (printBanner) {
@ -121,7 +130,7 @@ public class Shell implements Runnable {
// We build a new command every time, as they could have state from previous executions
systemRegistry.setCommandRegistries(new PicocliCommands(Artemis.buildCommand(isInstance, !isInstance, false)));
systemRegistry.cleanUp();
line = reader.readLine(prompt, rightPrompt, (MaskingCallback) null, null);
line = reader.readLine(getPrompt(), rightPrompt, (MaskingCallback) null, null);
systemRegistry.execute(line);
} catch (InterruptedException e) {
e.printStackTrace();
@ -129,6 +138,11 @@ public class Shell implements Runnable {
} catch (UserInterruptException userInterruptException) {
// ignore
} catch (EndOfFileException e) {
if (isConnected()) {
//if connected, [Ctrl + D] tries to disconnect instead of close
systemRegistry.execute("disconnect");
continue;
}
return;
} catch (Exception e) {
systemRegistry.trace(e);
@ -154,4 +168,27 @@ public class Shell implements Runnable {
System.out.print(org.apache.activemq.artemis.cli.Terminal.CLEAR_UNICODE);
}
private static String getPrompt() {
if (PROMPT.get() == null) {
setDefaultPrompt();
}
return PROMPT.get();
}
public static void setDefaultPrompt() {
try {
setPrompt(Artemis.getNameFromBanner());
} catch (Exception e) {
System.out.println("Error when getting prompt name from banner:");
e.printStackTrace();
setPrompt("Artemis Shell");
}
}
public static void setPrompt(String prompt) {
PROMPT.set(org.apache.activemq.artemis.cli.Terminal.YELLOW_UNICODE + prompt + " > " + org.apache.activemq.artemis.cli.Terminal.CLEAR_UNICODE);
}
}

View File

@ -17,6 +17,7 @@
package org.apache.activemq.artemis.cli.commands;
import org.apache.activemq.artemis.cli.Shell;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import picocli.CommandLine;
@ -30,6 +31,11 @@ public class Connect extends ConnectionAbstract {
CONNECTION_INFORMATION.remove();
createConnectionFactory();
context.out.println("Connection Successful!");
if (Shell.inShell()) {
Shell.setConnected(true);
}
} catch (Exception e) {
context.out.println("Connection Failure!");
e.printStackTrace();

View File

@ -17,6 +17,7 @@
package org.apache.activemq.artemis.cli.commands;
import org.apache.activemq.artemis.cli.Shell;
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import picocli.CommandLine;
@ -28,6 +29,12 @@ public class Disconnect extends ConnectionAbstract {
super.execute(context);
CONNECTION_INFORMATION.remove();
context.out.println("Connection information cleared!");
if (Shell.inShell()) {
Shell.setDefaultPrompt();
Shell.setConnected(false);
}
return null;
}
}

View File

@ -137,6 +137,13 @@ public class ConnectionConfigurationAbtract extends InputAbstract {
this.brokerURL = brokerURL;
this.user = user;
this.password = password;
if (user != null) {
Shell.setPrompt(user + "@" + brokerURL);
} else {
Shell.setPrompt(brokerURL);
}
}
}