001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.plugins;
003
004import java.net.URL;
005import java.util.ArrayList;
006import java.util.Arrays;
007import java.util.Collection;
008import java.util.Objects;
009
010import org.openstreetmap.josm.tools.Logging;
011
012/**
013 * Class loader for JOSM plugins.
014 * <p>
015 * In addition to the classes in the plugin jar file, it loads classes of required
016 * plugins. The JOSM core classes should be provided by the parent class loader.
017 * @since 12322
018 */
019public class PluginClassLoader extends DynamicURLClassLoader {
020
021    private final Collection<PluginClassLoader> dependencies;
022
023    static {
024        ClassLoader.registerAsParallelCapable();
025    }
026
027    /**
028     * Create a new PluginClassLoader.
029     * @param urls URLs of the plugin jar file (and extra libraries)
030     * @param parent the parent class loader (for JOSM core classes)
031     * @param dependencies class loaders of required plugin; can be null
032     */
033    public PluginClassLoader(URL[] urls, ClassLoader parent, Collection<PluginClassLoader> dependencies) {
034        super(urls, parent);
035        this.dependencies = dependencies == null ? new ArrayList<>() : new ArrayList<>(dependencies);
036    }
037
038    /**
039     * Add class loader of a required plugin.
040     * This plugin will have access to the classes of the dependent plugin
041     * @param dependency the class loader of the required plugin
042     * @return {@code true} if the collection of dependencies changed as a result of the call
043     * @since 12867
044     */
045    public boolean addDependency(PluginClassLoader dependency) {
046        // Add dependency only if not already present (directly or transitively through another one)
047        boolean result = !dependencies.contains(Objects.requireNonNull(dependency, "dependency"))
048                && dependencies.stream().noneMatch(pcl -> pcl.dependencies.contains(dependency))
049                && dependencies.add(dependency);
050        if (result) {
051            // Now, remove top-level single dependencies, which would be children of the added one
052            dependencies.removeIf(pcl -> pcl.dependencies.isEmpty() && dependency.dependencies.contains(pcl));
053        }
054        return result;
055    }
056
057    @Override
058    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
059        Class<?> result = findLoadedClass(name);
060        if (result == null) {
061            result = findClassInDependencies(name, resolve);
062            try {
063                // Will delegate to parent.loadClass(name, resolve) if needed
064                result = super.loadClass(name, resolve);
065            } catch (ClassNotFoundException e) {
066                Logging.trace("Plugin class not found in super {0}: {1}", this, e.getMessage());
067                Logging.trace(e);
068            }
069        }
070        // IcedTea-Web JNLPClassLoader overrides loadClass(String) but not loadClass(String, boolean)
071        if (result == null && getParent() != null) {
072            try {
073                result = getParent().loadClass(name);
074            } catch (ClassNotFoundException e) {
075                Logging.trace("Plugin class not found in parent {0}: {1}", getParent(), e.getMessage());
076                Logging.trace(e);
077            }
078        }
079        if (result != null) {
080            return result;
081        }
082        throw new ClassNotFoundException(name);
083    }
084
085    /**
086     * Try to find the specified class in this classes dependencies
087     * @param name The name of the class to find
088     * @param resolve {@code true} to resolve the class
089     * @return the class, if found, otherwise {@code null}
090     */
091    private Class<?> findClassInDependencies(String name, boolean resolve) {
092        for (PluginClassLoader dep : dependencies) {
093            try {
094                Class<?> result = dep.loadClass(name, resolve);
095                if (result != null) {
096                    return result;
097                }
098            } catch (ClassNotFoundException e) {
099                Logging.trace("Plugin class not found in dep {0}: {1}", dep, e.getMessage());
100                Logging.trace(e);
101            }
102        }
103        return null;
104    }
105
106    @Override
107    public URL findResource(String name) {
108        URL resource = super.findResource(name);
109        if (resource == null) {
110            for (PluginClassLoader dep : dependencies) {
111                resource = dep.findResource(name);
112                if (resource != null) {
113                    break;
114                }
115            }
116        }
117        return resource;
118    }
119
120    @Override
121    public String toString() {
122        return "PluginClassLoader [urls=" + Arrays.toString(getURLs()) +
123                (dependencies.isEmpty() ? "" : ", dependencies=" + dependencies) + ']';
124    }
125}