Search in sources :

Example 1 with CommandThrowableException

use of dmg.util.CommandThrowableException 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 CommandThrowableException

use of dmg.util.CommandThrowableException 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 CommandThrowableException

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

the class UserAdminShell method sendObject.

private Serializable sendObject(CellPath cellPath, Serializable object) throws NoRouteToCellException, InterruptedException, CommandException, AclException {
    CellAddressCore addr = cellPath.getCurrent();
    checkCdPermission(addr.isLocalAddress() ? addr.getCellName() : addr.toString());
    try {
        return _cellStub.send(cellPath, object, Serializable.class, _timeout).get();
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (_fullException) {
            return getStackTrace(cause);
        }
        Throwables.throwIfInstanceOf(cause, Error.class);
        Throwables.throwIfInstanceOf(cause, NoRouteToCellException.class);
        Throwables.throwIfInstanceOf(cause, CommandException.class);
        throw new CommandThrowableException(cause.toString(), cause);
    }
}
Also used : CellAddressCore(dmg.cells.nucleus.CellAddressCore) CommandThrowableException(dmg.util.CommandThrowableException) Serializable(java.io.Serializable) NoRouteToCellException(dmg.cells.nucleus.NoRouteToCellException) CommandException(dmg.util.CommandException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with CommandThrowableException

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

the class BillingCell method appendHeaders.

protected void appendHeaders(Path path) throws CommandThrowableException {
    try {
        String headers = getFormatHeaders();
        Files.write(path, headers.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND, StandardOpenOption.WRITE);
    } catch (NoSuchFileException ignored) {
    } catch (IOException e) {
        throw new CommandThrowableException("Failed to write to billing file " + path + ": " + e, e);
    }
}
Also used : CommandThrowableException(dmg.util.CommandThrowableException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 5 with CommandThrowableException

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

the class CellShell method objectCommand.

public Object objectCommand(String strin) throws CommandExitException {
    String str;
    if ((str = prepareCommand(strin)) == null) {
        return "";
    }
    try {
        Args args = new Args(strin);
        if (args.argc() == 0) {
            return "";
        }
        Object o;
        if (_externalInterpreter != null) {
            o = _externalInterpreter.command(args);
        } else {
            o = command(args);
        }
        _errorCode = 0;
        _errorMsg = null;
        if (o == null) {
            return "";
        }
        return o;
    } catch (CommandException ce) {
        _errorCode = ce.getErrorCode();
        _errorMsg = ce.getErrorMessage();
        switch(_doOnError) {
            case SHUTDOWN:
                throw new CommandExitException(ce.toString(), 666);
            case EXIT:
                throw new CommandExitException(ce.getErrorMessage(), ce.getErrorCode());
        }
        if (ce instanceof CommandSyntaxException) {
            CommandSyntaxException cse = (CommandSyntaxException) ce;
            StringBuilder sb = new StringBuilder();
            sb.append("Syntax Error : ").append(cse.getMessage());
            if (_helpMode == 1) {
                sb.append("\nUse 'help' for more information\n");
            } else if (_helpMode == 2) {
                String help = cse.getHelpText();
                if (help != null) {
                    sb.append('\n').append(help).append('\n');
                }
            }
            return sb.toString();
        } else if (ce instanceof CommandExitException) {
            if (_externalInterpreter != null) {
                _externalInterpreter = null;
                return "external shell exited ... ";
            } else {
                throw (CommandExitException) ce;
            }
        } else if (ce instanceof CommandThrowableException) {
            CommandThrowableException cte = (CommandThrowableException) ce;
            StringBuilder sb = new StringBuilder();
            sb.append(cte.getMessage()).append(" -> ");
            Throwable t = cte.getTargetException();
            sb.append(t.getClass().getName()).append(" : ").append(t.getMessage()).append('\n');
            return sb.toString();
        } else if (ce instanceof CommandPanicException) {
            CommandPanicException cpe = (CommandPanicException) ce;
            StringBuilder sb = new StringBuilder();
            sb.append("Panic : ").append(cpe.getMessage()).append('\n');
            Throwable t = cpe.getTargetException();
            sb.append(t.getClass().getName()).append(" : ").append(t.getMessage()).append('\n');
            return sb.toString();
        } else {
            return "CommandException  :" + ce.getMessage();
        }
    }
}
Also used : CommandThrowableException(dmg.util.CommandThrowableException) Args(org.dcache.util.Args) CommandException(dmg.util.CommandException) CommandExitException(dmg.util.CommandExitException) CommandSyntaxException(dmg.util.CommandSyntaxException) CommandPanicException(dmg.util.CommandPanicException)

Aggregations

CommandThrowableException (dmg.util.CommandThrowableException)6 CommandException (dmg.util.CommandException)4 Args (org.dcache.util.Args)4 CommandExitException (dmg.util.CommandExitException)3 CommandPanicException (dmg.util.CommandPanicException)3 IOException (java.io.IOException)3 CommandSyntaxException (dmg.util.CommandSyntaxException)2 Serializable (java.io.Serializable)2 CellAddressCore (dmg.cells.nucleus.CellAddressCore)1 DelayedReply (dmg.cells.nucleus.DelayedReply)1 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Ansi (org.fusesource.jansi.Ansi)1 BeanInstantiationException (org.springframework.beans.BeanInstantiationException)1