Author: ngn Date: Sat Jan 28 16:15:04 2006 New Revision: 373263 URL: http://svn.apache.org/viewcvs?rev=373263&view=rev Log: Specialized ExecuteException to handling failed executions only (with exit values). In other cases, throws IOException instead.
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java?rev=373263&r1=373262&r2=373263&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java Sat Jan 28 16:15:04 2006 @@ -240,7 +240,7 @@ public void execute(final CommandLine cmdl, final Map env, final InputStream in, final OutputStream out, - final OutputStream error) throws ExecuteException { + final OutputStream error) throws IOException { File savedDir = dir; // possibly altered in prepareExec Map environment; @@ -268,20 +268,20 @@ * if there are missing required parameters */ protected void checkConfiguration(final CommandLine cmdl) - throws ExecuteException { + throws IOException { if (cmdl.getExecutable() == null) { - throw new ExecuteException("No executable specified"); + throw new IOException("No executable specified"); } if (dir != null && !dir.exists()) { - throw new ExecuteException("The directory you specified does not " + throw new IOException("The directory you specified does not " + "exist"); } if (dir != null && !dir.isDirectory()) { - throw new ExecuteException("The directory you specified is not a " + throw new IOException("The directory you specified is not a " + "directory"); } if (spawn && incompatibleWithSpawn) { - throw new ExecuteException("Spawn also does not allow timeout"); + throw new IOException("Spawn also does not allow timeout"); } // setupRedirector(); } @@ -345,13 +345,13 @@ // test for and handle a forced process death if (exe.killedProcess()) { - throw new ExecuteException("Timeout: killed the sub-process"); + throw new IOException("Timeout: killed the sub-process"); } // redirector.complete(); if (Execute.isFailure(returnCode)) { throw new ExecuteException(exe.getCommandline().getExecutable() - + " returned: " + returnCode); + + " failed with return code", returnCode); } } else { exe.spawn(); @@ -369,15 +369,13 @@ * failIfExecFails is set to true (the default) */ protected void runExec(final Execute exe, final CommandLine cmdl) - throws ExecuteException { + throws IOException { // show the command log.debug(cmdl.toString()); exe.setCommandline(cmdl); try { runExecute(exe); - } catch (IOException e) { - throw new ExecuteException("Execute failed: " + e.toString(), e); } finally { // close the output file if required logFlush(); Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java?rev=373263&r1=373262&r2=373263&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Execute.java Sat Jan 28 16:15:04 2006 @@ -264,7 +264,13 @@ closeStreams(process); if (watchdog != null) { - watchdog.checkException(); + try{ + watchdog.checkException(); + } catch(Exception e) { + // TODO: include cause + throw new IOException(e.getMessage()); + } + } return getExitValue(); } finally { @@ -285,7 +291,7 @@ */ public void spawn() throws IOException { if (workingDirectory != null && !workingDirectory.exists()) { - throw new ExecuteException(workingDirectory + " doesn't exist."); + throw new IOException(workingDirectory + " doesn't exist."); } final Process process = launch(getCommandline(), getEnvironment(), workingDirectory); @@ -414,7 +420,7 @@ * if the command does not return 0. */ public static void runCommand(final CommandLine cmdline) - throws ExecuteException { + throws IOException { try { LOG.debug(cmdline); Execute exe = new Execute(new LogStreamHandler(999, 999)); @@ -423,10 +429,10 @@ int retval = exe.execute(); if (isFailure(retval)) { throw new ExecuteException(cmdline.getExecutable() - + " failed with return code " + retval); + + " failed with return code", retval); } } catch (java.io.IOException exc) { - throw new ExecuteException("Could not launch " + throw new IOException("Could not launch " + cmdline.getExecutable() + ": " + exc); } } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java?rev=373263&r1=373262&r2=373263&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java Sat Jan 28 16:15:04 2006 @@ -26,32 +26,26 @@ */ private static final long serialVersionUID = 3256443620654331699L; - /** - * Construct a new exception with <code>null</code> as its detail message. - */ - public ExecuteException() { - super(); - } + /** + * The underlying cause of this exception. + */ + private Throwable cause = null; + /** + * The exit value returned by the failed process + */ + private int exitValue = 0; + /** * Construct a new exception with the specified detail message. * * @param message * The detail message + * @param exitValue The exit value */ - public ExecuteException(final String message) { - super(message); - } - - /** - * Construct a new exception with the specified cause and a derived detail - * message. - * - * @param cause - * The underlying cause - */ - public ExecuteException(final Throwable cause) { - this((cause == null) ? null : cause.toString(), cause); + public ExecuteException(final String message, int exitValue) { + super(message + "(Exit value: " + exitValue + ")"); + this.exitValue = exitValue; } /** @@ -59,23 +53,28 @@ * * @param message * The detail message + * @param exitValue The exit value * @param cause * The underlying cause */ - public ExecuteException(final String message, final Throwable cause) { - super(message + " (Caused by " + cause + ")"); + public ExecuteException(final String message, int exitValue, final Throwable cause) { + super(message + " (Exit value: " + exitValue + ". Caused by " + cause + ")"); this.cause = cause; // Two-argument version requires JDK 1.4 or later + this.exitValue = exitValue; } /** - * The underlying cause of this exception. - */ - private Throwable cause = null; - - /** * Return the underlying cause of this exception (if any). */ public Throwable getCause() { return (this.cause); + } + + /** + * Gets the exit value returned by the failed process + * @return The exit value + */ + public int getExitValue() { + return exitValue; } } Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java?rev=373263&r1=373262&r2=373263&view=diff ============================================================================== --- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java (original) +++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java Sat Jan 28 16:15:04 2006 @@ -148,10 +148,9 @@ * a wrapped exception over the one that was silently swallowed * and stored during the process run. */ - public void checkException() throws IOException { + public void checkException() throws Exception { if (caught != null) { - throw new ExecuteException("Exception in ExecuteWatchdog.run: " - + caught.getMessage(), caught); + throw caught; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]