From b1504f8ebff85e1bdbb73e6535b17925c955f0bc Mon Sep 17 00:00:00 2001 From: Craig Miskell Date: Wed, 27 Apr 2011 20:30:43 +1200 Subject: [PATCH] Stab in the dark fix for NMS-4631 --- .../java/org/opennms/core/utils/ExecRunner.java | 121 +++++++++++--------- 1 files changed, 68 insertions(+), 53 deletions(-) diff --git a/core/lib/src/main/java/org/opennms/core/utils/ExecRunner.java b/core/lib/src/main/java/org/opennms/core/utils/ExecRunner.java index 1e8e387..d8b605d 100644 --- a/core/lib/src/main/java/org/opennms/core/utils/ExecRunner.java +++ b/core/lib/src/main/java/org/opennms/core/utils/ExecRunner.java @@ -242,7 +242,7 @@ public class ExecRunner { // ////////////////////////////////////////////////////////////// Runtime rt = Runtime.getRuntime(); - Process proc; + Process proc = null; String[] cmd = null; // First get the start time & calculate comparison numbers @@ -276,58 +276,73 @@ public class ExecRunner { } } - // Execute the command and start the two output gobblers - if (cmd != null && cmd.length > 0) { - proc = rt.exec(cmd); - } else { - throw new IOException("Insufficient commands!"); - } - - StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), stdoutWriter); - StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), stderrWriter); - outputGobbler.start(); - errorGobbler.start(); - - // Wait for the program to finish running and return the - // exit value obtained from the executable - while (true) { - - try { - exitVal = proc.exitValue(); - break; - } catch (IllegalThreadStateException e) { - - // If we get this exception, then the process isn't - // done executing and we determine if our time is up. - if (maxRunTimeSecs > 0) { - - Date endTime = new Date(); - long endTimeMs = endTime.getTime(); - if (endTimeMs > maxTimeMs) { - // Time's up - kill the process and the gobblers and - // return - proc.destroy(); - maxRunTimeExceeded = true; - stderrWriter.println(MAX_RUN_TIME_EXCEEDED_STRING); - outputGobbler.quit(); - errorGobbler.quit(); - return exitVal; - - } else { - // Time is not up yet so wait 100 ms before testing - // again - Thread.sleep(POLL_DELAY_MS); - } - - } - - } - - } - - // ////////////////////////////////////////////////////////////// - // Wait for output gobblers to finish forwarding the output - while (outputGobbler.isAlive() || errorGobbler.isAlive()) { + try { + // Execute the command and start the two output gobblers + if (cmd != null && cmd.length > 0) { + proc = rt.exec(cmd); + } else { + throw new IOException("Insufficient commands!"); + } + + StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), stdoutWriter); + StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), stderrWriter); + outputGobbler.start(); + errorGobbler.start(); + + // Wait for the program to finish running and return the + // exit value obtained from the executable + while (true) { + + try { + exitVal = proc.exitValue(); + break; + } catch (IllegalThreadStateException e) { + + // If we get this exception, then the process isn't + // done executing and we determine if our time is up. + if (maxRunTimeSecs > 0) { + + Date endTime = new Date(); + long endTimeMs = endTime.getTime(); + if (endTimeMs > maxTimeMs) { + // Time's up - kill the process and the gobblers and + // return + proc.destroy(); + maxRunTimeExceeded = true; + stderrWriter.println(MAX_RUN_TIME_EXCEEDED_STRING); + outputGobbler.quit(); + errorGobbler.quit(); + return exitVal; + + } else { + // Time is not up yet so wait 100 ms before testing + // again + Thread.sleep(POLL_DELAY_MS); + } + + } + + } + + } + + // ////////////////////////////////////////////////////////////// + // Wait for output gobblers to finish forwarding the output + while (outputGobbler.isAlive() || errorGobbler.isAlive()) { + } + + } finally { + if(proc != null) { + if(proc.getOutputStream() != null) { + proc.getOutputStream().close(); + } + if(proc.getErrorStream() != null) { + proc.getErrorStream().close(); + } + if(proc.getInputStream() != null) { + proc.getInputStream().close(); + } + } } // ////////////////////////////////////////////////////////////// -- 1.7.0.4