(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 9 Feb 2007 23:03:40 +0000 (23:03 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Fri, 9 Feb 2007 23:03:40 +0000 (23:03 +0000)
support/src/main/java/org/wamblee/io/DirectoryMonitor.java
support/src/main/java/org/wamblee/io/SimpleProcess.java [new file with mode: 0644]

index 5fe2e57348516868ef73b4e5b154c8bfd48ae222..f84d0378e4385403f38b94555681d6f56dff2aba 100644 (file)
@@ -47,13 +47,16 @@ public class DirectoryMonitor {
     
     public DirectoryMonitor(File aDirectory, FileFilter aFilefilter, Listener aListener) {
         _directory = aDirectory;
+        if ( !_directory.isDirectory()) { 
+            throw new IllegalArgumentException("Directory '" + _directory + "' does not exist");
+        }
         _filter = aFilefilter; 
         _listener = aListener;
         _contents = new HashMap<File,Date>();
     }
     
     public void poll() {
-        LOG.info("Polling " + _directory);
+        LOG.debug("Polling " + _directory);
         Map<File,Date> newContents = new HashMap<File,Date>();
         File[] files = _directory.listFiles(_filter);
         for (File file: files) { 
diff --git a/support/src/main/java/org/wamblee/io/SimpleProcess.java b/support/src/main/java/org/wamblee/io/SimpleProcess.java
new file mode 100644 (file)
index 0000000..6b922b9
--- /dev/null
@@ -0,0 +1,153 @@
+package org.wamblee.io;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class SimpleProcess {
+
+       private static final Log LOG = LogFactory.getLog(SimpleProcess.class);
+
+       private File _directory;
+
+       private String[] _cmd;
+       
+       private String _stdout; 
+       private String _stderr; 
+
+       public SimpleProcess(File aDirectory, String[] aCmd) {
+               _directory = aDirectory;
+               _cmd = aCmd;
+       }
+
+    /**
+     * @return the stdout
+     */
+    public String getStdout() {
+        return _stdout;
+    }
+    
+    /**
+     * @return the stderr
+     */
+    public String getStderr() {
+        return _stderr;
+    }
+    
+       /**
+        * Runs the process and blocks until it is done.
+        * 
+        * @return Exit status of the process.
+        * @throws IOException
+        *             In case of problems.
+        */
+       public int run() throws IOException {
+               return runImpl();
+       }
+
+       private int runImpl() throws IOException {
+               try {
+                   String fullcmd = ""; 
+                   for (String part: _cmd) {
+                       fullcmd += " " + part; 
+                   }
+                       LOG.debug("Executing '" + fullcmd + "' in directory '" + _directory
+                                       + "'");
+                       java.lang.Process proc = Runtime.getRuntime().exec(_cmd, null, _directory);
+
+                       // Read standard output and error in separate threads to avoid
+                       // deadlock.
+
+            StringWriter stdout = new StringWriter();
+            StringWriter stderr = new StringWriter();
+                       Thread stdoutReader = readAndLogStream("STDOUT>  ", proc
+                                       .getInputStream(), stdout);
+                       Thread stderrReader = readAndLogStream("STDERR>  ", proc
+                                       .getErrorStream(), stderr);
+
+                       try {
+                               proc.waitFor();
+                       } catch (InterruptedException e) {
+                               IOException exception = new IOException(
+                                               "Process was terminated: " + this);
+                               exception.initCause(e);
+                               throw exception;
+                       }
+                       waitForReader(stdoutReader);
+                       waitForReader(stderrReader);
+            
+            _stdout = stdout.toString();
+            _stderr = stderr.toString();
+
+                       if (proc.exitValue() != 0) {
+                               LOG.warn("Exit value was non-zero: " + this);
+                       } else { 
+                               LOG.debug("Process finished");
+                       }
+                       return proc.exitValue();
+               } catch (IOException e) {
+                       IOException exception = new IOException("Error executing process: "
+                                       + this);
+                       exception.initCause(e);
+                       throw exception;
+               }
+       }
+
+       private void waitForReader(Thread aReaderThread) {
+               try {
+                       aReaderThread.join();
+               } catch (InterruptedException e) {
+                       LOG
+                                       .warn(this
+                                                       + ": error waiting for output stream reader of process to finish");
+               }
+       }
+
+       private Thread readAndLogStream(final String aPrefix,
+                       final InputStream aStream, final Writer aOutput) {
+               Thread inputReader = new Thread() {
+                       @Override
+                       public void run() {
+                               BufferedReader br = null;
+                               try {
+                                       br = new BufferedReader(new InputStreamReader(aStream));
+                                       String str;
+                                       while ((str = br.readLine()) != null) {
+                                               LOG.debug(aPrefix + str);
+                        aOutput.write(str);
+                                       }
+                               } catch (IOException e) {
+                                       LOG.warn(SimpleProcess.this + ": error reading input stream", e);
+                               } finally {
+                                       if (br != null) {
+                                               try {
+                                                       br.close();
+                                               } catch (IOException e) {
+                                                       LOG.warn("Error closing stream " + aPrefix);
+                                               }
+                                       }
+                               }
+                       }
+               };
+               inputReader.start();
+               return inputReader;
+       }
+
+       @Override
+       public String toString() {
+        String fullcmd = "";
+        for (String part: _cmd) { 
+            fullcmd += part + " ";
+        }
+               return "process(dir = '" + _directory + "', cmd = '" + fullcmd + "')";
+       }
+}