Skip to content

Commit

Permalink
Move CLI to use aesh-readline.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfdenise committed Nov 4, 2016
1 parent c53ad78 commit 8a3e184
Show file tree
Hide file tree
Showing 11 changed files with 901 additions and 460 deletions.
2 changes: 1 addition & 1 deletion cli/pom.xml
Expand Up @@ -105,7 +105,7 @@
<dependencies>
<dependency>
<groupId>org.jboss.aesh</groupId>
<artifactId>aesh</artifactId>
<artifactId>aesh-readline</artifactId>
</dependency>

<dependency>
Expand Down
4 changes: 4 additions & 0 deletions cli/src/main/java/org/jboss/as/cli/impl/CLIPrintStream.java
Expand Up @@ -51,6 +51,10 @@ final class CLIPrintStream extends PrintStream {
this.delegate = this.baseDelegate = new PrintStream(consoleOutput);
}

PrintStream getDelegate() {
return delegate;
}

void captureOutput(PrintStream delegate) {
if (this.delegate != this.baseDelegate) {
throw new IllegalStateException("Output is already being captured");
Expand Down
172 changes: 37 additions & 135 deletions cli/src/main/java/org/jboss/as/cli/impl/CommandContextImpl.java
Expand Up @@ -71,16 +71,8 @@
import javax.security.sasl.RealmCallback;
import javax.security.sasl.RealmChoiceCallback;
import javax.security.sasl.SaslException;
import org.jboss.aesh.util.FileAccessPermission;

import org.jboss.aesh.console.AeshConsoleCallback;
import org.jboss.aesh.console.ConsoleCallback;
import org.jboss.aesh.console.ConsoleOperation;
import org.jboss.aesh.console.Process;
import org.jboss.aesh.console.helper.InterruptHook;
import org.jboss.aesh.console.settings.FileAccessPermission;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
import org.jboss.aesh.edit.actions.Action;
import org.jboss.as.cli.Attachments;
import org.jboss.as.cli.CliConfig;
import org.jboss.as.cli.CliEvent;
Expand Down Expand Up @@ -160,6 +152,8 @@
import org.jboss.as.cli.handlers.trycatch.EndTryHandler;
import org.jboss.as.cli.handlers.trycatch.FinallyHandler;
import org.jboss.as.cli.handlers.trycatch.TryHandler;
import org.jboss.as.cli.impl.ReadlineConsole.Settings;
import org.jboss.as.cli.impl.ReadlineConsole.SettingsBuilder;
import org.jboss.as.cli.operation.CommandLineParser;
import org.jboss.as.cli.operation.NodePathFormatter;
import org.jboss.as.cli.operation.OperationCandidatesProvider;
Expand Down Expand Up @@ -362,8 +356,10 @@ class CommandContextImpl implements CommandContext, ModelControllerClientFactory
configTimeout = config.getCommandTimeout() == null ? DEFAULT_TIMEOUT : config.getCommandTimeout();
setCommandTimeout(configTimeout);
resolveParameterValues = config.isResolveParameterValues();
cliPrintStream = configuration.getConsoleOutput() == null ? new CLIPrintStream() : new CLIPrintStream(configuration.getConsoleOutput());
// Stdio must be installed prior to have cliPrintStream to capture System.in/out.
// This is required by console implementation.
initStdIO();
cliPrintStream = configuration.getConsoleOutput() == null ? new CLIPrintStream() : new CLIPrintStream(configuration.getConsoleOutput());
try {
initCommands();
} catch (CommandLineException e) {
Expand Down Expand Up @@ -394,9 +390,6 @@ protected void addShutdownHook() {
shutdownHook = new CliShutdownHook.Handler() {
@Override
public void shutdown() {
if (CommandContextImpl.this.console != null) {
CommandContextImpl.this.console.interrupt();
}
terminateSession();
}};
CliShutdownHook.add(shutdownHook);
Expand All @@ -411,76 +404,28 @@ protected void initBasicConsole(InputStream consoleInput, boolean start) throws
assert console == null : "the console has already been initialized";
Settings settings = createSettings(consoleInput);
this.console = Console.Factory.getConsole(this, settings);
console.setCallback(initCallback());
if(start) {
console.start();
}
}

private ConsoleCallback initCallback() {
return new CLIAeshConsoleCallback() {

@Override
public int execute(ConsoleOperation output) throws InterruptedException {
// Track the active process
setActiveProcess(true);

try {
// Actual work stuff
if (cmdCompleter == null) {
throw new IllegalStateException("The console hasn't been initialized at construction time.");
}
if (output.getBuffer() == null)
terminateSession();
else {
handleSafe(output.getBuffer().trim());
if (INTERACT && terminate == RUNNING) {
console.setPrompt(getPrompt());
}
}
return 0;

}finally{
setActiveProcess(false);
}
this.console.setActionCallback((line) -> {
handleSafe(line);
if (console != null) {
console.setPrompt(getPrompt());
}
});
if (start) {
try {
console.start();
} catch (IOException ex) {
throw new CliInitializationException(ex);
}

};
}

abstract class CLIAeshConsoleCallback extends AeshConsoleCallback{

private Process process;

public boolean hasActiveProcess() {
return activeProcess;
}

public void setActiveProcess(boolean activeProcess) {
this.activeProcess = activeProcess;
}

private boolean activeProcess = false;

@Override
public void setProcess(org.jboss.aesh.console.Process process){
this.process = process;
}

public int getProcessPID(){
return process.getPID();
}
}

private Settings createSettings(InputStream consoleInput) {
SettingsBuilder settings = new SettingsBuilder();
if(consoleInput != null) {
if (consoleInput != null) {
settings.inputStream(consoleInput);
}
settings.outputStream(cliPrintStream);

settings.enableExport(false);

settings.disableHistory(!config.isHistoryEnabled());
settings.historyFile(new File(config.getHistoryFileDir(), config.getHistoryFileName()));
settings.historySize(config.getHistoryMaxSize());
Expand All @@ -491,21 +436,10 @@ private Settings createSettings(InputStream consoleInput) {
permissions.setWritableOwnerOnly(true);
settings.historyFilePermission(permissions);

settings.parseOperators(false);
settings.parsingQuotes(false);

settings.interruptHook(
new InterruptHook() {
@Override
public void handleInterrupt(org.jboss.aesh.console.Console console, Action action) {
if(shutdownHook != null ){
shutdownHook.shutdown();
}else {
terminateSession();
}
Thread.currentThread().interrupt();
}
}
// This is called when Ctrl-C is called.
settings.interruptHook(() -> {
terminateSession();
}
);

return settings.create();
Expand Down Expand Up @@ -994,15 +928,17 @@ private String readLine(String prompt, boolean password) throws CommandLineExcep
}

if (console == null) {
initBasicConsole(null);
} else if(!console.running()) {
console.start();
initBasicConsole(null, false);
}

if (password) {
return console.readLine(prompt, (char) 0x00);
} else {
return console.readLine(prompt);
try {
if (password) {
return console.readLine(prompt, (char) 0x00);
} else {
return console.readLine(prompt);
}
} catch (IOException ex) {
throw new CommandLineException(ex);
}

}
Expand Down Expand Up @@ -1601,25 +1537,13 @@ public void interact() {

console.setPrompt(getPrompt());
if(!console.running()) {
console.start();
}
// if console is already running before we have started interacting, we need to
// make sure that the prompt is correctly displayed
else {
console.redrawPrompt();
}
if(console.isControlled()) {
console.continuous();
}

while(/*!isTerminated() && */console.running()){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
// This call is blocking.
console.start();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

INTERACT = false;
}

Expand Down Expand Up @@ -1774,29 +1698,15 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa

@Override
public void run() {
boolean success = false;
boolean callContinuous = false;
try {
if (username == null || password == null) {
if (console == null) {
initBasicConsole(null, false);
}
console.controlled();
if (!console.running()) {
console.start();
} else {
callContinuous = true;
}
}
dohandle(callbacks);
success = true;
} catch (IOException | UnsupportedCallbackException | CliInitializationException e) {
throw new RuntimeException(e);
} finally {
// in case of success the console will continue after connectController has finished all the initialization required
if (!success || callContinuous) {
console.continuous();
}
}
}
});
Expand Down Expand Up @@ -1834,13 +1744,9 @@ private void dohandle(Callback[] callbacks) throws IOException, UnsupportedCallb
if(ERROR_ON_INTERACT) {
interactionDisabled();
}
initBasicConsole(null);
initBasicConsole(null, false);
}
console.setCompletion(false);
console.getHistory().setUseHistory(false);
username = readLine("Username: ", false);
console.getHistory().setUseHistory(true);
console.setCompletion(true);
} catch (CommandLineException e) {
// the messages of the cause are lost if nested here
throw new IOException("Failed to read username: " + e.getLocalizedMessage());
Expand All @@ -1862,13 +1768,9 @@ private void dohandle(Callback[] callbacks) throws IOException, UnsupportedCallb
if(ERROR_ON_INTERACT) {
interactionDisabled();
}
initBasicConsole(null);
initBasicConsole(null, false);
}
console.setCompletion(false);
console.getHistory().setUseHistory(false);
temp = readLine("Password: ", true);
console.getHistory().setUseHistory(true);
console.setCompletion(true);
} catch (CommandLineException e) {
// the messages of the cause are lost if nested here
throw new IOException("Failed to read password: " + e.getLocalizedMessage());
Expand Down

0 comments on commit 8a3e184

Please sign in to comment.