Search in sources :

Example 1 with ExternalTerminal

use of org.jline.terminal.impl.ExternalTerminal in project Payara by payara.

the class MultimodeCommand method executeCommand.

@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    LineReader reader = null;
    // restore echo flag, saved in validate
    programOpts.setEcho(echo);
    try {
        if (file == null) {
            System.out.println(strings.get("multimodeIntro"));
            Completer completer = getAllCommandsCompleter();
            Terminal asadminTerminal = TerminalBuilder.builder().name(ASADMIN).system(true).encoding(encoding != null ? Charset.forName(encoding) : Charset.defaultCharset()).build();
            reader = newLineReaderBuilder().terminal(asadminTerminal).completer(completer).build();
            reader.unsetOpt(LineReader.Option.INSERT_TAB);
        } else {
            printPrompt = false;
            if (!file.canRead()) {
                throw new CommandException("File: " + file + " can not be read");
            }
            OutputStream out = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    return;
                }

                @Override
                public void write(byte[] b) throws IOException {
                    return;
                }

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    return;
                }
            };
            Terminal asadminTerminal = new ExternalTerminal(ASADMIN, "", new FileInputStream(file), out, encoding != null ? Charset.forName(encoding) : Charset.defaultCharset());
            reader = newLineReaderBuilder().terminal(asadminTerminal).build();
        }
        return executeCommands(reader);
    } catch (IOException e) {
        throw new CommandException(e);
    } finally {
        try {
            if (file != null && reader != null && reader.getTerminal() != null) {
                reader.getTerminal().close();
            }
        } catch (Exception e) {
        // ignore it
        }
    }
}
Also used : LineReader(org.jline.reader.LineReader) OutputStream(java.io.OutputStream) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) Completer(org.jline.reader.Completer) StringsCompleter(org.jline.reader.impl.completer.StringsCompleter) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) Terminal(org.jline.terminal.Terminal) FileInputStream(java.io.FileInputStream) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) IOException(java.io.IOException) CommandValidationException(org.glassfish.api.admin.CommandValidationException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 2 with ExternalTerminal

use of org.jline.terminal.impl.ExternalTerminal in project Payara by payara.

the class LocalOSGiShellCommand method executeCommand.

@Override
protected int executeCommand() throws CommandException, CommandValidationException {
    LineReader reader = null;
    if (cmd == null) {
        throw new CommandException("Remote command 'osgi' is not available.");
    }
    // restore echo flag, saved in validate
    programOpts.setEcho(echo);
    try {
        if (encoding != null) {
            // see Configuration.getEncoding()...
            System.setProperty("input.encoding", encoding);
        }
        String[] args = new String[] { REMOTE_COMMAND, "asadmin-osgi-shell" };
        args = enhanceForTarget(args);
        shellType = cmd.executeAndReturnOutput(args).trim();
        if (file == null) {
            if (terminal != null) {
                // Pause the asadmin terminal
                terminal.pause();
            }
            System.out.println(strings.get("multimodeIntro"));
            Terminal osgiShellTerminal = TerminalBuilder.builder().name(REMOTE_COMMAND).system(true).streams(new FileInputStream(FileDescriptor.in), System.out).build();
            reader = LineReaderBuilder.builder().appName(REMOTE_COMMAND).terminal(osgiShellTerminal).completer(getCommandCompleter()).build();
            reader.unsetOpt(LineReader.Option.INSERT_TAB);
            return executeCommands(reader);
        }
        printPrompt = false;
        if (!file.canRead()) {
            throw new CommandException("File: " + file + " can not be read");
        }
        try (Terminal osgiShellTerminal = new ExternalTerminal(REMOTE_COMMAND, "", new FileInputStream(file), new NullOutputStream(), encoding != null ? Charset.forName(encoding) : Charset.defaultCharset())) {
            reader = LineReaderBuilder.builder().terminal(osgiShellTerminal).appName(REMOTE_COMMAND).build();
            // NB: wrapper on general in/out stream does not need closing by try-with-resource
            return executeCommands(reader);
        }
    } catch (IOException e) {
        throw new CommandException(e);
    } finally {
        if (reader != null && reader.getTerminal() != null) {
            try {
                reader.getTerminal().close();
            } catch (IOException ioe) {
                logger.log(Level.WARNING, "Error closing OSFI Shell terminal", ioe);
            }
        }
    }
}
Also used : LineReader(org.jline.reader.LineReader) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) InvalidCommandException(org.glassfish.api.admin.InvalidCommandException) CommandException(org.glassfish.api.admin.CommandException) ExternalTerminal(org.jline.terminal.impl.ExternalTerminal) Terminal(org.jline.terminal.Terminal)

Aggregations

CommandException (org.glassfish.api.admin.CommandException)2 InvalidCommandException (org.glassfish.api.admin.InvalidCommandException)2 LineReader (org.jline.reader.LineReader)2 Terminal (org.jline.terminal.Terminal)2 ExternalTerminal (org.jline.terminal.impl.ExternalTerminal)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 CommandValidationException (org.glassfish.api.admin.CommandValidationException)1 Completer (org.jline.reader.Completer)1 EndOfFileException (org.jline.reader.EndOfFileException)1 UserInterruptException (org.jline.reader.UserInterruptException)1 StringsCompleter (org.jline.reader.impl.completer.StringsCompleter)1