File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\FingerprintCleanupThread.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.Extension;
27 import jenkins.model.Jenkins;
28
29 import java.io.File;
30 import java.io.FileFilter;
31 import java.io.IOException;
32 import java.util.logging.Level;
33 import java.util.regex.Pattern;
34
35 /**
36 * Scans the fingerprint database and remove old records
37 * that are no longer relevant.
38 *
39 * <p>
40 * A {@link Fingerprint} is removed when none of the builds that
41 * it point to is available in the records.
42 *
43 * @author Kohsuke Kawaguchi
44 */
45 @Extension
46 public final class FingerprintCleanupThread extends AsyncPeriodicWork {
47
48 public FingerprintCleanupThread() {
49 super("Fingerprint cleanup");
50 }
51
52 public long getRecurrencePeriod() {
53 return DAY;
54 }
55
56 public static void invoke() {
57 getInstance().run();
58 }
59
60 private static FingerprintCleanupThread getInstance() {
61 return Jenkins.getInstance().getExtensionList(AsyncPeriodicWork.class).get(FingerprintCleanupThread.class);
62 }
63
64 protected void execute(TaskListener listener) {
65 int numFiles = 0;
66
67 File root = new File(Jenkins.getInstance().getRootDir(),"fingerprints");
68 File[] files1 = root.listFiles(LENGTH2DIR_FILTER);
69 if(files1!=null) {
70 for (File file1 : files1) {
71 File[] files2 = file1.listFiles(LENGTH2DIR_FILTER);
72 for(File file2 : files2) {
73 File[] files3 = file2.listFiles(FINGERPRINTFILE_FILTER);
74 for(File file3 : files3) {
75 if(check(file3))
76 numFiles++;
77 }
78 deleteIfEmpty(file2);
79 }
80 deleteIfEmpty(file1);
81 }
82 }
83
84 logger.log(Level.INFO, "Cleaned up "+numFiles+" records");
85 }
86
87 /**
88 * Deletes a directory if it's empty.
89 */
90 private void deleteIfEmpty(File dir) {
91 String[] r = dir.list();
92 if(r==null) return; // can happen in a rare occasion
93 if(r.length==0)
94 dir.delete();
95 }
96
97 /**
98 * Examines the file and returns true if a file was deleted.
99 */
100 private boolean check(File fingerprintFile) {
101 try {
102 Fingerprint fp = Fingerprint.load(fingerprintFile);
103 if(!fp.isAlive()) {
104 fingerprintFile.delete();
105 return true;
106 }
107 } catch (IOException e) {
108 logger.log(Level.WARNING, "Failed to process "+fingerprintFile, e);
109 }
110 return false;
111 }
112
113 private static final FileFilter LENGTH2DIR_FILTER = new FileFilter() {
114 public boolean accept(File f) {
115 return f.isDirectory() && f.getName().length()==2;
116 }
117 };
118
119 private static final FileFilter FINGERPRINTFILE_FILTER = new FileFilter() {
120 private final Pattern PATTERN = Pattern.compile("[0-9a-f]{28}\\.xml");
121
122 public boolean accept(File f) {
123 return f.isFile() && PATTERN.matcher(f.getName()).matches();
124 }
125 };
126 }
127