1 16 package com.google.gwt.junit.viewer.server; 17 18 import com.google.gwt.junit.client.Benchmark; 19 import com.google.gwt.junit.viewer.client.Report; 20 import com.google.gwt.junit.viewer.client.ReportSummary; 21 22 import org.w3c.dom.Document ; 23 24 import java.io.File ; 25 import java.io.FilenameFilter ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 35 42 public class ReportDatabase { 43 44 48 public static class BadPathException extends RuntimeException { 49 String path; 50 51 public BadPathException(String path) { 52 super("The path " + path + " does not exist."); 53 this.path = path; 54 } 55 56 public String getPath() { 57 return path; 58 } 59 } 60 61 private static class ReportEntry { 62 private ReportSummary summary; 63 private Report report; 64 private long lastModified; 65 66 public ReportEntry(Report report, ReportSummary summary, long lastModified) { 67 this.report = report; 68 this.summary = summary; 69 this.lastModified = lastModified; 70 } 71 } 72 73 private static class ReportFile { 74 File file; 75 long lastModified; 76 77 ReportFile(File f) { 78 this.file = f; 79 this.lastModified = f.lastModified(); 80 } 81 } 82 83 86 private static final int UPDATE_DURATION_MILLIS = 30000; 87 88 private static ReportDatabase database = new ReportDatabase(); 89 90 public static ReportDatabase getInstance() { 91 return database; 92 } 93 94 private static String getReportId(File f) { 95 return f.getName(); 96 } 97 98 101 private Map reports = new HashMap (); 102 103 106 private long lastUpdateMillis = -1L; 107 108 112 private Object updateLock = new Object (); 113 114 117 private boolean updating = false; 118 119 122 private Object reportsLock = new Object (); 123 124 127 private final String reportPath; 128 129 private ReportDatabase() throws BadPathException { 130 String path = System.getProperty(Benchmark.REPORT_PATH); 131 if (path == null || path.trim().equals("")) { 132 path = System.getProperty("user.dir"); 133 } 134 reportPath = path; 135 136 if (!new File (reportPath).exists()) { 137 throw new BadPathException(reportPath); 138 } 139 } 140 141 public Report getReport(String reportId) { 142 synchronized (reportsLock) { 143 ReportEntry entry = (ReportEntry) reports.get(reportId); 144 return entry == null ? null : entry.report; 145 } 146 } 147 148 public List getReportSummaries() { 149 150 154 155 boolean update = false; 156 157 synchronized (updateLock) { 160 if (!updating) { 161 long currentTime = System.currentTimeMillis(); 162 163 if (currentTime > lastUpdateMillis + UPDATE_DURATION_MILLIS) { 164 update = updating = true; 165 } 166 } 167 } 168 169 if (update) { 170 updateReports(); 171 } 172 173 synchronized (reportsLock) { 174 List summaries = new ArrayList ( 175 reports.size()); 176 for (Iterator it = reports.values().iterator(); it.hasNext();) { 177 ReportEntry entry = (ReportEntry) it.next(); 178 summaries.add(entry.summary); 179 } 180 return summaries; 181 } 182 } 183 184 private void updateReports() { 185 186 File path = new File (reportPath); 187 188 File [] files = path.listFiles(new FilenameFilter () { 189 public boolean accept(File f, String name) { 190 return name.startsWith("report-") && name.endsWith(".xml"); 191 } 192 }); 193 194 Map filesToUpdate = new HashMap (); 195 Map filesById = new HashMap (); 196 for (int i = 0; i < files.length; ++i) { 197 File f = files[i]; 198 filesById.put(getReportId(f), new ReportFile(f)); 199 } 200 201 synchronized (reportsLock) { 205 206 for (int i = 0; i < files.length; ++i) { 208 File file = files[i]; 209 String reportId = getReportId(file); 210 ReportEntry entry = (ReportEntry) reports.get(reportId); 211 if (entry == null || entry.lastModified < file.lastModified()) { 212 filesToUpdate.put(reportId, null); 213 } 214 } 215 216 for (Iterator it = reports.keySet().iterator(); it.hasNext();) { 218 String id = (String ) it.next(); 219 if (filesById.get(id) == null) { 220 it.remove(); 221 } 222 } 223 } 224 225 try { 226 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 227 factory.setIgnoringElementContentWhitespace(true); 228 factory.setIgnoringComments(true); 229 DocumentBuilder builder = factory.newDocumentBuilder(); 230 231 for (Iterator it = filesToUpdate.keySet().iterator(); it.hasNext();) { 232 String id = (String ) it.next(); 233 ReportFile reportFile = (ReportFile) filesById.get(id); 234 String filePath = reportFile.file.getAbsolutePath(); 235 Document doc = builder.parse(filePath); 236 Report report = ReportXml.fromXml(doc.getDocumentElement()); 237 report.setId(id); 238 ReportSummary summary = report.getSummary(); 239 long lastModified = new File (filePath).lastModified(); 240 filesToUpdate.put(id, new ReportEntry(report, summary, lastModified)); 241 } 242 243 synchronized (reportsLock) { 245 for (Iterator it = filesToUpdate.keySet().iterator(); it.hasNext();) { 246 String id = (String ) it.next(); 247 reports.put(id, filesToUpdate.get(id)); 248 } 249 } 250 } catch (Exception e) { 251 e.printStackTrace(); 255 } 256 257 synchronized (updateLock) { 258 updating = false; 259 lastUpdateMillis = System.currentTimeMillis(); 260 } 261 } 262 } 263 | Popular Tags |