Changeset 243 in josm for src/org/openstreetmap/josm/plugins
- Timestamp:
- 2007-05-22T21:58:09+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/plugins/PluginInformation.java
r215 r243 4 4 import java.io.FileInputStream; 5 5 import java.io.IOException; 6 import java.io.InputStream; 6 7 import java.net.URL; 7 8 import java.net.URLClassLoader; 8 9 import java.util.ArrayList; 10 import java.util.Collection; 9 11 import java.util.List; 10 12 import java.util.Map; … … 13 15 import java.util.jar.JarInputStream; 14 16 import java.util.jar.Manifest; 17 18 import org.openstreetmap.josm.Main; 15 19 16 20 /** … … 21 25 */ 22 26 public class PluginInformation { 23 24 /**25 * Whether to use the standard classloader to load the plugins or26 * 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 classpath28 *29 * Set via command line parameter to true.30 *31 * This switch is intended for debugging JOSM (or prepackaging plugins32 * together with JOSM).33 */34 public static boolean useJosmClassloader = false;35 27 36 28 public final File file; … … 49 41 */ 50 42 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; 51 48 this.file = file; 52 name = file.getName().substring(0, file.getName().length()-4);53 49 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 } 58 61 Attributes attr = manifest.getMainAttributes(); 59 62 className = attr.getValue("Plugin-Class"); … … 63 66 stage = stageStr == null ? 50 : Integer.parseInt(stageStr); 64 67 author = attr.getValue("Author"); 65 libraries.add(new URL(getURLString(file.getAbsolutePath()))); 68 if (file != null) 69 libraries.add(new URL(getURLString(file.getAbsolutePath()))); 66 70 String classPath = attr.getValue("Class-Path"); 67 71 if (classPath != null) { … … 85 89 for (Object o : attr.keySet()) 86 90 this.attr.put(o.toString(), attr.getValue(o.toString())); 87 jar.close(); 91 if (jar != null) 92 jar.close(); 88 93 } catch (IOException e) { 89 94 throw new PluginException(null, name, e); … … 107 112 public Class<?> loadClass() { 108 113 try { 109 if (useJosmClassloader)110 return Class.forName(className);111 112 114 URL[] urls = new URL[libraries.size()]; 113 115 urls = libraries.toArray(urls); … … 125 127 return "file://"+fileName; 126 128 } 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 } 127 191 }
Note:
See TracChangeset
for help on using the changeset viewer.