aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorGodin <mandrikov@gmail.com>2010-10-21 15:21:40 +0000
committerGodin <mandrikov@gmail.com>2010-10-21 15:21:40 +0000
commitbe88575d78fa32e4f493de883dfe2e50ae3af060 (patch)
tree1bb469aa10c35f97d0d53f21a44c7af89d0d6cdf /sonar-core
parenta21b4ac84deb666976e87f6a823d60eb977449e2 (diff)
downloadsonarqube-be88575d78fa32e4f493de883dfe2e50ae3af060.tar.gz
sonarqube-be88575d78fa32e4f493de883dfe2e50ae3af060.zip
SONAR-1861: Allow to access files stored in "extensions/rules/" during batch execution
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java13
-rw-r--r--sonar-core/src/main/java/org/sonar/core/classloaders/ResourcesClassLoader.java39
-rw-r--r--sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java20
3 files changed, 70 insertions, 2 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java b/sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java
index d929dc535e9..852649a49c6 100644
--- a/sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java
+++ b/sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java
@@ -22,6 +22,14 @@ import java.util.Collection;
* for all other plugins even if they located in dependent library.
* </p>
*
+ * <h4>Search order for {@link ClassRealm} :</h4>
+ * <ul>
+ * <li>parent class loader (passed via the constructor) if there is one</li>
+ * <li>imports</li>
+ * <li>realm's constituents</li>
+ * <li>parent realm</li>
+ * </ul>
+ *
* @since 2.4
*/
public class ClassLoadersCollection {
@@ -46,12 +54,13 @@ public class ClassLoadersCollection {
*/
public ClassLoader createClassLoader(String key, Collection<URL> urls, boolean childFirst) {
try {
+ ClassLoader resourcesClassLoader = new ResourcesClassLoader(urls, baseClassLoader);
final ClassRealm realm;
if (childFirst) {
- ClassRealm parentRealm = world.newRealm(key + "-parent", baseClassLoader);
+ ClassRealm parentRealm = world.newRealm(key + "-parent", resourcesClassLoader);
realm = parentRealm.createChildRealm(key);
} else {
- realm = world.newRealm(key, baseClassLoader);
+ realm = world.newRealm(key, resourcesClassLoader);
}
for (URL constituent : urls) {
realm.addConstituent(constituent);
diff --git a/sonar-core/src/main/java/org/sonar/core/classloaders/ResourcesClassLoader.java b/sonar-core/src/main/java/org/sonar/core/classloaders/ResourcesClassLoader.java
new file mode 100644
index 00000000000..e2298f42602
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/classloaders/ResourcesClassLoader.java
@@ -0,0 +1,39 @@
+package org.sonar.core.classloaders;
+
+import org.apache.commons.lang.StringUtils;
+
+import com.google.common.collect.Lists;
+
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * This class loader is used to load resources from a list of URLs - see SONAR-1861.
+ */
+public class ResourcesClassLoader extends URLClassLoader {
+ private Collection<URL> urls;
+
+ public ResourcesClassLoader(Collection<URL> urls, ClassLoader parent) {
+ super(new URL[] {}, parent);
+ List<URL> filtered = Lists.newArrayList();
+ for (URL url : urls) {
+ String path = url.getPath();
+ if ( !StringUtils.endsWith(path, ".jar") && !StringUtils.endsWith(path, "/")) {
+ filtered.add(url);
+ }
+ }
+ this.urls = filtered;
+ }
+
+ @Override
+ public URL findResource(String name) {
+ for (URL url : urls) {
+ if (StringUtils.endsWith(url.getPath(), name)) {
+ return url;
+ }
+ }
+ return null;
+ }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java b/sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java
new file mode 100644
index 00000000000..ad9fe8375d6
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java
@@ -0,0 +1,20 @@
+package org.sonar.core.classloaders;
+
+import org.junit.Test;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+
+public class ResourcesClassLoaderTest {
+
+ @Test
+ public void test() throws Exception {
+ List<URL> urls = Arrays.asList(new URL("http://localhost:9000/deploy/plugins/checkstyle/extension.xml"));
+ ResourcesClassLoader classLoader = new ResourcesClassLoader(urls, null);
+ assertThat(classLoader.findResource("extension.xml"), notNullValue());
+ }
+}