From d1eab260304ac2487799a75a3c77e29967e6e1b7 Mon Sep 17 00:00:00 2001 From: wisberg Date: Wed, 7 May 2003 10:21:04 +0000 Subject: [PATCH] envp and snooping error stream in process controller --- util/src/org/aspectj/util/FileUtil.java | 14 +++++++++++-- util/src/org/aspectj/util/LangUtil.java | 28 ++++++++++++++++++++----- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/util/src/org/aspectj/util/FileUtil.java b/util/src/org/aspectj/util/FileUtil.java index 1cdfc5e0e..89ed1fe4a 100644 --- a/util/src/org/aspectj/util/FileUtil.java +++ b/util/src/org/aspectj/util/FileUtil.java @@ -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; diff --git a/util/src/org/aspectj/util/LangUtil.java b/util/src/org/aspectj/util/LangUtil.java index 56ce9b510..5d8de7f7b 100644 --- a/util/src/org/aspectj/util/LangUtil.java +++ b/util/src/org/aspectj/util/LangUtil.java @@ -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) { -- 2.39.5