protected Object doExecute()

in gshell-wisdom/gshell-wisdom-core/src/main/java/org/apache/geronimo/gshell/wisdom/shell/CommandLineExecutorImpl.java [109:153]


    protected Object doExecute(final ShellContext context, final String path, final Object[] args) throws Exception {
        assert context != null;

        log.debug("Executing");

        // Locate the command
        Variables vars = context.getVariables();
        Command command = commandResolver.resolveCommand(path, vars);
        
        // Hijack the system streams in the current thread's context
        IO io = context.getIo();
        SystemOutputHijacker.register(io.outputStream, io.errorStream);

        // Setup command timings
        StopWatch watch = new StopWatch(true);
        
        CommandResult result;
        try {
            result = command.execute(context, args);

            log.debug("Command completed with result: {}, after: {}", result, watch);
        }
        finally {
            // Restore hijacked streams
            SystemOutputHijacker.deregister();

            // Make sure that the commands output has been flushed
            try {
                io.flush();
            }
            catch (Exception ignore) {}
        }

        // Decode the command result
        if (result.hasNotified()) {
            throw result.getNotification();
        }
        else if (result.hasFailed()) {
            // noinspection ThrowableResultOfMethodCallIgnored
            throw new CommandException(result.getFailure());
        }
        else {
            return result.getValue();
        }
    }