aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java
blob: dbfecc668b4a13a0984e4f882f332d4077e4307d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.server.plugins;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.platform.PluginMetadata;
import org.sonar.api.platform.PluginRepository;
import org.sonar.api.platform.ServerFileSystem;

import javax.annotation.Nullable;

import java.io.File;
import java.io.IOException;

/**
 * Ruby on Rails requires the files to be on filesystem but not in Java classpath (JAR). This component extracts
 * all the needed files from plugins and copy them to $SONAR_HOME/temp
 *
 * @since 3.0
 */
public class ApplicationDeployer {
  private static final Logger LOG = LoggerFactory.getLogger(ApplicationDeployer.class);
  private static final String ROR_PATH = "org/sonar/ror/";

  private final ServerFileSystem fileSystem;
  private final PluginRepository pluginRepository;

  public ApplicationDeployer(ServerFileSystem fileSystem, PluginRepository pluginRepository) {
    this.fileSystem = fileSystem;
    this.pluginRepository = pluginRepository;
  }

  public void start() {
    deployRubyRailsApps();
  }

  private void deployRubyRailsApps() {
    LOG.info("Deploy Ruby on Rails applications");
    File appsDir = prepareRubyRailsRootDirectory();

    for (PluginMetadata pluginMetadata : pluginRepository.getMetadata()) {
      String pluginKey = pluginMetadata.getKey();
      try {
        deployRubyRailsApp(appsDir, pluginKey, pluginRepository.getPlugin(pluginKey).getClass().getClassLoader());
      } catch (Exception e) {
        throw new IllegalStateException("Fail to deploy Ruby on Rails application: " + pluginKey, e);
      }
    }
  }

  @VisibleForTesting
  File prepareRubyRailsRootDirectory() {
    File appsDir = new File(fileSystem.getTempDir(), "ror");
    prepareDir(appsDir);
    return appsDir;
  }

  @VisibleForTesting
  static void deployRubyRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) {
    if (hasRubyRailsApp(pluginKey, appClassLoader)) {
      LOG.info("Deploy app: " + pluginKey);
      File appDir = new File(appsDir, pluginKey);
      ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function<String, String>() {
        public String apply(@Nullable String relativePath) {
          // Relocate the deployed files :
          // relativePath format is: org/sonar/ror/sqale/app/controllers/foo_controller.rb
          // app path is: org/sonar/ror/sqale
          // -> deployed file is app/controllers/foo_controller.rb
          return StringUtils.substringAfter(relativePath, pluginKey + "/");
        }
      });
    }
  }

  private static String pathToRubyInitFile(String pluginKey) {
    return ROR_PATH + pluginKey + "/init.rb";
  }

  @VisibleForTesting
  static boolean hasRubyRailsApp(String pluginKey, ClassLoader classLoader) {
    return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null;

  }

  private void prepareDir(File appsDir) {
    if (appsDir.exists() && appsDir.isDirectory()) {
      try {
        FileUtils.deleteDirectory(appsDir);
      } catch (IOException e) {
        throw new IllegalStateException("Fail to delete temp directory: " + appsDir, e);
      }
    }
    try {
      FileUtils.forceMkdir(appsDir);
    } catch (IOException e) {
      throw new IllegalStateException("Fail to create temp directory: " + appsDir, e);
    }
  }
}