]> source.dussan.org Git - aspectj.git/commitdiff
envp and snooping error stream in process controller
authorwisberg <wisberg>
Wed, 7 May 2003 10:21:04 +0000 (10:21 +0000)
committerwisberg <wisberg>
Wed, 7 May 2003 10:21:04 +0000 (10:21 +0000)
util/src/org/aspectj/util/FileUtil.java
util/src/org/aspectj/util/LangUtil.java

index 1cdfc5e0e1e1f0e0990760160875dac473a910a2..89ed1fe4ad1eb1d7437ff513deb6ef470fa1d2e5 100644 (file)
@@ -1224,8 +1224,9 @@ public class FileUtil {
      */
     public static class Pipe implements Runnable {
         private final InputStream in;
-        private final OutputStream out;
+        private final OutputStream out;        
         private final long sleep;
+        private ByteArrayOutputStream snoop;
         private long totalWritten;
         private Throwable thrown;
         private boolean halt;
@@ -1274,7 +1275,11 @@ public class FileUtil {
             this.closeOutput = closeOutput;
             this.sleep = Math.min(0l, Math.max(60l*1000l, sleep));
         }
-    
+
+        public void setSnoop(ByteArrayOutputStream snoop) {
+            this.snoop = snoop;
+        }
+
         /**
          * Run the pipe.
          * This halts on the first Throwable thrown or when a read returns
@@ -1289,9 +1294,14 @@ public class FileUtil {
                 final int MAX = 4096;
                 byte[] buf = new byte[MAX];
                 int count = in.read(buf, 0, MAX);
+                ByteArrayOutputStream mySnoop;
                 while ((halt && finishStream && (0 < count))
                     || (!halt && (-1 != count))) {
                     out.write(buf, 0, count);
+                    mySnoop = snoop;
+                    if (null != mySnoop) {
+                        mySnoop.write(buf, 0, count);
+                    }
                     totalWritten += count;
                     if (halt && !finishStream) { 
                         break; 
index 56ce9b5107b3ff2c7dc3363300824ebfd48ff6fe..5d8de7f7b8c4864ef325541ffb97d77cb3a79a53 100644 (file)
@@ -14,6 +14,7 @@
 package org.aspectj.util;
 
 
+import java.io.*;
 import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -1020,6 +1021,7 @@ public class LangUtil {
          * ...
          */
         private String[] command;
+        private String[] envp;
         private String label;
         
         private boolean init;
@@ -1032,6 +1034,7 @@ public class LangUtil {
         private FileUtil.Pipe errStream;
         private FileUtil.Pipe outStream;
         private FileUtil.Pipe inStream;
+        private ByteArrayOutputStream errSnoop;
 
         private int result;
         private Thrown thrown;     
@@ -1091,17 +1094,29 @@ public class LangUtil {
         }        
         
         public final void init(String[] command, String label) {
-            LangUtil.throwIaxIfNotAssignable(command, String.class, "command");
-            if (1 > command.length) {
+            this.command = (String[]) LangUtil.safeCopy(command, new String[0]);
+            if (1 > this.command.length) {
                 throw new IllegalArgumentException("empty command");
             }
-            this.command = new String[command.length];
-            System.arraycopy(command, 0, this.command, 0, command.length);
             this.label = LangUtil.isEmpty(label) ? command[0] : label;
             this.init = true;
             reinit();
         }
 
+        public final void setEnvp(String[] envp) {
+            this.envp = (String[]) LangUtil.safeCopy(envp, new String[0]);
+            if (1 > this.envp.length) {
+                throw new IllegalArgumentException("empty envp");
+            }
+        }
+
+        public final void setErrSnoop(ByteArrayOutputStream snoop) {        
+            errSnoop = snoop;
+            if (null != errStream) {
+                errStream.setSnoop(errSnoop);
+            }
+        }
+        
         /** 
          * Start running the process and pipes asynchronously.
          * @return Thread started or null if unable to start thread
@@ -1124,6 +1139,9 @@ public class LangUtil {
                 return null;
             }
             errStream = new FileUtil.Pipe(process.getErrorStream(), System.err);
+            if (null != errSnoop) {
+                errStream.setSnoop(errSnoop);
+            }
             outStream = new FileUtil.Pipe(process.getInputStream(), System.out);
             inStream = new FileUtil.Pipe(System.in, process.getOutputStream());
             // start 4 threads, process & pipes for in, err, out
@@ -1242,7 +1260,7 @@ public class LangUtil {
                 process.destroy();        
             }
             if (null != inStream) {
-                inStream.halt(true,true);
+                inStream.halt(false, true); // this will block if waiting
                 inStream = null;
             }
             if (null != outStream) {