Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PluginUninstaller.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.Collection;
  24. import java.util.Collections;
  25. import org.apache.commons.io.FileUtils;
  26. import org.picocontainer.Startable;
  27. import org.sonar.core.platform.PluginInfo;
  28. import org.sonar.core.util.stream.MoreCollectors;
  29. import org.sonar.server.platform.ServerFileSystem;
  30. import static java.lang.String.format;
  31. import static org.apache.commons.io.FileUtils.forceMkdir;
  32. public class PluginUninstaller implements Startable {
  33. private static final String PLUGIN_EXTENSION = "jar";
  34. private final ServerPluginRepository serverPluginRepository;
  35. private final File uninstallDir;
  36. public PluginUninstaller(ServerPluginRepository serverPluginRepository, ServerFileSystem fs) {
  37. this.serverPluginRepository = serverPluginRepository;
  38. this.uninstallDir = fs.getUninstalledPluginsDir();
  39. }
  40. private static Collection<File> listJarFiles(File dir) {
  41. if (dir.exists()) {
  42. return FileUtils.listFiles(dir, new String[] {PLUGIN_EXTENSION}, false);
  43. }
  44. return Collections.emptyList();
  45. }
  46. @Override
  47. public void start() {
  48. try {
  49. forceMkdir(uninstallDir);
  50. } catch (IOException e) {
  51. throw new IllegalStateException("Fail to create the directory: " + uninstallDir, e);
  52. }
  53. }
  54. @Override
  55. public void stop() {
  56. // Nothing to do
  57. }
  58. public void uninstall(String pluginKey) {
  59. ensurePluginIsInstalled(pluginKey);
  60. serverPluginRepository.uninstall(pluginKey, uninstallDir);
  61. }
  62. public void cancelUninstalls() {
  63. serverPluginRepository.cancelUninstalls(uninstallDir);
  64. }
  65. /**
  66. * @return the list of plugins to be uninstalled as {@link PluginInfo} instances
  67. */
  68. public Collection<PluginInfo> getUninstalledPlugins() {
  69. return listJarFiles(uninstallDir)
  70. .stream()
  71. .map(PluginInfo::create)
  72. .collect(MoreCollectors.toList());
  73. }
  74. private void ensurePluginIsInstalled(String key) {
  75. if (!serverPluginRepository.hasPlugin(key)) {
  76. throw new IllegalArgumentException(format("Plugin [%s] is not installed", key));
  77. }
  78. }
  79. }