From 198f987a5e79d2816a9d7172112aff1dfc669bfd Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 8 Feb 2018 15:33:50 +0100 Subject: [PATCH] SONAR-10395 Improve time compressing plugins in SonarCloud --- .../server/plugins/PluginCompression.java | 23 +++++++++--- .../plugins/ServerPluginJarExploder.java | 2 +- .../server/plugins/PluginCompressionTest.java | 35 +++++++++++++++---- .../plugins/ServerPluginJarExploderTest.java | 2 +- 4 files changed, 49 insertions(+), 13 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginCompression.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginCompression.java index e0b3020f381..9bb993e5306 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginCompression.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginCompression.java @@ -50,12 +50,25 @@ public class PluginCompression { this.configuration = configuration; } - public void compressJar(String pluginKey, Path jarFile) { + public void compressJar(String pluginKey, Path sourceDir, Path targetJarFile) { if (configuration.getBoolean(PROPERTY_PLUGIN_COMPRESSION_ENABLE).orElse(false)) { - Path pack200Path = FileUtils.getPack200FilePath(jarFile); - pack200(jarFile, pack200Path, pluginKey); - String hash = calculateMd5(pack200Path); - RemotePluginFile compressedPlugin = new RemotePluginFile(pack200Path.getFileName().toString(), hash); + Path targetPack200Path = FileUtils.getPack200FilePath(targetJarFile); + Path sourcePack200Path = sourceDir.resolve(targetPack200Path.getFileName()); + + // check if packed file was deployed alongside the jar. If that's the case, use it instead of generating it (SONAR-10395). + if (Files.isRegularFile(sourcePack200Path)) { + try { + LOG.debug("Found pack200: " + sourcePack200Path); + Files.copy(sourcePack200Path, targetPack200Path); + } catch (IOException e) { + throw new IllegalStateException("Failed to copy pack200 file from " + sourcePack200Path + " to " + targetPack200Path, e); + } + } else { + pack200(targetJarFile, targetPack200Path, pluginKey); + } + + String hash = calculateMd5(targetPack200Path); + RemotePluginFile compressedPlugin = new RemotePluginFile(targetPack200Path.getFileName().toString(), hash); compressedPlugins.put(pluginKey, compressedPlugin); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java index 0f92bc6e628..4a5942084e3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java @@ -58,7 +58,7 @@ public class ServerPluginJarExploder extends PluginJarExploder { File jarTarget = new File(toDir, jarSource.getName()); FileUtils.copyFile(jarSource, jarTarget); - pluginCompression.compressJar(pluginInfo.getKey(), jarTarget.toPath()); + pluginCompression.compressJar(pluginInfo.getKey(), jarSource.toPath().getParent(), jarTarget.toPath()); ZipUtils.unzip(jarSource, toDir, newLibFilter()); return explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); } catch (Exception e) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/PluginCompressionTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/PluginCompressionTest.java index 8939cb2d156..99e0d81410f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/PluginCompressionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/PluginCompressionTest.java @@ -35,21 +35,26 @@ public class PluginCompressionTest { public TemporaryFolder temp = new TemporaryFolder(); private MapSettings settings = new MapSettings(); - private Path jarPath; + private Path targetJarPath; + private Path targetFolder; + private Path sourceFolder; private PluginCompression underTest; @Before public void setUp() throws IOException { - jarPath = temp.newFile("test.jar").toPath(); + sourceFolder = temp.newFolder("source").toPath(); + targetFolder = temp.newFolder("target").toPath(); + targetJarPath = targetFolder.resolve("test.jar"); + Files.createFile(targetJarPath); } @Test public void disable_if_proparty_not_set() throws IOException { underTest = new PluginCompression(settings.asConfig()); - underTest.compressJar("key", jarPath); + underTest.compressJar("key", sourceFolder, targetJarPath); - assertThat(Files.list(jarPath.getParent())).containsOnly(jarPath); + assertThat(Files.list(targetFolder)).containsOnly(targetJarPath); assertThat(underTest.getPlugins()).isEmpty(); } @@ -57,10 +62,28 @@ public class PluginCompressionTest { public void should_compress_plugin() throws IOException { settings.setProperty(PluginCompression.PROPERTY_PLUGIN_COMPRESSION_ENABLE, true); underTest = new PluginCompression(settings.asConfig()); - underTest.compressJar("key", jarPath); + underTest.compressJar("key", targetFolder, targetJarPath); - assertThat(Files.list(jarPath.getParent())).containsOnly(jarPath, jarPath.getParent().resolve("test.pack.gz")); + assertThat(Files.list(targetFolder)).containsOnly(targetJarPath, targetFolder.resolve("test.pack.gz")); assertThat(underTest.getPlugins()).hasSize(1); assertThat(underTest.getPlugins().get("key").getFilename()).isEqualTo("test.pack.gz"); } + + @Test + public void should_use_deployed_packed_file() throws IOException { + Path packedPath = sourceFolder.resolve("test.pack.gz"); + Files.write(packedPath, new byte[] {1, 2, 3}); + + settings.setProperty(PluginCompression.PROPERTY_PLUGIN_COMPRESSION_ENABLE, true); + underTest = new PluginCompression(settings.asConfig()); + underTest.compressJar("key", sourceFolder, targetJarPath); + + assertThat(Files.list(targetFolder)).containsOnly(targetJarPath, targetFolder.resolve("test.pack.gz")); + assertThat(underTest.getPlugins()).hasSize(1); + assertThat(underTest.getPlugins().get("key").getFilename()).isEqualTo("test.pack.gz"); + + // check that the file was copied, not generated + assertThat(targetFolder.resolve("test.pack.gz")).hasSameContentAs(packedPath); + } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginJarExploderTest.java b/server/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginJarExploderTest.java index fff4600e1bf..92ab9ff73fd 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginJarExploderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginJarExploderTest.java @@ -61,6 +61,6 @@ public class ServerPluginJarExploderTest { assertThat(lib).exists().isFile(); assertThat(lib.getCanonicalPath()).startsWith(pluginDeployDir.getCanonicalPath()); } - verify(pluginCompression).compressJar(info.getKey(), exploded.getMain().toPath()); + verify(pluginCompression).compressJar(info.getKey(), jar.toPath().getParent(), exploded.getMain().toPath()); } } -- 2.39.5