View Javadoc

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  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          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              // Give up if not supported.
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  * 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     {
133         this.is = inputStream;
134         this.log = logger;
135     }
136 
137     /**
138      * Run the thread until the InputStream reaches its end.
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  * 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     {
191         this.is = inputStream;
192         this.file = outputFile;
193         this.log = logger;
194 
195     }
196 
197     /**
198      * Run the thread until the InputStream reaches its end.
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 }