Ignore:
Timestamp:
2010-09-15T19:01:04+02:00 (15 years ago)
Author:
stoecker
Message:

remove tabs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/smed/src/smed/plug/util/SmedPluginLoader.java

    r23178 r23193  
    1919public class SmedPluginLoader {
    2020
    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         }
     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    }
    3333
    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         }
     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    }
    5050
    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                 }
     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        }
    5757
    58                 if(classes.isEmpty()) return null;
    59                 else return classes;
    60         }
     58        if(classes.isEmpty()) return null;
     59        else return classes;
     60    }
    6161
    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         }
     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    }
    8686
    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         }
     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    }
    9494
    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         }
     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    }
    108108}
Note: See TracChangeset for help on using the changeset viewer.