package ie.dcu.plugin; import ie.dcu.util.OsUtils; import java.io.File; import java.net.*; public class PluginClassLoader extends URLClassLoader { private final File root; public PluginClassLoader(File root, URL[] urls, ClassLoader parent) { super(urls, parent); this.root = root; } @Override protected String findLibrary(String libname) { String path = findLibrary0(libname); System.out.println("findLibrary: " + libname + " => " + path); return path; } private String[] getPossibleLibraryNames(String libname) { if (OsUtils.isLinux64()) { return new String[] { libname, System.mapLibraryName(libname + "64"), System.mapLibraryName(libname) }; } else { return new String[] { libname, System.mapLibraryName(libname) }; } } private String findLibrary0(String libname) { String path; // Get a list of potential library names String[] libnames = getPossibleLibraryNames(libname); // Try plugin base folder if ((path = getPathIfExists(root, libnames)) != null) { return path; } // Try plugin lib/ subfolder File libFolder = new File(root, "lib"); if (libFolder.isDirectory()) { if ((path = getPathIfExists(libFolder, libnames)) != null) { return path; } } // Pass off to the superclass return super.findLibrary(libname); } private String getPathIfExists(File root, String[] filenames) { for (String filename : filenames) { File file = new File(root, filename); if (file.isFile()) { return file.getAbsolutePath(); } } return null; } }