diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-12-05 16:22:37 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-12-07 15:40:03 +0100 |
commit | 204f88df32a21b401b4bff3190ee0e3cfb7628a3 (patch) | |
tree | 409e119e64c5ebe5d007064420f539b1ead27a04 /sonar-home/src | |
parent | 181170cd55d3f0bd930e895cc0871c20643ac91a (diff) | |
download | sonarqube-204f88df32a21b401b4bff3190ee0e3cfb7628a3.tar.gz sonarqube-204f88df32a21b401b4bff3190ee0e3cfb7628a3.zip |
SONAR-10140 Compress plugins using pack200
Diffstat (limited to 'sonar-home/src')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileCache.java | 53 |
1 files changed, 43 insertions, 10 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 febb4ef460d..4a03ec24794 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 @@ -19,9 +19,18 @@ */ package org.sonar.home.cache; +import java.io.BufferedInputStream; +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; import javax.annotation.CheckForNull; /** @@ -73,24 +82,48 @@ public class FileCache { void download(String filename, File toFile) throws IOException; } - public File get(String filename, String hash, Downloader downloader) { + public File get(String jarFilename, 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); + File targetFile = new File(hashDir, jarFilename); if (!targetFile.exists()) { - File tempFile = newTempFile(); - download(downloader, filename, tempFile); - String downloadedHash = hashes.of(tempFile); - if (!hash.equals(downloadedHash)) { - throw new IllegalStateException("INVALID HASH: File " + tempFile.getAbsolutePath() + " was expected to have hash " + hash - + " but was downloaded with hash " + downloadedHash); - } + 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(tempFile, targetFile); + renameQuietly(tempJarFile, targetFile); + } return targetFile; } + private static String getPackedFileName(String jarName) { + return jarName.substring(0, jarName.length() - 3) + "pack.gz"; + } + + private static void unpack200(Path tempFile, Path targetFile) { + 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)))) { + unpacker.unpack(in, jarStream); + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + private static void download(Downloader downloader, String filename, File tempFile) { try { downloader.download(filename, tempFile); |