Changeset 23193 in osm for applications/editors/josm/plugins/smed/src/smed/plug/util/SmedPluginLoader.java
- Timestamp:
- 2010-09-15T19:01:04+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/smed/src/smed/plug/util/SmedPluginLoader.java
r23178 r23193 19 19 public class SmedPluginLoader { 20 20 21 22 23 24 25 26 27 28 29 30 31 32 21 public static List<SmedPluggable> loadPlugins(File plugDir) throws IOException { 22 File[] plugJars = plugDir.listFiles(new JARFileFilter()); 23 24 URL[] urls = fileArrayToURLArray(plugJars); 25 if(urls == null) return null; 26 27 ClassLoader cl = new URLClassLoader(urls); 28 List<Class<SmedPluggable>> plugClasses = extractClassesFromJARs(plugJars, cl); 29 30 if(plugClasses == null) return null; 31 else return createPluggableObjects(plugClasses); 32 } 33 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 34 private static List<SmedPluggable> createPluggableObjects(List<Class<SmedPluggable>> pluggables) { 35 List<SmedPluggable> plugs = new ArrayList<SmedPluggable>(pluggables.size()); 36 for(Class<SmedPluggable> plug : pluggables) { 37 try { 38 plugs.add(plug.newInstance()); 39 } catch (InstantiationException e) { 40 System.err.println("Can't instantiate plugin: " + plug.getName()); 41 e.printStackTrace(); 42 } catch (IllegalAccessException e) { 43 System.err.println("IllegalAccess for plugin: " + plug.getName()); 44 e.printStackTrace(); 45 } 46 } 47 48 return plugs; 49 } 50 50 51 52 53 54 55 56 51 private static List<Class<SmedPluggable>> extractClassesFromJARs(File[] jars, ClassLoader cl) throws FileNotFoundException, IOException { 52 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>(); 53 54 for(File jar : jars) { 55 classes.addAll(extractClassesFromJAR(jar, cl)); 56 } 57 57 58 59 60 58 if(classes.isEmpty()) return null; 59 else return classes; 60 } 61 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 62 @SuppressWarnings("unchecked") 63 private static Collection<? extends Class<SmedPluggable>> extractClassesFromJAR (File jar, ClassLoader cl) throws FileNotFoundException, IOException { 64 List<Class<SmedPluggable>> classes = new ArrayList<Class<SmedPluggable>>(); 65 JarInputStream jaris = new JarInputStream(new FileInputStream(jar)); 66 JarEntry ent = null; 67 68 while ((ent = jaris.getNextJarEntry()) != null) { 69 String entName = ent.getName(); //.toLowerCase(); 70 71 if (entName.endsWith(".class")) { 72 try { 73 Class<?> cls = cl.loadClass(entName.substring(0, entName.length()- 6).replace('/', '.')); 74 if(isPluggableSmedClass(cls)) classes.add((Class<SmedPluggable>) cls); 75 } catch (ClassNotFoundException e) { 76 System.err.println("Can't load Class" + entName); 77 e.printStackTrace(); 78 } 79 } 80 } 81 82 jaris.close(); 83 84 return classes; 85 } 86 86 87 88 89 90 91 92 93 87 private static boolean isPluggableSmedClass(Class<?> cls) { 88 for (Class<?> i: cls.getInterfaces()) { 89 if (i.equals(SmedPluggable.class)) return true; 90 } 91 92 return false; 93 } 94 94 95 96 97 98 99 100 101 102 103 104 105 106 107 95 private static URL[] fileArrayToURLArray(File[] files) throws MalformedURLException { 96 // splug contains at least smed_ifc.jar, but smed_ifc.jar isn't pluggable 97 if(files.length <= 1) return null; 98 99 URL[] urls = new URL[files.length]; 100 101 102 for(int i = 0; i < files.length; i++) { 103 urls[i] = files[i].toURI().toURL(); 104 } 105 106 return urls; 107 } 108 108 }
Note:
See TracChangeset
for help on using the changeset viewer.