Changeset 267 in josm
- Timestamp:
- 2007-06-30T22:01:02+02:00 (18 years ago)
- Files:
-
- 5 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/Main.java
r266 r267 234 234 // iterate all plugins and collect all libraries of all plugins: 235 235 List<URL> allPluginLibraries = new ArrayList<URL>(); 236 for (Collection<PluginInformation> c : p.values()) { 237 for (PluginInformation info : c) { 238 allPluginLibraries.addAll(info.getLibraries()); 239 } 240 } 236 for (Collection<PluginInformation> c : p.values()) 237 for (PluginInformation info : c) 238 allPluginLibraries.addAll(info.libraries); 241 239 // create a classloader for all plugins: 242 240 URL[] jarUrls = new URL[allPluginLibraries.size()]; 243 241 jarUrls = allPluginLibraries.toArray(jarUrls); 244 242 URLClassLoader pluginClassLoader = new URLClassLoader(jarUrls, Main.class.getClassLoader()); 245 246 243 247 244 for (Collection<PluginInformation> c : p.values()) { 248 245 for (PluginInformation info : c) { 249 246 try { 250 info.setClassLoader(pluginClassLoader); // set the common classloader 251 Class<?> klass = info.loadClass(); 247 Class<?> klass = info.loadClass(pluginClassLoader); 252 248 ImageProvider.sources.add(0, klass); 253 249 System.out.println("loading "+info.name); -
src/org/openstreetmap/josm/gui/dialogs/HistoryDialog.java
r203 r267 72 72 } 73 73 }; 74 74 75 /** 75 76 * Main table. 3 columns: … … 80 81 81 82 private Map<OsmPrimitive, List<HistoryItem>> cache = new HashMap<OsmPrimitive, List<HistoryItem>>(); 82 private JLabel notLoaded = new JLabel("<html><i> <p align=\"center\">"+tr("Click Reload to refresh list")+"</p></i></html>");83 private JLabel notLoaded = new JLabel("<html><i>"+tr("Click Reload to refresh list")+"</i></html>"); 83 84 private JButton reloadButton = new JButton(tr("Reload"), ImageProvider.get("dialogs/refresh")); 84 85 private JButton revertButton = new JButton(tr("Revert"), ImageProvider.get("dialogs/revert")); … … 88 89 historyPane.setVisible(false); 89 90 notLoaded.setVisible(true); 91 notLoaded.setHorizontalAlignment(JLabel.CENTER); 90 92 91 93 history.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){ … … 131 133 reloadButton.setToolTipText(tr("Reload all currently selected objects and refresh the list.")); 132 134 reloadButton.putClientProperty("help", "Dialog/History/Reload"); 135 133 136 revertButton.addActionListener(new ActionListener(){ 134 137 public void actionPerformed(ActionEvent e) { -
src/org/openstreetmap/josm/plugins/Plugin.java
r237 r267 40 40 public abstract class Plugin { 41 41 42 privateString name;42 String name; 43 43 44 44 public Plugin() { 45 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs(); 46 name = urls[urls.length-1].toString(); 47 if (name.toLowerCase().endsWith(".jar")) { 48 int lastSlash = name.lastIndexOf('/'); 49 name = name.substring(lastSlash+1, name.length()-4); 50 } 45 try { 46 URL[] urls = ((URLClassLoader)getClass().getClassLoader()).getURLs(); 47 name = urls[urls.length-1].toString(); 48 if (name.toLowerCase().endsWith(".jar")) { 49 int lastSlash = name.lastIndexOf('/'); 50 name = name.substring(lastSlash+1, name.length()-4); 51 } 52 } catch (RuntimeException e) { 53 name = "unknown"; 54 } 51 55 } 52 56 53 57 /** 54 58 * @return The name of this plugin. This is the name of the .jar file. 59 * @deprecated Plugins have to know their name by themself. 55 60 */ 56 public final String getName() { 61 @Deprecated public final String getName() { 57 62 return name; 58 63 } 59 64 /** 60 65 * @return The directory for the plugin to store all kind of stuff. 66 * @deprecated Use <code>Main.pref.getPreferencesDir()+"plugins/"+name+"/";</code> instead. 61 67 */ 62 public final String getPluginDir() { 68 @Deprecated public final String getPluginDir() { 63 69 return Main.pref.getPreferencesDir()+"plugins/"+name+"/"; 64 70 } -
src/org/openstreetmap/josm/plugins/PluginException.java
r149 r267 11 11 */ 12 12 public class PluginException extends RuntimeException { 13 p rivatefinal PluginProxy plugin;14 p rivatefinal String name;13 public final PluginProxy plugin; 14 public final String name; 15 15 16 16 public PluginException(PluginProxy plugin, String name, Throwable cause) { … … 19 19 this.name = name; 20 20 } 21 22 public PluginProxy getPlugin() {23 return plugin;24 }25 public String getName() {26 return name;27 }28 21 } -
src/org/openstreetmap/josm/plugins/PluginInformation.java
r250 r267 6 6 import java.io.InputStream; 7 7 import java.net.URL; 8 import java.net.URLClassLoader;9 8 import java.util.ArrayList; 10 9 import java.util.Collection; … … 33 32 public final String author; 34 33 public final int stage; 35 protected final List<URL> libraries = new ArrayList<URL>(); 36 protected ClassLoader classLoader; 34 public final List<URL> libraries = new ArrayList<URL>(); 37 35 38 36 public final Map<String, String> attr = new TreeMap<String, String>(); … … 82 80 s = entry.toString(); 83 81 entry = new StringBuilder(); 84 if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:")) 82 if (!s.startsWith("/") && !s.startsWith("\\") && !s.matches("^.\\:") && file != null) 85 83 s = file.getParent() + File.separator + s; 86 84 libraries.add(new URL(getURLString(s))); … … 111 109 * Load the class of the plugin 112 110 */ 113 public Class<?> loadClass() { 111 public Class<?> loadClass(ClassLoader classLoader) { 114 112 try { 115 Class<?> realClass = Class.forName(className, true, getClassLoader());113 Class<?> realClass = Class.forName(className, true, classLoader); 116 114 return realClass; 117 115 } catch (Exception e) { … … 126 124 } 127 125 128 /**129 * Returns all libraries the plugin needs (the plugin's jar file itself and all jars declared130 * 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 classLoader139 */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 set152 */153 public void setClassLoader(ClassLoader classLoader) {154 this.classLoader = classLoader;155 }156 157 126 /** 158 127 * Try to find a plugin after some criterias. Extract the plugin-information -
src/org/openstreetmap/josm/plugins/PluginProxy.java
r238 r267 24 24 this.plugin = plugin; 25 25 this.info = info; 26 27 // setting name of the plugin by reflection 28 if (plugin instanceof Plugin) { 29 try { 30 Plugin.class.getDeclaredField("name").set(plugin, info.name); 31 } catch (Exception e) { 32 if (e instanceof RuntimeException) 33 throw (RuntimeException)e; 34 throw new RuntimeException(e); 35 } 36 } 26 37 } 27 38 -
src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java
r243 r267 43 43 // Check for an explicit problem when calling a plugin function 44 44 if (e instanceof PluginException) { 45 PluginProxy plugin = ((PluginException)e). getPlugin();45 PluginProxy plugin = ((PluginException)e).plugin; 46 46 if (plugin != null && !plugin.misbehaving) { 47 47 JOptionPane.showMessageDialog(Main.parent, tr("The plugin {0} throwed an exception: {1}\nIt may be outdated. Please contact the plugin's autor.\nThis message will not shown again until JOSM is restarted.", plugin.info.name, e.getMessage())); -
test/org/openstreetmap/josm/data/osm/OsmPrimitiveTest.java
r146 r267 44 44 } 45 45 46 public void testHashCodeReturnsIdIfNotZeroAndNotZeroIfIdIsZero() {47 osm.id = 23;48 assertEquals(23, osm.hashCode());49 osm.id = 0;50 assertTrue(0 != osm.hashCode());51 }52 53 46 public void testVisit() { 54 47 osm.visit(new Visitor(){ -
test/org/openstreetmap/josm/plugins/PluginExceptionTest.java
r153 r267 9 9 PluginException e = new PluginException(new PluginProxy(new String(), null), "42", barEx); 10 10 assertEquals(barEx, e.getCause()); 11 assertEquals("42", e. getName());11 assertEquals("42", e.name); 12 12 } 13 13 -
test/org/openstreetmap/josm/plugins/PluginInformationTest.java
r169 r267 1 1 package org.openstreetmap.josm.plugins; 2 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 3 5 import java.io.File; 6 import java.net.URL; 7 import java.net.URLClassLoader; 8 import java.util.Arrays; 9 import java.util.Collection; 10 import java.util.jar.JarInputStream; 4 11 5 12 import junit.framework.TestCase; 6 13 14 import org.openstreetmap.josm.Main; 15 import org.openstreetmap.josm.data.Preferences; 16 7 17 public class PluginInformationTest extends TestCase { 18 19 @Override protected void setUp() throws Exception { 20 super.setUp(); 21 Main.pref = new Preferences(){ 22 @Override public Collection<String> getAllPossiblePreferenceDirs() { 23 return Arrays.asList(new String[]{getClass().getResource("..").getFile()}); 24 } 25 }; 26 } 8 27 9 28 public void testConstructorExtractsAttributesFromManifest() throws Exception { 10 29 PluginInformation info = new PluginInformation(new File(getClass().getResource("simple.jar").getFile())); 11 30 String s = getClass().getResource(".").getFile(); 12 13 assertEquals(4, info.libraries.size()); 14 assertEquals(s+"foo", info.libraries.get(1).getFile()); 15 assertEquals(s+"bar", info.libraries.get(2).getFile()); 16 assertEquals(s+"C:/Foo%20and%20Bar", info.libraries.get(3).getFile()); 17 18 assertEquals("imi", info.author); 19 assertEquals("Simple", info.className); 20 assertEquals("Simpler", info.description); 21 assertEquals(true, info.early); 31 assertEquals(4, info.libraries.size()); 32 assertEquals(s+"foo", info.libraries.get(1).getFile()); 33 assertEquals(s+"bar", info.libraries.get(2).getFile()); 34 assertEquals(s+"C:/Foo%20and%20Bar", info.libraries.get(3).getFile()); 35 36 assertEquals("imi", info.author); 37 assertEquals("Simple", info.className); 38 assertEquals("Simpler", info.description); 39 assertEquals(true, info.early); 40 } 41 42 public void testConstructorRequiresJarWithManifest() throws Exception { 43 try { 44 new PluginInformation(new File(getClass().getResource("no_manifest.jar").getFile())); 45 fail("Exception because missing manifest excpected"); 46 } catch (PluginException e) { 47 } 48 } 49 50 public void testConstructorWithInputStream() throws Exception { 51 JarInputStream f = new JarInputStream(getClass().getResourceAsStream("simple.jar")); 52 ByteArrayOutputStream out = new ByteArrayOutputStream(); 53 f.getManifest().write(out); 54 55 PluginInformation info = new PluginInformation(null, "simple", new ByteArrayInputStream(out.toByteArray())); 56 assertEquals("Only the 3 external classpaths are added (as we are using bootstrap classpath for plugin", 57 3, info.libraries.size()); 58 } 59 60 public void testLoadClassInstantiatePlugin() throws Exception { 61 PluginInformation info = new PluginInformation(new File(getClass().getResource("working.jar").getFile())); 62 ClassLoader cl = new URLClassLoader(new URL[]{getClass().getResource("working.jar")}); 63 assertNotNull(info.load(info.loadClass(cl))); 64 } 65 66 // This is so the bugtracker always detect coding problems as "plugin problems" 67 public void testLoadThrowsPluginExceptionOnRuntimeException() throws Exception { 68 PluginInformation info = new PluginInformation(new File(getClass().getResource("working.jar").getFile())); 69 try { 70 info.load(null); 71 fail("Exception excpected because null-Class"); 72 } catch (PluginException e) { 73 } 74 try { 75 info.loadClass(null); 76 fail("Exception excpected because null-ClassLoader"); 77 } catch (PluginException e) { 78 } 79 } 80 81 public void testFindPluginReturnsInformationFromBootstrapClasspath() throws Exception { 82 PluginInformation info = PluginInformation.findPlugin("test_simple"); 83 assertEquals("Simpler", info.description); 84 } 85 86 public void testFindPluginReturnsFromPreferencesDirs() throws Exception { 87 PluginInformation info = PluginInformation.findPlugin("simple"); 88 assertEquals("Simpler", info.description); 89 } 90 91 public void testFindPluginForUnknownReturnsNull() throws Exception { 92 assertNull(PluginInformation.findPlugin("asdf")); 93 } 94 95 public void testPluginLocationsReturnModifiedPreferenceLocations() throws Exception { 96 setUp(); 97 Collection<String> locations = PluginInformation.getPluginLocations(); 98 assertEquals(1, locations.size()); 99 assertTrue(locations.iterator().next().endsWith("/plugins")); 22 100 } 23 101 } -
test/org/openstreetmap/josm/tools/XmlObjectParserTest.java
r193 r267 52 52 } 53 53 54 public void testManyTags() throws Exception {55 StringBuilder b = new StringBuilder("<all>");56 for (int i = 0; i < 50000; ++i) {57 if (Math.random() > 0.5) {58 b.append("<foo bar='blob");59 b.append(i);60 b.append("'/>");61 } else {62 b.append("<foo><bar>yuppel");63 b.append(i);64 b.append("</bar></foo>");65 }66 }67 b.append("</all>");68 69 System.gc();70 long memBefore = Runtime.getRuntime().freeMemory();71 parser = createParser(b.toString());72 Thread.sleep(300);73 System.gc();74 long memAfter = Runtime.getRuntime().freeMemory();75 assertTrue("2MB should be more than enough. "+(memAfter-memBefore), memAfter-memBefore < 2*1024*1024);76 77 for (int i = 0; i < 50000; ++i) {78 Foo f = (Foo)parser.next();79 assertTrue(f.bar.equals("blob"+i) || f.bar.equals("yuppel"+i));80 }81 assertFalse(parser.hasNext());82 }83 84 54 public void testIterable() throws Exception { 85 55 parser = createParser("<xml><foo bar='yo'/><foo bar='yo'/><foo bar='yo'/></xml>");
Note:
See TracChangeset
for help on using the changeset viewer.