public File getCompressed(String filename, String hash, Downloader downloader) { File hashDir = hashDir(hash); File compressedFile = new File(hashDir, filename); File jarFile = new File(compressedFile.getParentFile(), getUnpackedFileName(compressedFile.getName())); if (!jarFile.exists()) { if (!compressedFile.exists()) { cacheMiss(compressedFile, hash, downloader); } File tempFile = newTempFile(); unpack200(compressedFile.toPath(), tempFile.toPath()); renameQuietly(tempFile, jarFile); } return jarFile; }
public FileCache build() { if (userHome == null) { userHome = findHome(); } File cacheDir = new File(userHome, "cache"); return FileCache.create(cacheDir, logger); }
public File get(String filename, String hash, Downloader downloader) { // Does not fail if another process tries to create the directory at the same time. File hashDir = hashDir(hash); File targetFile = new File(hashDir, filename); if (!targetFile.exists()) { cacheMiss(targetFile, hash, downloader); } return targetFile; }
private void cacheMiss(File targetFile, String expectedHash, Downloader downloader) { File tempFile = newTempFile(); download(downloader, targetFile.getName(), tempFile); String downloadedHash = hashes.of(tempFile); if (!expectedHash.equals(downloadedHash)) { throw new IllegalStateException("INVALID HASH: File " + tempFile.getAbsolutePath() + " was expected to have hash " + expectedHash + " but was downloaded with hash " + downloadedHash); } mkdirQuietly(targetFile.getParentFile()); renameQuietly(tempFile, targetFile); }
@VisibleForTesting File download(final RemotePlugin remote) { try { final RemotePluginFile file = remote.file(); return fileCache.get(file.getFilename(), file.getHash(), new FileDownloader(remote.getKey())); } catch (Exception e) { throw new IllegalStateException("Fail to download plugin: " + remote.getKey(), e); } }
public static FileCache create(File dir, Logger logger) { return new FileCache(dir, new FileHashes(), logger); }
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = fileCache.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }
@Override public File pluginFile(final RemotePlugin remote) { try { final RemotePluginFile file = remote.file(); return fileCache.get(file.getFilename(), file.getHash(), new FileCache.Downloader() { @Override public void download(String filename, File toFile) throws IOException { String url = "/deploy/plugins/" + remote.getKey() + "/" + file.getFilename(); if (LOG.isDebugEnabled()) { LOG.debug("Download {} to {}", url, toFile.getAbsolutePath()); } else { LOG.info("Download {}", file.getFilename()); } server.download(url, toFile); } }); } catch (Exception e) { throw new IllegalStateException("Fail to download plugin: " + remote.getKey(), e); } }
public void start() { if (!analysisMode.isPreview()) { try { LOG.info("Install JDBC driver"); String[] nameAndHash = downloadJdbcDriverIndex(); if (nameAndHash.length > 0) { String filename = nameAndHash[0]; String hash = nameAndHash[1]; File jdbcDriver = fileCache.get(filename, hash, new FileCache.Downloader() { @Override public void download(String filename, File toFile) throws IOException { String url = "/deploy/" + filename; if (LOG.isDebugEnabled()) { LOG.debug("Download {} to {}", url, toFile.getAbsolutePath()); } else { LOG.info("Download {}", filename); } serverClient.download(url, toFile); } }); classLoader = initClassloader(jdbcDriver); } } catch (SonarException e) { throw e; } catch (Exception e) { throw new SonarException("Fail to install JDBC driver", e); } } }