]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9684 Use api/plugins/installed to load plugins
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 7 Aug 2017 14:22:59 +0000 (16:22 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 7 Sep 2017 06:33:31 +0000 (08:33 +0200)
sonar-core/src/main/java/org/sonar/core/platform/RemotePlugin.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java
sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json [new file with mode: 0644]

index 3ee0b721faf1c166ffce397177376c12d1a17223..714322a64de041e4806ff671dd4b617f26c92c68 100644 (file)
@@ -25,6 +25,11 @@ import java.io.IOException;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.lang.StringUtils;
 
+/**
+ * @deprecated since 6.6 Used for deprecated deploy/plugin/index.txt
+ *
+ */
+@Deprecated
 public class RemotePlugin {
   private String pluginKey;
   private boolean sonarLintSupported;
index 4b0db1f328e24e88173ea06826ca74def4cd4b3b..a8f40fdb6e1fe44011fb05acf272b4f08ca02f0d 100644 (file)
 package org.sonar.scanner.bootstrap;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.gson.Gson;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.CharUtils;
-import org.apache.commons.lang.StringUtils;
 import org.sonar.api.Plugin;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.utils.log.Profiler;
 import org.sonar.core.platform.PluginInfo;
-import org.sonar.core.platform.RemotePlugin;
-import org.sonar.core.platform.RemotePluginFile;
 import org.sonar.home.cache.FileCache;
 import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.WsResponse;
@@ -53,7 +47,7 @@ import static java.lang.String.format;
 public class ScannerPluginInstaller implements PluginInstaller {
 
   private static final Logger LOG = Loggers.get(ScannerPluginInstaller.class);
-  private static final String PLUGINS_INDEX_URL = "/deploy/plugins/index.txt";
+  private static final String PLUGINS_WS_URL = "/api/plugins/installed";
 
   private final FileCache fileCache;
   private final ScannerPluginPredicate pluginPredicate;
@@ -67,17 +61,17 @@ public class ScannerPluginInstaller implements PluginInstaller {
 
   @Override
   public Map<String, PluginInfo> installRemotes() {
-    return loadPlugins(listRemotePlugins());
+    return loadPlugins(listInstalledPlugins());
   }
 
-  private Map<String, PluginInfo> loadPlugins(List<RemotePlugin> remotePlugins) {
-    Map<String, PluginInfo> infosByKey = new HashMap<>(remotePlugins.size());
+  private Map<String, PluginInfo> loadPlugins(InstalledPlugin[] remotePlugins) {
+    Map<String, PluginInfo> infosByKey = new HashMap<>(remotePlugins.length);
 
     Profiler profiler = Profiler.create(LOG).startDebug("Load plugins");
 
-    for (RemotePlugin remotePlugin : remotePlugins) {
-      if (pluginPredicate.apply(remotePlugin.getKey())) {
-        File jarFile = download(remotePlugin);
+    for (InstalledPlugin installedPlugin : remotePlugins) {
+      if (pluginPredicate.apply(installedPlugin.key)) {
+        File jarFile = download(installedPlugin);
         PluginInfo info = PluginInfo.create(jarFile);
         infosByKey.put(info.getKey(), info);
       }
@@ -97,12 +91,11 @@ public class ScannerPluginInstaller implements PluginInstaller {
   }
 
   @VisibleForTesting
-  File download(final RemotePlugin remote) {
+  File download(final InstalledPlugin remote) {
     try {
-      final RemotePluginFile file = remote.file();
-      return fileCache.get(file.getFilename(), file.getHash(), new FileDownloader(remote.getKey()));
+      return fileCache.get(remote.filename, remote.hash, new FileDownloader(remote.key));
     } catch (Exception e) {
-      throw new IllegalStateException("Fail to download plugin: " + remote.getKey(), e);
+      throw new IllegalStateException("Fail to download plugin: " + remote.key, e);
     }
   }
 
@@ -110,33 +103,28 @@ public class ScannerPluginInstaller implements PluginInstaller {
    * Gets information about the plugins installed on server (filename, checksum)
    */
   @VisibleForTesting
-  List<RemotePlugin> listRemotePlugins() {
-    try {
-      String pluginIndex = loadPluginIndex();
-      String[] rows = StringUtils.split(pluginIndex, CharUtils.LF);
-      List<RemotePlugin> result = new ArrayList<>();
-      for (String row : rows) {
-        result.add(RemotePlugin.unmarshal(row));
-      }
-      return result;
-
-    } catch (Exception e) {
-      throw new IllegalStateException("Fail to load plugin index: " + PLUGINS_INDEX_URL, e);
-    }
-  }
-
-  private String loadPluginIndex() {
+  InstalledPlugin[] listInstalledPlugins() {
     Profiler profiler = Profiler.create(LOG).startInfo("Load plugins index");
-    GetRequest getRequest = new GetRequest(PLUGINS_INDEX_URL);
-    String str;
+    GetRequest getRequest = new GetRequest(PLUGINS_WS_URL);
+    InstalledPlugins installedPlugins;
     try (Reader reader = wsClient.call(getRequest).contentReader()) {
-      str = IOUtils.toString(reader);
+      installedPlugins = new Gson().fromJson(reader, InstalledPlugins.class);
     } catch (IOException e) {
       throw new IllegalStateException(e);
     }
 
     profiler.stopInfo();
-    return str;
+    return installedPlugins.plugins;
+  }
+
+  private static class InstalledPlugins {
+    InstalledPlugin[] plugins;
+  }
+
+  static class InstalledPlugin {
+    String key;
+    String hash;
+    String filename;
   }
 
   private class FileDownloader implements FileCache.Downloader {
index ebbfecb79d72d36fa31ad3f3a71b8d37e9b103f6..580ccd31c21e54f7789c5b0d9f6705de34a72686 100644 (file)
 package org.sonar.scanner.bootstrap;
 
 import java.io.File;
-import java.io.StringReader;
-import java.util.List;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
-import org.sonar.core.platform.RemotePlugin;
 import org.sonar.home.cache.FileCache;
 import org.sonar.scanner.WsTestUtil;
+import org.sonar.scanner.bootstrap.ScannerPluginInstaller.InstalledPlugin;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
@@ -56,11 +56,12 @@ public class ScannerPluginInstallerTest {
 
   @Test
   public void listRemotePlugins() {
-    WsTestUtil.mockReader(wsClient, "/deploy/plugins/index.txt", new StringReader("checkstyle\nsqale"));
+    WsTestUtil.mockReader(wsClient, "/api/plugins/installed",
+      new InputStreamReader(this.getClass().getResourceAsStream("ScannerPluginInstallerTest/installed-plugins-ws.json"), StandardCharsets.UTF_8));
     ScannerPluginInstaller underTest = new ScannerPluginInstaller(wsClient, fileCache, pluginPredicate);
 
-    List<RemotePlugin> remotePlugins = underTest.listRemotePlugins();
-    assertThat(remotePlugins).extracting("key").containsOnly("checkstyle", "sqale");
+    InstalledPlugin[] remotePlugins = underTest.listInstalledPlugins();
+    assertThat(remotePlugins).extracting("key").containsOnly("scmgit", "java", "scmsvn");
   }
 
   @Test
@@ -70,7 +71,10 @@ public class ScannerPluginInstallerTest {
 
     ScannerPluginInstaller underTest = new ScannerPluginInstaller(wsClient, fileCache, pluginPredicate);
 
-    RemotePlugin remote = new RemotePlugin("checkstyle").setFile("checkstyle-plugin.jar", "fakemd5_1");
+    InstalledPlugin remote = new InstalledPlugin();
+    remote.key = "checkstyle";
+    remote.filename = "checkstyle-plugin.jar";
+    remote.hash = "fakemd5_1";
     File file = underTest.download(remote);
 
     assertThat(file).isEqualTo(pluginJar);
@@ -78,7 +82,7 @@ public class ScannerPluginInstallerTest {
 
   @Test
   public void should_fail_to_get_plugin_index() {
-    WsTestUtil.mockException(wsClient, "/deploy/plugins/index.txt", new IllegalStateException());
+    WsTestUtil.mockException(wsClient, "/api/plugins/installed", new IllegalStateException());
     thrown.expect(IllegalStateException.class);
 
     new ScannerPluginInstaller(wsClient, fileCache, pluginPredicate).installRemotes();
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json
new file mode 100644 (file)
index 0000000..2e0c49b
--- /dev/null
@@ -0,0 +1,52 @@
+{
+  "plugins": [
+    {
+      "key": "scmgit",
+      "name": "Git",
+      "description": "Git SCM Provider.",
+      "version": "1.0",
+      "license": "GNU LGPL 3",
+      "organizationName": "SonarSource",
+      "organizationUrl": "http://www.sonarsource.com",
+      "homepageUrl": "https://redirect.sonarsource.com/plugins/scmgit.html",
+      "issueTrackerUrl": "http://jira.sonarsource.com/browse/SONARSCGIT",
+      "implementationBuild": "9ce9d330c313c296fab051317cc5ad4b26319e07",
+      "filename": "sonar-scm-git-plugin-1.0.jar",
+      "hash": "abcdef123456",
+      "sonarLintSupported": false,
+      "updatedAt": 123456789
+    },
+    {
+      "key": "java",
+      "name": "Java",
+      "description": "SonarQube rule engine.",
+      "version": "3.0",
+      "license": "GNU LGPL 3",
+      "organizationName": "SonarSource",
+      "organizationUrl": "http://www.sonarsource.com",
+      "homepageUrl": "https://redirect.sonarsource.com/plugins/java.html",
+      "issueTrackerUrl": "http://jira.sonarsource.com/browse/SONARJAVA",
+      "implementationBuild": "65396a609ddface8b311a6a665aca92a7da694f1",
+      "filename": "sonar-java-plugin-3.0.jar",
+      "hash": "abcdef123456",
+      "sonarLintSupported": true,
+      "updatedAt": 123456789
+    },
+    {
+      "key": "scmsvn",
+      "name": "SVN",
+      "description": "SVN SCM Provider.",
+      "version": "1.0",
+      "license": "GNU LGPL 3",
+      "organizationName": "SonarSource",
+      "organizationUrl": "http://www.sonarsource.com",
+      "homepageUrl": "https://redirect.sonarsource.com/plugins/scmsvn.html",
+      "issueTrackerUrl": "http://jira.sonarsource.com/browse/SONARSCSVN",
+      "implementationBuild": "213fc8a8b582ff530b12dd4a59a6512be1071234",
+      "filename": "sonar-scm-svn-plugin-1.0.jar",
+      "hash": "abcdef123456",
+      "sonarLintSupported": false,
+      "updatedAt": 123456789
+    }
+  ]
+}
\ No newline at end of file