diff --git a/permazen-cli-telnet/src/main/java/io/permazen/cli/telnet/TelnetConsole.java b/permazen-cli-telnet/src/main/java/io/permazen/cli/telnet/TelnetConsole.java index 87443e667a..84e43c4e59 100644 --- a/permazen-cli-telnet/src/main/java/io/permazen/cli/telnet/TelnetConsole.java +++ b/permazen-cli-telnet/src/main/java/io/permazen/cli/telnet/TelnetConsole.java @@ -28,16 +28,17 @@ *

* This class can be used to implement an embedded CLI console within an application, accessed via telnet. For example: *

- *      // Accept a new connection
- *      final Socket socket = ...
+ *      // Accept a new connection and setup telnet CLI console
+ *      try (final Socket socket = /* accept new TCP connection */;
+ *           final TelnetConsole console = TelnetConsole.create(database, socket)) {
  *
- *      // Set up telnet CLI console
- *      final TelnetConsole console = TelnetConsole.create(database, socket);
- *      console.getSession().loadFunctionsFromClasspath();
- *      console.getSession().loadCommandsFromClasspath();
+ *          // Install commands and functions
+ *          console.getSession().loadFunctionsFromClasspath();
+ *          console.getSession().loadCommandsFromClasspath();
  *
- *      // Run the console
- *      console.run();
+ *          // Run the console
+ *          console.run();
+ *      }
  * 
*/ public final class TelnetConsole extends Console { @@ -223,6 +224,16 @@ public void setEchoEnabled(boolean enabled) { public String getOutputEncoding() { return this.terminal.getOutputEncoding(); } + + @Override + public void enableInterruptCharacter() { + this.terminal.enableInterruptCharacter(); + } + + @Override + public void disableInterruptCharacter() { + this.terminal.disableInterruptCharacter(); + } } // TelnetTerminal diff --git a/permazen-cli/src/main/java/io/permazen/cli/Console.java b/permazen-cli/src/main/java/io/permazen/cli/Console.java index 551d63b507..272403dec1 100644 --- a/permazen-cli/src/main/java/io/permazen/cli/Console.java +++ b/permazen-cli/src/main/java/io/permazen/cli/Console.java @@ -15,6 +15,7 @@ import io.permazen.parse.ParseException; import io.permazen.util.ParseContext; +import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -41,7 +42,7 @@ /** * CLI console. */ -public class Console { +public class Console implements Closeable { protected final Logger log = LoggerFactory.getLogger(this.getClass()); protected final ConsoleReader console; @@ -345,7 +346,6 @@ public void run() throws IOException { if (this.history != null) this.history.flush(); this.console.flush(); - this.console.shutdown(); } } @@ -402,6 +402,13 @@ public static Terminal getTerminal() throws IOException { return TerminalFactory.get(); } +// Closeable + + @Override + public void close() { + this.console.close(); + } + // ConsoleCompleter private class ConsoleCompleter implements Completer { diff --git a/permazen-cliapp/src/main/java/io/permazen/cli/app/Main.java b/permazen-cliapp/src/main/java/io/permazen/cli/app/Main.java index 408545e1d7..736e9cfc20 100644 --- a/permazen-cliapp/src/main/java/io/permazen/cli/app/Main.java +++ b/permazen-cliapp/src/main/java/io/permazen/cli/app/Main.java @@ -144,69 +144,73 @@ public int run(String[] args) throws Exception { } // Set up console - final Console console; + final Console console0; switch (this.mode) { case KEY_VALUE: - console = new Console(db.getKVDatabase(), new FileInputStream(FileDescriptor.in), System.out); + console0 = new Console(db.getKVDatabase(), new FileInputStream(FileDescriptor.in), System.out); break; case CORE_API: - console = new Console(db, new FileInputStream(FileDescriptor.in), System.out); + console0 = new Console(db, new FileInputStream(FileDescriptor.in), System.out); break; case PERMAZEN: - console = new Console(jdb, new FileInputStream(FileDescriptor.in), System.out); + console0 = new Console(jdb, new FileInputStream(FileDescriptor.in), System.out); break; default: - console = null; + console0 = null; assert false; break; } - if (this.keyboardInput && !this.batchMode) - console.setHistoryFile(this.historyFile); - - // Set up CLI session - final CliSession session = console.getSession(); - session.setDatabaseDescription(this.getDatabaseDescription()); - session.setReadOnly(this.readOnly); - session.setVerbose(this.verbose); - session.setSchemaModel(schemaModel); - session.setSchemaVersion(this.schemaVersion); - session.setAllowNewSchema(this.allowNewSchema); - session.loadFunctionsFromClasspath(); - session.loadCommandsFromClasspath(); - - // Handle file input - for (String filename : this.execFiles) { - final File file = new File(filename); - try (final InputStream input = new FileInputStream(file)) { - if (!this.parseAndExecuteCommands(console, new InputStreamReader(input), file.getName())) + try (final Console console = console0) { + + // Configure history + if (this.keyboardInput && !this.batchMode) + console.setHistoryFile(this.historyFile); + + // Set up CLI session + final CliSession session = console.getSession(); + session.setDatabaseDescription(this.getDatabaseDescription()); + session.setReadOnly(this.readOnly); + session.setVerbose(this.verbose); + session.setSchemaModel(schemaModel); + session.setSchemaVersion(this.schemaVersion); + session.setAllowNewSchema(this.allowNewSchema); + session.loadFunctionsFromClasspath(); + session.loadCommandsFromClasspath(); + + // Handle file input + for (String filename : this.execFiles) { + final File file = new File(filename); + try (final InputStream input = new FileInputStream(file)) { + if (!this.parseAndExecuteCommands(console, new InputStreamReader(input), file.getName())) + return 1; + } catch (IOException e) { + session.getWriter().println("Error: error opening " + file.getName() + ": " + e); return 1; - } catch (IOException e) { - session.getWriter().println("Error: error opening " + file.getName() + ": " + e); - return 1; + } } - } - // Handle command-line commands - for (String command : this.execCommands) { - if (!this.parseAndExecuteCommands(console, new StringReader(command), null)) - return 1; - } + // Handle command-line commands + for (String command : this.execCommands) { + if (!this.parseAndExecuteCommands(console, new StringReader(command), null)) + return 1; + } - // Handle standard input - if (!this.keyboardInput) { - if (!this.parseAndExecuteCommands(console, new InputStreamReader(System.in), "(stdin)")) - return 1; - } + // Handle standard input + if (!this.keyboardInput) { + if (!this.parseAndExecuteCommands(console, new InputStreamReader(System.in), "(stdin)")) + return 1; + } - // Run console if not in batch mode - if (this.keyboardInput && !this.batchMode) - console.run(); + // Run console if not in batch mode + if (this.keyboardInput && !this.batchMode) + console.run(); - // Shut down KV database - this.shutdownKVDatabase(); + // Shut down KV database + this.shutdownKVDatabase(); - // Done - return 0; + // Done + return 0; + } } private boolean parseAndExecuteCommands(Console console, Reader input, String inputDescription) { diff --git a/pom.xml b/pom.xml index c0b3ccd21e..3e4d49b130 100644 --- a/pom.xml +++ b/pom.xml @@ -100,7 +100,7 @@ 3.0.1-b09 1.6.1 9.2.24.v20180105 - 2.14.2 + 2.14.6 0.9 1.2.17 3.5.3