Coverage Report - net.sf.statsvn.util.ProcessUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessUtils
34%
11/32
25%
3/12
1.6
 
 1  
 package net.sf.statsvn.util;
 2  
 
 3  
 import java.io.BufferedInputStream;
 4  
 import java.io.File;
 5  
 import java.io.IOException;
 6  
 import java.io.InputStream;
 7  
 import java.io.InputStreamReader;
 8  
 
 9  
 import net.sf.statcvs.util.LookaheadReader;
 10  
 import net.sf.statsvn.output.SvnConfigurationOptions;
 11  
 
 12  
 /**
 13  
  * This class provides a way of launching new processes. It is not the best way
 14  
  * and it surely does not work well in multi-threaded environments. It is
 15  
  * sufficient for StatSVN's single thread.
 16  
  * 
 17  
  * We should be launching two threads with readers for both, but we are lazy.
 18  
  * http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html
 19  
  * 
 20  
  * @author jkealey <jkealey@shade.ca>
 21  
  * 
 22  
  */
 23  
 public final class ProcessUtils {
 24  
         private BufferedInputStream inputStream;
 25  
 
 26  
         private BufferedInputStream errorStream;
 27  
 
 28  
         /**
 29  
          * A utility class (only static methods) should be final and have
 30  
          * a private constructor.
 31  
          */
 32  59
         public ProcessUtils() {
 33  59
         }
 34  
 
 35  
         public static synchronized ProcessUtils call(final String sCommand) throws IOException {
 36  0
                 final ProcessUtils util = new ProcessUtils();
 37  0
                 final Process lastProcess = Runtime.getRuntime().exec(sCommand, null, getWorkingFolder());
 38  0
                 util.errorStream = new BufferedInputStream(lastProcess.getErrorStream());
 39  0
                 util.inputStream = new BufferedInputStream(lastProcess.getInputStream());
 40  
 
 41  0
                 return util;
 42  
         }
 43  
 
 44  
         public void close() throws IOException {
 45  59
                 if (errorStream != null) {
 46  0
                         errorStream.close();
 47  0
                         errorStream = null;
 48  
                 }
 49  59
                 if (inputStream != null) {
 50  59
                         inputStream.close();
 51  59
                         inputStream = null;
 52  
                 }
 53  59
         }
 54  
 
 55  
         private static File getWorkingFolder() {
 56  0
                 return SvnConfigurationOptions.getCheckedOutDirectoryAsFile();
 57  
         }
 58  
 
 59  
         protected boolean hasErrorOccured() throws IOException {
 60  59
                 return errorStream != null && errorStream.available() > 0;
 61  
         }
 62  
 
 63  
         protected String getErrorMessage() {
 64  0
                 if (errorStream == null) {
 65  0
                         return null;
 66  
                 } else {
 67  0
                         final LookaheadReader diffReader = new LookaheadReader(new InputStreamReader(errorStream));
 68  0
                         final StringBuffer builder = new StringBuffer();
 69  
                         try {
 70  0
                                 while (diffReader.hasNextLine()) {
 71  0
                                         builder.append(diffReader.nextLine());
 72  
                                 }
 73  0
                         } catch (final IOException e) {
 74  0
                                 SvnConfigurationOptions.getTaskLogger().error(e.toString());
 75  0
                         }
 76  
 
 77  0
                         return builder.toString();
 78  
                 }
 79  
         }
 80  
 
 81  
         /**
 82  
          * @return the errorStream
 83  
          */
 84  
         public BufferedInputStream getErrorStream() {
 85  0
                 return errorStream;
 86  
         }
 87  
 
 88  
         /**
 89  
          * @return the inputStream
 90  
          */
 91  
         public BufferedInputStream getInputStream() {
 92  59
                 return inputStream;
 93  
         }
 94  
 
 95  
         /**
 96  
          * @param errorStream the errorStream to set
 97  
          */
 98  
         public void setErrorStream(final InputStream errorStream) {
 99  0
                 this.errorStream = new BufferedInputStream(errorStream);
 100  0
         }
 101  
 
 102  
         /**
 103  
          * @param inputStream the inputStream to set
 104  
          */
 105  
         public void setInputStream(final InputStream inputStream) {
 106  59
                 this.inputStream = new BufferedInputStream(inputStream);
 107  59
         }
 108  
 }