Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-44898] Add findResource to PluginFirstClassLoader (#2916)
* [FIXED JENKINS-44898] Add findResource to PluginFirstClassLoader This fixes GroovyClassLoader.loadClass for a .groovy file in a plugin with a PluginFirstClassLoader, specifically by fixing fast-loading via the UberClassLoader. * Move common code to AntWithFindResourceClassLoader. * Adding testing of new PluginFirstClassLoader behavior.
- Loading branch information
1 parent
a5dc255
commit 303a9f7
Showing
5 changed files
with
91 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
core/src/main/java/jenkins/util/AntWithFindResourceClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package jenkins.util; | ||
|
||
import org.apache.tools.ant.Project; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.net.URL; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.Vector; | ||
|
||
/** | ||
* As of 1.8.0, {@link org.apache.tools.ant.AntClassLoader} doesn't implement {@link #findResource(String)} | ||
* in any meaningful way, which breaks fast lookup. Implement it properly. | ||
*/ | ||
public class AntWithFindResourceClassLoader extends AntClassLoader implements Closeable { | ||
private final Vector pathComponents; | ||
|
||
public AntWithFindResourceClassLoader(ClassLoader parent, boolean parentFirst) { | ||
super(parent, parentFirst); | ||
|
||
try { | ||
Field $pathComponents = AntClassLoader.class.getDeclaredField("pathComponents"); | ||
$pathComponents.setAccessible(true); | ||
pathComponents = (Vector)$pathComponents.get(this); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
public void addPathFiles(Collection<File> paths) throws IOException { | ||
for (File f : paths) | ||
addPathFile(f); | ||
} | ||
|
||
public void close() throws IOException { | ||
cleanup(); | ||
} | ||
|
||
@Override | ||
protected URL findResource(String name) { | ||
URL url = null; | ||
|
||
// try and load from this loader if the parent either didn't find | ||
// it or wasn't consulted. | ||
Enumeration e = pathComponents.elements(); | ||
while (e.hasMoreElements() && url == null) { | ||
File pathComponent = (File) e.nextElement(); | ||
url = getResourceURL(pathComponent, name); | ||
if (url != null) { | ||
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG); | ||
} | ||
} | ||
|
||
return url; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.