| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.plugin; |
| 27 |
import java.io.File; |
| 28 |
import java.io.FileFilter; |
| 29 |
import java.net.URL; |
| 30 |
import java.net.URLClassLoader; |
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.List; |
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
public class PluginLoader { |
| 36 |
|
| 37 |
public static File getPluginFolder() { |
| 38 |
String pluginFolderFromProperty = null; |
| 39 |
try { |
| 40 |
pluginFolderFromProperty = java.lang.System.getProperty("beast.plugins.dir"); |
| 41 |
} catch (Exception ex) { |
| 42 |
|
| 43 |
} |
| 44 |
if (pluginFolderFromProperty != null) { |
| 45 |
return new File(pluginFolderFromProperty); |
| 46 |
} |
| 47 |
final String PLUGIN_FOLDER = "plugins"; |
| 48 |
final File PLUGIN_FILE = new File(PLUGIN_FOLDER); |
| 49 |
return PLUGIN_FILE; |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
public static List<String> getAvailablePlugins(){ |
| 54 |
|
| 55 |
List<String> plugins = new ArrayList<String> (); |
| 56 |
File pluginFile = PluginLoader.getPluginFolder(); |
| 57 |
|
| 58 |
Logger.getLogger("dr.app.plugin").info("Looking for plugins in " + pluginFile.getAbsolutePath()); |
| 59 |
|
| 60 |
File[] classFolderFiles = pluginFile.listFiles(new FileFilter() { |
| 61 |
public boolean accept(File pathname) { |
| 62 |
String name = pathname.getName(); |
| 63 |
if(!pathname.isDirectory() || name.endsWith("CVS") || name.endsWith(".classes")) |
| 64 |
return false; |
| 65 |
File[] directoryContents = pathname.listFiles(new FileFilter() { |
| 66 |
public boolean accept(File pathname) { |
| 67 |
String name = pathname.getName(); |
| 68 |
return name.endsWith(".jar"); |
| 69 |
} |
| 70 |
}); |
| 71 |
return directoryContents.length != 0; |
| 72 |
} |
| 73 |
}); |
| 74 |
|
| 75 |
if (classFolderFiles != null) { |
| 76 |
for (File folder : classFolderFiles) { |
| 77 |
plugins.add(folder.getName()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
File[] pluginJarFiles = pluginFile.listFiles(new FileFilter() { |
| 82 |
public boolean accept(File pathname) { |
| 83 |
return !pathname.isDirectory() && pathname.getAbsolutePath().endsWith(".jar"); |
| 84 |
} |
| 85 |
}); |
| 86 |
|
| 87 |
if (pluginJarFiles != null) { |
| 88 |
for (File jarFile : pluginJarFiles) { |
| 89 |
String name = jarFile.getName(); |
| 90 |
name =name.substring(0, name.length()- 4); |
| 91 |
if(! plugins.contains(name)) |
| 92 |
plugins.add(name); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return plugins; |
| 97 |
} |
| 98 |
|
| 99 |
public static Plugin loadPlugin(final String pluginName) { |
| 100 |
|
| 101 |
|
| 102 |
final String loggerName = "dr.app.plugin"; |
| 103 |
Logger.getLogger(loggerName).info("Loading plugin " + pluginName); |
| 104 |
File pluginDir = PluginLoader.getPluginFolder(); |
| 105 |
File file = new File(pluginDir, pluginName); |
| 106 |
|
| 107 |
try { |
| 108 |
URL[] urls; |
| 109 |
if (!file.exists()) { |
| 110 |
Logger.getLogger(loggerName).info("Loading jar file"); |
| 111 |
file = new File(pluginDir, pluginName + ".jar"); |
| 112 |
urls = new URL[]{file.toURL()}; |
| 113 |
} |
| 114 |
else { |
| 115 |
File classFiles = new File(pluginDir, "classes"); |
| 116 |
final boolean classesExist = classFiles.exists(); |
| 117 |
|
| 118 |
File[] files = file.listFiles(new FileFilter() { |
| 119 |
public boolean accept(File pathname) { |
| 120 |
String name = pathname.getName(); |
| 121 |
if(!name.endsWith(".jar")) return false; |
| 122 |
name = name.substring(0, name.length()- 4); |
| 123 |
return !(name.equals(pluginName) && classesExist); |
| 124 |
} |
| 125 |
}); |
| 126 |
if(files == null) files = new File[0]; |
| 127 |
|
| 128 |
int length = files.length+1; |
| 129 |
if(classesExist) length ++; |
| 130 |
|
| 131 |
urls = new URL[length]; |
| 132 |
int count = 0; |
| 133 |
if( classesExist ) { |
| 134 |
urls[ count ++ ] = classFiles.toURL(); |
| 135 |
} |
| 136 |
urls[ count ++ ] = file.toURL(); |
| 137 |
|
| 138 |
Logger.getLogger(loggerName).info("Adding " + file + " to class path"); |
| 139 |
|
| 140 |
for (File jarFile : files) { |
| 141 |
urls[count++] = jarFile.toURL(); |
| 142 |
} |
| 143 |
} |
| 144 |
final URLClassLoader classLoader = new URLClassLoader(urls); |
| 145 |
|
| 146 |
for (URL url : classLoader.getURLs()) { |
| 147 |
Logger.getLogger(loggerName).info("URL from loader: " + url.toString() + "\n"); |
| 148 |
} |
| 149 |
|
| 150 |
final Class myClass = classLoader.loadClass(pluginName); |
| 151 |
|
| 152 |
final Object plugin = myClass.newInstance(); |
| 153 |
|
| 154 |
|
| 155 |
if (!(plugin instanceof Plugin)){ |
| 156 |
throw new Exception("Class should be " + Plugin.class.getName()); |
| 157 |
} |
| 158 |
return (Plugin)plugin; |
| 159 |
|
| 160 |
} catch (Exception e) { |
| 161 |
Logger.getLogger(loggerName).severe(e.getMessage()); |
| 162 |
} |
| 163 |
return null; |
| 164 |
} |
| 165 |
} |