public static FileCache create(File dir, Logger logger) { return new FileCache(dir, new FileHashes(), logger); }
@Override public String load(String id) throws IOException { return cache.getString(id); } };
@Override public InputStream load(String id) throws IOException { return cache.getStream(id); } };
public FileCache provide(Settings settings) { if (cache == null) { String home = settings.getString("sonar.userHome"); cache = new FileCacheBuilder().setLog(new Slf4jLog(FileCache.class)).setUserHome(home).build(); } return cache; } }
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 provide(Settings settings) { if (cache == null) { String home = settings.getString("sonar.userHome"); cache = new FileCacheBuilder(new Slf4jLogger()).setUserHome(home).build(); } return cache; } }
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); }
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; }
/** * Computes the hash of given stream. The stream is closed by this method. */ public String of(InputStream input) { try(InputStream is = input) { MessageDigest digest = MessageDigest.getInstance("MD5"); byte[] hash = digest(is, digest); return toHex(hash); } catch (Exception e) { throw new IllegalStateException("Fail to compute hash", e); } }
@Override protected File extractPluginDependencies(File pluginFile, File pluginBasedir) throws IOException { return cache.unzip(pluginFile); }
@Override public void stop() { lock.unlock(); }
private static void download(Downloader downloader, String filename, File tempFile) { try { downloader.download(filename, tempFile); } catch (IOException e) { throw new IllegalStateException("Fail to download " + filename + " to " + tempFile, e); } }
private Path getStatusFilePath() { return cache.getDirectory().resolve(STATUS_FILENAME); } }
public String of(File file) { try { return of(new FileInputStream(file)); } catch (IOException e) { throw new IllegalStateException("Fail to compute hash of: " + file.getAbsolutePath(), e); } }
private File createDir(File dir, String debugTitle) { if (!dir.isDirectory() || !dir.exists()) { logger.debug("Create : " + dir.getAbsolutePath()); try { Files.createDirectories(dir.toPath()); } catch (IOException e) { throw new IllegalStateException("Unable to create " + debugTitle + dir.getAbsolutePath(), e); } } return dir; } }
public void tryLock() { try { if (!lock.tryLock()) { failAlreadyInProgress(null); } } catch (OverlappingFileLockException e) { failAlreadyInProgress(e); } }
public WSLoader provide(AnalysisMode mode, PersistentCache cache, BatchWsClient client, AnalysisProperties props) { if (wsLoader == null) { // recreate cache directory if needed for this analysis cache.reconfigure(); wsLoader = new WSLoader(getStrategy(mode, props), cache, client); } return wsLoader; }
@Override public void delete() { cache.clear(); deleteQuietly(getStatusFilePath().toFile()); }