1 23 package org.hammurapi; 24 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.sql.SQLException ; 28 import java.sql.Timestamp ; 29 import java.text.MessageFormat ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import javax.sql.DataSource ; 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 listeners; 67 private long start=System.currentTimeMillis(); 68 private DispatchingVisitor dispatcher; 69 70 DataSource 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 title, Collection listeners) throws ClassNotFoundException , IOException , SQLException { 87 this.task = task; 88 this.listeners=listeners; 89 90 final String 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 { 99 try { 100 processor.executeScript(new InputStreamReader (getClass().getClassLoader().getResourceAsStream(initScript))); 101 } catch (IOException 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 { 113 dataSource.shutdown(); 114 } 115 116 private Integer order=new Integer (Integer.MIN_VALUE+1); 118 119 public Integer 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 messageFormat=new MessageFormat ( 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 { 139 Iterator 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 168 int resultId = processor.nextPK("PRIMARY_KEY", "RESULT"); 169 String packageName = compilationUnit.getPackage().getName(); 170 engine.insertResult( 171 resultId, 172 results.getCodeBase(), 173 new Timestamp (results.getDate().getTime()), 174 results.getMaxSeverity()==null ? null : new Short (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 Iterator mit=results.getMetrics().values().iterator(); 188 while (mit.hasNext()) { 189 Metric metric=(Metric) mit.next(); 190 198 engine.insertMetric( 199 resultId, 200 metric.getName(), 201 metric.getMin(), 202 metric.getMax(), 203 metric.getTotal(), 204 metric.getNumber()); 205 } 206 207 Iterator sit=results.getSeveritySummary().values().iterator(); 209 while (sit.hasNext()) { 210 Iterator iit=((Map ) 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 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 Iterator 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 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 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 (source.getLine()), 289 source==null ? null : new Integer (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 [] { 302 new Date (now), 303 new Integer (counter), 304 new Long (min), 305 new Long (sec)}, 306 new StringBuffer (), null).toString(), Project.MSG_INFO); 307 } 308 309 312 private void log(String message) { 313 task.log(message); 314 } 315 316 public void setDispatcher(DispatchingVisitor dispatcher) { 317 this.dispatcher=dispatcher; 318 } 319 } | Popular Tags |