Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Selcuk
- */
- public class PluginLoader {
- public ArrayList<String> getClassList(String jarFilePath)
- {
- File jarFile = new File(jarFilePath);
- return getClassList(jarFile);
- }
- public ArrayList<String> getClassList(File jarFile)
- {
- ArrayList<String> classList = new ArrayList<>();
- try {
- JarFile jf = new JarFile(jarFile);
- Enumeration<JarEntry> en = jf.entries();
- while (en.hasMoreElements()) {
- JarEntry entry = new JarEntry(en.nextElement());
- String name = entry.getName();
- if (!entry.isDirectory() && name.toLowerCase().endsWith(".class"))
- {
- classList.add(name.replace(".class", ""));
- }
- }
- } catch (IOException ex) {
- Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
- }
- return classList;
- }
- public Plugin loadURL(URL pluginUrl, String className)
- {
- Plugin plugin = null;
- URLClassLoader classLoader = new URLClassLoader(new URL[] {pluginUrl}, this.getClass().getClassLoader());
- Class pluginClass = null;
- try {
- System.out.println("Loading plugin class : " + className);
- pluginClass = Class.forName(className, true, classLoader);
- //if (pluginClass.isAssignableFrom(Plugin.class))
- if (Plugin.class.isAssignableFrom(pluginClass))
- {
- plugin = (Plugin) pluginClass.newInstance();
- }
- else
- {
- System.out.println(className + " is not subclass of Plugin");
- }
- } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
- Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
- }
- return plugin;
- }
- public Plugin loadPlugin(File pluginFile)
- {
- Plugin plugin = null;
- ArrayList<String> classList = getClassList(pluginFile);
- try {
- for (int i = 0; i < classList.size(); i++) {
- plugin = loadURL(pluginFile.toURI().toURL(), classList.get(i).replace("/", "."));
- if (plugin != null)
- break;
- }
- } catch (MalformedURLException ex) {
- Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
- }
- if (plugin != null)
- plugin.getProperties().setLocation(pluginFile.getPath());
- return plugin;
- }
- public Plugin loadPlugin(String pluginPath)
- {
- File pluginFile = new File(pluginPath);
- return loadPlugin(pluginFile);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement