1 7 package com.inversoft.savant; 8 9 10 import java.io.BufferedInputStream ; 11 import java.io.BufferedOutputStream ; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileNotFoundException ; 15 import java.io.FileOutputStream ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.security.DigestInputStream ; 19 import java.security.MessageDigest ; 20 import java.security.NoSuchAlgorithmException ; 21 import java.util.Arrays ; 22 import java.util.Date ; 23 24 import com.inversoft.savant.log.Log; 25 26 27 49 public class LocalCacheStore { 50 51 private File localCache; 52 53 54 61 public LocalCacheStore(File localCache) throws SavantException { 62 if (localCache == null) { 63 String localCacheDir = System.getProperty("savant.local.repository"); 64 if (localCacheDir == null) { 65 localCacheDir = System.getProperty("user.home") + File.separator + 66 ".savant_repository"; 67 } 68 69 localCache = new File (localCacheDir); 70 } 71 72 this.localCache = localCache; 73 if (!localCache.exists()) { 74 localCache.mkdir(); 75 } else if (localCache.isFile() || !localCache.canWrite() || 76 !localCache.canRead()) { 77 throw new SavantException("Local cache directory [" + 78 localCache.getAbsolutePath() + " is invalid"); 79 } 80 } 81 82 87 public File getLocation() { 88 return localCache; 89 } 90 91 101 public boolean resolveArtifactDependencies(Artifact artifact) 102 throws SavantException { 103 String artDepsFile = artifact.getArtifactDepsFile(); 104 File file = new File (localCache, artDepsFile); 105 if (!file.exists() || !file.isFile()) { 106 return false; 107 } 108 109 ArtifactTools.resolveArtifactDependencies(artifact, file); 110 return true; 111 } 112 113 121 public File find(Artifact artifact) { 122 String artifactFile = artifact.getArtifactFile(); 123 File file = new File (localCache, artifactFile); 124 if (!file.exists() || !file.isFile()) { 125 file = null; 126 } else { 127 128 long curTimeMins = System.currentTimeMillis() / 60000; 130 int expiry = artifact.getExpireminutes(); 131 if (expiry > 0) { 132 long time = file.lastModified() / 60000; 133 if ((time + expiry) < curTimeMins) { 134 file = null; 135 } 136 } 137 138 long curTime = System.currentTimeMillis(); 139 Date expiryDate = artifact.getExpiretime(); 140 if (expiryDate != null && curTime > expiryDate.getTime()) { 141 file = null; 142 } 143 } 144 145 return file; 146 } 147 148 157 public File store(Artifact artifact, File file) throws SavantException { 158 try { 159 FileInputStream stream = new FileInputStream (file); 160 File cached = store(artifact, stream, null, true); 161 stream.close(); 162 return cached; 163 } catch (FileNotFoundException fnfe) { 164 throw new SavantException(fnfe); 165 } catch (IOException ioe) { 166 throw new SavantException(ioe); 167 } 168 } 169 170 190 public File store(Artifact artifact, InputStream stream, byte[] remoteMD5, 191 boolean failonmd5) 192 throws SavantException { 193 String artifactFile = artifact.getArtifactFile(); 194 File file = new File (localCache, artifactFile); 195 construct(file); 196 197 try { 198 MessageDigest digest = null; 200 try { 201 digest = MessageDigest.getInstance("MD5"); 202 digest.reset(); 203 } catch (NoSuchAlgorithmException e) { 204 System.err.println("Unable to locate MD5 algorithm"); 205 System.exit(1); 206 } 207 208 FileOutputStream os = new FileOutputStream (file); 209 BufferedOutputStream bos = new BufferedOutputStream (os); 210 BufferedInputStream bis = new BufferedInputStream (stream); 211 DigestInputStream dis = new DigestInputStream (bis, digest); 212 dis.on(true); 213 214 byte[] ba = new byte[1024]; 215 int count; 216 while ((count = dis.read(ba, 0, 1024)) != -1) { 217 bos.write(ba, 0, count); 218 } 219 220 dis.close(); 221 bos.close(); 222 bis.close(); 223 os.close(); 224 225 byte[] localMD5 = digest.digest(); 226 if (remoteMD5 != null && localMD5 != null && 227 !Arrays.equals(localMD5, remoteMD5)) { 228 229 if (failonmd5) { 230 file.delete(); 231 throw new SavantException("Downloaded artifact [" + artifact + 232 "] corrupt according to MD5. Attempt rebuilding when remote" + 233 " repository is fixed"); 234 } else { 235 Log.log("Downloaded artifact [" + artifact + "] corrupt" + 236 " according to MD5.", Log.WARN); 237 } 238 } 239 } catch (FileNotFoundException fnfe) { 240 throw new SavantException(fnfe); 241 } catch (IOException ioe) { 242 throw new SavantException(ioe); 243 } 244 245 return file; 246 } 247 248 256 public void storeDeps(Artifact artifact, File file) throws SavantException { 257 try { 258 File cached = new File (localCache, artifact.getArtifactDepsFile()); 259 FileInputStream fis = new FileInputStream (file); 260 FileTools.output(fis, cached); 261 } catch (IOException ioe) { 262 throw new SavantException(ioe); 263 } 264 } 265 266 273 public boolean delete(Artifact artifact) { 274 File file = new File (localCache, artifact.getArtifactFile()); 275 boolean deleted = false; 276 if (file.exists()) { 277 deleted = file.delete(); 278 } 279 280 return deleted; 281 } 282 283 288 private void construct(File file) throws SavantException { 289 290 if (file.exists() && file.isDirectory()) { 291 throw new SavantException("ArtifactType location is a directory [" + 292 file.getAbsolutePath() +"]"); 293 } 294 295 if (file.exists() && file.isFile()) { 296 file.delete(); 297 } else if (!file.exists()) { 298 File dir = file.getParentFile(); 299 dir.mkdirs(); 300 } 301 302 try { 303 file.createNewFile(); 304 } catch (IOException ioe) { 305 throw new SavantException(ioe); 306 } 307 } 308 }
| Popular Tags
|