1 23 package org.hammurapi; 24 25 import java.text.MessageFormat ; 26 import java.util.Collection ; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 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 listeners; 50 private Map packageResults=new HashMap (); 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 listeners) { 57 this.inspectorSet=inspectorSet; 58 this.waiverSet=waiverSet; 59 this.task = task; 60 this.listeners=listeners; 61 this.summary=summary; 62 } 63 64 68 CompositeResults getPackageResult(String 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 private Integer order=new Integer (Integer.MIN_VALUE+1); 81 82 public Integer 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 Iterator 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 messageFormat=new MessageFormat ( 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 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 [] { 174 new Date (now), 175 new Integer (percentage), 176 new Integer (counter), 177 new Integer (repoSize), 178 new Long (min), 179 new Long (sec), 180 new Long (rmin), 181 new Long (rsec)}, 182 new StringBuffer (), null).toString(), Project.MSG_INFO); 183 } 184 185 public void visit(Package pkg) { 186 ResultsFactory.pushThreadResults(getPackageResult(pkg.getName())); 187 } 188 189 public void leave(Package 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 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 219 repoSize = repository.size(); 220 task.log("Review started at "+new Date (start)+", "+repoSize+" files to review", Project.MSG_INFO); 221 } 222 223 public void leave(Repository repository) throws HammurapiException { 224 log(new Date ()+" Completing results collection ..."); 225 226 ResultsFactory.getInstance().join(); 227 228 log(new Date ()+" Building summary"); 229 230 if (!task.skipIntactPackages || summary.isNew()) { 231 Iterator it=listeners.iterator(); 232 while (it.hasNext()) { 233 ((Listener) it.next()).onSummary(summary, inspectorSet); 234 } 235 } 236 237 Iterator 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 [] { 253 new Double ( 254 (double) summary.getCodeBase() * 1000 255 / (finish - start))})); 256 257 258 Integer 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 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 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 e) { 281 throw new HammurapiNonConsumableException("Sigma is not valid"); 282 } 283 } 284 }.accept(summary); 285 } 286 287 Integer 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 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 314 private void log(String message) { 315 task.log(message); 316 } 317 318 public void setDispatcher(DispatchingVisitor dispatcher) { 319 this.dispatcher=dispatcher; 320 } 321 324 CompositeResults getSummary() { 325 return summary; 326 } 327 328 } | Popular Tags |