package ij.io;
import ij.IJ;
import java.io.*;
import java.net.*;
public class PluginClassLoader extends URLClassLoader {
protected String path;
public PluginClassLoader(String path) {
super(new URL[0], IJ.class.getClassLoader());
init(path);
}
public PluginClassLoader(String path, boolean callSuper) {
super(new URL[0], Thread.currentThread().getContextClassLoader());
init(path);
}
void init(String path) {
this.path = path;
File f = new File(path);
try {
addURL(f.toURI().toURL());
} catch (MalformedURLException e) {
ij.IJ.log("PluginClassLoader: "+e);
}
String[] list = f.list();
if (list==null)
return;
for (int i=0; i<list.length; i++) {
if (list[i].equals(".rsrc"))
continue;
File f2=new File(path, list[i]);
if (f2.isDirectory())
addDirectory(f2);
else
addJar(f2);
}
addDirectory(f, "jars"); }
private void addDirectory(File f) {
try {
addURL(f.toURI().toURL());
} catch (MalformedURLException e) {
ij.IJ.log("PluginClassLoader: "+e);
}
String[] innerlist = f.list();
if (innerlist==null)
return;
for (int j=0; j<innerlist.length; j++) {
File g = new File(f,innerlist[j]);
if (g.isFile())
addJar(g);
}
}
private void addJar(File f) {
if (f.getName().endsWith(".jar") || f.getName().endsWith(".zip")) {
try {
addURL(f.toURI().toURL());
} catch (MalformedURLException e) {
ij.IJ.log("PluginClassLoader: "+e);
}
}
}
private void addDirectory(File f, String name) {
f = f.getParentFile();
if (f==null)
return;
f = new File(f, name);
if (f==null)
return;
if (f.isDirectory())
addDirectory(f);
}
}