[12322] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.plugins;
|
---|
| 3 |
|
---|
| 4 | import java.net.URL;
|
---|
| 5 | import java.util.ArrayList;
|
---|
[12867] | 6 | import java.util.Arrays;
|
---|
[12322] | 7 | import java.util.Collection;
|
---|
[12867] | 8 | import java.util.Objects;
|
---|
[12322] | 9 |
|
---|
[12336] | 10 | import org.openstreetmap.josm.tools.Logging;
|
---|
| 11 |
|
---|
[12322] | 12 | /**
|
---|
| 13 | * Class loader for JOSM plugins.
|
---|
| 14 | * <p>
|
---|
| 15 | * In addition to the classes in the plugin jar file, it loads classes of required
|
---|
[12867] | 16 | * plugins. The JOSM core classes should be provided by the parent class loader.
|
---|
[12322] | 17 | * @since 12322
|
---|
| 18 | */
|
---|
[14234] | 19 | public class PluginClassLoader extends DynamicURLClassLoader {
|
---|
[12322] | 20 |
|
---|
[12867] | 21 | private final Collection<PluginClassLoader> dependencies;
|
---|
[12322] | 22 |
|
---|
| 23 | static {
|
---|
| 24 | ClassLoader.registerAsParallelCapable();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Create a new PluginClassLoader.
|
---|
| 29 | * @param urls URLs of the plugin jar file (and extra libraries)
|
---|
| 30 | * @param parent the parent class loader (for JOSM core classes)
|
---|
| 31 | * @param dependencies class loaders of required plugin; can be null
|
---|
| 32 | */
|
---|
| 33 | public PluginClassLoader(URL[] urls, ClassLoader parent, Collection<PluginClassLoader> dependencies) {
|
---|
| 34 | super(urls, parent);
|
---|
| 35 | this.dependencies = dependencies == null ? new ArrayList<>() : new ArrayList<>(dependencies);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Add class loader of a required plugin.
|
---|
| 40 | * This plugin will have access to the classes of the dependent plugin
|
---|
| 41 | * @param dependency the class loader of the required plugin
|
---|
[12867] | 42 | * @return {@code true} if the collection of dependencies changed as a result of the call
|
---|
| 43 | * @since 12867
|
---|
[12322] | 44 | */
|
---|
[12867] | 45 | public boolean addDependency(PluginClassLoader dependency) {
|
---|
| 46 | // Add dependency only if not already present (directly or transitively through another one)
|
---|
| 47 | boolean result = !dependencies.contains(Objects.requireNonNull(dependency, "dependency"))
|
---|
[13849] | 48 | && dependencies.stream().noneMatch(pcl -> pcl.dependencies.contains(dependency))
|
---|
[12867] | 49 | && dependencies.add(dependency);
|
---|
| 50 | if (result) {
|
---|
| 51 | // Now, remove top-level single dependencies, which would be children of the added one
|
---|
| 52 | dependencies.removeIf(pcl -> pcl.dependencies.isEmpty() && dependency.dependencies.contains(pcl));
|
---|
| 53 | }
|
---|
| 54 | return result;
|
---|
[12322] | 55 | }
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
|
---|
[12867] | 59 | Class<?> result = findLoadedClass(name);
|
---|
| 60 | if (result == null) {
|
---|
| 61 | for (PluginClassLoader dep : dependencies) {
|
---|
| 62 | try {
|
---|
| 63 | result = dep.loadClass(name, resolve);
|
---|
| 64 | if (result != null) {
|
---|
| 65 | return result;
|
---|
| 66 | }
|
---|
| 67 | } catch (ClassNotFoundException e) {
|
---|
| 68 | // do nothing
|
---|
| 69 | Logging.trace("Plugin class not found in {0}: {1}", dep, e.getMessage());
|
---|
[13581] | 70 | Logging.trace(e);
|
---|
[12322] | 71 | }
|
---|
[12323] | 72 | }
|
---|
[12867] | 73 | result = super.loadClass(name, resolve);
|
---|
[12322] | 74 | }
|
---|
| 75 | if (result != null) {
|
---|
| 76 | return result;
|
---|
| 77 | }
|
---|
| 78 | throw new ClassNotFoundException(name);
|
---|
| 79 | }
|
---|
[12867] | 80 |
|
---|
| 81 | @Override
|
---|
[14232] | 82 | public URL findResource(String name) {
|
---|
| 83 | URL resource = super.findResource(name);
|
---|
| 84 | if (resource == null) {
|
---|
| 85 | for (PluginClassLoader dep : dependencies) {
|
---|
| 86 | resource = dep.findResource(name);
|
---|
| 87 | if (resource != null) {
|
---|
| 88 | break;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | return resource;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | @Override
|
---|
[12867] | 96 | public String toString() {
|
---|
| 97 | return "PluginClassLoader [urls=" + Arrays.toString(getURLs()) +
|
---|
| 98 | (dependencies.isEmpty() ? "" : ", dependencies=" + dependencies) + ']';
|
---|
| 99 | }
|
---|
[12322] | 100 | }
|
---|