Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

2.0 KB
Raw Blame
package opensuite.extensions;

import opensuite.core.ToolModule;

import java.net.URL;
import java.net.URLClassLoader;

/**
 * The class loader of one extension. It tries its own jar (and the jars beside
 * it on its class path) before the suite's, so an extension that bundles its
 * own copy of a library gets the copy it bundled rather than the suite's. Two
 * things always come from the suite: {@code java.*}, which only the platform
 * may define, and {@code opensuite.*}, so that {@code ToolModule} is the same
 * class for the extension as for the suite and an extension can never stand in
 * for a suite class. Resources are looked up as the JDK looks them up, from
 * the suite first.
 */
final class ExtensionClassLoader extends URLClassLoader implements opensuite.core.ExtensionCode {

    private volatile opensuite.core.ExtensionRunner runner;

    ExtensionClassLoader(URL[] urls) {
        // The parent is the loader that defines the contract.
        super(urls, ToolModule.class.getClassLoader());
    }

    /** Say which extension's thread runs the classes of this loader; called before any of them is loaded. */
    void bind(opensuite.core.ExtensionRunner runner) { this.runner = runner; }

    @Override public opensuite.core.ExtensionRunner runner() { return runner; }

    @Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        if (fromSuite(name)) return super.loadClass(name, resolve);
        synchronized (getClassLoadingLock(name)) {
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                try { c = findClass(name); }
                catch (ClassNotFoundException notHere) { return super.loadClass(name, resolve); }
            }
            if (resolve) resolveClass(c);
            return c;
        }
    }

    /** Whether a class of this name is the suite's own and is taken from there first. */
    private static boolean fromSuite(String name) {
        return name.startsWith("java.") || name.startsWith("opensuite.");
    }
}