1 package hudson.model; 2 3 import java.io.File ; 4 import java.io.FileFilter ; 5 import java.io.IOException ; 6 import java.util.logging.Level ; 7 import java.util.regex.Pattern ; 8 9 19 public final class FingerprintCleanupThread extends PeriodicWork { 20 21 private static FingerprintCleanupThread theInstance; 22 23 public FingerprintCleanupThread() { 24 super("Fingerprint cleanup"); 25 theInstance = this; 26 } 27 28 public static void invoke() { 29 theInstance.run(); 30 } 31 32 protected void execute() { 33 int numFiles = 0; 34 35 File root = new File (Hudson.getInstance().getRootDir(),"fingerprints"); 36 File [] files1 = root.listFiles(LENGTH2DIR_FILTER); 37 if(files1!=null) { 38 for (File file1 : files1) { 39 File [] files2 = file1.listFiles(LENGTH2DIR_FILTER); 40 for(File file2 : files2) { 41 File [] files3 = file2.listFiles(FINGERPRINTFILE_FILTER); 42 for(File file3 : files3) { 43 if(check(file3)) 44 numFiles++; 45 } 46 deleteIfEmpty(file2); 47 } 48 deleteIfEmpty(file1); 49 } 50 } 51 52 logger.log(Level.INFO, "Cleaned up "+numFiles+" records"); 53 } 54 55 58 private void deleteIfEmpty(File dir) { 59 String [] r = dir.list(); 60 if(r==null) return; if(r.length==0) 62 dir.delete(); 63 } 64 65 68 private boolean check(File fingerprintFile) { 69 try { 70 Fingerprint fp = Fingerprint.load(fingerprintFile); 71 if(!fp.isAlive()) { 72 fingerprintFile.delete(); 73 return true; 74 } 75 } catch (IOException e) { 76 logger.log(Level.WARNING, "Failed to process "+fingerprintFile, e); 77 } 78 return false; 79 } 80 81 private static final FileFilter LENGTH2DIR_FILTER = new FileFilter () { 82 public boolean accept(File f) { 83 return f.isDirectory() && f.getName().length()==2; 84 } 85 }; 86 87 private static final FileFilter FINGERPRINTFILE_FILTER = new FileFilter () { 88 private final Pattern PATTERN = Pattern.compile("[0-9a-f]{28}\\.xml"); 89 90 public boolean accept(File f) { 91 return f.isFile() && PATTERN.matcher(f.getName()).matches(); 92 } 93 }; 94 } 95 | Popular Tags |