Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PluginFileSystem.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.BufferedInputStream;
  22. import java.io.BufferedOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.OutputStream;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.util.Collection;
  29. import java.util.HashMap;
  30. import java.util.Map;
  31. import java.util.Optional;
  32. import java.util.jar.JarInputStream;
  33. import java.util.jar.Pack200;
  34. import java.util.zip.GZIPOutputStream;
  35. import org.sonar.api.config.Configuration;
  36. import org.sonar.api.server.ServerSide;
  37. import org.sonar.api.utils.log.Logger;
  38. import org.sonar.api.utils.log.Loggers;
  39. import org.sonar.api.utils.log.Profiler;
  40. import org.sonar.core.platform.PluginInfo;
  41. import org.sonar.server.plugins.InstalledPlugin.FileAndMd5;
  42. import static com.google.common.base.Preconditions.checkState;
  43. @ServerSide
  44. public class PluginFileSystem {
  45. public static final String PROPERTY_PLUGIN_COMPRESSION_ENABLE = "sonar.pluginsCompression.enable";
  46. private static final Logger LOG = Loggers.get(PluginFileSystem.class);
  47. private final Configuration configuration;
  48. private final Map<String, InstalledPlugin> installedFiles = new HashMap<>();
  49. public PluginFileSystem(Configuration configuration) {
  50. this.configuration = configuration;
  51. }
  52. /**
  53. * @param plugin
  54. * @param loadedJar the JAR loaded by classloaders. It differs from {@code plugin.getJarFile()}
  55. * which is the initial location of JAR as seen by users
  56. */
  57. public void addInstalledPlugin(PluginInfo plugin, File loadedJar) {
  58. checkState(!installedFiles.containsKey(plugin.getKey()), "Plugin %s is already loaded", plugin.getKey());
  59. checkState(loadedJar.exists(), "loadedJar does not exist: %s", loadedJar);
  60. Optional<File> compressed = compressJar(plugin, loadedJar);
  61. InstalledPlugin installedFile = new InstalledPlugin(
  62. plugin,
  63. new FileAndMd5(loadedJar),
  64. compressed.map(FileAndMd5::new).orElse(null));
  65. installedFiles.put(plugin.getKey(), installedFile);
  66. }
  67. public Optional<InstalledPlugin> getInstalledPlugin(String pluginKey) {
  68. return Optional.ofNullable(installedFiles.get(pluginKey));
  69. }
  70. public Collection<InstalledPlugin> getInstalledFiles() {
  71. return installedFiles.values();
  72. }
  73. private Optional<File> compressJar(PluginInfo plugin, File jar) {
  74. if (!configuration.getBoolean(PROPERTY_PLUGIN_COMPRESSION_ENABLE).orElse(false)) {
  75. return Optional.empty();
  76. }
  77. Path targetPack200 = getPack200Path(jar.toPath());
  78. Path sourcePack200Path = getPack200Path(plugin.getNonNullJarFile().toPath());
  79. // check if packed file was deployed alongside the jar. If that's the case, use it instead of generating it (SONAR-10395).
  80. if (sourcePack200Path.toFile().exists()) {
  81. try {
  82. LOG.debug("Found pack200: " + sourcePack200Path);
  83. Files.copy(sourcePack200Path, targetPack200);
  84. } catch (IOException e) {
  85. throw new IllegalStateException("Failed to copy pack200 file from " + sourcePack200Path + " to " + targetPack200, e);
  86. }
  87. } else {
  88. pack200(jar.toPath(), targetPack200, plugin.getKey());
  89. }
  90. return Optional.of(targetPack200.toFile());
  91. }
  92. private static void pack200(Path jarPath, Path toPack200Path, String pluginKey) {
  93. Profiler profiler = Profiler.create(LOG);
  94. profiler.startInfo("Compressing plugin " + pluginKey + " [pack200]");
  95. try (JarInputStream in = new JarInputStream(new BufferedInputStream(Files.newInputStream(jarPath)));
  96. OutputStream out = new GZIPOutputStream(new BufferedOutputStream(Files.newOutputStream(toPack200Path)))) {
  97. Pack200.newPacker().pack(in, out);
  98. } catch (IOException e) {
  99. throw new IllegalStateException(String.format("Fail to pack200 plugin [%s] '%s' to '%s'", pluginKey, jarPath, toPack200Path), e);
  100. }
  101. profiler.stopInfo();
  102. }
  103. private static Path getPack200Path(Path jar) {
  104. String jarFileName = jar.getFileName().toString();
  105. String filename = jarFileName.substring(0, jarFileName.length() - 3) + "pack.gz";
  106. return jar.resolveSibling(filename);
  107. }
  108. }