Method method;
    public void invoke(String cmdLineStr) {        commandShell.addCommandToHistory(cmdLineStr);        InputStream inputStream = System.in; // will also be dynamic later        InputStream nextInputStream = null;        PrintStream errStream = System.err; // will also be dynamic later        PrintStream outputStream = null;        CommandLine cmdLine;        Method method;        CommandRunner cr;        CommandInfo cmdInfo;        String[] commands = cmdLineStr.split("\\|");        String command;        ByteArrayOutputStream byteArrayOutputStream = null;        for (int i = 0; i < commands.length; i++) {            command = commands[i].trim();            cmdLine = new CommandLine(command);            if (!cmdLine.hasNext())                continue;            cmdName = cmdLine.next();            try {                cmdInfo = commandShell.getCommandClass(cmdName);                try {                    method = cmdInfo.getCommandClass().getMethod(                            EXECUTE_METHOD, EXECUTE_ARG_TYPES);                    if (cmdLine.sendToOutFile()) {                        File file = new File(cmdLine.getOutFileName());                        try {                            FileOutputStream fileOutputStream = new FileOutputStream(                                    file);                            outputStream = new PrintStream(fileOutputStream);                        } catch (SecurityException e) {                            e.printStackTrace();                        } catch (FileNotFoundException e) {                            e.printStackTrace();                        }                    } else if (i + 1 < commands.length) {                        byteArrayOutputStream = new ByteArrayOutputStream();                        outputStream = new PrintStream(byteArrayOutputStream);                    } else {                        outputStream = System.out;                    }                    if (byteArrayOutputStream != null) {                        nextInputStream = new ByteArrayInputStream(                                byteArrayOutputStream.toByteArray());                    }                    if (nextInputStream != null)                        inputStream = nextInputStream;                    CommandLine commandLine = null;                    if (inputStream.available() > 0) {                        commandLine = new CommandLine(inputStream);                    } else {                        commandLine = cmdLine.getRemainder();                    }                    commandLine.setOutFileName(cmdLine.getOutFileName());                    cr = new CommandRunner(cmdInfo.getCommandClass(), method,                            new Object[] { commandLine, inputStream,                                    outputStream, errStream });                } catch (NoSuchMethodException e) {                    method = cmdInfo.getCommandClass().getMethod(MAIN_METHOD,                            MAIN_ARG_TYPES);                    cr = new CommandRunner(cmdInfo.getCommandClass(), method,                            new Object[] { cmdLine.getRemainder()                                    .toStringArray() });                }                try {                    if (cmdInfo.isInternal()) {                        cr.run();                    } else {                        threadProcess = new Thread(cr, cmdName);                        threadProcess.start();                        this.blocking = true;                        this.blockingThread = Thread.currentThread();                        while (blocking) {                            try {                                Thread.sleep(6000);                            } catch (InterruptedException interrupted) {                                if (!blocking) {                                    // interruption was okay, break normally.                                } else {                                    // abnormal interruption                                    interrupted.printStackTrace();                                }                            }                        }                    }                    if (outputStream != null && outputStream != System.out) {                        outputStream.close();                    }                    // System.err.println("Finished invoke.");                } catch (Exception ex) {                    err.println("Exception in command");                    ex.printStackTrace(err);                } catch (Error ex) {                    err.println("Fatal error in command");                    ex.printStackTrace(err);                }            } catch (NoSuchMethodException ex) {                err.println("Alias class has no main method " + cmdName);            } catch (ClassNotFoundException ex) {                err.println("Unknown alias class " + ex.getMessage());            } catch (ClassCastException ex) {                err.println("Invalid command " + cmdName);            } catch (Exception ex) {                err.println("Unknown error: " + ex.getMessage());                ex.printStackTrace(err);            }        }        nextInputStream = null;    }