1 26 27 package net.sourceforge.groboutils.codecoverage.v2.datastore; 28 29 import java.io.BufferedReader ; 30 import java.io.BufferedWriter ; 31 import java.io.File ; 32 import java.io.FileReader ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 36 import net.sourceforge.groboutils.codecoverage.v2.IAnalysisModule; 37 import net.sourceforge.groboutils.util.io.v1.ExtensionFilenameFilter; 38 39 47 class DirMetaDataIO 48 { 49 private static final org.apache.log4j.Logger LOG = 50 org.apache.log4j.Logger.getLogger( DirMetaDataIO.class ); 51 52 private static final String MODULE_SET_FILENAME = "moduleset.txt"; 53 private static final String CLASS_FILE_EXT = ".classdata.txt"; 54 private static final String MARK_FILE_EXT = ".classmarks.txt"; 55 56 57 private File basedir; 58 private static final ExtensionFilenameFilter CLASS_FILE_FILTER = 59 new ExtensionFilenameFilter( CLASS_FILE_EXT, true ); 60 static { 61 CLASS_FILE_FILTER.allowsDirectories( false ); 62 } 63 64 public DirMetaDataIO( File dir ) 65 throws IOException 66 { 67 if (dir == null) 68 { 69 throw new IllegalArgumentException ( "No null args." ); 70 } 71 if (dir.exists() && !dir.isDirectory()) 72 { 73 throw new IllegalArgumentException ( 74 "File "+dir+" is not a directory." ); 75 } 76 if (!dir.exists()) 77 { 78 dir.mkdir(); 79 } 80 this.basedir = dir; 81 } 82 83 84 87 public AnalysisModuleSet getAnalysisModuleSet() 88 throws IOException 89 { 90 checkClosed(); 91 AnalysisModuleSetIO amsr = new AnalysisModuleSetIO(); 92 AnalysisModuleSet ams; 93 File amsf = getAnalysisModuleSetFile(); 94 if (!amsf.exists()) 95 { 96 ams = new AnalysisModuleSet(); 97 } 98 else 99 { 100 BufferedReader fr = new BufferedReader ( new FileReader ( amsf ) ); 101 try 102 { 103 ams = amsr.readAnalysisModuleSet( fr ); 104 } 105 finally 106 { 107 fr.close(); 108 } 109 } 110 return ams; 111 } 112 113 114 117 public void putAnalysisModuleSet( AnalysisModuleSet ams ) 118 throws IOException 119 { 120 if (ams == null) 121 { 122 throw new IllegalArgumentException ("no null args"); 123 } 124 checkClosed(); 125 AnalysisModuleSetIO amsw = new AnalysisModuleSetIO(); 126 File amsf = getAnalysisModuleSetFile(); 127 BufferedWriter bw = new BufferedWriter ( new FileWriter ( amsf ) ); 128 try 129 { 130 amsw.writeAnalysisModuleSet( ams, bw ); 131 } 132 finally 133 { 134 bw.close(); 135 } 136 } 137 138 139 142 public String [] getClassSigsForAnalysisModule( IAnalysisModule am ) 143 throws IOException 144 { 145 if (am == null) 146 { 147 throw new IllegalArgumentException ("no null args"); 148 } 149 checkClosed(); 150 File amDir = getAnalysisModuleDir( am ); 151 String names[] = amDir.list( CLASS_FILE_FILTER ); 152 for (int i = 0; i < names.length; ++i) 153 { 154 names[i] = names[i].substring( 0, names[i].length() - 155 CLASS_FILE_EXT.length() ); 156 } 157 return names; 158 } 159 160 161 164 public ClassRecord getClassRecord( IAnalysisModule am, String classSig ) 165 throws IOException 166 { 167 return getClassRecord( getAnalysisModuleSet(), am, classSig ); 168 } 169 170 171 174 public ClassRecord getClassRecord( AnalysisModuleSet ams, 175 IAnalysisModule am, String classSig ) 176 throws IOException 177 { 178 if (am == null || classSig == null || ams == null) 179 { 180 throw new IllegalArgumentException ("no null args"); 181 } 182 checkClosed(); 183 File cf = getClassDataFileForModule( am, classSig ); 184 ClassRecordIO crr = new ClassRecordIO(); 185 BufferedReader fr; 186 try 187 { 188 fr = new BufferedReader ( new FileReader ( cf ) ); 189 } 190 catch (java.io.FileNotFoundException fnfe) 191 { 192 LOG.warn( "Couldn't find the file '"+cf+ 193 "' containing the class details." ); 194 return null; 195 } 196 197 ClassRecord cr; 198 try 199 { 200 cr = crr.readClass( ams, fr ); 201 } 202 finally 203 { 204 fr.close(); 205 } 206 207 File mcf = getClassMarkFileForModule( am, cr.getClassSignature() ); 209 try 210 { 211 fr = new BufferedReader ( new FileReader ( mcf ) ); 212 } 213 catch (java.io.FileNotFoundException fnfe) 214 { 215 LOG.warn( "Could not find file '"+mcf+"' containing marks." ); 216 return cr; 217 } 218 219 try 220 { 221 crr.readMarks( cr, fr ); 222 } 223 finally 224 { 225 fr.close(); 226 } 227 228 return cr; 229 } 230 231 232 235 public void putClassRecord( IAnalysisModule am, ClassRecord cr ) 236 throws IOException 237 { 238 if (am == null || cr == null) 239 { 240 throw new IllegalArgumentException ("no null args"); 241 } 242 checkClosed(); 243 File cf = getClassDataFileForModule( am, cr.getClassSignature() ); 244 ClassRecordIO crw = new ClassRecordIO(); 245 BufferedWriter fw = new BufferedWriter ( new FileWriter ( cf ) ); 246 try 247 { 248 crw.writeClass( cr, fw ); 249 } 250 finally 251 { 252 fw.close(); 253 } 254 255 File mcf = getClassMarkFileForModule( am, cr.getClassSignature() ); 257 fw = new BufferedWriter ( new FileWriter ( mcf ) ); 258 try 259 { 260 crw.writeMarksForAnalysisModule( cr, am, fw ); 261 } 262 finally 263 { 264 fw.close(); 265 } 266 } 267 268 269 272 public void deleteClassRecord( IAnalysisModule am, ClassRecord cr ) 273 throws IOException 274 { 275 if (am == null || cr == null) 276 { 277 throw new IllegalArgumentException ("no null args"); 278 } 279 checkClosed(); 280 File cf = getClassDataFileForModule( am, cr.getClassSignature() ); 281 if (cf.exists()) 282 { 283 cf.delete(); 284 } 285 } 286 287 288 291 private File getAnalysisModuleSetFile() 292 { 293 File moduleSetFile = new File ( this.basedir, MODULE_SET_FILENAME ); 294 return moduleSetFile; 295 } 296 297 298 301 private File getAnalysisModuleDir( IAnalysisModule am ) 302 throws IOException 303 { 304 File newDir = new File ( this.basedir, am.getMeasureName() ); 305 if (newDir.exists() && !newDir.isDirectory()) 306 { 307 throw new IOException ( "Expected measure directory '"+newDir+ 308 "', but it isn't a directory." ); 309 } 310 if (!newDir.exists()) 311 { 312 newDir.mkdir(); 313 } 314 return newDir; 315 } 316 317 318 321 private File getClassDataFileForModule( IAnalysisModule am, 322 String classSig ) 323 throws IOException 324 { 325 File mf = getAnalysisModuleDir( am ); 326 File cf = new File ( mf, classSig + CLASS_FILE_EXT ); 327 return cf; 328 } 329 330 331 334 private File getClassMarkFileForModule( IAnalysisModule am, 335 String classSig ) 336 throws IOException 337 { 338 File mf = getAnalysisModuleDir( am ); 339 File cf = new File ( mf, classSig + MARK_FILE_EXT ); 340 return cf; 341 } 342 343 344 345 348 public void close() 349 throws IOException 350 { 351 LOG.debug( "Closing DirMetaDataIO." ); 352 this.basedir = null; 353 } 354 355 356 359 private void checkClosed() 360 throws IOException 361 { 362 if (isClosed()) 363 { 364 throw new IOException ( "Directory storage has been closed." ); 365 } 366 } 367 368 369 372 boolean isClosed() 373 { 374 return (this.basedir == null); 375 } 376 377 378 protected void finalize() throws Throwable 379 { 380 Exception ex = null; 381 if (!isClosed()) 382 { 383 ex = new IllegalStateException ("Did not close writer."); 384 close(); 385 } 386 387 super.finalize(); 388 389 if (ex != null) 391 { 392 throw ex; 393 } 394 } 395 } 396 397 | Popular Tags |