Changeset 250 in josm


Ignore:
Timestamp:
2007-05-31T00:29:59+02:00 (17 years ago)
Author:
framm
Message:
  • patch by Christof Dallermassl that will use a common classloader for plugins in order to allow them to reference each other.
Location:
src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/Main.java

    r243 r250  
    1212import java.net.URISyntaxException;
    1313import java.util.Arrays;
     14import java.net.URL;
     15import java.net.URLClassLoader;
     16import java.util.ArrayList;
    1417import java.util.Collection;
    1518import java.util.LinkedList;
     
    208211                if (plugins.isEmpty())
    209212                        return;
    210 
    211213                SortedMap<Integer, Collection<PluginInformation>> p = new TreeMap<Integer, Collection<PluginInformation>>();
    212214                for (String pluginName : plugins) {
     
    225227                        }
    226228                }
     229               
     230                // iterate all plugins and collect all libraries of all plugins:
     231                List<URL> allPluginLibraries = new ArrayList<URL>();
     232                for (Collection<PluginInformation> c : p.values()) {
     233                        for (PluginInformation info : c) {
     234                                allPluginLibraries.addAll(info.getLibraries());
     235                        }
     236                }
     237                // create a classloader for all plugins:
     238                URL[] jarUrls = new URL[allPluginLibraries.size()];
     239                jarUrls = allPluginLibraries.toArray(jarUrls);
     240                URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader());
     241               
     242               
    227243                for (Collection<PluginInformation> c : p.values()) {
    228244                        for (PluginInformation info : c) {
    229245                                try {
     246                                        info.setClassLoader(pluginClassLoader); // set the common classloader
    230247                                        Class<?> klass = info.loadClass();
    231248                                        ImageProvider.sources.add(0, klass);
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r243 r250  
    3333        public final String author;
    3434        public final int stage;
    35         public final List<URL> libraries = new ArrayList<URL>();
     35        protected final List<URL> libraries = new ArrayList<URL>();
     36        protected ClassLoader classLoader;
    3637
    3738        public final Map<String, String> attr = new TreeMap<String, String>();
     
    6869                        if (file != null)
    6970                                libraries.add(new URL(getURLString(file.getAbsolutePath())));
    70                         String classPath = attr.getValue("Class-Path");
     71                        String classPath = attr.getValue(Attributes.Name.CLASS_PATH);
    7172                        if (classPath != null) {
    7273                                String[] cp = classPath.split(" ");
     
    112113        public Class<?> loadClass() {
    113114                try {
    114                         URL[] urls = new URL[libraries.size()];
    115                         urls = libraries.toArray(urls);
    116                         ClassLoader loader = URLClassLoader.newInstance(urls, getClass().getClassLoader());
    117                         Class<?> realClass = Class.forName(className, true, loader);
     115                        Class<?> realClass = Class.forName(className, true, getClassLoader());
    118116                        return realClass;
    119117                } catch (Exception e) {
     
    127125                return "file://"+fileName;
    128126        }
     127
     128        /**
     129         * Returns all libraries the plugin needs (the plugin's jar file itself and all jars declared
     130         * in the manifest's Class-Path section).
     131         * @return the libraries a list of URLs to the libraries.
     132         */
     133        public List<URL> getLibraries() {
     134                return libraries;
     135        }
     136
     137        /**
     138     * @return the classLoader
     139     */
     140    public ClassLoader getClassLoader() {
     141                // if I have no classloader set, create one for me and my libraries:
     142                if(classLoader == null) {
     143                        URL[] urls = new URL[libraries.size()];
     144                        urls = libraries.toArray(urls);
     145                        classLoader = URLClassLoader.newInstance(urls, getClass().getClassLoader());
     146                }
     147        return classLoader;
     148    }
     149
     150        /**
     151     * @param classLoader the classLoader to set
     152     */
     153    public void setClassLoader(ClassLoader classLoader) {
     154        this.classLoader = classLoader;
     155    }
    129156       
    130157        /**
     
    190217    }
    191218}
     219
Note: See TracChangeset for help on using the changeset viewer.