You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EditionPluginDownloader.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins.edition;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.net.URI;
  24. import java.net.URISyntaxException;
  25. import java.nio.file.Files;
  26. import java.nio.file.Path;
  27. import java.util.HashSet;
  28. import java.util.Set;
  29. import org.sonar.api.utils.HttpDownloader;
  30. import org.sonar.api.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. import org.sonar.core.util.FileUtils;
  33. import org.sonar.server.platform.ServerFileSystem;
  34. import org.sonar.updatecenter.common.Release;
  35. import org.sonar.updatecenter.common.UpdateCenter;
  36. import org.sonar.updatecenter.common.Version;
  37. import static org.apache.commons.io.FileUtils.toFile;
  38. import static org.apache.commons.lang.StringUtils.substringAfterLast;
  39. public class EditionPluginDownloader {
  40. private static final Logger LOG = Loggers.get(EditionPluginDownloader.class);
  41. private static final String PLUGIN_EXTENSION = "jar";
  42. private final Path tmpDir;
  43. private final Path downloadDir;
  44. private final HttpDownloader downloader;
  45. public EditionPluginDownloader(HttpDownloader downloader, ServerFileSystem fileSystem) {
  46. this.downloadDir = fileSystem.getEditionDownloadedPluginsDir().toPath();
  47. this.downloader = downloader;
  48. this.tmpDir = downloadDir.resolveSibling(downloadDir.getFileName() + "_tmp");
  49. }
  50. public void downloadEditionPlugins(Set<String> pluginKeys, UpdateCenter updateCenter) {
  51. try {
  52. Set<Release> pluginsToInstall = new HashSet<>();
  53. for (String pluginKey : pluginKeys) {
  54. pluginsToInstall.addAll(updateCenter.findInstallablePlugins(pluginKey, Version.create("")));
  55. }
  56. FileUtils.deleteDirectory(tmpDir);
  57. Files.createDirectories(tmpDir);
  58. for (Release r : pluginsToInstall) {
  59. download(r);
  60. }
  61. FileUtils.deleteDirectory(downloadDir);
  62. Files.move(tmpDir, downloadDir);
  63. } catch (IOException e) {
  64. throw new IllegalStateException(e.getMessage(), e);
  65. } finally {
  66. FileUtils.deleteQuietly(tmpDir);
  67. }
  68. }
  69. protected void download(Release release) {
  70. try {
  71. LOG.info("Downloading plugin: {}", release.getArtifact().getKey());
  72. downloadRelease(release);
  73. } catch (Exception e) {
  74. String message = String.format("Fail to download the plugin (%s, version %s) from %s (error is : %s)",
  75. release.getArtifact().getKey(), release.getVersion().getName(), release.getDownloadUrl(), e.getMessage());
  76. LOG.debug(message, e);
  77. throw new IllegalStateException(message, e);
  78. }
  79. }
  80. private void downloadRelease(Release release) throws URISyntaxException, IOException {
  81. String url = release.getDownloadUrl();
  82. URI uri = new URI(url);
  83. if (url.startsWith("file:")) {
  84. // used for tests
  85. File file = toFile(uri.toURL());
  86. Files.copy(file.toPath(), tmpDir.resolve(file.getName()));
  87. } else {
  88. String filename = substringAfterLast(uri.getPath(), "/");
  89. if (!filename.endsWith("." + PLUGIN_EXTENSION)) {
  90. filename = release.getKey() + "-" + release.getVersion() + "." + PLUGIN_EXTENSION;
  91. }
  92. Path targetFile = tmpDir.resolve(filename);
  93. downloader.download(uri, targetFile.toFile());
  94. }
  95. }
  96. }