Search in sources :

Example 1 with CommandPanicException

use of dmg.util.CommandPanicException in project dcache by dCache.

the class ShellApplication method execute.

/**
 * Executes a single command with the output being printed to the console.
 */
public void execute(Args args) throws Throwable {
    if (args.argc() == 0) {
        return;
    }
    String out;
    try {
        if (isAnsiSupported && args.argc() > 0) {
            if (args.argv(0).equals("help")) {
                args.shift();
                args = new Args("help -format=" + HelpFormat.ANSI + " " + args.toString());
            }
        }
        try {
            out = Objects.toString(commandInterpreter.command(args), null);
        } catch (CommandThrowableException e) {
            throw e.getCause();
        }
    } catch (CommandSyntaxException e) {
        Ansi sb = Ansi.ansi();
        sb.fg(RED).a("Syntax error: " + e.getMessage() + "\n").reset();
        String help = e.getHelpText();
        if (help != null) {
            sb.a(help);
        }
        out = sb.toString();
    } catch (CommandExitException e) {
        throw e;
    } catch (CommandPanicException e) {
        Ansi sb = Ansi.ansi();
        sb.fg(RED).a("Bug detected! ").reset().a("Please email the following details to <support@dcache.org>:\n");
        Throwable t = e.getCause() == null ? e : e.getCause();
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        out = sb.a(sw.toString()).toString();
    } catch (Exception e) {
        out = Ansi.ansi().fg(RED).a(e.getMessage()).reset().toString();
    }
    if (!isNullOrEmpty(out)) {
        console.print(out);
        if (out.charAt(out.length() - 1) != '\n') {
            console.println();
        }
    }
    console.flush();
}
Also used : CommandThrowableException(dmg.util.CommandThrowableException) Args(org.dcache.util.Args) StringWriter(java.io.StringWriter) Ansi(org.fusesource.jansi.Ansi) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandExitException(dmg.util.CommandExitException) CommandThrowableException(dmg.util.CommandThrowableException) CommandExitException(dmg.util.CommandExitException) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandException(dmg.util.CommandException) IOException(java.io.IOException) CommandPanicException(dmg.util.CommandPanicException) CommandPanicException(dmg.util.CommandPanicException) PrintWriter(java.io.PrintWriter)

Example 2 with CommandPanicException

use of dmg.util.CommandPanicException in project dcache by dCache.

the class UniversalSpringCell method executeSetup.

private void executeSetup(CommandInterpreter interpreter, String source, byte[] data) throws CommandException {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data), UTF_8));
        int lineCount = 1;
        for (String line = in.readLine(); line != null; line = in.readLine(), lineCount++) {
            line = line.trim();
            if (line.isEmpty() || line.charAt(0) == '#') {
                continue;
            }
            try {
                Serializable result = interpreter.command(new Args(line));
                if (result instanceof DelayedReply) {
                    ((DelayedReply) result).take();
                }
            } catch (InterruptedException e) {
                throw new CommandExitException("Error at " + source + ":" + lineCount + ": command interrupted");
            } catch (CommandPanicException e) {
                throw new CommandPanicException("Error at " + source + ":" + lineCount + ": " + e.getMessage(), e);
            } catch (CommandException e) {
                throw new CommandThrowableException("Error at " + source + ":" + lineCount + ": " + e.getMessage(), e);
            }
        }
    } catch (IOException e) {
        // Should not be possible
        throw new RuntimeException(e);
    }
}
Also used : CommandThrowableException(dmg.util.CommandThrowableException) Serializable(java.io.Serializable) Args(org.dcache.util.Args) InputStreamReader(java.io.InputStreamReader) DelayedReply(dmg.cells.nucleus.DelayedReply) CommandException(dmg.util.CommandException) IOException(java.io.IOException) CommandExitException(dmg.util.CommandExitException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) CommandPanicException(dmg.util.CommandPanicException)

Example 3 with CommandPanicException

use of dmg.util.CommandPanicException in project dcache by dCache.

the class DelayedCommand method run.

@Override
public void run() {
    Serializable result;
    try {
        result = execute();
    } catch (Exception e) {
        try {
            Method method = ReflectionUtils.getAnyMethod(getClass(), "execute");
            if (!ReflectionUtils.hasDeclaredException(method, e)) {
                LOGGER.error("Command failed due to a bug, please contact support@dcache.org.", e);
                e = new CommandPanicException("Command failed: " + e, e);
            }
        } catch (NoSuchMethodException suppressed) {
            e.addSuppressed(suppressed);
        }
        result = e;
    }
    reply(result);
}
Also used : Serializable(java.io.Serializable) Method(java.lang.reflect.Method) CommandPanicException(dmg.util.CommandPanicException) CommandPanicException(dmg.util.CommandPanicException)

Example 4 with CommandPanicException

use of dmg.util.CommandPanicException in project dcache by dCache.

the class AnsiTerminalCommand method runAsciiMode.

private void runAsciiMode() throws IOException {
    Ansi.setEnabled(_useColors);
    while (true) {
        String prompt = Ansi.ansi().bold().a(_userAdminShell.getPrompt()).boldOff().toString();
        Object result;
        try {
            String str = _console.readLine(prompt);
            try {
                if (str == null) {
                    throw new CommandExitException();
                }
                result = _userAdminShell.executeCommand(str);
            } catch (IllegalArgumentException e) {
                result = e.toString();
            } catch (SerializationException e) {
                result = "There is a bug here, please report to support@dcache.org";
                LOGGER.error("This must be a bug, please report to support@dcache.org.", e);
            } catch (CommandSyntaxException e) {
                result = e;
            } catch (CommandExitException e) {
                break;
            } catch (CommandPanicException e) {
                result = "Command '" + str + "' triggered a bug (" + e.getTargetException() + "); the service log file contains additional information. Please " + "contact support@dcache.org.";
            } catch (CommandException e) {
                result = e.getMessage();
            } catch (NoRouteToCellException e) {
                result = "Cell name does not exist or cell is not started: " + e.getMessage();
                LOGGER.warn("The cell the command was sent to is no " + "longer there: {}", e.getMessage());
            } catch (RuntimeException e) {
                result = String.format("Command '%s' triggered a bug (%s); please" + " locate this message in the log file of the admin service and" + " send an email to support@dcache.org with this line and the" + " following stack-trace", str, e);
                LOGGER.error((String) result, e);
            }
        } catch (InterruptedIOException e) {
            _console.getCursorBuffer().clear();
            _console.println();
            result = null;
        } catch (InterruptedException e) {
            _console.println("^C");
            _console.flush();
            _console.getCursorBuffer().clear();
            result = null;
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            result = e.getMessage();
            if (result == null) {
                result = e.getClass().getSimpleName() + ": (null)";
            }
        }
        if (result != null) {
            if (result instanceof CommandSyntaxException) {
                CommandSyntaxException e = (CommandSyntaxException) result;
                Ansi sb = Ansi.ansi();
                sb.fg(RED).a("Syntax error: ").a(e.getMessage()).newline();
                String help = e.getHelpText();
                if (help != null) {
                    sb.fg(CYAN);
                    sb.a("Help : ").newline();
                    sb.a(help);
                }
                _console.println(sb.reset().toString());
            } else {
                String s;
                s = Strings.toMultilineString(result);
                if (!s.isEmpty()) {
                    _console.println(s);
                    _console.flush();
                }
            }
        }
        _console.flush();
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SerializationException(dmg.cells.nucleus.SerializationException) CommandException(dmg.util.CommandException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CommandExitException(dmg.util.CommandExitException) CommandExitException(dmg.util.CommandExitException) InterruptedIOException(java.io.InterruptedIOException) SerializationException(dmg.cells.nucleus.SerializationException) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandException(dmg.util.CommandException) IOException(java.io.IOException) CommandPanicException(dmg.util.CommandPanicException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) Ansi(org.fusesource.jansi.Ansi) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandPanicException(dmg.util.CommandPanicException)

Example 5 with CommandPanicException

use of dmg.util.CommandPanicException in project dcache by dCache.

the class DirectCommand method executeCommands.

private void executeCommands() {
    for (String command : commands) {
        Object error = null;
        try {
            Object result = shell.executeCommand(command);
            String s = Strings.toString(result);
            if (!s.isEmpty()) {
                outWriter.println(s);
            }
            outWriter.flush();
        } catch (IllegalArgumentException e) {
            error = e.toString();
        } catch (SerializationException e) {
            error = "There is a bug here, please report to support@dcache.org";
            LOGGER.error("This must be a bug, please report to support@dcache.org.", e);
        } catch (CommandSyntaxException e) {
            error = e;
        } catch (CommandEvaluationException | CommandAclException e) {
            error = e.getMessage();
        } catch (CommandExitException e) {
            break;
        } catch (CommandPanicException e) {
            error = String.format("Command '%s' triggered a bug (%s);" + "the service log file contains additional information. Please " + "contact support@dcache.org.", command, e.getTargetException());
        } catch (CommandException e) {
            error = e.getMessage();
        } catch (NoRouteToCellException e) {
            error = "Cell name does not exist or cell is not started: " + e.getMessage();
            LOGGER.warn("Command cannot be executed in the cell, cell is gone {}", e.getMessage());
        } catch (RuntimeException e) {
            error = String.format("Command '%s' triggered a bug (%s); please" + " locate this message in the log file of the admin service and" + " send an email to support@dcache.org with this line and the" + " following stack-trace", command, e);
            LOGGER.error((String) error, e);
        } catch (Exception e) {
            error = e.getMessage();
            if (error == null) {
                error = e.getClass().getSimpleName() + ": (null)";
            }
        }
        if (error != null) {
            if (error instanceof CommandSyntaxException) {
                CommandSyntaxException e = (CommandSyntaxException) error;
                errorWriter.append("Syntax error: ").println(e.getMessage());
            } else {
                errorWriter.println(error);
            }
            errorWriter.flush();
        }
    }
}
Also used : SerializationException(dmg.cells.nucleus.SerializationException) CommandEvaluationException(dmg.util.CommandEvaluationException) CommandException(dmg.util.CommandException) CommandExitException(dmg.util.CommandExitException) CommandException(dmg.util.CommandException) CommandAclException(dmg.util.CommandAclException) CommandPanicException(dmg.util.CommandPanicException) CommandExitException(dmg.util.CommandExitException) SerializationException(dmg.cells.nucleus.SerializationException) CommandSyntaxException(dmg.util.CommandSyntaxException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) CommandEvaluationException(dmg.util.CommandEvaluationException) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandAclException(dmg.util.CommandAclException) CommandPanicException(dmg.util.CommandPanicException)

Aggregations

CommandPanicException (dmg.util.CommandPanicException)10 CommandException (dmg.util.CommandException)9 CommandExitException (dmg.util.CommandExitException)8 CommandSyntaxException (dmg.util.CommandSyntaxException)6 IOException (java.io.IOException)6 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)4 SerializationException (dmg.cells.nucleus.SerializationException)4 CommandEvaluationException (dmg.util.CommandEvaluationException)4 CommandThrowableException (dmg.util.CommandThrowableException)3 Serializable (java.io.Serializable)3 Args (org.dcache.util.Args)3 Ansi (org.fusesource.jansi.Ansi)3 CommandAclException (dmg.util.CommandAclException)2 BufferedReader (java.io.BufferedReader)2 InterruptedIOException (java.io.InterruptedIOException)2 PingMessage (dmg.cells.network.PingMessage)1 DelayedReply (dmg.cells.nucleus.DelayedReply)1 AuthorizedString (dmg.util.AuthorizedString)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1