Hi,

Here I am again, OH NO!!!  :-)

This is the patch that we discussed about allowing executing forked processes 
asynchronously.
Requested by Ken Wood. It provides a new "detach" attribute for the <exec> and 
<java> tasks.

Whether the forked command continues running after the JVM stops, I think 
depends on the
OS and on whether it performs IO on the standard streams or not.
The changes do not rely on any OS specifics (all Java) and you can still use 
the timeout
option to stop the forked process (as long as ANT's VM is still running).

Only tested on Win2K, were the process will keep on running after the VM ends, 
unless
it tries to do IO on the standard streams.

Hope this patch passes the commiters muster ;-)

Cheers,

Jose Alberto

Index: docs/manual/CoreTasks/exec.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/manual/CoreTasks/exec.html,v
retrieving revision 1.7
diff -u -r1.7 exec.html
--- docs/manual/CoreTasks/exec.html     2001/10/30 10:05:34     1.7
+++ docs/manual/CoreTasks/exec.html     2001/11/07 12:05:16
@@ -63,6 +63,14 @@
     <td align="center" valign="top">No</td>
   </tr>
   <tr>
+    <td valign="top">detach</td>
+    <td valign="top">Run the command asynchronously. Whether the
+      command will continue running after the JVM exits depends
+      on the OS and whether the command performs any IO on the
+      standard streams.</td>
+    <td align="center" valign="top">No, default is <i>false</i></td>
+  </tr>
+  <tr>
     <td valign="top">failonerror</td>
     <td valign="top">Stop the buildprocess if the command exits with a
       returncode other than 0. Defaults to false</td>
Index: docs/manual/CoreTasks/java.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/manual/CoreTasks/java.html,v
retrieving revision 1.4
diff -u -r1.4 java.html
--- docs/manual/CoreTasks/java.html     2001/10/30 10:05:34     1.4
+++ docs/manual/CoreTasks/java.html     2001/11/07 12:05:16
@@ -57,6 +57,14 @@
     <td align="center" valign="top">No</td>
   </tr>
   <tr>
+    <td valign="top">detach</td>
+    <td valign="top">Run the command asynchronously in another VM. 
+      Whether the command will continue running after the JVM exits
+      depends on the OS and whether the command performs any IO on the
+      standard streams (disabled by default).</td>
+    <td align="center" valign="top">No</td>
+  </tr>
+  <tr>
     <td valign="top">jvm</td>
     <td valign="top">the command used to invoke the Java Virtual Machine,
       default is 'java'.  The command is resolved by java.lang.Runtime.exec().
Index: src/main/org/apache/tools/ant/taskdefs/ExecTask.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/ExecTask.java,v
retrieving revision 1.17
diff -u -r1.17 ExecTask.java
--- src/main/org/apache/tools/ant/taskdefs/ExecTask.java        2001/10/28 
21:26:29     1.17
+++ src/main/org/apache/tools/ant/taskdefs/ExecTask.java        2001/11/07 
12:05:19
@@ -92,6 +92,7 @@
     private FileOutputStream fos = null;
     private ByteArrayOutputStream baos = null;
     private String outputprop;
+    private boolean detached = false;
 
     /** Controls whether the VM (1.3 and above) is used to execute the command 
*/
     private boolean vmLauncher = true;
@@ -164,6 +165,13 @@
     }
 
     /**
+     * Execute command asynchronously
+     */
+    public void setDetach(boolean detached) {
+       this.detached = detached;
+    }
+
+    /**
      * Add a nested env element - an environment variable.
      */
     public void addEnv(Environment.Variable var) {
@@ -238,7 +246,8 @@
         exe.setAntRun(project);
         exe.setWorkingDirectory(dir);
         exe.setVMLauncher(vmLauncher);
-        String[] environment = env.getVariables();
+        exe.setDetach(detached);
+       String[] environment = env.getVariables();
         if (environment != null) {
             for (int i=0; i<environment.length; i++) {
                 log("Setting environment variable: "+environment[i],
Index: src/main/org/apache/tools/ant/taskdefs/Execute.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Execute.java,v
retrieving revision 1.24
diff -u -r1.24 Execute.java
--- src/main/org/apache/tools/ant/taskdefs/Execute.java 2001/11/02 07:56:52     
1.24
+++ src/main/org/apache/tools/ant/taskdefs/Execute.java 2001/11/07 12:05:23
@@ -90,6 +90,7 @@
     private File workingDirectory = null;
     private Project project = null;
     private boolean newEnvironment = false;
+    private boolean detached = false;
 
     /** Controls whether the VM is used to launch commands, where possible */
     private boolean useVMLauncher = true;    
@@ -342,6 +343,15 @@
     }
 
     /**
+     * Execute command asynchronously.
+     *
+     * @param detached whether to execute the command asynchronously or not.
+     */
+    public void setDetach(boolean detached) {
+       this.detached = detached;
+    }
+
+    /**
      * Set the name of the antRun script using the project's value.
      *
      * @param project the current project.
@@ -385,13 +395,33 @@
             process.destroy();
             throw e;
         }
-        streamHandler.start();
-        if (watchdog != null) watchdog.start(process);
-        waitFor(process);
-        if (watchdog != null) watchdog.stop();
-        streamHandler.stop();
-        if (watchdog != null) watchdog.checkException();
-        return getExitValue();
+       
+       if (!detached) {
+           runProcess(process);
+           return getExitValue();
+       }
+       else {
+           Thread t = new Thread(){
+                   public void run() {
+                       try {
+                           runProcess(process);
+                       }
+                       catch(IOException io){}
+                   }
+               };
+           t.setDaemon(true);
+           t.start();
+           return 0;
+       }
+    }
+
+    private void runProcess(Process process) throws IOException {
+       streamHandler.start();
+       if (watchdog != null) watchdog.start(process);
+       waitFor(process);
+       if (watchdog != null) watchdog.stop();
+       streamHandler.stop();
+       if (watchdog != null) watchdog.checkException();
     }
 
     protected void waitFor(Process process) {
Index: src/main/org/apache/tools/ant/taskdefs/Java.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Java.java,v
retrieving revision 1.30
diff -u -r1.30 Java.java
--- src/main/org/apache/tools/ant/taskdefs/Java.java    2001/10/28 21:26:29     
1.30
+++ src/main/org/apache/tools/ant/taskdefs/Java.java    2001/11/07 12:05:26
@@ -82,6 +82,7 @@
 
     private CommandlineJava cmdl = new CommandlineJava();
     private boolean fork = false;
+    private boolean detached = false;
     private File dir = null;
     private File out;
     private PrintStream outStream = null;
@@ -206,6 +207,15 @@
     }
 
     /**
+     * Set the flag to execute VM asynchronously.
+     */
+    public void setDetach(boolean s) {
+       this.detached = s;
+       // Detach=true implies Fork=true
+       if (s) this.fork = true;
+    }
+
+    /**
      * Set the command line arguments for the JVM.
      */
     public void setJvmargs(String s) {
@@ -339,7 +349,7 @@
             }
             
             exe.setWorkingDirectory(dir);
-            
+            exe.setDetach(detached);
             exe.setCommandline(command);
             try {
                 return exe.execute();

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to