diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-07 16:22:59 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-09-07 08:33:31 +0200 |
commit | 82b2e9e5c327119ede2ee66cd63c65722b40409c (patch) | |
tree | 51b4f092485d8398d75503e9c31c40ceccea89f8 /sonar-scanner-engine/src | |
parent | 5312732c79afcddb26b81e6be4278279f186cc22 (diff) | |
download | sonarqube-82b2e9e5c327119ede2ee66cd63c65722b40409c.tar.gz sonarqube-82b2e9e5c327119ede2ee66cd63c65722b40409c.zip |
SONAR-9684 Use api/plugins/installed to load plugins
Diffstat (limited to 'sonar-scanner-engine/src')
3 files changed, 90 insertions, 46 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java index 4b0db1f328e..a8f40fdb6e1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerPluginInstaller.java @@ -20,26 +20,20 @@ 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 { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java index ebbfecb79d7..580ccd31c21 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest.java @@ -20,16 +20,16 @@ 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 index 00000000000..2e0c49bf832 --- /dev/null +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/bootstrap/ScannerPluginInstallerTest/installed-plugins-ws.json @@ -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 |