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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2011, 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.Util;
27 import hudson.util.KeyedDataStorage;
28 import jenkins.model.Jenkins;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.Locale;
33
34 /**
35 * Cache of {@link Fingerprint}s.
36 *
37 * <p>
38 * This implementation makes sure that no two {@link Fingerprint} objects
39 * lie around for the same hash code, and that unused {@link Fingerprint}
40 * will be adequately GC-ed to prevent memory leak.
41 *
42 * @author Kohsuke Kawaguchi
43 * @see Jenkins#getFingerprintMap()
44 */
45 public final class FingerprintMap extends KeyedDataStorage<Fingerprint,FingerprintMap.FingerprintParams> {
46
47 /**
48 * Returns true if there's some data in the fingerprint database.
49 */
50 public boolean isReady() {
51 return new File(Jenkins.getInstance().getRootDir(),"fingerprints").exists();
52 }
53
54 /**
55 * @param build
56 * set to non-null if {@link Fingerprint} to be created (if so)
57 * will have this build as the owner. Otherwise null, to indicate
58 * an owner-less build.
59 */
60 public Fingerprint getOrCreate(AbstractBuild build, String fileName, byte[] md5sum) throws IOException {
61 return getOrCreate(build,fileName, Util.toHexString(md5sum));
62 }
63
64 public Fingerprint getOrCreate(AbstractBuild build, String fileName, String md5sum) throws IOException {
65 return super.getOrCreate(md5sum, new FingerprintParams(build,fileName));
66 }
67
68 public Fingerprint getOrCreate(Run build, String fileName, String md5sum) throws IOException {
69 return super.getOrCreate(md5sum, new FingerprintParams(build,fileName));
70 }
71
72 @Override
73 protected Fingerprint get(String md5sum, boolean createIfNotExist, FingerprintParams createParams) throws IOException {
74 // sanity check
75 if(md5sum.length()!=32)
76 return null; // illegal input
77 md5sum = md5sum.toLowerCase(Locale.ENGLISH);
78
79 return super.get(md5sum,createIfNotExist,createParams);
80 }
81
82 private byte[] toByteArray(String md5sum) {
83 byte[] data = new byte[16];
84 for( int i=0; i<md5sum.length(); i+=2 )
85 data[i/2] = (byte)Integer.parseInt(md5sum.substring(i,i+2),16);
86 return data;
87 }
88
89 protected Fingerprint create(String md5sum, FingerprintParams createParams) throws IOException {
90 return new Fingerprint(createParams.build, createParams.fileName, toByteArray(md5sum));
91 }
92
93 protected Fingerprint load(String key) throws IOException {
94 return Fingerprint.load(toByteArray(key));
95 }
96
97 static class FingerprintParams {
98 /**
99 * Null if the build isn't claiming to be the owner.
100 */
101 final Run build;
102 final String fileName;
103
104 public FingerprintParams(Run build, String fileName) {
105 this.build = build;
106 this.fileName = fileName;
107
108 assert fileName!=null;
109 }
110 }
111 }
112