KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > QuickResultsCollector


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23 package org.hammurapi;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.sql.DataSource JavaDoc;
36
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.Project;
39 import org.hammurapi.results.InspectorSummary;
40 import org.hammurapi.results.ResultsFactory;
41 import org.hammurapi.results.ReviewResults;
42 import org.hammurapi.results.quick.Inspector;
43 import org.hammurapi.results.quick.ResultsEngine;
44
45 import com.pavelvlasov.jsel.CompilationUnit;
46 import com.pavelvlasov.metrics.Metric;
47 import com.pavelvlasov.review.Signed;
48 import com.pavelvlasov.review.SourceMarker;
49 import com.pavelvlasov.sql.SQLProcessor;
50 import com.pavelvlasov.sql.Transaction;
51 import com.pavelvlasov.sql.hypersonic.HypersonicDataSource;
52 import com.pavelvlasov.sql.hypersonic.HypersonicStandaloneDataSource;
53 import com.pavelvlasov.sql.hypersonic.HypersonicTmpDataSource;
54 import com.pavelvlasov.util.DispatcherAware;
55 import com.pavelvlasov.util.DispatchingVisitor;
56 import com.pavelvlasov.util.OrderedTarget;
57 import com.pavelvlasov.util.DispatchingVisitor.Stats;
58
59
60 public class QuickResultsCollector implements DispatcherAware, OrderedTarget {
61     static final byte RESULT_NEW=0;
62     static final byte RESULT_PREV=1;
63     static final byte RESULT_UNTOUCHED=2;
64     
65     private final QuickHammurapiTask task;
66     private Collection JavaDoc listeners;
67     private long start=System.currentTimeMillis();
68     private DispatchingVisitor dispatcher;
69     
70     DataSource JavaDoc getDataSource() {
71         return dataSource;
72     }
73     
74     ResultsEngine getEngine() {
75         return engine;
76     }
77     
78     SQLProcessor getProcessor() {
79         return processor;
80     }
81     
82     private HypersonicDataSource dataSource;
83     private SQLProcessor processor;
84     private ResultsEngine engine;
85     
86     QuickResultsCollector(QuickHammurapiTask task, String JavaDoc title, Collection JavaDoc listeners) throws ClassNotFoundException JavaDoc, IOException JavaDoc, SQLException JavaDoc {
87         this.task = task;
88         this.listeners=listeners;
89         
90         final String JavaDoc initScript = "org/hammurapi/results/simple/Quickurapi.Hypersonic.sql";
91         if (task.database==null) {
92             dataSource=new HypersonicTmpDataSource(initScript);
93         } else {
94             dataSource = new HypersonicStandaloneDataSource(
95                     task.database.getAbsolutePath(),
96                     new Transaction() {
97
98                         public boolean execute(SQLProcessor processor) throws SQLException JavaDoc {
99                             try {
100                                 processor.executeScript(new InputStreamReader JavaDoc(getClass().getClassLoader().getResourceAsStream(initScript)));
101                             } catch (IOException JavaDoc e) {
102                                 throw new BuildException("Cannot initialize database", e);
103                             }
104                             return true;
105                         }
106                     });
107         }
108         processor=new SQLProcessor(dataSource, null);
109         engine=new ResultsEngine(processor);
110     }
111     
112     public void shutdown() throws SQLException JavaDoc {
113         dataSource.shutdown();
114     }
115         
116     // Second to visit, second from the end to leave
117
private Integer JavaDoc order=new Integer JavaDoc(Integer.MIN_VALUE+1);
118     
119     public Integer JavaDoc getOrder() {
120         return order;
121     }
122     
123     public boolean visit(CompilationUnit compilationUnit) {
124         log(compilationUnit.getRelativeName()+" - reviewing");
125         ResultsFactory.pushThreadResults(ResultsFactory.getInstance().newReviewResults(compilationUnit));
126         
127         if (dispatcher!=null) {
128             dispatcher.getThreadStats().reset();
129         }
130         return true;
131     }
132     
133     private int counter=0;
134     
135     private static MessageFormat JavaDoc messageFormat=new MessageFormat JavaDoc(
136             "{0,date, yyyy/MM/dd HH:mm:ss} Progress: {1} file(s). Elapsed time: {2} min. {3} sec. ");
137     
138     public void leave(CompilationUnit compilationUnit) throws HammurapiException, SQLException JavaDoc {
139         Iterator JavaDoc it=listeners.iterator();
140         ReviewResults results = (ReviewResults) ResultsFactory.popThreadResults();
141         results.commit();
142         while (it.hasNext()) {
143             if (dispatcher!=null) {
144                 Stats threadStats = dispatcher.getThreadStats();
145                 results.setCodeBase(threadStats.getVisits());
146                 results.setReviewsNumber(threadStats.getInvocations());
147             }
148             ((Listener) it.next()).onReview(results);
149         }
150         
151         // Result
152
/*
153          * int Id,
154          * long Codebase,
155          * java.sql.Timestamp ResultDate,
156          * Short MaxSeverity,
157          * long Reviews,
158          * double ViolationLevel,
159          * long Violations,
160          * long WaivedViolations,
161          * String Name,
162          * boolean HasWarnings,
163          * String PackageName,
164          * byte State,
165          * long CuSize,
166          * long CuChecksum
167          */

168         int resultId = processor.nextPK("PRIMARY_KEY", "RESULT");
169         String JavaDoc packageName = compilationUnit.getPackage().getName();
170         engine.insertResult(
171                 resultId,
172                 results.getCodeBase(),
173                 new Timestamp JavaDoc(results.getDate().getTime()),
174                 results.getMaxSeverity()==null ? null : new Short JavaDoc(results.getMaxSeverity().shortValue()),
175                 results.getReviewsNumber(),
176                 results.getViolationLevel(),
177                 results.getViolationsNumber(),
178                 results.getWaivedViolationsNumber(),
179                 results.getName(),
180                 results.hasWarnings(),
181                 packageName,
182                 RESULT_NEW,
183                 compilationUnit.getSize(),
184                 compilationUnit.getCheckSum());
185                 
186         // Metric
187
Iterator JavaDoc mit=results.getMetrics().values().iterator();
188         while (mit.hasNext()) {
189             Metric metric=(Metric) mit.next();
190             /*
191              * int ResultId,
192              * String Name,
193              * double MinValue,
194              * double MaxValue,
195              * double MetricTotal,
196              * int Measurements
197              */

198             engine.insertMetric(
199                     resultId,
200                     metric.getName(),
201                     metric.getMin(),
202                     metric.getMax(),
203                     metric.getTotal(),
204                     metric.getNumber());
205         }
206         
207         // Inspector
208
Iterator JavaDoc sit=results.getSeveritySummary().values().iterator();
209         while (sit.hasNext()) {
210             Iterator JavaDoc iit=((Map JavaDoc) sit.next()).values().iterator();
211             while (iit.hasNext()) {
212                 InspectorSummary is=(InspectorSummary) iit.next();
213                 Inspector dis = engine.getInspector(packageName, is.getName(), resultId);
214                 if (dis==null) {
215                     /*
216                      * String PackageName,
217                      * String Name,
218                      * short Severity,
219                      * String Description,
220                      * String ConfigInfo,
221                      * int Violations,
222                      * int WaivedViolations
223                      */

224                     engine.insertInspector(
225                             packageName,
226                             is.getName(),
227                             resultId,
228                             is.getSeverity()==null ? 0 : is.getSeverity().shortValue(),
229                             is.getDescription(),
230                             is.getConfigInfo(),
231                             is.getLocationsCount(),
232                             0);
233                 } else {
234                     engine.addInspectorViolations(is.getLocationsCount(), packageName, is.getName());
235                 }
236             }
237         }
238         
239         // Warning
240
Iterator JavaDoc wit=results.getWarnings().iterator();
241         while (wit.hasNext()) {
242             Violation warning=(Violation) wit.next();
243             InspectorDescriptor descriptor = warning.getDescriptor();
244             if (descriptor!=null) {
245                 Inspector dis = engine.getInspector(packageName, descriptor.getName(), resultId);
246                 if (dis==null) {
247                     /*
248                      * String PackageName,
249                      * String Name,
250                      * short Severity,
251                      * String Description,
252                      * String ConfigInfo,
253                      * int Violations,
254                      * int WaivedViolations
255                      */

256                     engine.insertInspector(
257                             packageName,
258                             descriptor.getName(),
259                             resultId,
260                             descriptor.getSeverity()==null ? 0 : descriptor.getSeverity().shortValue(),
261                             descriptor.getDescription(),
262                             null,
263                             0,
264                             0);
265                 }
266             }
267             
268             int warningId = processor.nextPK("PRIMARY_KEY", "WARNING");
269             /*
270              * int Id,
271              * int ResultId,
272              * String PackageName,
273              * String Inspector,
274              * String Message,
275              * String Source,
276              * Integer Line,
277              * Integer Col,
278              * String SourceSignature
279              */

280             SourceMarker source = warning.getSource();
281             engine.insertWarning(
282                     warningId,
283                     resultId,
284                     packageName,
285                     descriptor.getName(),
286                     warning.getMessage(),
287                     source==null ? null : source.getSourceURL(),
288                     source==null ? null : new Integer JavaDoc(source.getLine()),
289                     source==null ? null : new Integer JavaDoc(source.getColumn()),
290                     source instanceof Signed ? ((Signed) source).getSignature() : null);
291         }
292                 
293         ++counter;
294         long now=System.currentTimeMillis();
295         
296         long elapsedSec = (now-start)/1000;
297         long min=elapsedSec/60;
298         long sec=elapsedSec % 60;
299         
300         task.log(messageFormat.format(
301                 new Object JavaDoc[] {
302                         new Date JavaDoc(now),
303                         new Integer JavaDoc(counter),
304                         new Long JavaDoc(min),
305                         new Long JavaDoc(sec)},
306                 new StringBuffer JavaDoc(), null).toString(), Project.MSG_INFO);
307     }
308         
309     /**
310      * @param string
311      */

312     private void log(String JavaDoc message) {
313         task.log(message);
314     }
315
316     public void setDispatcher(DispatchingVisitor dispatcher) {
317         this.dispatcher=dispatcher;
318     }
319 }
Popular Tags