*/
package mir.util;
+import mir.log.LoggerWrapper;
+
import java.io.File;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
/**
* Execute system commands. Warning: the current implementation is
* Executes a full command (including arguments) in a subshell.
* Standard input and output go to /dev/null
*/
- public static void simpleExec(String command)
- throws IOException {
+ public static void simpleExec(String command) throws IOException {
int exitStatus;
try {
// WARNING: unix specific
- exitStatus = Runtime.getRuntime().exec(new String[]{
- "/bin/sh", "-c",
- command + " " + ">/dev/null 2>/dev/null"
- }).waitFor();
+// exitStatus =
+ Process process = Runtime.getRuntime().exec(
+ new String[]{
+ "/bin/sh", "-c", command
+ });
+ Reader errorStreamReader = new InputStreamReader(process.getErrorStream());
+ ReaderLogger logger = new ReaderLogger(errorStreamReader, new LoggerWrapper("Utility"));
+ new Thread(logger).start();
+
+ Reader outputStreamReader = new InputStreamReader(process.getInputStream());
+ logger = new ReaderLogger(outputStreamReader, new LoggerWrapper("Utility"));
+ new Thread(logger).start();
+
+ exitStatus = process.waitFor();
}
catch (InterruptedException e) {
- throw new IOException(e.toString());
+ throw new IOException("Interrupted");
}
+
if (exitStatus != 0) {
throw new IOException("command exit satus:" + exitStatus);
}
}
+ private static class ReaderLogger implements Runnable {
+ private Reader reader;
+ private PrintWriter writer;
+
+ ReaderLogger(Reader aReader, LoggerWrapper aLogger) {
+ reader = aReader;
+ writer = aLogger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE);
+ }
+
+ public void run() {
+ try {
+ int size;
+ char[] buffer = new char[1024];
+ while ((size = reader.read(buffer)) >= 0) {
+ writer.write(buffer,0,size);
+ }
+ }
+ catch (IOException e) {
+ }
+ writer.close();
+ try {
+ reader.close();
+ }
+ catch (Exception e) {
+ }
+ }
+ }
+
}