summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-04-02 19:16:58 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-04-02 19:17:05 +0200
commit6148bb0cd6d0f33b66b611a7f85061d33c829f0d (patch)
tree2b3f1d544615b57763b7808a5138e4348934eb19
parent11b79f21ddd61b3306838119d3fc646334c3c16f (diff)
downloadsonarqube-6148bb0cd6d0f33b66b611a7f85061d33c829f0d.tar.gz
sonarqube-6148bb0cd6d0f33b66b611a7f85061d33c829f0d.zip
Improve loading of ruby on rails applications
-rw-r--r--sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java8
-rw-r--r--sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java30
-rw-r--r--sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java3
-rw-r--r--sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jarbin2845 -> 3497 bytes
4 files changed, 29 insertions, 12 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java b/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java
index ea22a3dae35..e3b9599ed50 100644
--- a/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java
+++ b/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java
@@ -82,7 +82,7 @@ public class ApplicationDeployer {
if (hasRubyRailsApp(pluginKey, appClassLoader)) {
LOG.info("Deploy app: " + pluginKey);
File appDir = new File(appsDir, pluginKey);
- ClassLoaderUtils.copyResources(appClassLoader, ROR_PATH + pluginKey, appDir, new Function<String, String>() {
+ ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function<String, String>() {
@Override
public String apply(@Nullable String relativePath) {
// Relocate the deployed files :
@@ -95,9 +95,13 @@ public class ApplicationDeployer {
}
}
+ private static String pathToRubyInitFile(String pluginKey) {
+ return ROR_PATH + pluginKey + "/init.rb";
+ }
+
@VisibleForTesting
static boolean hasRubyRailsApp(String pluginKey, ClassLoader classLoader) {
- return classLoader.getResource(ROR_PATH + pluginKey) != null;
+ return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null;
}
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java
index c99e4b7debb..58e2d5ad0ad 100644
--- a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java
+++ b/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java
@@ -79,35 +79,47 @@ public final class ClassLoaderUtils {
});
}
+
+ public static Collection<String> listResources(ClassLoader classLoader, String rootPath) {
+ return listResources(classLoader, rootPath, Predicates.<String>alwaysTrue());
+ }
+
/**
- * Finds directories and files within a given directory and its subdirectories
+ * Finds directories and files within a given directory and its subdirectories.
*
* @param classLoader
- * @param rootPath the root directory, for example org/sonar/sqale
+ * @param rootPath the root directory, for example org/sonar/sqale, or a file in this root directory, for example org/sonar/sqale/index.txt
+ * @param
* @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null.
*/
- public static Collection<String> listResources(ClassLoader classLoader, String rootPath) {
- return listResources(classLoader, rootPath, Predicates.<String>alwaysTrue());
- }
-
- public static Collection<String> listResources(ClassLoader classloader, String rootPath, Predicate<String> predicate) {
+ public static Collection<String> listResources(ClassLoader classLoader, String rootPath, Predicate<String> predicate) {
try {
Collection<String> paths = Lists.newArrayList();
rootPath = StringUtils.removeStart(rootPath, "/");
- URL root = classloader.getResource(rootPath);
+ URL root = classLoader.getResource(rootPath);
if (root == null) {
return paths;
}
if (!"jar".equals(root.getProtocol())) {
throw new IllegalStateException("Unsupported protocol: " + root.getProtocol());
}
+
+ // Path of the root directory
+ // Examples :
+ // org/sonar/sqale/index.txt -> rootDirectory is org/sonar/sqale
+ // org/sonar/sqale/ -> rootDirectory is org/sonar/sqale
+ // org/sonar/sqale -> rootDirectory is org/sonar/sqale
+ String rootDirectory = rootPath;
+ if (StringUtils.substringAfterLast(rootPath, "/").indexOf('.') >= 0) {
+ rootDirectory = StringUtils.substringBeforeLast(rootPath, "/");
+ }
String jarPath = root.getPath().substring(5, root.getPath().indexOf("!")); //strip out only the JAR file
JarFile jar = new JarFile(URLDecoder.decode(jarPath, CharEncoding.UTF_8));
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
String name = entries.nextElement().getName();
- if (name.startsWith(rootPath) && predicate.apply(name)) {
+ if (name.startsWith(rootDirectory) && predicate.apply(name)) {
paths.add(name);
}
}
diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java
index cb350f6ad08..d2904c5855c 100644
--- a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java
@@ -62,7 +62,8 @@ public class ApplicationDeployerTest {
File appDir = new File(tempDir, "fake");
assertThat(appDir.isDirectory(), is(true));
assertThat(appDir.exists(), is(true));
- assertThat(FileUtils.listFiles(appDir, null, true).size(), is(2));
+ assertThat(FileUtils.listFiles(appDir, null, true).size(), is(3));
+ assertThat(new File(appDir, "init.rb").exists(), is(true));
assertThat(new File(appDir, "app/controllers/fake_controller.rb").exists(), is(true));
assertThat(new File(appDir, "app/views/fake/index.html.erb").exists(), is(true));
}
diff --git a/sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar b/sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar
index 583b3e983d9..9ed866620ec 100644
--- a/sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar
+++ b/sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar
Binary files differ