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 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 if ( statConf.isStatSVN() )
57 {
58 commad = new String[] { "svn", "log", baseDir.getAbsolutePath(), "-v", "--xml", "-r", "HEAD:1" };
59 }
60 else if ( statConf.isStatCVS() )
61 {
62 commad = new String[] { "cvs", "log" };
63 }
64 else
65 {
66
67 log.warn( "Can not get log file for SCM Repository Type." );
68 return false;
69 }
70
71 File file = new File( statConf.getSCMLogFileName() );
72 try
73 {
74 log.info( "scm log > " + file.getAbsolutePath() );
75
76 Runtime rt = Runtime.getRuntime();
77 Process proc = rt.exec( commad );
78
79 ConsoleErrorStream errorGobbler = new ConsoleErrorStream( proc.getErrorStream(), log );
80 ConsoleFileStream logStream = new ConsoleFileStream( proc.getInputStream(), file, log );
81
82 errorGobbler.start();
83 logStream.start();
84 while ( errorGobbler.isAlive() || logStream.isAlive() )
85 {
86 Thread.sleep( MILLISECONDS_BETEEW_THREAD_POLL );
87 }
88
89 log.info( "scm log ...; exitValue: " + proc.waitFor() );
90 }
91 catch ( IOException e )
92 {
93 log.error( "Error Getting SCM log.", e );
94 }
95 catch ( InterruptedException e )
96 {
97 log.error( "Error Getting SCM log.", e );
98 }
99 catch ( Exception e )
100 {
101 log.error( "Error Getting SCM log.", e );
102 }
103 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 {
133 this.is = inputStream;
134 this.log = logger;
135 }
136
137
138
139
140 public void run()
141 {
142 try
143 {
144 InputStreamReader isr = new InputStreamReader( is );
145 BufferedReader br = new BufferedReader( isr );
146 String line = null;
147 while ( ( line = br.readLine() ) != null )
148 {
149 log.warn( line );
150 }
151 }
152 catch ( IOException ioe )
153 {
154 log.error( ioe.getMessage(), ioe );
155 }
156 }
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 {
191 this.is = inputStream;
192 this.file = outputFile;
193 this.log = logger;
194
195 }
196
197
198
199
200 public void run()
201 {
202 FileOutputStream fos = null;
203 try
204 {
205
206 DataInputStream dis = new DataInputStream( is );
207 fos = new FileOutputStream( file, false );
208 while ( true )
209 {
210 fos.write( dis.readByte() );
211 }
212 }
213 catch ( EOFException eofe )
214 {
215 log.info( file.toString() + " EOF." );
216 }
217 catch ( IOException ioe )
218 {
219 log.error( ioe.getMessage(), ioe );
220 }
221 finally
222 {
223 try
224 {
225 fos.close();
226 }
227 catch ( IOException e )
228 {
229 log.error( e.getMessage(), e );
230 fos = null;
231 }
232 }
233
234 }
235 }