Search in sources :

Example 1 with CommandExitException

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

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

the class ShellApplication method console.

/**
 * Start an interactive session.  The user is supplied a prompt and their input is executed as a
 * command.  This repeats until they indicate that they wish to exit the session.
 */
public void console() throws Throwable {
    onInteractiveStart();
    try {
        while (true) {
            String prompt = Ansi.ansi().bold().a(getPrompt()).boldOff().toString();
            String str = console.readLine(prompt);
            if (str == null) {
                console.println();
                break;
            }
            execute(new Args(str));
        }
    } catch (CommandExitException ignored) {
    }
}
Also used : Args(org.dcache.util.Args) CommandExitException(dmg.util.CommandExitException)

Example 3 with CommandExitException

use of dmg.util.CommandExitException 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 4 with CommandExitException

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

the class DCapDoorInterpreterV3 method com_hello.

// ////////////////////////////////////////////////////////////////
// 
// the command functions  String com_<commandName>(int,int,Args)
// 
public String com_hello(int sessionId, int commandId, VspArgs args) throws CommandException {
    if (args.argc() < 2) {
        throw new CommandExitException("Command Syntax Exception", 2);
    }
    try {
        int major = Integer.parseInt(args.argv(2));
        int minor = Integer.parseInt(args.argv(3));
        Integer bugfix = args.argc() > 4 ? Integer.parseInt(args.argv(4)) : null;
        String patch = args.argv(5);
        _version = new Version(major, minor, bugfix, patch);
    } catch (NumberFormatException e) {
        _log.error("Syntax error in client version number {} : {}", args, e.toString());
        throw new CommandException("Invalid client version number", e);
    }
    /*
          replace current values if alternatives are provided
        */
    _pid = args.getOption("pid", _pid);
    _uid = args.getIntOption("uid", _uid);
    _gid = args.getIntOption("gid", _gid);
    _log.debug("Client Version : {}", _version);
    if (_settings.getMinClientVersion().matches(_version) > 0 || _settings.getMaxClientVersion().matches(_version) < 0) {
        _log.error("Client {} (proc \"{}\" running with uid:{} gid:{}) rejected: bad client version: {}", InetAddresses.toAddrString(_clientAddress), _pid, _uid, _gid, _version);
        throw new CommandExitException("Client version rejected : " + _version, 1);
    }
    String yourName = args.getName();
    if (yourName.equals("server")) {
        _ourName = "client";
    }
    return "0 0 " + _ourName + " welcome " + _version.getMajor() + " " + _version.getMinor();
}
Also used : CommandException(dmg.util.CommandException) CommandExitException(dmg.util.CommandExitException) CellEndpoint(dmg.cells.nucleus.CellEndpoint)

Example 5 with CommandExitException

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

the class DcapLineBasedInterpreterAdapter method execute.

@Override
public void execute(String cmd) throws CommandExitException {
    LOGGER.info("Executing command: {}", cmd);
    commandCounter++;
    lastCommand = cmd;
    VspArgs args;
    try {
        args = new VspArgs(cmd);
    } catch (IllegalArgumentException e) {
        println("protocol violation: " + e.getMessage());
        LOGGER.debug("protocol violation [{}] from {}", e.getMessage(), clientAddress);
        throw new CommandExitException("Protocol violation");
    }
    try {
        String answer = execute(args);
        if (answer != null) {
            LOGGER.info("Our answer : {}", answer);
            println(answer);
        }
    } catch (CommandExitException e) {
        hasExited = true;
        LOGGER.info("ComThread : protocol ended");
        throw e;
    }
}
Also used : CommandExitException(dmg.util.CommandExitException)

Aggregations

CommandExitException (dmg.util.CommandExitException)14 CommandException (dmg.util.CommandException)11 CommandPanicException (dmg.util.CommandPanicException)8 IOException (java.io.IOException)8 CommandSyntaxException (dmg.util.CommandSyntaxException)6 Args (org.dcache.util.Args)6 NoRouteToCellException (dmg.cells.nucleus.NoRouteToCellException)4 SerializationException (dmg.cells.nucleus.SerializationException)4 CommandEvaluationException (dmg.util.CommandEvaluationException)4 CommandThrowableException (dmg.util.CommandThrowableException)3 BufferedReader (java.io.BufferedReader)3 InterruptedIOException (java.io.InterruptedIOException)3 Ansi (org.fusesource.jansi.Ansi)3 CellEndpoint (dmg.cells.nucleus.CellEndpoint)2 CommandAclException (dmg.util.CommandAclException)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 StringWriter (java.io.StringWriter)2 CacheException (diskCacheV111.util.CacheException)1