Ignore:
Timestamp:
2010-09-12T11:46:34+02:00 (14 years ago)
Author:
postfix
Message:

toms has now a plug interface
added simple as a pluggable example

Location:
applications/editors/josm/plugins
Files:
8 added
4 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/toms/build.xml

    r23001 r23119  
    113113                                <attribute name="Plugin-Mainversion" value="${plugin.main.version}"/>
    114114                                <attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
     115                                <attribute name="Class-Path" value="./tplug/ifc.jar"/>
    115116                        </manifest>
    116117                </jar>
  • applications/editors/josm/plugins/toms/src/toms/Toms.java

    r23079 r23119  
    88
    99import java.awt.event.KeyEvent;
     10import java.io.BufferedInputStream;
     11import java.io.BufferedOutputStream;
     12import java.io.File;
     13import java.io.FileInputStream;
     14import java.io.FileNotFoundException;
     15import java.io.FileOutputStream;
     16import java.io.IOException;
     17import java.io.InputStream;
     18import java.io.ObjectOutputStream;
     19import java.lang.reflect.Method;
     20import java.net.URL;
     21import java.net.URLClassLoader;
     22import java.util.ArrayList;
     23import java.util.List;
    1024import java.util.Properties;
    1125import java.util.Set;
     26import java.util.jar.JarEntry;
     27import java.util.jar.JarFile;
     28import java.util.jar.JarInputStream;
     29import java.util.jar.JarOutputStream;
     30import java.util.zip.ZipEntry;
    1231
    1332import javax.swing.JMenuItem;
     
    2342import toms.dialogs.SmpDialogAction;
    2443import toms.plug.PluginApp;
     44import toms.plug.ifc.Pluggable;
     45import toms.plug.util.PluginLoader;
    2546import toms.seamarks.SeaMark;
    2647
     
    4869                SmpDialog.setUserHome(userHome);
    4970                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();
    5076               
    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                }
    52141        }
     142
    53143
    54144        @Override
  • applications/editors/josm/plugins/toms/src/toms/plug/PluginApp.java

    r23079 r23119  
    22
    33import java.io.File;
     4import java.io.IOException;
    45import java.util.List;
    56
     7import org.openstreetmap.josm.Main;
     8
     9
    610import toms.plug.ifc.Pluggable;
     11import toms.plug.ifc.PluginManager;
    712import toms.plug.util.PluginLoader;
    813
    914public class PluginApp {
    1015       
    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();
    1436        }
    1537
  • applications/editors/josm/plugins/toms/src/toms/plug/util/PluginLoader.java

    r23079 r23119  
    22
    33import java.io.File;
     4import java.io.FileInputStream;
     5import java.io.FileNotFoundException;
     6import java.io.IOException;
     7import java.net.MalformedURLException;
     8import java.net.URL;
     9import java.net.URLClassLoader;
     10import java.util.ArrayList;
     11import java.util.Collection;
    412import java.util.List;
     13import java.util.jar.JarEntry;
     14import java.util.jar.JarInputStream;
    515
    616import toms.plug.ifc.Pluggable;
    717
     18
    819public class PluginLoader {
    920
    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;
    13104        }
    14105
Note: See TracChangeset for help on using the changeset viewer.