private ExecuteStreamHandler createStreamHandler() throws IOException { out = new ByteArrayOutputStream(); err = new ByteArrayOutputStream(); PipedOutputStream outPiped = new PipedOutputStream(); InputStream inPiped = new PipedInputStream(outPiped); in = outPiped; TeeOutputStream teeOut = new TeeOutputStream(out, System.out); TeeOutputStream teeErr = new TeeOutputStream(err, System.err); return new PumpStreamHandler(teeOut, teeErr, inPiped); }
/** * run a command with the out, err and in. * * @param cmd * the command line * @param out * the console outputstream * @param err * the error outputstream * @param in * the inputstream * @param workdir * the working dir * @return the result * @throws IOException * throw IOException if error */ public static int run(String cmd, OutputStream out, OutputStream err, InputStream in, String workdir) throws IOException { try { CommandLine cmdLine = CommandLine.parse(cmd); ExecuteStreamHandler stream = new PumpStreamHandler(out, err, in); int[] exit = new int[512]; for (int i = 0; i < exit.length; i++) { exit[i] = i - 256; } executor.setExitValues(exit); executor.setStreamHandler(stream); if (!X.isEmpty(workdir)) { executor.setWorkingDirectory(new File(workdir)); } // ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); // executor.setWatchdog(watchdog); // watchdog.destroyProcess(); return executor.execute(cmdLine); } catch (IOException e) { log.error("cmd=" + cmd, e); throw e; } }
/** * using bash to execute the lines. * * @param lines * the command lines * @param out * the output stream * @param err * the error stream * @param in * the input stream * @param workdir * the workdir * @return the int * @throws IOException * throw IOException if error */ public static int bash(String lines, OutputStream out, OutputStream err, InputStream in, String workdir) throws IOException { File f = new File(UID.id(lines).toLowerCase() + ".bash"); try { if (!X.isEmpty(workdir)) { lines = "cd " + workdir + ";" + lines; } FileUtils.writeStringToFile(f, lines); // Execute the file we just creted. No flags are due if it is // executed with bash directly CommandLine commandLine = CommandLine.parse("bash " + f.getCanonicalPath()); ExecuteStreamHandler stream = new PumpStreamHandler(out, err, in); DefaultExecutor executor = new DefaultExecutor(); int[] exit = new int[3]; for (int i = 0; i < exit.length; i++) { exit[i] = i - 1; } executor.setExitValues(exit); executor.setStreamHandler(stream); if (!X.isEmpty(workdir)) { executor.setWorkingDirectory(new File(workdir)); } return executor.execute(commandLine); } catch (IOException e) { log.error("lines=" + lines, e); throw e; } finally { f.delete(); } }
public void execute(final Application application, ExecuteResultHandler executeResultHandler, ExecuteStreamHandler streamHandler) throws ExecuteException, IOException { final String commandLine = application.getCommandLine(); DefaultExecutor executor = new DefaultExecutor(); CommandLine command = new CommandLine("/bin/sh"); command.addArgument("-c", false); command.addArgument(commandLine, false); executor.setStreamHandler(streamHandler); executor.execute(command, System.getenv(), executeResultHandler); LOG.debug("Launched the execution of task: [{}], uuid: [{}]", commandLine, application.getId()); }
/** * The stream handler is transmitted in the constructor. * @throws UnsupportedOperationException */ @Override public void setStreamHandler(ExecuteStreamHandler streamHandler) { throw new UnsupportedOperationException(); }
/** * * @param printJobTimeout * the printJobTimeout (ms) before the watchdog terminates the * print process * @param printInBackground * printing done in the background or blocking * @param streamHandler * @return a print result handler (implementing a future) * @throws IOException * the test failed */ public static PrintResultHandler executeProgram(CommandLine commandLine, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException { return executeProgram(commandLine, null, printJobTimeout, printInBackground, successExitValue, streamHandler); }