Changeset 23119 in osm for applications/editors/josm
- Timestamp:
- 2010-09-12T11:46:34+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins
- Files:
-
- 8 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/toms/build.xml
r23001 r23119 113 113 <attribute name="Plugin-Mainversion" value="${plugin.main.version}"/> 114 114 <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/> 115 <attribute name="Class-Path" value="./tplug/ifc.jar"/> 115 116 </manifest> 116 117 </jar> -
applications/editors/josm/plugins/toms/src/toms/Toms.java
r23079 r23119 8 8 9 9 import java.awt.event.KeyEvent; 10 import java.io.BufferedInputStream; 11 import java.io.BufferedOutputStream; 12 import java.io.File; 13 import java.io.FileInputStream; 14 import java.io.FileNotFoundException; 15 import java.io.FileOutputStream; 16 import java.io.IOException; 17 import java.io.InputStream; 18 import java.io.ObjectOutputStream; 19 import java.lang.reflect.Method; 20 import java.net.URL; 21 import java.net.URLClassLoader; 22 import java.util.ArrayList; 23 import java.util.List; 10 24 import java.util.Properties; 11 25 import java.util.Set; 26 import java.util.jar.JarEntry; 27 import java.util.jar.JarFile; 28 import java.util.jar.JarInputStream; 29 import java.util.jar.JarOutputStream; 30 import java.util.zip.ZipEntry; 12 31 13 32 import javax.swing.JMenuItem; … … 23 42 import toms.dialogs.SmpDialogAction; 24 43 import toms.plug.PluginApp; 44 import toms.plug.ifc.Pluggable; 45 import toms.plug.util.PluginLoader; 25 46 import toms.seamarks.SeaMark; 26 47 … … 48 69 SmpDialog.setUserHome(userHome); 49 70 Smp.setEnabled(false); 71 72 File pluginDir = Main.pref.getPluginsDirectory(); 73 String pluginDirName = pluginDir.getAbsolutePath(); 74 File tplug = new File(pluginDirName + "/tplug"); 75 if(!tplug.exists()) tplug.mkdir(); 50 76 51 PluginApp.runPlugins(); 77 // build ifc.jar from toms.jar 78 JarEntry ent = null; 79 BufferedInputStream inp = null; 80 String entName = null; 81 byte[] buffer = new byte[16384]; 82 int len; 83 84 try { 85 JarFile file = new JarFile(pluginDirName + "/toms.jar"); 86 FileOutputStream fos = new FileOutputStream(pluginDirName + "/tplug/ifc.jar"); 87 JarOutputStream jos = new JarOutputStream(fos); 88 BufferedOutputStream oos = new BufferedOutputStream( jos); 89 90 ent = file.getJarEntry("toms/plug/ifc/Pluggable.class"); 91 inp = new BufferedInputStream(file.getInputStream( ent )); 92 entName = ent.getName(); 93 94 jos.putNextEntry(new JarEntry(entName)); 95 96 while ((len = inp.read(buffer)) > 0) { 97 oos.write(buffer, 0, len); 98 } 99 100 oos.flush(); 101 inp.close(); 102 103 ent = file.getJarEntry("toms/plug/ifc/PluginManager.class"); 104 inp = new BufferedInputStream(file.getInputStream( ent )); 105 entName = ent.getName(); 106 jos.putNextEntry(new JarEntry(entName)); 107 108 while ((len = inp.read(buffer)) > 0) { 109 oos.write(buffer, 0, len); 110 } 111 112 oos.flush(); 113 oos.close(); 114 fos.flush(); 115 fos.close(); 116 inp.close(); 117 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 122 123 // add ifc.jar to classpath (josm need this archive, or perhaps only the interface) 124 File f = new java.io.File(pluginDirName + "/tplug/ifc.jar"); 125 ClassLoader myClassLoader = Thread.currentThread().getContextClassLoader(); 126 127 try { 128 Method addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); 129 addUrlMethod.setAccessible(true); 130 addUrlMethod.invoke(myClassLoader, f.toURI().toURL()); 131 } catch (Exception e) { 132 e.printStackTrace(); 133 } 134 135 136 try { 137 PluginApp.runPlugins(); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } 52 141 } 142 53 143 54 144 @Override -
applications/editors/josm/plugins/toms/src/toms/plug/PluginApp.java
r23079 r23119 2 2 3 3 import java.io.File; 4 import java.io.IOException; 4 5 import java.util.List; 5 6 7 import org.openstreetmap.josm.Main; 8 9 6 10 import toms.plug.ifc.Pluggable; 11 import toms.plug.ifc.PluginManager; 7 12 import toms.plug.util.PluginLoader; 8 13 9 14 public class PluginApp { 10 15 11 public static void runPlugins() { 12 List<Pluggable> plugins = PluginLoader.loadPlugins(new File("./tplug")); 13 System.out.println("hello world"); 16 public static void runPlugins() throws IOException { 17 String pluginDirName = Main.pref.getPluginsDirectory().getAbsolutePath(); 18 19 List<Pluggable> plugins = PluginLoader.loadPlugins(new File(pluginDirName + "/tplug")); 20 21 if(plugins == null) return; 22 23 PluginManager manager = new PluginManagerImpl(); 24 25 for(Pluggable p : plugins) p.setPluginManager(manager); 26 for(Pluggable p : plugins) p.start(); 27 28 // wait 29 try { 30 Thread.sleep(10000); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 35 for(Pluggable p: plugins) p.stop(); 14 36 } 15 37 -
applications/editors/josm/plugins/toms/src/toms/plug/util/PluginLoader.java
r23079 r23119 2 2 3 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 import java.net.URLClassLoader; 10 import java.util.ArrayList; 11 import java.util.Collection; 4 12 import java.util.List; 13 import java.util.jar.JarEntry; 14 import java.util.jar.JarInputStream; 5 15 6 16 import toms.plug.ifc.Pluggable; 7 17 18 8 19 public class PluginLoader { 9 20 10 public static List<Pluggable> loadPlugins(File file) { 11 System.out.println("Hier ist der PluginLoader"); 12 return null; 21 public static List<Pluggable> loadPlugins(File plugDir) throws IOException { 22 File[] plugJars = plugDir.listFiles(new JARFileFilter()); 23 ClassLoader cl = new URLClassLoader(PluginLoader.fileArrayToURLArray(plugJars)); 24 25 if(cl == null) return null; 26 27 List<Class<Pluggable>> plugClasses = PluginLoader.extractClassesFromJARs(plugJars, cl); 28 29 return PluginLoader.createPluggableObjects(plugClasses); 30 } 31 32 private static List<Pluggable> createPluggableObjects(List<Class<Pluggable>> pluggables) { 33 List<Pluggable> plugs = new ArrayList<Pluggable>(pluggables.size()); 34 for(Class<Pluggable> plug : pluggables) { 35 try { 36 plugs.add(plug.newInstance()); 37 } catch (InstantiationException e) { 38 System.err.println("Can't instantiate plugin: " + plug.getName()); 39 e.printStackTrace(); 40 } catch (IllegalAccessException e) { 41 System.err.println("IllegalAccess for plugin: " + plug.getName()); 42 e.printStackTrace(); 43 } 44 } 45 46 return plugs; 47 48 } 49 50 private static List<Class<Pluggable>> extractClassesFromJARs( File[] jars, ClassLoader cl) throws FileNotFoundException, IOException { 51 List<Class<Pluggable>> classes = new ArrayList<Class<Pluggable>>(); 52 53 for(File jar : jars) { 54 classes.addAll(PluginLoader.extractClassesFromJAR(jar, cl)); 55 } 56 57 return classes; 58 } 59 60 @SuppressWarnings("unchecked") 61 private static Collection<? extends Class<Pluggable>> extractClassesFromJAR(File jar, ClassLoader cl) throws FileNotFoundException, IOException { 62 List<Class<Pluggable>> classes = new ArrayList<Class<Pluggable>>(); 63 JarInputStream jaris = new JarInputStream(new FileInputStream(jar)); 64 JarEntry ent = null; 65 66 while ((ent = jaris.getNextJarEntry()) != null) { 67 String entName = ent.getName(); //.toLowerCase(); 68 69 if (entName.endsWith(".class")) { 70 try { 71 Class<?> cls = cl.loadClass(entName.substring(0, entName.length()- 6).replace('/', '.')); 72 if (PluginLoader.isPluggableClass(cls)) classes.add((Class<Pluggable>) cls); 73 } catch (ClassNotFoundException e) { 74 System.err.println("Can't load Class" + entName); 75 e.printStackTrace(); 76 } 77 } 78 } 79 80 jaris.close(); 81 82 return classes; 83 } 84 85 private static boolean isPluggableClass(Class<?> cls) { 86 for (Class<?> i: cls.getInterfaces()) { 87 if (i.equals(Pluggable.class)) return true; 88 } 89 90 return false; 91 92 } 93 94 private static URL[] fileArrayToURLArray(File[] files) throws MalformedURLException { 95 URL[] urls = new URL[files.length]; 96 97 if(urls == null) return null; 98 99 for(int i = 0; i < files.length; i++) { 100 urls[i] = files[i].toURI().toURL(); 101 } 102 103 return urls; 13 104 } 14 105
Note:
See TracChangeset
for help on using the changeset viewer.