| 1 | |
package net.sf.statscm; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | 3 | public class SrcManager |
| 38 | |
{ |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
private static final int MILLISECONDS_BETEEW_THREAD_POLL = 200; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 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 | |
|
| 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 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
class ConsoleErrorStream extends Thread |
| 114 | |
{ |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private InputStream is; |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
private Log log; |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
ConsoleErrorStream( InputStream inputStream, Log logger ) |
| 132 | 3 | { |
| 133 | 3 | this.is = inputStream; |
| 134 | 3 | this.log = logger; |
| 135 | 3 | } |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 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 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
class ConsoleFileStream extends Thread |
| 166 | |
{ |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
private Log log; |
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
private File file; |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
private InputStream is; |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 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 | |
|
| 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 | |
} |