Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RailsAppsDeployer.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.platform;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.base.Function;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.picocontainer.Startable;
  26. import org.sonar.api.Plugin;
  27. import org.sonar.api.platform.ServerFileSystem;
  28. import org.sonar.api.utils.log.Logger;
  29. import org.sonar.api.utils.log.Loggers;
  30. import org.sonar.core.platform.PluginInfo;
  31. import org.sonar.core.platform.PluginRepository;
  32. import javax.annotation.Nullable;
  33. import java.io.File;
  34. import java.io.IOException;
  35. /**
  36. * Ruby on Rails requires the files to be on filesystem but not in Java classpath (JAR). This component extracts
  37. * all the needed files from plugins and copy them to $SONAR_HOME/temp
  38. *
  39. * @since 3.0
  40. */
  41. public class RailsAppsDeployer implements Startable {
  42. private static final Logger LOG = Loggers.get(RailsAppsDeployer.class);
  43. private static final String ROR_PATH = "org/sonar/ror/";
  44. private final ServerFileSystem fs;
  45. private final PluginRepository pluginRepository;
  46. public RailsAppsDeployer(ServerFileSystem fs, PluginRepository pluginRepository) {
  47. this.fs = fs;
  48. this.pluginRepository = pluginRepository;
  49. }
  50. @Override
  51. public void start() {
  52. LOG.info("Deploying Ruby on Rails applications");
  53. File appsDir = prepareRailsDirectory();
  54. for (PluginInfo pluginInfo : pluginRepository.getPluginInfos()) {
  55. String pluginKey = pluginInfo.getKey();
  56. Plugin plugin = pluginRepository.getPluginInstance(pluginKey);
  57. try {
  58. deployRailsApp(appsDir, pluginKey, plugin.getClass().getClassLoader());
  59. } catch (Exception e) {
  60. throw new IllegalStateException(String.format("Fail to deploy Ruby on Rails application of plugin [%s]", pluginKey), e);
  61. }
  62. }
  63. }
  64. @Override
  65. public void stop() {
  66. // do nothing
  67. }
  68. @VisibleForTesting
  69. File prepareRailsDirectory() {
  70. File appsDir = new File(fs.getTempDir(), "ror");
  71. prepareDir(appsDir);
  72. return appsDir;
  73. }
  74. @VisibleForTesting
  75. static void deployRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) {
  76. if (hasRailsApp(pluginKey, appClassLoader)) {
  77. LOG.info("Deploying app: " + pluginKey);
  78. File appDir = new File(appsDir, pluginKey);
  79. ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function<String, String>() {
  80. @Override
  81. public String apply(@Nullable String relativePath) {
  82. // Relocate the deployed files :
  83. // relativePath format is: org/sonar/ror/sqale/app/controllers/foo_controller.rb
  84. // app path is: org/sonar/ror/sqale
  85. // -> deployed file is app/controllers/foo_controller.rb
  86. return StringUtils.substringAfter(relativePath, pluginKey + "/");
  87. }
  88. });
  89. }
  90. }
  91. private static String pathToRubyInitFile(String pluginKey) {
  92. return ROR_PATH + pluginKey + "/init.rb";
  93. }
  94. @VisibleForTesting
  95. static boolean hasRailsApp(String pluginKey, ClassLoader classLoader) {
  96. return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null;
  97. }
  98. private void prepareDir(File appsDir) {
  99. if (appsDir.exists() && appsDir.isDirectory()) {
  100. try {
  101. FileUtils.deleteDirectory(appsDir);
  102. } catch (IOException e) {
  103. throw new IllegalStateException("Fail to delete temp directory: " + appsDir, e);
  104. }
  105. }
  106. try {
  107. FileUtils.forceMkdir(appsDir);
  108. } catch (IOException e) {
  109. throw new IllegalStateException("Fail to create temp directory: " + appsDir, e);
  110. }
  111. }
  112. }