aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/pom.xml14
-rw-r--r--sonar-core/src/main/java/org/sonar/core/classloaders/ClassLoadersCollection.java40
-rw-r--r--sonar-core/src/test/java/org/sonar/core/classloaders/ClassLoadersCollectionTest.java25
-rw-r--r--sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java26
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/bar.jarbin0 -> 1057 bytes
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/foo.jarbin0 -> 313 bytes
6 files changed, 82 insertions, 23 deletions
diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml
index 1aa796d728d..23fe854c88d 100644
--- a/sonar-core/pom.xml
+++ b/sonar-core/pom.xml
@@ -30,6 +30,12 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sonar-plugin-api</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>classworlds</groupId>
+ <artifactId>classworlds</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
@@ -86,5 +92,11 @@
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-classworlds</artifactId>
+ <!-- TODO version should be defined in parent -->
+ <version>2.2.3</version>
+ </dependency>
</dependencies>
-</project> \ No newline at end of file
+</project>
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 d81bc38a083..30ccc5a99d3 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
@@ -17,25 +17,26 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
-package org.sonar.core.classloaders;
-import com.google.common.collect.Lists;
-import org.apache.commons.lang.StringUtils;
-import org.codehaus.classworlds.ClassRealm;
-import org.codehaus.classworlds.ClassWorld;
-import org.codehaus.classworlds.DuplicateRealmException;
-import org.codehaus.classworlds.NoSuchRealmException;
-import org.sonar.api.utils.Logs;
-import org.sonar.api.utils.SonarException;
+package org.sonar.core.classloaders;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
+import org.apache.commons.lang.StringUtils;
+import org.codehaus.plexus.classworlds.ClassWorld;
+import org.codehaus.plexus.classworlds.realm.ClassRealm;
+import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
+import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
+import org.sonar.api.utils.SonarException;
+
+import com.google.common.collect.Lists;
+
/**
- * Encapsulates manipulations with ClassLoaders, such as creation and establishing dependencies.
- * Current implementation based on {@link ClassWorld}.
+ * Encapsulates manipulations with ClassLoaders, such as creation and establishing dependencies. Current implementation based on
+ * {@link ClassWorld}.
*
* <h3>IMPORTANT</h3>
* <p>
@@ -70,8 +71,8 @@ public class ClassLoadersCollection {
* @param key plugin key
* @param urls libraries
* @param childFirst true, if child-first delegation model required instead of parent-first
- * @return created ClassLoader, but actually this method shouldn't return anything,
- * because dependencies must be established - see {@link #done()}.
+ * @return created ClassLoader, but actually this method shouldn't return anything, because dependencies must be established - see
+ * {@link #done()}.
*/
public ClassLoader createClassLoader(String key, Collection<URL> urls, boolean childFirst) {
try {
@@ -98,9 +99,9 @@ public class ClassLoadersCollection {
realm = world.newRealm(key, parent);
}
for (URL url : others) {
- realm.addConstituent(url);
+ realm.addURL(url);
}
- return realm.getClassLoader();
+ return realm;
} catch (DuplicateRealmException e) {
throw new SonarException(e);
}
@@ -115,8 +116,8 @@ public class ClassLoadersCollection {
if ( !StringUtils.endsWith(realm.getId(), "-parent")) {
String[] packagesToExport = new String[PREFIXES_TO_EXPORT.length];
for (int i = 0; i < PREFIXES_TO_EXPORT.length; i++) {
- // important to have dot at the end of package name
- packagesToExport[i] = PREFIXES_TO_EXPORT[i] + realm.getId() + ".api.";
+ // important to have dot at the end of package name only for classworlds 1.1
+ packagesToExport[i] = PREFIXES_TO_EXPORT[i] + realm.getId() + ".api";
}
export(realm, packagesToExport);
}
@@ -127,7 +128,8 @@ public class ClassLoadersCollection {
* Exports specified packages from given ClassRealm to all others.
*/
private void export(ClassRealm realm, String... packages) {
- Logs.INFO.debug("Exporting " + Arrays.toString(packages) + " from " + realm.getId());
+ // Logs.INFO.debug("Exporting " + Arrays.toString(packages) + " from " + realm.getId());
+ System.out.println("Exporting " + Arrays.toString(packages) + " from " + realm.getId());
for (Object o : world.getRealms()) {
ClassRealm dep = (ClassRealm) o;
if ( !StringUtils.equals(dep.getId(), realm.getId())) {
@@ -148,7 +150,7 @@ public class ClassLoadersCollection {
*/
public ClassLoader get(String key) {
try {
- return world.getRealm(key).getClassLoader();
+ return world.getRealm(key);
} catch (NoSuchRealmException e) {
return null;
}
diff --git a/sonar-core/src/test/java/org/sonar/core/classloaders/ClassLoadersCollectionTest.java b/sonar-core/src/test/java/org/sonar/core/classloaders/ClassLoadersCollectionTest.java
new file mode 100644
index 00000000000..a6348aecc3f
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/classloaders/ClassLoadersCollectionTest.java
@@ -0,0 +1,25 @@
+package org.sonar.core.classloaders;
+
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class ClassLoadersCollectionTest {
+
+ @Test
+ public void shouldImport() throws Exception {
+ String className = getClass().getName().replace(".", "/");
+ ClassLoadersCollection collection = new ClassLoadersCollection(null);
+ collection.createClassLoader("foo", Arrays.asList(getClass().getResource("/" + className + "/foo.jar")), false);
+ collection.createClassLoader("bar", Arrays.asList(getClass().getResource("/" + className + "/bar.jar")), false);
+ collection.done();
+
+ String resourceName = "org/sonar/plugins/bar/api/resource.txt";
+ assertThat(collection.get("bar").getResourceAsStream(resourceName), notNullValue());
+ assertThat(collection.get("foo").getResourceAsStream(resourceName), notNullValue());
+ }
+
+}
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
index ad9fe8375d6..001ca44f690 100644
--- a/sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/classloaders/ResourcesClassLoaderTest.java
@@ -1,13 +1,33 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+
package org.sonar.core.classloaders;
-import org.junit.Test;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.notNullValue;
+import org.junit.Test;
public class ResourcesClassLoaderTest {
diff --git a/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/bar.jar b/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/bar.jar
new file mode 100644
index 00000000000..343ad65f133
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/bar.jar
Binary files differ
diff --git a/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/foo.jar b/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/foo.jar
new file mode 100644
index 00000000000..505311c008b
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/classloaders/ClassLoadersCollectionTest/foo.jar
Binary files differ