Skip to content
Snippets Groups Projects
Commit 6bb09c62 authored by Nils Christian Ehmke's avatar Nils Christian Ehmke
Browse files

Modified the plugin class loader.

parent 7139168b
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
......@@ -52,15 +53,17 @@ public final class PluginClassLoader extends ClassLoader {
* The singleton instance of this class.
*/
private static PluginClassLoader instance;
/**
* This list contains a class loader for each url added to this class loader.
*/
private final Map<String, URLClassLoader> classLoaders = new HashMap<String, URLClassLoader>();
private final List<URL> list = new ArrayList<URL>();
private URLClassLoader classLoader = null;
/**
* The default constructor of this class. During the creation all available libraries will be added to the class loader.
*/
private PluginClassLoader() {
classLoader = AnalysisController.class.getClassLoader();
/* Make sure that all libs are loaded. */
final List<MIDependency> libs = FileManager.getInstance().loadAllDependencies();
for (final MIDependency lib : libs) {
......@@ -79,15 +82,9 @@ public final class PluginClassLoader extends ClassLoader {
* The URL of the dependency to be added.
*/
public void addURL(final URL url) {
/* Create the new class loader within a privileged block. */
final URLClassLoader newClassLoader = (URLClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
@Override
public Object run() {
return new URLClassLoader(new URL[] { url }, AnalysisController.class.getClassLoader());
}
});
this.classLoaders.put(url.toString(), newClassLoader);
list.add(url);
classLoader = new URLClassLoader(list.toArray(new URL[list.size()]), AnalysisController.class.getClassLoader());
}
/**
......@@ -97,7 +94,9 @@ public final class PluginClassLoader extends ClassLoader {
* The URL of the dependency to be added.
*/
public void removeURL(final URL url) {
// TODO Implement
list.remove(url);
classLoader = new URLClassLoader(list.toArray(new URL[list.size()]), AnalysisController.class.getClassLoader());
}
/**
......@@ -131,25 +130,6 @@ public final class PluginClassLoader extends ClassLoader {
*/
@Override
public Class<?> loadClass(final String name) throws ClassNotFoundException {
try {
return ClassLoader.getSystemClassLoader().loadClass(name);
} catch (final ClassNotFoundException ex) {
/* Ignore exception. */
}
synchronized (this) {
/* Run through all available class loaders and try to find the correct class. */
final Iterator<URLClassLoader> classLoaderIter = this.classLoaders.values().iterator();
while (classLoaderIter.hasNext()) {
final URLClassLoader currClassLoader = classLoaderIter.next();
/* If no exception is thrown, we found the correct class and return it. Otherwise we continue the search. */
try {
return currClassLoader.loadClass(name);
} catch (final ClassNotFoundException ex) {
// Ignore error
}
}
}
/* We were not able to found any class and throw an exception. */
throw new ClassNotFoundException();
return classLoader.loadClass(name);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment