Advertisement
selcux

Java Jar Loader

Apr 16th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Selcuk
  4.  */
  5. public class PluginLoader {
  6.    
  7.     public ArrayList<String> getClassList(String jarFilePath)
  8.     {
  9.         File jarFile = new File(jarFilePath);
  10.         return getClassList(jarFile);
  11.     }
  12.    
  13.     public ArrayList<String> getClassList(File jarFile)
  14.     {
  15.         ArrayList<String> classList = new ArrayList<>();
  16.         try {
  17.             JarFile jf = new JarFile(jarFile);
  18.             Enumeration<JarEntry> en = jf.entries();
  19.            
  20.             while (en.hasMoreElements()) {                
  21.                 JarEntry entry = new JarEntry(en.nextElement());
  22.                 String name = entry.getName();
  23.                 if (!entry.isDirectory() && name.toLowerCase().endsWith(".class"))
  24.                 {
  25.                     classList.add(name.replace(".class", ""));
  26.                 }
  27.             }
  28.         } catch (IOException ex) {
  29.             Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
  30.         }
  31.        
  32.         return classList;
  33.     }
  34.    
  35.     public Plugin loadURL(URL pluginUrl, String className)
  36.     {
  37.         Plugin plugin = null;
  38.         URLClassLoader classLoader = new URLClassLoader(new URL[] {pluginUrl}, this.getClass().getClassLoader());
  39.         Class pluginClass = null;
  40.         try {
  41.             System.out.println("Loading plugin class : " + className);
  42.             pluginClass = Class.forName(className, true, classLoader);
  43.             //if (pluginClass.isAssignableFrom(Plugin.class))
  44.             if (Plugin.class.isAssignableFrom(pluginClass))
  45.             {
  46.                 plugin = (Plugin) pluginClass.newInstance();
  47.             }
  48.             else
  49.             {
  50.                 System.out.println(className + " is not subclass of Plugin");
  51.             }
  52.         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
  53.             Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
  54.         }
  55.         return plugin;
  56.     }
  57.    
  58.     public Plugin loadPlugin(File pluginFile)
  59.     {
  60.         Plugin plugin = null;
  61.         ArrayList<String> classList = getClassList(pluginFile);
  62.         try {
  63.             for (int i = 0; i < classList.size(); i++) {
  64.                 plugin = loadURL(pluginFile.toURI().toURL(), classList.get(i).replace("/", "."));
  65.                 if (plugin != null)
  66.                     break;
  67.             }
  68.         } catch (MalformedURLException ex) {
  69.             Logger.getLogger(PluginLoader.class.getName()).log(Level.SEVERE, null, ex);
  70.         }
  71.        
  72.         if (plugin != null)
  73.            plugin.getProperties().setLocation(pluginFile.getPath());
  74.        
  75.         return plugin;
  76.     }
  77.    
  78.     public Plugin loadPlugin(String pluginPath)
  79.     {
  80.         File pluginFile = new File(pluginPath);
  81.         return loadPlugin(pluginFile);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement