source: josm/trunk/src/org/openstreetmap/josm/plugins/PluginClassLoader.java@ 12322

Last change on this file since 12322 was 12322, checked in by bastiK, 7 years ago

fixed #14901 - restrict plugin classpath

Separate class loader for each plugin instead of unified class loader.

  • Property svn:eol-style set to native
File size: 2.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins;
3
4import java.net.URL;
5import java.net.URLClassLoader;
6import java.util.ArrayList;
7import java.util.Collection;
8
9/**
10 * Class loader for JOSM plugins.
11 * <p>
12 * In addition to the classes in the plugin jar file, it loads classes of required
13 * plugins. The JOSM core classes should be provided by the the parent class loader.
14 * @since 12322
15 */
16public class PluginClassLoader extends URLClassLoader {
17
18 Collection<PluginClassLoader> dependencies;
19
20 static {
21 ClassLoader.registerAsParallelCapable();
22 }
23
24 /**
25 * Create a new PluginClassLoader.
26 * @param urls URLs of the plugin jar file (and extra libraries)
27 * @param parent the parent class loader (for JOSM core classes)
28 * @param dependencies class loaders of required plugin; can be null
29 */
30 public PluginClassLoader(URL[] urls, ClassLoader parent, Collection<PluginClassLoader> dependencies) {
31 super(urls, parent);
32 this.dependencies = dependencies == null ? new ArrayList<>() : new ArrayList<>(dependencies);
33 }
34
35 /**
36 * Add class loader of a required plugin.
37 * This plugin will have access to the classes of the dependent plugin
38 * @param dependency the class loader of the required plugin
39 */
40 public void addDependency(PluginClassLoader dependency) {
41 dependencies.add(dependency);
42 }
43
44 @Override
45 protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
46 for (PluginClassLoader dep : dependencies) {
47 try {
48 Class<?> result = dep.loadClass(name, resolve);
49 if (result != null) {
50 return result;
51 }
52 } catch (ClassNotFoundException e) {}
53 }
54 Class<?> result = super.loadClass(name, resolve);
55 if (result != null) {
56 return result;
57 }
58 throw new ClassNotFoundException(name);
59 }
60}
Note: See TracBrowser for help on using the repository browser.