Author: jsong
Date: Thu Aug 19 14:16:51 2004
New Revision: 36636

Added:
   
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/utils/StreamCapture.java
Modified:
   incubator/beehive/trunk/controls/test/infra/mantis/mantis.jar
   
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/MantisTask.java
Log:
Contributed by Jamie Zyskowski: add timeout to MantisTask.java and default it 
to 5 seconds to counter any hangs in the apt call

Modified: incubator/beehive/trunk/controls/test/infra/mantis/mantis.jar
==============================================================================
Binary files. No diff available.

Modified: 
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/MantisTask.java
==============================================================================
--- 
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/MantisTask.java
    (original)
+++ 
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/MantisTask.java
    Thu Aug 19 14:16:51 2004
@@ -2,13 +2,15 @@
 
 import java.io.File;
 import java.io.FileWriter;
-import java.io.BufferedInputStream;
+import java.io.InputStream;
 import java.io.IOException;
 
 import org.apache.tools.ant.taskdefs.MatchingTask;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.DirectoryScanner;
 
+import org.apache.beehive.mantis.utils.StreamCapture;
+
 public class MantisTask extends MatchingTask
 {
   private final String P = System.getProperty("file.separator");
@@ -23,7 +25,7 @@
   // optional attributes
   private boolean _compile = false;
   private String _processor = null;
-  private int _timeout = 30000;
+  private int _timeout = 5000;
   // use _srcgen if not provided
   private String _logdir = null;
 
@@ -74,6 +76,11 @@
     _logdir = p_logdir;
   }
 
+  public void setTimeout(int p_timeout)
+  {
+    _timeout = p_timeout;
+  }
+
   public void execute() throws BuildException
   {
     // create the file of all files to process
@@ -122,14 +129,10 @@
 
     // capture mantis stderr/stdout to log file
     // while executing the command line apt call
-    BufferedInputStream stdout = null;
-    BufferedInputStream stderr = null;
     try
     {
       Process p = Runtime.getRuntime().exec(cmd.toString());
-      stdout = new BufferedInputStream(p.getInputStream());
-      stderr = new BufferedInputStream(p.getErrorStream());
-      logStreams(stdout,stderr);
+      logStreams(p);
       System.out.println("RETURN: "+p.exitValue());
     }
     catch(IOException ioe)
@@ -137,14 +140,16 @@
       ioe.printStackTrace();
       System.out.println("ERROR: IOException during apt execution");
     }
-
+    catch(InterruptedException ie)
+    {
+      ie.printStackTrace();
+      System.out.println("ERROR: InterruptedException during apt execution");
+    }
   }
 
-  private void logStreams(BufferedInputStream p_stdout, BufferedInputStream 
p_stderr) throws IOException
+  private void logStreams(Process p_proc) throws 
IOException,InterruptedException
   {
-    int c;
-
-    // capture stderr
+    // ensure log dir exists
     if(null == _logdir)
       _logdir = _srcgen;
     else
@@ -152,23 +157,16 @@
       File fLogdir = new File(_logdir);
       fLogdir.mkdirs();
     }
-    FileWriter stderrWriter = new FileWriter(new File(_logdir+P+"mantis.err"));
-    while( (c = p_stderr.read()) != -1)
-    {
-      stderrWriter.write(c);
-    }
-    stderrWriter.flush();
-    stderrWriter.close();
 
-    // capture stdout
-    FileWriter stdoutWriter = new FileWriter(new File(_logdir+P+"mantis.out"));
-    while( (c=p_stdout.read()) != -1)
-    {
-      stdoutWriter.write(c);
-    }
-    stdoutWriter.flush();
-    stdoutWriter.close();
+    // capture stderr and stdout to log files
+    StreamCapture stderr = new StreamCapture(p_proc.getErrorStream(),"file", 
_logdir+P+"mantis.err");
+    StreamCapture stdout = new StreamCapture(p_proc.getInputStream(),"file", 
_logdir+P+"mantis.out");
+    stderr.start();
+    stdout.start();
+
+    // wait up to 5 seconds for proc to finish
+    stderr.join(_timeout);
+    stdout.join(_timeout);
   }
-
 
 }

Added: 
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/utils/StreamCapture.java
==============================================================================
--- (empty file)
+++ 
incubator/beehive/trunk/controls/test/tools/mantis/src/org/apache/beehive/mantis/utils/StreamCapture.java
   Thu Aug 19 14:16:51 2004
@@ -0,0 +1,100 @@
+package org.apache.beehive.mantis.utils;
+
+import java.io.*;
+import java.lang.StringBuffer;
+
+/**
+ * A utility for reading from an InputStream
+ */
+public class StreamCapture extends Thread {
+    private InputStream is;
+    private String type;
+    private String filename;
+    private String bufferContents = "";   
+
+    /**
+     * 
+     * @param is InputStream to read from
+     * @param type An identifier for this stream capture (valid values: 
stderr, stdout)
+     */
+    public StreamCapture(InputStream is, String type)
+    {
+        this.is = is;
+        this.type = type;
+    }
+
+    /**
+     * 
+     * @param is InputStream to read from
+     * @param type An identifier for this stream capture (valid values: 
stderr, stdout, file)
+     * @param file If given, the data is saved to this file vs. an in-memory 
string.
+     */
+    public StreamCapture(InputStream is, String type, String file)
+    {
+        this.is = is;
+        this.type = type;
+        this.filename = file;
+    }
+
+
+    /**
+     * @return The buffer contents as a string
+     */
+    public String getString()
+    {
+        return bufferContents;
+    }
+
+
+    /**
+     * Read the InputStream supplied and put into 
+     * a string which is accessed via getString() or 
+     * a file is type==file and a filename is given.
+     */
+    public void run()
+    {
+        // if type == "file" save the contents to a file
+        if(type.equalsIgnoreCase("file"))
+        {
+          try
+          {
+            InputStreamReader isr = new InputStreamReader(is);
+            BufferedReader br = new BufferedReader(isr);
+            String line = null;
+            FileWriter fw = new FileWriter(filename);
+            BufferedWriter bw = new BufferedWriter(fw);
+            while( (line = br.readLine()) != null )
+            {
+              bw.write(line);
+              bw.newLine();
+              bw.flush();
+            }
+            bw.flush();
+            bw.close();            
+          }
+          catch (IOException ioe)
+          {
+              ioe.printStackTrace();
+          }          
+        }
+        else
+        {        
+          try
+          {
+              InputStreamReader isr = new InputStreamReader(is);
+              BufferedReader br = new BufferedReader(isr);
+              StringBuffer sb = new StringBuffer();         
+              String line = null;
+              while ((line = br.readLine()) != null)
+              {
+                  sb.append(line);
+              }
+              bufferContents = sb.toString();
+          }
+          catch (IOException ioe)
+          {
+              ioe.printStackTrace();
+          }
+        }  
+    }
+}

Reply via email to