public ClassLoader getClassLoader() throws MalformedURLException {
if (classLoader == null) {
// Setup the ClassLoader
classLoader = Main.class.getClassLoader();
if (!extensions.isEmpty() || !activeMQClassPath.isEmpty()) {
ArrayList<URL> urls = new ArrayList<URL>();
for (Iterator<File> iter = activeMQClassPath.iterator(); iter.hasNext();) {
File dir = iter.next();
urls.add(dir.toURI().toURL());
}
for (Iterator<File> iter = extensions.iterator(); iter.hasNext();) {
File dir = iter.next();
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
// Sort the jars so that classpath built is consistently in the same
// order. Also allows us to use jar names to control classpath order.
Arrays.sort(files, new Comparator<File>() {
public int compare(File f1, File f2) {
return f1.getName().compareTo(f2.getName());
}
});
for (int j = 0; j < files.length; j++) {
if (files[j].getName().endsWith(".zip") || files[j].getName().endsWith(".jar")) {
urls.add(files[j].toURI().toURL());
}
}
}
}
}
URL u[] = new URL[urls.size()];
urls.toArray(u);
classLoader = new URLClassLoader(u, classLoader);
}
Thread.currentThread().setContextClassLoader(classLoader);
}
return classLoader;
}