Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED JENKINS-44898] Add findResource to PluginFirstClassLoader #2916

Merged
merged 3 commits into from
Jun 17, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions core/src/main/java/hudson/PluginFirstClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.Project;

/**
* classLoader which use first /WEB-INF/lib/*.jar and /WEB-INF/classes before core classLoader
Expand All @@ -45,7 +48,21 @@ public class PluginFirstClassLoader
extends AntClassLoader
implements Closeable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implements Closeable redundant

{


private final Vector pathComponents;

public PluginFirstClassLoader() {
super();

try {
Field $pathComponents = AntClassLoader.class.getDeclaredField("pathComponents");
$pathComponents.setAccessible(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like you need to introduce a common ancestor with AntClassLoader2 to avoid code duplication.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or we should just stop using AntClassLoader at all. I am not sure why we ever did.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm generally mystified but wanted to go with the least-disruptive-change-possible in case there was some legitimate reason somewhere for it. I'll add a common ancestor.

pathComponents = (Vector)$pathComponents.get(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new Error(e);
}
}

private List<URL> urls = new ArrayList<URL>();

public void addPathFiles( Collection<File> paths )
Expand Down Expand Up @@ -100,6 +117,27 @@ public InputStream getResourceAsStream( String name )
{
InputStream is = super.getResourceAsStream( name );
return is;
}

}

/**
* As of 1.8.0, {@link AntClassLoader} doesn't implement {@link #findResource(String)}
* in any meaningful way, which breaks fast lookup. Implement it properly.
*/
@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;
}
}