]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10395 Improve time compressing plugins in SonarCloud
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 8 Feb 2018 14:33:50 +0000 (15:33 +0100)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 8 Feb 2018 16:00:47 +0000 (17:00 +0100)
server/sonar-server/src/main/java/org/sonar/server/plugins/PluginCompression.java
server/sonar-server/src/main/java/org/sonar/server/plugins/ServerPluginJarExploder.java
server/sonar-server/src/test/java/org/sonar/server/plugins/PluginCompressionTest.java
server/sonar-server/src/test/java/org/sonar/server/plugins/ServerPluginJarExploderTest.java

index e0b3020f3817726668096e2002a948b83faf4b2d..9bb993e5306af8965108499e4a8f892cfd2488ff 100644 (file)
@@ -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);
     }
   }
index 0f92bc6e628b545c74809539afa0014e067aea6e..4a5942084e33517de9951db173e321f284c486f7 100644 (file)
@@ -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) {
index 8939cb2d1562e1c73817a326702cb5ff72089015..99e0d81410fbf0ef20e438395d992aab10d27a9d 100644 (file)
@@ -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);
+  }
+
 }
index fff4600e1bfc27b83469a9dbe261ef4a6cab1007..92ab9ff73fd6c8c2994942d912487554a3218732 100644 (file)
@@ -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());
   }
 }