Changeset 243 in josm for src/org/openstreetmap/josm/plugins


Ignore:
Timestamp:
2007-05-22T21:58:09+02:00 (17 years ago)
Author:
imi
Message:
  • added josm.plugins - Java property to load plugins regardless of preferences
  • fixed posibility to load Plugins from bootstrap classloader (with full manifest support)
  • added System wide plugin and ressource directories
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

    r215 r243  
    44import java.io.FileInputStream;
    55import java.io.IOException;
     6import java.io.InputStream;
    67import java.net.URL;
    78import java.net.URLClassLoader;
    89import java.util.ArrayList;
     10import java.util.Collection;
    911import java.util.List;
    1012import java.util.Map;
     
    1315import java.util.jar.JarInputStream;
    1416import java.util.jar.Manifest;
     17
     18import org.openstreetmap.josm.Main;
    1519
    1620/**
     
    2125 */
    2226public class PluginInformation {
    23        
    24         /**
    25          * Whether to use the standard classloader to load the plugins or
    26          * an seperate class loader. Note that in case of the standard classloader,
    27          * all plugin files has to be included in the main josm.jar classpath
    28          *
    29          * Set via command line parameter to true.
    30          *
    31          * This switch is intended for debugging JOSM (or prepackaging plugins
    32          * together with JOSM).
    33          */
    34         public static boolean useJosmClassloader = false;
    3527       
    3628        public final File file;
     
    4941         */
    5042        public PluginInformation(File file) {
     43                this(file, file.getName().substring(0, file.getName().length()-4), null);
     44        }
     45       
     46        public PluginInformation(File file, String name, InputStream manifestStream) {
     47                this.name = name;
    5148                this.file = file;
    52                 name = file.getName().substring(0, file.getName().length()-4);
    5349                try {
    54                         JarInputStream jar = new JarInputStream(new FileInputStream(file));
    55                         Manifest manifest = jar.getManifest();
    56                         if (manifest == null)
    57                                 throw new IOException(file+" contains no manifest.");
     50                        Manifest manifest;
     51                        JarInputStream jar = null;
     52                        if (file != null) {
     53                                jar = new JarInputStream(new FileInputStream(file));
     54                                manifest = jar.getManifest();
     55                                if (manifest == null)
     56                                        throw new IOException(file+" contains no manifest.");
     57                        } else {
     58                                manifest = new Manifest();
     59                        manifest.read(manifestStream);
     60                        }
    5861                        Attributes attr = manifest.getMainAttributes();
    5962                        className = attr.getValue("Plugin-Class");
     
    6366                        stage = stageStr == null ? 50 : Integer.parseInt(stageStr);
    6467                        author = attr.getValue("Author");
    65                         libraries.add(new URL(getURLString(file.getAbsolutePath())));
     68                        if (file != null)
     69                                libraries.add(new URL(getURLString(file.getAbsolutePath())));
    6670                        String classPath = attr.getValue("Class-Path");
    6771                        if (classPath != null) {
     
    8589                        for (Object o : attr.keySet())
    8690                                this.attr.put(o.toString(), attr.getValue(o.toString()));
    87                         jar.close();
     91                        if (jar != null)
     92                                jar.close();
    8893                } catch (IOException e) {
    8994                        throw new PluginException(null, name, e);
     
    107112        public Class<?> loadClass() {
    108113                try {
    109                         if (useJosmClassloader)
    110                                 return Class.forName(className);
    111 
    112114                        URL[] urls = new URL[libraries.size()];
    113115                        urls = libraries.toArray(urls);
     
    125127                return "file://"+fileName;
    126128        }
     129       
     130        /**
     131         * Try to find a plugin after some criterias. Extract the plugin-information
     132         * from the plugin and return it. The plugin is searched in the following way:
     133         *
     134         *<li>first look after an MANIFEST.MF in the package org.openstreetmap.josm.plugins.<plugin name>
     135         *    (After removing all fancy characters from the plugin name).
     136         *    If found, the plugin is loaded using the bootstrap classloader.
     137         *<li>If not found, look for a jar file in the user specific plugin directory
     138         *    (~/.josm/plugins/<plugin name>.jar)
     139         *<li>If not found and the environment variable JOSM_RESSOURCES + "/plugins/" exist, look there.
     140         *<li>Try for the java property josm.ressources + "/plugins/" (set via java -Djosm.plugins.path=...)
     141         *<li>If the environment variable ALLUSERSPROFILE and APPDATA exist, look in
     142         *    ALLUSERSPROFILE/<the last stuff from APPDATA>/JOSM/plugins.
     143         *    (*sic* There is no easy way under Windows to get the All User's application
     144         *    directory)
     145         *<li>Finally, look in some typical unix paths:<ul>
     146         *    <li>/usr/local/share/josm/plugins/
     147         *    <li>/usr/local/lib/josm/plugins/
     148         *    <li>/usr/share/josm/plugins/
     149         *    <li>/usr/lib/josm/plugins/
     150         *
     151         * If a plugin class or jar file is found earlier in the list but seem not to
     152         * be working, an PluginException is thrown rather than continuing the search.
     153         * This is so JOSM can detect broken user-provided plugins and do not go silently
     154         * ignore them.
     155         *
     156         * The plugin is not initialized. If the plugin is a .jar file, it is not loaded
     157         * (only the manifest is extracted). In the classloader-case, the class is
     158         * bootstraped (e.g. static {} - declarations will run. However, nothing else is done.
     159         *
     160         * @param pluginName The name of the plugin (in all lowercase). E.g. "lang-de"
     161         * @return Information about the plugin or <code>null</code>, if the plugin
     162         *             was nowhere to be found.
     163         * @throws PluginException In case of broken plugins.
     164         */
     165        public static PluginInformation findPlugin(String pluginName) throws PluginException {
     166        String name = pluginName;
     167        name = name.replaceAll("[-. ]", "");
     168        InputStream manifestStream = PluginInformation.class.getResourceAsStream("/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF");
     169        if (manifestStream != null)
     170                return new PluginInformation(null, pluginName, manifestStream);
     171
     172        Collection<String> locations = getPluginLocations();
     173
     174        for (String s : locations) {
     175                File pluginFile = new File(s+"/"+pluginName+".jar");
     176                if (pluginFile.exists()) {
     177                                PluginInformation info = new PluginInformation(pluginFile);
     178                                return info;
     179                }
     180        }
     181        return null;
     182        }
     183
     184        public static Collection<String> getPluginLocations() {
     185            Collection<String> locations = Main.pref.getAllPossiblePreferenceDirs();
     186            Collection<String> all = new ArrayList<String>(locations.size());
     187            for (String s : locations)
     188                all.add(s+"plugins");
     189            return all;
     190    }
    127191}
Note: See TracChangeset for help on using the changeset viewer.