diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-12-07 11:38:28 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-12-07 15:40:03 +0100 |
commit | 0001182a007e70261d447023f1fa5ad5e1a0b69f (patch) | |
tree | eaae3a5bb2cf5f54a68ca42ac744494d0ead5908 /sonar-home/src/main | |
parent | 204f88df32a21b401b4bff3190ee0e3cfb7628a3 (diff) | |
download | sonarqube-0001182a007e70261d447023f1fa5ad5e1a0b69f.tar.gz sonarqube-0001182a007e70261d447023f1fa5ad5e1a0b69f.zip |
SONAR-10142 Download compressed plugins
Diffstat (limited to 'sonar-home/src/main')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileCache.java | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java index 4a03ec24794..e3cbafd22de 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java @@ -24,10 +24,8 @@ import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.jar.JarOutputStream; import java.util.jar.Pack200; import java.util.zip.GZIPInputStream; @@ -43,7 +41,7 @@ public class FileCache { /** Maximum loop count when creating temp directories. */ private static final int TEMP_DIR_ATTEMPTS = 10_000; - private final File dir; + private final File cacheDir; private final File tmpDir; private final FileHashes hashes; private final Logger logger; @@ -51,7 +49,7 @@ public class FileCache { FileCache(File dir, FileHashes fileHashes, Logger logger) { this.hashes = fileHashes; this.logger = logger; - this.dir = createDir(dir, "user cache: "); + this.cacheDir = createDir(dir, "user cache: "); logger.info(String.format("User cache: %s", dir.getAbsolutePath())); this.tmpDir = createDir(new File(dir, "_tmp"), "temp dir"); } @@ -61,7 +59,7 @@ public class FileCache { } public File getDir() { - return dir; + return cacheDir; } /** @@ -70,7 +68,7 @@ public class FileCache { */ @CheckForNull public File get(String filename, String hash) { - File cachedFile = new File(new File(dir, hash), filename); + File cachedFile = new File(new File(cacheDir, hash), filename); if (cachedFile.exists()) { return cachedFile; } @@ -82,41 +80,54 @@ public class FileCache { void download(String filename, File toFile) throws IOException; } - public File get(String jarFilename, String hash, Downloader downloader) { + 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, jarFilename); + File targetFile = new File(hashDir, filename); if (!targetFile.exists()) { - File tempPackedFile = newTempFile(); - File tempJarFile = newTempFile(); - String packedFileName = getPackedFileName(jarFilename); - download(downloader, packedFileName, tempPackedFile); - - logger.debug("Unpacking plugin " + jarFilename); - - unpack200(tempPackedFile.toPath(), tempJarFile.toPath()); - logger.debug("Done"); - String downloadedHash = hashes.of(tempJarFile); - // if (!hash.equals(downloadedHash)) { - // throw new IllegalStateException("INVALID HASH: File " + tempJarFile.getAbsolutePath() + " was expected to have hash " + hash - // + " but was downloaded with hash " + downloadedHash); - // } - mkdirQuietly(hashDir); - renameQuietly(tempJarFile, targetFile); - + cacheMiss(targetFile, hash, downloader); } return targetFile; } - private static String getPackedFileName(String jarName) { - return jarName.substring(0, jarName.length() - 3) + "pack.gz"; + 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 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; + } + + private static String getUnpackedFileName(String packedName) { + return packedName.substring(0, packedName.length() - 7) + "jar"; } - private static void unpack200(Path tempFile, Path targetFile) { + private void unpack200(Path compressedFile, Path jarFile) { + logger.debug("Unpacking plugin " + compressedFile); Pack200.Unpacker unpacker = Pack200.newUnpacker(); try { - try (JarOutputStream jarStream = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(targetFile))); - InputStream in = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(tempFile)))) { + try (JarOutputStream jarStream = new JarOutputStream(new BufferedOutputStream(Files.newOutputStream(jarFile))); + InputStream in = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(compressedFile)))) { unpacker.unpack(in, jarStream); } } catch (IOException e) { @@ -147,7 +158,7 @@ public class FileCache { } private File hashDir(String hash) { - return new File(dir, hash); + return new File(cacheDir, hash); } private static void mkdirQuietly(File hashDir) { |