You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2007/11/30 00:20:44 UTC

svn commit: r599622 - in /commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec: DefaultExecutor.java Executor.java

Author: sgoeschl
Date: Thu Nov 29 15:20:34 2007
New Revision: 599622

URL: http://svn.apache.org/viewvc?rev=599622&view=rev
Log:
SANDBOX-203 Adding support for setting custom exit values - the standard implementation throws an exception otherwise

Modified:
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java?rev=599622&r1=599621&r2=599622&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java Thu Nov 29 15:20:34 2007
@@ -28,146 +28,82 @@
  */
 public class DefaultExecutor implements Executor {
 
-    private ExecuteStreamHandler streamHandler = new LogStreamHandler(1, 1);
+    /** taking care of output and error stream */
+    private ExecuteStreamHandler streamHandler;
 
+    /** the working directory of the process */
     private File workingDirectory;
 
+    /** monitoring of long running processes */
     private ExecuteWatchdog watchdog;
 
+    /** the exit values considerd to be successful */
+    private int[] exitValues;
+
     // TODO replace with generic launcher
-    private CommandLauncher launcher = CommandLauncherFactory
-            .createVMLauncher();
+    private CommandLauncher launcher;
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
+     * Default Constrctor
+     */
+    public DefaultExecutor() {
+        this.streamHandler = new PumpStreamHandler();
+        this.launcher = CommandLauncherFactory.createVMLauncher();
+        this.exitValues = new int[0];
+    }
+
+    /**
      * @see org.apache.commons.exec.Executor#getStreamHandler()
      */
     public ExecuteStreamHandler getStreamHandler() {
         return streamHandler;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.commons.exec.Executor#setStreamHandler(org.apache.commons.exec.ExecuteStreamHandler)
      */
     public void setStreamHandler(ExecuteStreamHandler streamHandler) {
         this.streamHandler = streamHandler;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.commons.exec.Executor#getWatchdog()
      */
     public ExecuteWatchdog getWatchdog() {
         return watchdog;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.commons.exec.Executor#setWatchdog(org.apache.commons.exec.ExecuteWatchdog)
      */
     public void setWatchdog(ExecuteWatchdog watchDog) {
         this.watchdog = watchDog;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.commons.exec.Executor#getWorkingDirectory()
      */
     public File getWorkingDirectory() {
         return workingDirectory;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
+    /**
      * @see org.apache.commons.exec.Executor#setWorkingDirectory(java.io.File)
      */
     public void setWorkingDirectory(File dir) {
         this.workingDirectory = dir;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.commons.exec.Executor#execute(java.lang.String[])
+    /**
+     * @see org.apache.commons.exec.Executor#execute(CommandLine)
      */
     public int execute(final CommandLine command) throws ExecuteException,
             IOException {
         return execute(command, (Map) null);
     }
 
-    private int executeInternal(final CommandLine command, final Map environment, 
-            final File dir, final ExecuteStreamHandler streams) throws IOException {
-
-        final Process process = launch(command, environment, dir);
-
-        try {
-            streams.setProcessInputStream(process.getOutputStream());
-            streams.setProcessOutputStream(process.getInputStream());
-            streams.setProcessErrorStream(process.getErrorStream());
-        } catch (IOException e) {
-            process.destroy();
-            throw e;
-        }
-        streams.start();
-
-        try {
-            // add the process to the list of those to destroy if the VM exits
-            //
-            // processDestroyer.add(process);
-
-            if (watchdog != null) {
-                watchdog.start(process);
-            }
-            int exitValue = Executor.INVALID_EXITVALUE;
-            try {
-                exitValue = process.waitFor();
-            } catch (InterruptedException e) {
-                process.destroy();
-            }
-
-            if (watchdog != null) {
-                watchdog.stop();
-            }
-            streams.stop();
-            closeStreams(process);
-
-            if (watchdog != null) {
-                try {
-                    watchdog.checkException();
-                } catch (Exception e) {
-                    // TODO: include cause
-                    throw new IOException(e.getMessage());
-                }
-
-            }
-
-            if(isFailure(exitValue)) {
-                throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
-            }
-            
-            return exitValue;
-        } finally {
-            // remove the process to the list of those to destroy if the VM
-            // exits
-            //
-            // processDestroyer.remove(process);
-        }
-
-        
-    }
-    
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.commons.exec.Executor#execute(java.lang.String[],
-     *      java.util.Map)
+    /**
+     * @see org.apache.commons.exec.Executor#execute(CommandLine, java.util.Map)
      */
     public int execute(final CommandLine command, Map environment)
             throws ExecuteException, IOException {
@@ -181,49 +117,16 @@
     }
 
     /**
-     * Creates a process that runs a command.
-     * 
-     * @param command
-     *            the command to run
-     * @param env
-     *            the environment for the command
-     * @param dir
-     *            the working directory for the command
-     * @return the process started
-     * @throws IOException
-     *             forwarded from the particular launcher used
-     */
-    private Process launch(final CommandLine command, final Map env,
-            final File dir) throws IOException {
-        CommandLauncher launcher = this.launcher;
-
-        if (launcher == null) {
-            throw new IllegalStateException("CommandLauncher can not be null");
-
-        }
-
-        if (dir != null && !dir.exists()) {
-            throw new IOException(dir + " doesn't exist.");
-        }
-        return launcher.exec(command, env, dir);
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.commons.exec.Executor#execute(java.lang.String[],
+     * @see org.apache.commons.exec.Executor#execute(CommandLine,
      *      org.apache.commons.exec.ExecuteResultHandler)
      */
     public void execute(final CommandLine command, ExecuteResultHandler handler)
             throws ExecuteException, IOException {
         execute(command, null, handler);
-
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.commons.exec.Executor#execute(java.lang.String[],
+    /**
+     * @see org.apache.commons.exec.Executor#execute(CommandLine,
      *      java.util.Map, org.apache.commons.exec.ExecuteResultHandler)
      */
     public void execute(final CommandLine command, final Map environment,
@@ -259,30 +162,24 @@
         }.start();
     }
 
-    /**
-     * Close the streams belonging to the given Process.
-     * 
-     * @param process
-     *            the <CODE>Process</CODE>.
+
+    /*
+     * Define the exit code of the process to considered
+     * successful.
      */
-    private void closeStreams(final Process process) {
-        try {
-            process.getInputStream().close();
-        } catch (IOException eyeOhEx) {
-            // ignore error
-        }
-        try {
-            process.getOutputStream().close();
-        } catch (IOException eyeOhEx) {
-            // ignore error
-        }
-        try {
-            process.getErrorStream().close();
-        } catch (IOException eyeOhEx) {
-            // ignore error
-        }
+    public void setExitValue( int value ) {
+        this.setExitValues(new int[] {value});
     }
-    
+
+
+    /*
+     * Define the exist code of the process to considered
+     * successful.
+     */
+    public void setExitValues( int[] values ) {
+        this.exitValues = values;
+    }
+
     /**
      * Checks whether <code>exitValue</code> signals a failure on the current
      * system (OS specific).
@@ -307,5 +204,134 @@
             // non zero exit value signals failure
             return exitValue != 0;
         }
+    }
+
+
+    /**
+     * Close the streams belonging to the given Process.
+     *
+     * @param process
+     *            the <CODE>Process</CODE>.
+     */
+    private void closeStreams(final Process process) {
+        try {
+            process.getInputStream().close();
+        } catch (IOException eyeOhEx) {
+            // ignore error
+        }
+        try {
+            process.getOutputStream().close();
+        } catch (IOException eyeOhEx) {
+            // ignore error
+        }
+        try {
+            process.getErrorStream().close();
+        } catch (IOException eyeOhEx) {
+            // ignore error
+        }
+    }
+
+    /**
+     * Creates a process that runs a command.
+     *
+     * @param command
+     *            the command to run
+     * @param env
+     *            the environment for the command
+     * @param dir
+     *            the working directory for the command
+     * @return the process started
+     * @throws IOException
+     *             forwarded from the particular launcher used
+     */
+    private Process launch(final CommandLine command, final Map env,
+            final File dir) throws IOException {
+        CommandLauncher launcher = this.launcher;
+
+        if (launcher == null) {
+            throw new IllegalStateException("CommandLauncher can not be null");
+        }
+
+        if (dir != null && !dir.exists()) {
+            throw new IOException(dir + " doesn't exist.");
+        }
+        return launcher.exec(command, env, dir);
+    }
+    
+    private int executeInternal(final CommandLine command, final Map environment,
+            final File dir, final ExecuteStreamHandler streams) throws IOException {
+
+        final Process process = launch(command, environment, dir);
+
+        try {
+            streams.setProcessInputStream(process.getOutputStream());
+            streams.setProcessOutputStream(process.getInputStream());
+            streams.setProcessErrorStream(process.getErrorStream());
+        } catch (IOException e) {
+            process.destroy();
+            throw e;
+        }
+        streams.start();
+
+        try {
+            // add the process to the list of those to destroy if the VM exits
+            //
+            // processDestroyer.add(process);
+
+            if (watchdog != null) {
+                watchdog.start(process);
+            }
+            int exitValue = Executor.INVALID_EXITVALUE;
+            try {
+                exitValue = process.waitFor();
+            } catch (InterruptedException e) {
+                process.destroy();
+            }
+
+            if (watchdog != null) {
+                watchdog.stop();
+            }
+            streams.stop();
+            closeStreams(process);
+
+            if (watchdog != null) {
+                try {
+                    watchdog.checkException();
+                } catch (Exception e) {
+                    // TODO: include cause
+                    throw new IOException(e.getMessage());
+                }
+
+            }
+
+            if(!this.isSuccess(exitValue)) {
+                throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
+            }
+
+            return exitValue;
+        } finally {
+            // remove the process to the list of those to destroy if the VM
+            // exits
+            //
+            // processDestroyer.remove(process);
+        }
+    }
+
+    private boolean isSuccess(final int exitValue) {
+
+        if(this.exitValues == null) {
+            return true;
+        }
+        else if(this.exitValues.length == 0) {
+            return !isFailure(exitValue);
+        }
+        else {
+            for(int i=0; i<this.exitValues.length; i++) {
+                if(this.exitValues[i] == exitValue) {
+                    return true;
+                }
+            }
+        }
+        return false;
     }
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java?rev=599622&r1=599621&r2=599622&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java Thu Nov 29 15:20:34 2007
@@ -25,9 +25,25 @@
 public interface Executor {
 
     /** Invalid exit code. * */
-    public static final int INVALID_EXITVALUE = Integer.MAX_VALUE;
+    int INVALID_EXITVALUE = Integer.MAX_VALUE;
+
+    /*
+     * Define the exit code of the process to considered
+     * successful.
+     */
+    void setExitValue(int value);
+
+    /*
+     * Define the exit code of the process to considered
+     * successful using one of the following values
+     * <ul>
+     *  <li>an array of exit values to be considered successful</li>
+     *  <li>an empty array for auto-detect of successful exit codes</li>
+     *  <li>null to indicate to skip checking of exit codes</li>
+     * </ul>
+     */
+    void setExitValues(int[] values);
 
-    
     /*
      * StreamHandlers are used for providing input, 
      * retriving the output. Also used for logging.  
@@ -41,7 +57,10 @@
      */
     ExecuteWatchdog getWatchdog();
     void setWatchdog(ExecuteWatchdog watchDog);
-    
+
+    /*
+     * Set the working directory of the created process.
+     */
     File getWorkingDirectory();
     void setWorkingDirectory(File dir);
 
@@ -57,5 +76,4 @@
      */
     void execute(CommandLine command, ExecuteResultHandler handler) throws ExecuteException, IOException;
     void execute(CommandLine command, Map environment, ExecuteResultHandler handler) throws ExecuteException, IOException;
-
 }