Skip to content

Commit

Permalink
Align resource loading logic in AntClassLoader with upstream (jenki…
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Aug 15, 2021
1 parent 8e76805 commit 95d9b55
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 93 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/ClassicPluginStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import jenkins.ExtensionFilter;
import jenkins.plugins.DetachedPluginsUtil;
import jenkins.util.AntClassLoader;
import jenkins.util.AntWithFindResourceClassLoader;
import jenkins.util.SystemProperties;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.tools.ant.BuildException;
Expand Down Expand Up @@ -302,7 +301,7 @@ protected ClassLoader createClassLoader(List<File> paths, ClassLoader parent, At
}
}

AntWithFindResourceClassLoader classLoader = new AntWithFindResourceClassLoader(parent, true);
AntClassLoader classLoader = new AntClassLoader(parent, true);
classLoader.addPathFiles(paths);
return classLoader;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/PluginFirstClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.util.Enumeration;
import java.util.List;

import jenkins.util.AntWithFindResourceClassLoader;
import jenkins.util.AntClassLoader;

/**
* classLoader which use first /WEB-INF/lib/*.jar and /WEB-INF/classes before core classLoader
Expand All @@ -41,7 +41,7 @@
* @since 1.371
*/
public class PluginFirstClassLoader
extends AntWithFindResourceClassLoader
extends AntClassLoader
{

public PluginFirstClassLoader() {
Expand Down
17 changes: 8 additions & 9 deletions core/src/main/java/jenkins/ClassLoaderReflectionToolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.util.AntClassLoader;
import jenkins.util.AntWithFindResourceClassLoader;

/**
* Reflective access to various {@link ClassLoader} methods which are otherwise {@code protected}.
Expand Down Expand Up @@ -47,8 +46,8 @@ private static <T extends Exception> Object invoke(Method method, Class<T> excep
}

private static Object getClassLoadingLock(ClassLoader cl, String name) {
if (cl instanceof AntWithFindResourceClassLoader) {
return ((AntWithFindResourceClassLoader) cl).getClassLoadingLock(name);
if (cl instanceof AntClassLoader) {
return ((AntClassLoader) cl).getClassLoadingLock(name);
}
return invoke(GetClassLoadingLock.GET_CLASS_LOADING_LOCK, RuntimeException.class, cl, name);
}
Expand All @@ -75,8 +74,8 @@ private static class GetClassLoadingLock {
public static @CheckForNull Class<?> _findLoadedClass(ClassLoader cl, String name) {
synchronized (getClassLoadingLock(cl, name)) {
Class<?> c;
if (cl instanceof AntWithFindResourceClassLoader) {
c = ((AntWithFindResourceClassLoader) cl).findLoadedClass2(name);
if (cl instanceof AntClassLoader) {
c = ((AntClassLoader) cl).findLoadedClass2(name);
} else {
c = (Class) invoke(FindLoadedClass.FIND_LOADED_CLASS, RuntimeException.class, cl, name);
}
Expand Down Expand Up @@ -132,8 +131,8 @@ private static class FindClass {
*/
public static @CheckForNull URL _findResource(ClassLoader cl, String name) {
URL url;
if (cl instanceof AntWithFindResourceClassLoader) {
url = ((AntWithFindResourceClassLoader) cl).findResource(name);
if (cl instanceof AntClassLoader) {
url = ((AntClassLoader) cl).findResource(name);
} else if (cl instanceof URLClassLoader) {
url = ((URLClassLoader) cl).findResource(name);
} else {
Expand Down Expand Up @@ -162,8 +161,8 @@ private static class FindResource {
*/
public static @NonNull Enumeration<URL> _findResources(ClassLoader cl, String name) throws IOException {
Enumeration<URL> urls;
if (cl instanceof AntWithFindResourceClassLoader) {
urls = ((AntWithFindResourceClassLoader) cl).findResources(name);
if (cl instanceof AntClassLoader) {
urls = ((AntClassLoader) cl).findResources(name);
} else {
urls = (Enumeration<URL>) invoke(FindResources.FIND_RESOURCES, IOException.class, cl, name);
}
Expand Down
78 changes: 54 additions & 24 deletions core/src/main/java/jenkins/util/AntClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
Expand All @@ -48,6 +47,7 @@
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -284,7 +284,6 @@ private void findNextResource() {
* @param classpath The classpath to use to load classes.
*/
public AntClassLoader(final ClassLoader parent, final Project project, final Path classpath) {
super(parent); // KK patch for JENKINS-21579
setParent(parent);
setClassPath(classpath);
setProject(project);
Expand Down Expand Up @@ -371,7 +370,6 @@ public AntClassLoader(final Project project, final Path classpath,
* load the a class through this loader.
*/
public AntClassLoader(final ClassLoader parent, final boolean parentFirst) {
super(parent); // KK patch for JENKINS-21579
setParent(parent);
project = null;
this.parentFirst = parentFirst;
Expand Down Expand Up @@ -514,6 +512,12 @@ public void addPathComponent(final File file) {
pathComponents.addElement(file);
}

public void addPathFiles(Collection<File> paths) throws IOException {
for (File f : paths) {
addPathFile(f);
}
}

/**
* Add a file to the path.
* Reads the manifest, if available, and adds any additional class path jars
Expand Down Expand Up @@ -902,9 +906,7 @@ public URL getResource(final String name) {
if (url != null) {
log("Resource " + name + " loaded from parent loader", Project.MSG_DEBUG);
} else {
// try and load from this loader if the parent either didn't find
// it or wasn't consulted.
url = getUrl(pathComponents, name);
url = getUrl(name);
}
if (url == null && !isParentFirst(name)) {
// this loader was first but it didn't find it - try the parent
Expand All @@ -924,22 +926,25 @@ public URL getResource(final String name) {
}

/**
* Finds a matching file by iterating pathComponents.
* Finds a matching file by iterating through path components.
*
* @param pathComponents Path to a folder, split into the individual folder names
* @param name File to find
* @return Url to found object
* @return A {@code URL} object for reading the resource, or {@code null} if the
* resource could not be found
*/
@CheckForNull
protected URL getUrl(Iterable<File> pathComponents, String name) {
private URL getUrl(String name) {
URL url = null;

// try and load from this loader if the parent either didn't find
// it or wasn't consulted.
for (final File pathComponent : pathComponents) {
url = getResourceURL(pathComponent, name);
if (url != null) {
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
break;
}
}

return url;
}

Expand All @@ -948,9 +953,6 @@ protected URL getUrl(Iterable<File> pathComponents, String name) {
* data (images, audio, text, etc) that can be accessed by class
* code in a way that is independent of the location of the code.
*
* <p>Would override getResources if that wasn't final in Java
* 1.4.</p>
*
* @param name name of the resource
* @return possible URLs as enumeration
* @throws IOException if something goes wrong
Expand All @@ -962,6 +964,18 @@ public Enumeration<URL> getNamedResources(final String name)
return findResources(name, false);
}

/**
* Finds the resource with the given name.
*
* @param name The resource name
* @return A {@code URL} object for reading the resource, or {@code null} if the
* resource could not be found
*/
@Override
public URL findResource(final String name) {
return getUrl(name);
}

/**
* Returns an enumeration of URLs representing all the resources with the
* given name by searching the class loader's classpath.
Expand All @@ -982,26 +996,21 @@ public Enumeration<URL> findResources(final String name) throws IOException {
*
* @param name The resource name to search for.
* Must not be {@code null}.
* @param parentHasBeenSearched whether ClassLoader.this.parent
* has been searched - will be true if the method is (indirectly)
* called from ClassLoader.getResources
* @param skipParent whether to skip searching the parent first - will be false if the method is
* invoked from {@link #getResources(String)} or {@link #getNamedResources(String)} and true
* if the method is invoked from {@link #findResources(String)}.
* @return an enumeration of URLs for the resources
* @exception IOException if I/O errors occurs (can't happen)
*/
protected Enumeration<URL> findResources(final String name,
final boolean parentHasBeenSearched)
final boolean skipParent)
throws IOException {
final Enumeration<URL> mine = new ResourceEnumeration(name);
Enumeration<URL> base;
if (parent != null && (!parentHasBeenSearched || parent != getParent())) {
if (parent != null && !skipParent) {
// Delegate to the parent:
base = parent.getResources(name);
// Note: could cause overlaps in case
// ClassLoader.this.parent has matches and
// parentHasBeenSearched is true
} else {
// ClassLoader.this.parent is already delegated to for example from
// ClassLoader.getResources, no need:
base = Collections.emptyEnumeration();
}
if (isParentFirst(name)) {
Expand Down Expand Up @@ -1580,6 +1589,27 @@ public String toString() {
return "AntClassLoader[" + getClasspath() + "]";
}

/**
* Public version of {@link ClassLoader#findLoadedClass(String)}
*/
public Class<?> findLoadedClass2(String name) {
return findLoadedClass(name);
}

/**
* Public version of {@link ClassLoader#getClassLoadingLock(String)}
*/
@Override
public Object getClassLoadingLock(String className) {
return super.getClassLoadingLock(className);
}

/** {@inheritDoc} */
@Override
public Enumeration<URL> getResources(String name) throws IOException {
return getNamedResources(name);
}

/** {@inheritDoc} */
@Override
public void close() {
Expand Down

This file was deleted.

0 comments on commit 95d9b55

Please sign in to comment.