KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > ResultsCollector


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.text.MessageFormat JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import org.apache.tools.ant.Project;
33 import org.hammurapi.results.CompositeResults;
34 import org.hammurapi.results.ResultsFactory;
35 import org.hammurapi.results.ReviewResults;
36
37 import com.pavelvlasov.jsel.CompilationUnit;
38 import com.pavelvlasov.jsel.Package;
39 import com.pavelvlasov.jsel.Repository;
40 import com.pavelvlasov.util.DispatcherAware;
41 import com.pavelvlasov.util.DispatchingVisitor;
42 import com.pavelvlasov.util.OrderedTarget;
43 import com.pavelvlasov.util.DispatchingVisitor.Stats;
44
45
46 public class ResultsCollector implements DispatcherAware, OrderedTarget {
47     private final HammurapiTask task;
48     private InspectorSet inspectorSet;
49     private Collection JavaDoc listeners;
50     private Map JavaDoc packageResults=new HashMap JavaDoc();
51     private CompositeResults summary;
52     private long start;
53     private DispatchingVisitor dispatcher;
54     private WaiverSet waiverSet;
55     
56     ResultsCollector(HammurapiTask task, InspectorSet inspectorSet, WaiverSet waiverSet, CompositeResults summary, Collection JavaDoc listeners) {
57         this.inspectorSet=inspectorSet;
58         this.waiverSet=waiverSet;
59         this.task = task;
60         this.listeners=listeners;
61         this.summary=summary;
62     }
63     
64     /**
65      * @ant:ignore
66      * @param result
67      */

68     CompositeResults getPackageResult(String JavaDoc packageName) {
69         synchronized (packageResults) {
70             CompositeResults ret=(CompositeResults) packageResults.get(packageName);
71             if (ret==null) {
72                 ret=ResultsFactory.getInstance().newCompositeResults(packageName);
73                 packageResults.put(packageName, ret);
74             }
75             return ret;
76         }
77     }
78     
79     // Second to visit, second from the end to leave
80
private Integer JavaDoc order=new Integer JavaDoc(Integer.MIN_VALUE+1);
81     
82     public Integer JavaDoc getOrder() {
83         return order;
84     }
85     
86     public boolean visit(CompilationUnit compilationUnit) {
87         ReviewResults result = task.isForce() ? null : ResultsFactory.getInstance().findReviewResults(compilationUnit);
88         if (result!=null) {
89             if (task.isForceOnWarnings() && result.hasWarnings()) {
90                 result=null;
91             } else if (task.isForceOnWaivers()) {
92                 // Checking if there are violations to be waived
93
Iterator JavaDoc it=result.getViolations().iterator();
94                 while (it.hasNext()) {
95                     Violation violation=(Violation) it.next();
96                     if (waiverSet.requestWaiver(violation, true)!=null) {
97                         result=null;
98                         break;
99                     }
100                 }
101             }
102         }
103         
104         
105         if (result==null) {
106             ResultsFactory.getThreadResults().addMetric(null, "Change ratio", 1);
107             
108             log(compilationUnit.getRelativeName()+" - reviewing");
109             result = ResultsFactory.getInstance().newReviewResults(compilationUnit);
110             ResultsFactory.pushThreadResults(result);
111             
112             if (dispatcher!=null) {
113                 dispatcher.getThreadStats().reset();
114             }
115             return true;
116         }
117         
118         ResultsFactory.getThreadResults().addMetric(null, "Change ratio", 0);
119         
120         log(compilationUnit.getRelativeName()+" - skipped");
121         synchronized (packageResults) {
122             getPackageResult(compilationUnit.getPackage().getName()).add(result);
123         }
124         
125         return false;
126     }
127     
128     private static MessageFormat JavaDoc messageFormat=new MessageFormat JavaDoc(
129             "{0,date, yyyy/MM/dd HH:mm:ss} Progress: {1,number,integer}% ({2}/{3}). " +
130             "Elapsed time: {4} min. {5} sec. Remaining time: {6} min. {7} sec. ");
131     
132     public void leave(final CompilationUnit compilationUnit) throws HammurapiException {
133         final ReviewResults results = (ReviewResults) ResultsFactory.popThreadResults();
134         
135         Stats threadStats = dispatcher==null ? null : dispatcher.getThreadStats();
136         
137         final long visits = threadStats==null ? 0 : threadStats.getVisits();
138         final long invocations = threadStats==null ? 0 : threadStats.getInvocations();
139         
140         final CompositeResults packageResult = getPackageResult(compilationUnit.getPackage().getName());
141         ResultsFactory.getInstance().execute(
142                 new ResultsFactory.Task() {
143
144                     public void execute() throws HammurapiException {
145                         results.commit();
146                         Iterator JavaDoc it=listeners.iterator();
147                         while (it.hasNext()) {
148                             if (dispatcher!=null) {
149                                 results.setCodeBase(visits);
150                                 results.setReviewsNumber(invocations);
151                             }
152                             ((Listener) it.next()).onReview(results);
153                         }
154                         
155                         packageResult.add(results);
156                     }
157                 });
158         
159         ++counter;
160         double progress= (double) counter/(double) repoSize;
161         int percentage =(int) (100*progress);
162         long now=System.currentTimeMillis();
163         
164         long elapsedSec = (now-start)/1000;
165         long min=elapsedSec/60;
166         long sec=elapsedSec % 60;
167         
168         long remainingSec = counter==0 ? 0 : (long) (elapsedSec*(1-progress)/progress);
169         long rmin= remainingSec/60;
170         long rsec= remainingSec % 60;
171                 
172         task.log(messageFormat.format(
173                 new Object JavaDoc[] {
174                         new Date JavaDoc(now),
175                         new Integer JavaDoc(percentage),
176                         new Integer JavaDoc(counter),
177                         new Integer JavaDoc(repoSize),
178                         new Long JavaDoc(min),
179                         new Long JavaDoc(sec),
180                         new Long JavaDoc(rmin),
181                         new Long JavaDoc(rsec)},
182                 new StringBuffer JavaDoc(), null).toString(), Project.MSG_INFO);
183     }
184     
185     public void visit(Package JavaDoc pkg) {
186         ResultsFactory.pushThreadResults(getPackageResult(pkg.getName()));
187     }
188     
189     public void leave(Package JavaDoc pkg) throws HammurapiException {
190         final CompositeResults packageResult = (CompositeResults) ResultsFactory.popThreadResults();
191         packageResult.commit();
192         
193         ResultsFactory.getInstance().execute(
194                 new ResultsFactory.Task() {
195
196                     public void execute() throws HammurapiException {
197                         if (!task.skipIntactPackages || packageResult.isNew()) {
198                             Iterator JavaDoc it=listeners.iterator();
199                             while (it.hasNext()) {
200                                 ((Listener) it.next()).onPackage(packageResult);
201                             }
202                             
203                             synchronized (summary) {
204                                 summary.add(packageResult);
205                             }
206                         }
207                     }
208                     
209                 });
210     }
211     
212     private int counter;
213     private int repoSize;
214     
215     public void visit(Repository repository) {
216         start=System.currentTimeMillis();
217         //ResultsFactory.pushThreadResults(summary);
218

219         repoSize = repository.size();
220         task.log("Review started at "+new Date JavaDoc(start)+", "+repoSize+" files to review", Project.MSG_INFO);
221     }
222     
223     public void leave(Repository repository) throws HammurapiException {
224         log(new Date JavaDoc()+" Completing results collection ...");
225         
226         ResultsFactory.getInstance().join();
227         
228         log(new Date JavaDoc()+" Building summary");
229         
230         if (!task.skipIntactPackages || summary.isNew()) {
231             Iterator JavaDoc it=listeners.iterator();
232             while (it.hasNext()) {
233                 ((Listener) it.next()).onSummary(summary, inspectorSet);
234             }
235         }
236         
237         Iterator JavaDoc it=task.getReviewAcceptorEntries().iterator();
238         while (it.hasNext()) {
239             ((ReviewAcceptor) ((ReviewAcceptorEntry) it.next()).getObject(null)).accept(summary);
240         }
241         
242         long finish=System.currentTimeMillis();
243         
244         long elapsedSec = (finish-start)/1000;
245         long min=elapsedSec/60;
246         long sec=elapsedSec % 60;
247         
248         log("Time: "+min+" min. "+sec+" sec.");
249         log(
250                 MessageFormat.format(
251                         "Performance {0, number,###.000000}",
252                         new Object JavaDoc[] {
253                                 new Double JavaDoc(
254                                         (double) summary.getCodeBase() * 1000
255                                         / (finish - start))}));
256         
257         
258         Integer JavaDoc severityThreshold = task.getSeverityThreshold();
259         if (severityThreshold!=null) {
260             final int sth=this.task.getSeverityThreshold().intValue();
261             new ReviewAcceptor() {
262                 public void accept(CompositeResults summary) throws HammurapiException {
263                     Number JavaDoc severity=summary.getMaxSeverity();
264                     if (severity!=null && severity.intValue()<=sth) {
265                         throw new HammurapiNonConsumableException("Severity threshold ("+sth+") infringed");
266                     }
267                 }
268             }.accept(summary);
269         }
270         
271         Double JavaDoc sigmaThreshold = task.getSigmaThreshold();
272         if (sigmaThreshold!=null) {
273             final double cth=sigmaThreshold.doubleValue();
274             new ReviewAcceptor() {
275                 public void accept(CompositeResults summary) throws HammurapiException {
276                     try {
277                         if (Double.parseDouble(summary.getSigma())<cth) {
278                             throw new HammurapiNonConsumableException("Sigma is below threshold ("+cth+")");
279                         }
280                     } catch (NumberFormatException JavaDoc e) {
281                         throw new HammurapiNonConsumableException("Sigma is not valid");
282                     }
283                 }
284             }.accept(summary);
285         }
286         
287         Integer JavaDoc dpmoThreshold = task.getDpmoThreshold();
288         if (dpmoThreshold!=null) {
289             final int cth=dpmoThreshold.intValue();
290             new ReviewAcceptor() {
291                 public void accept(CompositeResults summary) throws HammurapiException {
292                     try {
293                         if (Integer.parseInt(summary.getDPMO())>cth) {
294                             throw new HammurapiNonConsumableException("DPMO is above threshold ("+cth+")");
295                         }
296                     } catch (NumberFormatException JavaDoc e) {
297                         throw new HammurapiNonConsumableException("DPMO is not valid");
298                     }
299                 }
300             }.accept(summary);
301         }
302         
303         if (this.task.isFailOnWarnings() && !summary.getWarnings().isEmpty()) {
304             throw new HammurapiNonConsumableException("There have been warnings during execution.");
305         }
306                 
307         ResultsFactory.popThreadResults();
308         summary.commit();
309     }
310
311     /**
312      * @param string
313      */

314     private void log(String JavaDoc message) {
315         task.log(message);
316     }
317
318     public void setDispatcher(DispatchingVisitor dispatcher) {
319         this.dispatcher=dispatcher;
320     }
321     /**
322      * @return Returns the summary.
323      */

324     CompositeResults getSummary() {
325         return summary;
326     }
327
328 }
Popular Tags