1 25 26 package net.sourceforge.cobertura.coveragedata; 27 28 import java.io.File ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 36 import net.sourceforge.cobertura.util.FileLocker; 37 38 public class ProjectData extends CoverageDataContainer implements HasBeenInstrumented 39 { 40 41 private static final long serialVersionUID = 6; 42 43 private static ProjectData globalProjectData = null; 44 45 private static SaveTimer saveTimer = null; 46 47 48 private Map classes = new HashMap (); 49 50 public void addClassData(ClassData classData) 51 { 52 String packageName = classData.getPackageName(); 53 PackageData packageData = (PackageData)children.get(packageName); 54 if (packageData == null) 55 { 56 packageData = new PackageData(packageName); 57 this.children.put(packageName, packageData); 60 } 61 packageData.addClassData(classData); 62 this.classes.put(classData.getName(), classData); 63 } 64 65 public ClassData getClassData(String name) 66 { 67 return (ClassData)this.classes.get(name); 68 } 69 70 73 public ClassData getOrCreateClassData(String name) 74 { 75 ClassData classData = (ClassData)this.classes.get(name); 76 if (classData == null) 77 { 78 classData = new ClassData(name); 79 addClassData(classData); 80 } 81 return classData; 82 } 83 84 public Collection getClasses() 85 { 86 return this.classes.values(); 87 } 88 89 public int getNumberOfClasses() 90 { 91 return this.classes.size(); 92 } 93 94 public int getNumberOfSourceFiles() 95 { 96 return getSourceFiles().size(); 97 } 98 99 public SortedSet getPackages() 100 { 101 return new TreeSet (this.children.values()); 102 } 103 104 public Collection getSourceFiles() 105 { 106 SortedSet sourceFileDatas = new TreeSet (); 107 Iterator iter = this.children.values().iterator(); 108 while (iter.hasNext()) 109 { 110 PackageData packageData = (PackageData)iter.next(); 111 sourceFileDatas.addAll(packageData.getSourceFiles()); 112 } 113 return sourceFileDatas; 114 } 115 116 126 public SortedSet getSubPackages(String packageName) 127 { 128 SortedSet subPackages = new TreeSet (); 129 Iterator iter = this.children.values().iterator(); 130 while (iter.hasNext()) 131 { 132 PackageData packageData = (PackageData)iter.next(); 133 if (packageData.getName().startsWith(packageName)) 134 subPackages.add(packageData); 135 } 136 return subPackages; 137 } 138 139 public void merge(CoverageData coverageData) 140 { 141 super.merge(coverageData); 142 143 ProjectData projectData = (ProjectData)coverageData; 144 for (Iterator iter = projectData.classes.keySet().iterator(); iter.hasNext();) 145 { 146 Object key = iter.next(); 147 if (!this.classes.containsKey(key)) 148 { 149 this.classes.put(key, projectData.classes.get(key)); 150 } 151 } 152 } 153 154 161 public static ProjectData getGlobalProjectData() 162 { 163 if (globalProjectData != null) 164 return globalProjectData; 165 166 globalProjectData = new ProjectData(); 167 initialize(); 168 return globalProjectData; 169 } 170 171 private static void initialize() 173 { 174 if (System.getProperty("catalina.home") != null) 178 { 179 saveGlobalProjectData(); 180 181 ClassData.class.toString(); 185 CoverageData.class.toString(); 186 CoverageDataContainer.class.toString(); 187 FileLocker.class.toString(); 188 HasBeenInstrumented.class.toString(); 189 LineData.class.toString(); 190 PackageData.class.toString(); 191 SourceFileData.class.toString(); 192 } 193 194 saveTimer = new SaveTimer(); 196 Runtime.getRuntime().addShutdownHook(new Thread (saveTimer)); 197 198 } 202 203 public static void saveGlobalProjectData() 204 { 205 ProjectData projectDataToSave = globalProjectData; 206 207 212 globalProjectData = new ProjectData(); 213 214 219 try 220 { 221 Thread.sleep(1000); 222 } 223 catch (InterruptedException e) 224 { 225 } 226 227 File dataFile = CoverageDataFileHandler.getDefaultDataFile(); 229 FileLocker fileLocker = new FileLocker(dataFile); 230 231 if (fileLocker.lock()) 234 { 235 ProjectData datafileProjectData = loadCoverageDataFromDatafile(dataFile); 236 if (datafileProjectData == null) 237 { 238 datafileProjectData = projectDataToSave; 239 } 240 else 241 { 242 datafileProjectData.merge(projectDataToSave); 243 } 244 CoverageDataFileHandler.saveCoverageData(datafileProjectData, dataFile); 245 } 246 247 fileLocker.release(); 249 } 250 251 private static ProjectData loadCoverageDataFromDatafile(File dataFile) 252 { 253 ProjectData projectData = null; 254 255 if (dataFile.isFile()) 257 { 258 projectData = CoverageDataFileHandler.loadCoverageData(dataFile); 259 } 260 261 if (projectData == null) 262 { 263 System.out.println("Cobertura: Coverage data file " + dataFile.getAbsolutePath() 265 + " either does not exist or is not readable. Creating a new data file."); 266 } 267 268 return projectData; 269 } 270 271 } 272 | Popular Tags |