Coverage Report - net.sf.statscm.SrcManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsoleErrorStream
77%
10/13
50%
1/2
3.6
ConsoleFileStream
67%
14/21
N/A
3.6
SrcManager
62%
16/26
62%
5/8
3.6
 
 1  
 package net.sf.statscm;
 2  
 
 3  
 /*
 4  
  * Copyright 2006 Doug Culnane
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.BufferedReader;
 20  
 import java.io.DataInputStream;
 21  
 import java.io.EOFException;
 22  
 import java.io.File;
 23  
 import java.io.FileOutputStream;
 24  
 import java.io.IOException;
 25  
 import java.io.InputStream;
 26  
 import java.io.InputStreamReader;
 27  
 import org.apache.maven.plugin.logging.Log;
 28  
 
 29  
 
 30  
 /**
 31  
  * Source Code Management Object, provides access to SCM system.
 32  
  * 
 33  
  * 
 34  
  * @author DoCulnan
 35  
  *
 36  
  */
 37  3
 public class SrcManager
 38  
 {
 39  
     /**
 40  
      * Number of milisconds to sleep between polls of threads for state information.
 41  
      */
 42  
     private static final int MILLISECONDS_BETEEW_THREAD_POLL = 200;
 43  
 
 44  
     /**
 45  
      * Get the log file.
 46  
      * 
 47  
      * @todo Get the scm log in a more elgant way than with the command line.
 48  
      * @param baseDir Base directory.
 49  
      * @param log logger for writing user feedback.
 50  
      * @return Success.
 51  
      */
 52  
     public boolean log( File baseDir, Log log, StatConf statConf )
 53  
     {
 54  
         String[] commad;
 55  
 
 56  3
         if ( statConf.isStatSVN() )
 57  
         {
 58  3
             commad = new String[] { "svn", "log", baseDir.getAbsolutePath(), "-v", "--xml", "-r", "HEAD:1" };
 59  
         }
 60  0
         else if ( statConf.isStatCVS() )
 61  
         {
 62  0
             commad = new String[] { "cvs", "log" };
 63  
         }
 64  
         else
 65  
         {
 66  
             // Give up if not supported.
 67  0
             log.warn( "Can not get log file for SCM Repository Type." );
 68  0
             return false;
 69  
         }
 70  
 
 71  3
         File file = new File( statConf.getSCMLogFileName() );
 72  
         try
 73  
         {
 74  3
             log.info( "scm log > " + file.getAbsolutePath() );
 75  
 
 76  3
             Runtime rt = Runtime.getRuntime();
 77  3
             Process proc = rt.exec( commad );
 78  
 
 79  3
             ConsoleErrorStream errorGobbler = new ConsoleErrorStream( proc.getErrorStream(), log );
 80  3
             ConsoleFileStream logStream = new ConsoleFileStream( proc.getInputStream(), file, log );
 81  
 
 82  3
             errorGobbler.start();
 83  3
             logStream.start();
 84  227
             while ( errorGobbler.isAlive() || logStream.isAlive() )
 85  
             {
 86  224
                 Thread.sleep( MILLISECONDS_BETEEW_THREAD_POLL );
 87  
             }
 88  
 
 89  3
             log.info( "scm log ...; exitValue: " + proc.waitFor() );
 90  
         }
 91  0
         catch ( IOException e )
 92  
         {
 93  0
             log.error( "Error Getting SCM log.", e );
 94  
         }
 95  0
         catch ( InterruptedException e )
 96  
         {
 97  0
             log.error( "Error Getting SCM log.", e );
 98  
         }
 99  0
         catch ( Exception e )
 100  
         {
 101  0
             log.error( "Error Getting SCM log.", e );
 102  3
         }
 103  3
         return true;
 104  
     }
 105  
 }
 106  
 
 107  
 /**
 108  
  * Thread class for collecting Console error output and logging it at WARN level.
 109  
  * 
 110  
  * @author DoCulnan
 111  
  *
 112  
  */
 113  
 class ConsoleErrorStream extends Thread
 114  
 {
 115  
     /**
 116  
      * Input Stream to read.
 117  
      */
 118  
     private InputStream is;
 119  
 
 120  
     /**
 121  
      * Logger for user feedback.
 122  
      */
 123  
     private Log log;
 124  
 
 125  
     /**
 126  
      * Create an thread ready to run.
 127  
      * 
 128  
      * @param inputStream InputStream to read.
 129  
      * @param logger Logger for user feedback.
 130  
      */
 131  
     ConsoleErrorStream( InputStream inputStream, Log logger )
 132  3
     {
 133  3
         this.is = inputStream;
 134  3
         this.log = logger;
 135  3
     }
 136  
 
 137  
     /**
 138  
      * Run the thread until the InputStream reaches its end.
 139  
      */
 140  
     public void run()
 141  
     {
 142  
         try
 143  
         {
 144  3
             InputStreamReader isr = new InputStreamReader( is );
 145  3
             BufferedReader br = new BufferedReader( isr );
 146  3
             String line = null;
 147  3
             while ( ( line = br.readLine() ) != null ) 
 148  
             {
 149  0
                 log.warn( line );
 150  
             }
 151  
         }
 152  0
         catch ( IOException ioe )
 153  
         {
 154  0
             log.error( ioe.getMessage(), ioe );
 155  3
         }
 156  3
     }
 157  
 }
 158  
 
 159  
 /**
 160  
  * Thread class for collecting console output and writing it to a file.
 161  
  * 
 162  
  * @author DoCulnan
 163  
  *
 164  
  */
 165  
 class ConsoleFileStream extends Thread
 166  
 {
 167  
     /**
 168  
      * Logger for user feedback.
 169  
      */
 170  
     private Log log;
 171  
 
 172  
     /**
 173  
      * File to write stream to.
 174  
      */
 175  
     private File file;
 176  
 
 177  
     /**
 178  
      * Input Stream to read.
 179  
      */
 180  
     private InputStream is;
 181  
 
 182  
     /**
 183  
      * Create an thread ready to run.
 184  
      * 
 185  
      * @param inputStream InputStream to read.
 186  
      * @param outputFile File to write to.
 187  
      * @param logger Logger for user feedback.
 188  
      */
 189  
     ConsoleFileStream( InputStream inputStream, File outputFile, Log logger )
 190  3
     {
 191  3
         this.is = inputStream;
 192  3
         this.file = outputFile;
 193  3
         this.log = logger;
 194  
 
 195  3
     }
 196  
 
 197  
     /**
 198  
      * Run the thread until the InputStream reaches its end.
 199  
      */
 200  
     public void run()
 201  
     {
 202  3
         FileOutputStream fos = null;
 203  
         try
 204  
         {
 205  
 
 206  3
             DataInputStream dis = new DataInputStream( is );
 207  3
             fos = new FileOutputStream( file, false );
 208  
             while ( true )
 209  
             {
 210  149571
                 fos.write( dis.readByte() );
 211  
             }
 212  
         }
 213  3
         catch ( EOFException eofe )
 214  
         {
 215  3
             log.info( file.toString() + " EOF." );
 216  
         }
 217  0
         catch ( IOException ioe )
 218  
         {
 219  0
             log.error( ioe.getMessage(), ioe );
 220  
         }
 221  
         finally
 222  
         {
 223  0
             try
 224  
             {
 225  3
                 fos.close();
 226  
             }
 227  0
             catch ( IOException e )
 228  
             {
 229  0
                 log.error( e.getMessage(), e );
 230  0
                 fos = null;
 231  3
             }
 232  0
         }
 233  
 
 234  3
     }
 235  
 }