KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > FingerprintCleanupThread


1 package hudson.model;
2
3 import java.io.File JavaDoc;
4 import java.io.FileFilter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 /**
10  * Scans the fingerprint database and remove old records
11  * that are no longer relevant.
12  *
13  * <p>
14  * A {@link Fingerprint} is removed when none of the builds that
15  * it point to is available in the records.
16  *
17  * @author Kohsuke Kawaguchi
18  */

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 JavaDoc root = new File JavaDoc(Hudson.getInstance().getRootDir(),"fingerprints");
36         File JavaDoc[] files1 = root.listFiles(LENGTH2DIR_FILTER);
37         if(files1!=null) {
38             for (File JavaDoc file1 : files1) {
39                 File JavaDoc[] files2 = file1.listFiles(LENGTH2DIR_FILTER);
40                 for(File JavaDoc file2 : files2) {
41                     File JavaDoc[] files3 = file2.listFiles(FINGERPRINTFILE_FILTER);
42                     for(File JavaDoc 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     /**
56      * Deletes a directory if it's empty.
57      */

58     private void deleteIfEmpty(File JavaDoc dir) {
59         String JavaDoc[] r = dir.list();
60         if(r==null) return; // can happen in a rare occasion
61
if(r.length==0)
62             dir.delete();
63     }
64
65     /**
66      * Examines the file and returns true if a file was deleted.
67      */

68     private boolean check(File JavaDoc fingerprintFile) {
69         try {
70             Fingerprint fp = Fingerprint.load(fingerprintFile);
71             if(!fp.isAlive()) {
72                 fingerprintFile.delete();
73                 return true;
74             }
75         } catch (IOException JavaDoc e) {
76             logger.log(Level.WARNING, "Failed to process "+fingerprintFile, e);
77         }
78         return false;
79     }
80
81     private static final FileFilter JavaDoc LENGTH2DIR_FILTER = new FileFilter JavaDoc() {
82         public boolean accept(File JavaDoc f) {
83             return f.isDirectory() && f.getName().length()==2;
84         }
85     };
86
87     private static final FileFilter JavaDoc FINGERPRINTFILE_FILTER = new FileFilter JavaDoc() {
88         private final Pattern JavaDoc PATTERN = Pattern.compile("[0-9a-f]{28}\\.xml");
89
90         public boolean accept(File JavaDoc f) {
91             return f.isFile() && PATTERN.matcher(f.getName()).matches();
92         }
93     };
94 }
95
Popular Tags