@Test public void verify_execution_failure() throws IOException, InterruptedException, ExecutionException { String javaHome = System.getProperty("java.home"); final ByteArrayOutputStream pout = new ByteArrayOutputStream(); final ByteArrayOutputStream perr = new ByteArrayOutputStream(); final FutureBox<Integer> pexit = new FutureBox<Integer>(); String exec = new File(new File(new File(javaHome), "bin"), "java").getAbsolutePath(); exec += ".no-such-file"; ProcessHandler handler = new ProcessHandler() { @Override public void started(OutputStream stdIn, InputStream stdOut, InputStream stdErr) { try { stdIn.close(); StreamHelper.copy(stdOut, pout); StreamHelper.copy(stdErr, perr); } catch (IOException e) { } } @Override public void finished(int exitCode) { pexit.setData(exitCode); } }; console.startProcess(".", new String[]{exec, "-version"}, null, handler); assertEquals("Exit code", Integer.MIN_VALUE, pexit.get().intValue()); System.out.write(pout.toByteArray()); System.err.write(perr.toByteArray()); }
@Override public Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler) { ensureRunning(); ProcessObserver observer; try { Process process = startProcess(workingDir, command, env); observer = new ProcessObserver(process, handler); Thread thread = new Thread(observer); thread.setDaemon(false); thread.setName("EXEC[" + shortCmd(command) + "]." + process.hashCode()); thread.start(); return observer; } catch (IOException e) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bos); ps.println("Failed to start process with command " + Arrays.asList(command)); e.printStackTrace(ps); ps.flush(); OutputStream stdIn = new ClosedOutputStream(); InputStream stdOut = new ByteArrayInputStream(new byte[0]); InputStream stdErr = new ByteArrayInputStream(bos.toByteArray()); handler.started(stdIn, stdOut, stdErr); handler.finished(Integer.MIN_VALUE); return new DestroyableStub(); } }
LocalControlConsole implements HostControlConsole { @Override public Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler) { ensureRunning(); ProcessObserver observer; try { Process process = startProcess(workingDir, command, env); observer = new ProcessObserver(process, handler); Thread thread = new Thread(observer); thread.setDaemon(false); thread.setName("EXEC[" + shortCmd(command) + "]." + process.hashCode()); thread.start(); return observer; } catch (IOException e) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bos); ps.println("Failed to start process with command " + Arrays.asList(command)); e.printStackTrace(ps); ps.flush(); OutputStream stdIn = new ClosedOutputStream(); InputStream stdOut = new ByteArrayInputStream(new byte[0]); InputStream stdErr = new ByteArrayInputStream(bos.toByteArray()); handler.started(stdIn, stdOut, stdErr); handler.finished(Integer.MIN_VALUE); return new DestroyableStub(); } } }
LocalControlConsole implements HostControlConsole { @Override public Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler) { ensureRunning(); ProcessObserver observer; try { Process process = startProcess(workingDir, command, env); observer = new ProcessObserver(process, handler); Thread thread = new Thread(observer); thread.setDaemon(false); thread.setName("EXEC[" + shortCmd(command) + "]." + process.hashCode()); thread.start(); return observer; } catch (IOException e) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bos); ps.println("Failed to start process with command " + Arrays.asList(command)); e.printStackTrace(ps); ps.flush(); OutputStream stdIn = new ClosedOutputStream(); InputStream stdOut = new ByteArrayInputStream(new byte[0]); InputStream stdErr = new ByteArrayInputStream(bos.toByteArray()); handler.started(stdIn, stdOut, stdErr); handler.finished(Integer.MIN_VALUE); return new DestroyableStub(); } } LocalControlConsole();  LocalControlConsole(String tmpDir); }
LocalControlConsole implements HostControlConsole { @Override public Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler) { ensureRunning(); ProcessObserver observer; try { Process process = startProcess(workingDir, command, env); observer = new ProcessObserver(process, handler); Thread thread = new Thread(observer); thread.setDaemon(false); thread.setName("EXEC[" + shortCmd(command) + "]." + process.hashCode()); thread.start(); return observer; } catch (IOException e) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bos); ps.println("Failed to start process with command " + Arrays.asList(command)); e.printStackTrace(ps); ps.flush(); OutputStream stdIn = new ClosedOutputStream(); InputStream stdOut = new ByteArrayInputStream(new byte[0]); InputStream stdErr = new ByteArrayInputStream(bos.toByteArray()); handler.started(stdIn, stdOut, stdErr); handler.finished(Integer.MIN_VALUE); return new DestroyableStub(); } } LocalControlConsole();  LocalControlConsole(String tmpDir); @Override boolean isLocalFileSystem(); @Override String cacheFile(FileBlob blob); @Override List<String> cacheFiles(List<? extends FileBlob> blobs); @Override Destroyable openSocket(SocketHandler handler); @Override Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler); @Override void terminate(); File getCacheDir(); }
LocalControlConsole implements HostControlConsole { @Override public Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler) { ensureRunning(); ProcessObserver observer; try { Process process = startProcess(workingDir, command, env); observer = new ProcessObserver(process, handler); Thread thread = new Thread(observer); thread.setDaemon(false); thread.setName("EXEC[" + shortCmd(command) + "]." + process.hashCode()); thread.start(); return observer; } catch (IOException e) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bos); ps.println("Failed to start process with command " + Arrays.asList(command)); e.printStackTrace(ps); ps.flush(); OutputStream stdIn = new ClosedOutputStream(); InputStream stdOut = new ByteArrayInputStream(new byte[0]); InputStream stdErr = new ByteArrayInputStream(bos.toByteArray()); handler.started(stdIn, stdOut, stdErr); handler.finished(Integer.MIN_VALUE); return new DestroyableStub(); } } LocalControlConsole();  LocalControlConsole(String tmpDir); @Override boolean isLocalFileSystem(); @Override String cacheFile(FileBlob blob); @Override List<String> cacheFiles(List<? extends FileBlob> blobs); @Override Destroyable openSocket(SocketHandler handler); @Override Destroyable startProcess(String workingDir, String[] command, Map<String, String> env, ProcessHandler handler); @Override void terminate(); File getCacheDir();  }