List of usage examples for java.lang Process exitValue
public abstract int exitValue();
From source file:org.clickframes.graph.GraphUtils.java
private static File createHtmlFile(GraphingEngine graphingEngine, File dotFile, File retVal, String imageFilename) throws IOException { // run this command 'dot test.dot -Timap' // in the output look for lines like: shape url coords Process p; String error = null;/*ww w . j av a 2 s. c o m*/ try { String[] cmd = new String[] { graphingEngine.getCommand(), dotFile.getAbsolutePath(), "-Timap" }; boolean reportErrors = false; p = Runtime.getRuntime().exec(cmd); List<String> lines = null; InputStream in = p.getInputStream(); boolean finished = false; // Set to true when p is finished while (!finished) { try { while (in.available() > 0) { // Print the output of our system call. // Character c = new Character((char) in.read()); // System.out.print(c); lines = IOUtils.readLines(in); } // Ask the process for its exitValue. If the process // is not finished, an IllegalThreadStateException // is thrown. If it is finished, we fall through and // the variable finished is set to true. p.exitValue(); finished = true; } catch (IllegalThreadStateException e) { Thread.currentThread(); // Sleep a little to save on CPU cycles Thread.sleep(500); } } Pattern pattern = Pattern.compile("^(\\S+) (\\S+) (.+)$"); List<ImageMapArea> areas = new ArrayList<ImageMapArea>(); for (String line : lines) { Matcher m = pattern.matcher(line); if (m.matches()) { String shape = m.group(1); String href = m.group(2); String coords = m.group(3).trim(); ImageMapArea area = new ImageMapArea(); area.setShape(shape); area.setHref(href.replaceAll("\\\\l", "").replaceAll("\\\\", "")); area.setCoords(coords); areas.add(area); } } Map<String, Object> params = new HashMap<String, Object>(); params.put("imageName", imageFilename); params.put("areaList", areas); return VelocityHelper.runMacro(params, "imageMap.vm", retVal); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:processing.app.CommandLineTest.java
@Test public void testCommandLineVersion() throws Exception { Runtime rt = Runtime.getRuntime(); Process pr = rt.exec(new String[] { arduinoPath.getAbsolutePath(), "--version", }); pr.waitFor();/*from ww w . j a va 2s. c om*/ Assertions.assertThat(pr.exitValue()).as("Process will finish with exit code 0 in --version").isEqualTo(0); Assertions.assertThat(new String(IOUtils.toByteArray(pr.getInputStream()))) .matches("Arduino: \\d+\\.\\d+\\.\\d+.*"); }
From source file:edu.northwestern.bioinformatics.studycalendar.utility.osgimosis.BidirectionalObjectStoreTest.java
public void testMemoryUtilizationIsSoft() throws Exception { if (canDoMemoryTest()) { Process p = performMemoryTest("soft"); assertEquals("Memory test process completed with error", 0, p.exitValue()); }// w ww . j a v a 2 s .c om }
From source file:org.apache.accumulo.test.functional.MonitorLoggingIT.java
@Test public void logToMonitor() throws Exception { // Start the monitor. log.debug("Starting Monitor"); Process monitor = cluster.exec(Monitor.class); // Get monitor location to ensure it is running. String monitorLocation = null; for (int i = 0; i < NUM_LOCATION_PASSES; i++) { Thread.sleep(LOCATION_DELAY_SECS * 1000); try {//from w ww . j a v a2 s. c o m monitorLocation = getMonitor(); break; } catch (KeeperException e) { log.debug("Monitor not up yet, trying again in " + LOCATION_DELAY_SECS + " secs"); } } assertNotNull("Monitor failed to start within " + (LOCATION_DELAY_SECS * NUM_LOCATION_PASSES) + " secs", monitorLocation); log.debug("Monitor running at " + monitorLocation); // The tserver has to observe that the log-forwarding address // changed in ZooKeeper. If we cause the error before the tserver // updates, we'll never see the error on the monitor. Thread.sleep(10000); // Attempt a scan with an invalid iterator to force a log message in the monitor. try { Connector c = getConnector(); Scanner s = c.createScanner("accumulo.root", new Authorizations()); IteratorSetting cfg = new IteratorSetting(100, "incorrect", "java.lang.String"); s.addScanIterator(cfg); s.iterator().next(); } catch (RuntimeException e) { // expected, the iterator was bad } String result = ""; while (true) { Thread.sleep(LOCATION_DELAY_SECS * 1000); // extra precaution to ensure monitor has opportunity to log // Verify messages were received at the monitor. URL url = new URL("http://" + monitorLocation + "/log"); log.debug("Fetching web page " + url); result = FunctionalTestUtils.readAll(url.openStream()); if (result.contains("<pre class='logevent'>")) { break; } log.debug("No messages found, waiting a little longer..."); } assertTrue("No log messages found", result.contains("<pre class='logevent'>")); // Shutdown cleanly. log.debug("Stopping mini accumulo cluster"); Process shutdown = cluster.exec(Admin.class, "stopAll"); shutdown.waitFor(); assertTrue(shutdown.exitValue() == 0); log.debug("success!"); monitor.destroy(); }
From source file:edu.northwestern.bioinformatics.studycalendar.utility.osgimosis.BidirectionalObjectStoreTest.java
public void testMemoryUtilizationTestFailsWithoutSoftRefs() throws Exception { if (canDoMemoryTest()) { Process p = performMemoryTest("strong"); assertEquals("Memory test process completed without error", 1, p.exitValue()); }//from w ww. j av a2s . c o m }
From source file:fitnesse.testsystems.CommandRunner.java
private boolean isDead(Process process) { try {//from w ww. j ava2 s . co m process.exitValue(); return true; } catch (IllegalThreadStateException e) { return false; } }
From source file:com.thoughtworks.go.util.ProcessWrapperTest.java
@Test public void shouldReturnTrueWhenAProcessIsRunning() { Process process = getMockedProcess(mock(OutputStream.class)); when(process.exitValue()).thenThrow(new IllegalThreadStateException()); ProcessWrapper processWrapper = new ProcessWrapper(process, "", "", inMemoryConsumer(), null, null); assertThat(processWrapper.isRunning(), is(true)); }
From source file:com.thoughtworks.go.util.ProcessWrapperTest.java
@Test public void shouldReturnFalseWhenAProcessHasExited() { Process process = getMockedProcess(mock(OutputStream.class)); when(process.exitValue()).thenReturn(1); ProcessWrapper processWrapper = new ProcessWrapper(process, "", "", inMemoryConsumer(), null, null); assertThat(processWrapper.isRunning(), is(false)); }
From source file:org.sipfoundry.sipxconfig.admin.commserver.InitialConfig.java
private void createArchive(String locationName) { try {/*from www . j a va2s . c o m*/ String[] cmdLine = new String[] { m_binDirectory + INITIAL_CONFIG, locationName }; ProcessBuilder pb = new ProcessBuilder(cmdLine); java.lang.Process proc = pb.directory(new File(m_tmpDirectory)).start(); LOG.debug("Executing: " + StringUtils.join(cmdLine, " ")); proc.waitFor(); if (proc.exitValue() != 0) { throw new UserException("Script finished with exit code: " + proc.exitValue()); } } catch (IOException e) { throw new UserException(e); } catch (InterruptedException e) { throw new UserException(e); } }
From source file:gov.nist.appvet.tool.sigverifier.Service.java
private static boolean execute(String command, StringBuffer output) { List<String> commandArgs = Arrays.asList(command.split("\\s+")); ProcessBuilder pb = new ProcessBuilder(commandArgs); Process process = null; IOThreadHandler outputHandler = null; IOThreadHandler errorHandler = null; int exitValue = -1; try {// w w w . ja v a 2 s .c o m if (command == null || command.isEmpty()) { log.error("Command is null or empty"); return false; } log.debug("Executing " + command); process = pb.start(); outputHandler = new IOThreadHandler(process.getInputStream()); outputHandler.start(); errorHandler = new IOThreadHandler(process.getErrorStream()); errorHandler.start(); if (process.waitFor(Properties.commandTimeout, TimeUnit.MILLISECONDS)) { // Process has waited and exited within the timeout exitValue = process.exitValue(); if (exitValue == 0) { log.debug("Command terminated normally: \n" + outputHandler.getOutput() + "\nErrors: " + errorHandler.getOutput()); StringBuffer resultOut = outputHandler.getOutput(); output.append(resultOut); return true; } else { log.error("Command terminated abnormally: \n" + outputHandler.getOutput() + "\nErrors: " + errorHandler.getOutput()); StringBuffer resultError = errorHandler.getOutput(); output.append(resultError); return false; } } else { // Process exceed timeout or was interrupted log.error("Command timed-out or was interrupted: \n" + outputHandler.getOutput() + "\nErrors: " + errorHandler.getOutput()); StringBuffer resultOutput = outputHandler.getOutput(); StringBuffer resultError = errorHandler.getOutput(); if (resultOutput != null) { output.append(resultOutput); return false; } else if (resultError != null) { output.append(resultError); } else { output.append(Properties.toolName + " timed-out"); } return false; } } catch (IOException e) { e.printStackTrace(); return false; } catch (InterruptedException e) { e.printStackTrace(); return false; } finally { if (outputHandler.isAlive()) { try { outputHandler.inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (errorHandler.isAlive()) { try { errorHandler.inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (process.isAlive()) { process.destroy(); } } }