1 package hudson.model; 2 3 import hudson.Util; 4 5 import java.io.File ; 6 import java.io.IOException ; 7 import java.lang.ref.WeakReference ; 8 import java.util.concurrent.ConcurrentHashMap ; 9 10 20 public final class FingerprintMap { 21 28 private final ConcurrentHashMap <String ,Object > core = new ConcurrentHashMap <String ,Object >(); 29 30 35 private static class Loading { 36 private Fingerprint value; 37 private boolean set; 38 39 public synchronized void set(Fingerprint value) { 40 this.set = true; 41 this.value = value; 42 notifyAll(); 43 } 44 45 49 public synchronized Fingerprint get() { 50 try { 51 while(!set) 52 wait(); 53 return value; 54 } catch (InterruptedException e) { 55 Thread.currentThread().interrupt(); 57 return null; 58 } 59 } 60 } 61 62 65 public boolean isReady() { 66 return new File ( Hudson.getInstance().getRootDir(),"fingerprints").exists(); 67 } 68 69 75 public Fingerprint getOrCreate(AbstractBuild build, String fileName, byte[] md5sum) throws IOException { 76 return getOrCreate(build,fileName, Util.toHexString(md5sum)); 77 } 78 79 public Fingerprint getOrCreate(AbstractBuild build, String fileName, String md5sum) throws IOException { 80 assert build!=null; 81 assert fileName!=null; 82 Fingerprint fp = get(md5sum); 83 if(fp!=null) 84 return fp; 86 fp = new Fingerprint(build,fileName,toByteArray(md5sum)); 91 92 core.put(md5sum,new WeakReference <Fingerprint>(fp)); 93 94 return fp; 95 } 96 97 public synchronized Fingerprint get(String md5sum) throws IOException { 98 if(md5sum.length()!=32) 99 return null; md5sum = md5sum.toLowerCase(); 101 102 while(true) { 103 Object value = core.get(md5sum); 104 105 if(value instanceof WeakReference ) { 106 WeakReference <Fingerprint> wfp = (WeakReference <Fingerprint>) value; 107 Fingerprint fp = wfp.get(); 108 if(fp!=null) 109 return fp; } 111 if(value instanceof Loading) { 112 return ((Loading)value).get(); 114 } 115 116 Loading l = new Loading(); 119 if(value==null ? core.putIfAbsent(md5sum,l)!=null : !core.replace(md5sum,value,l)) { 120 continue; 123 } 124 125 Fingerprint fp = Fingerprint.load(toByteArray(md5sum)); 126 l.set(fp); 128 129 if(fp!=null) 131 core.put(md5sum,new WeakReference <Fingerprint>(fp)); 132 else 133 core.remove(md5sum); 134 135 return fp; 136 } 137 138 } 139 140 private byte[] toByteArray(String md5sum) { 141 byte[] data = new byte[16]; 142 for( int i=0; i<md5sum.length(); i+=2 ) 143 data[i/2] = (byte)Integer.parseInt(md5sum.substring(i,i+2),16); 144 return data; 145 } 146 147 } 148 | Popular Tags |