Skip to content

Commit f8b1e63

Browse files
committed
More efficient JarURLConnection implementation
1 parent 8177e69 commit f8b1e63

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

plugin/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsGroovyShell.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
import java.util.logging.Logger;
1515
import edu.umd.cs.findbugs.annotations.CheckForNull;
1616
import edu.umd.cs.findbugs.annotations.NonNull;
17+
import java.io.File;
18+
import java.io.InputStream;
19+
import java.net.MalformedURLException;
20+
import java.net.URI;
21+
import java.net.URLConnection;
22+
import java.net.URLStreamHandler;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
1725
import org.codehaus.groovy.control.CompilationFailedException;
1826
import org.codehaus.groovy.control.CompilationUnit;
1927
import org.codehaus.groovy.control.CompilerConfiguration;
@@ -76,6 +84,46 @@ private static final class CleanGroovyClassLoader extends GroovyClassLoader {
7684
return new CleanClassCollector(unit, su);
7785
}
7886

87+
private static final Pattern JAR_URL = Pattern.compile("jar:(file:/.+[.]jar)!/.+");
88+
89+
// Avoid expensive and (JDK-6956385) leaky implementations of certain JarURLConnection methods:
90+
@Override public URL findResource(String name) {
91+
URL url = super.findResource(name);
92+
if (url != null && url.getProtocol().equals("jar")) {
93+
try {
94+
return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile(), new URLStreamHandler() {
95+
@Override protected URLConnection openConnection(URL url2) throws IOException {
96+
URLConnection delegate = url.openConnection();
97+
return new URLConnection(url2) {
98+
@Override public void connect() throws IOException {
99+
delegate.connect();
100+
}
101+
@Override public InputStream getInputStream() throws IOException {
102+
return delegate.getInputStream();
103+
}
104+
@Override public String getHeaderField(String name) {
105+
return delegate.getHeaderField(name);
106+
}
107+
@Override public long getLastModified() {
108+
Matcher m = JAR_URL.matcher(url.toString());
109+
if (m.matches()) {
110+
return new File(URI.create(m.group(1))).lastModified();
111+
}
112+
return delegate.getLastModified();
113+
}
114+
@Override public String getContentEncoding() {
115+
return null;
116+
}
117+
};
118+
}
119+
});
120+
} catch (MalformedURLException x) {
121+
LOGGER.log(Level.WARNING, null, x);
122+
}
123+
}
124+
return url;
125+
}
126+
79127
private final class CleanClassCollector extends ClassCollector {
80128

81129
CleanClassCollector(CompilationUnit unit, SourceUnit su) {

0 commit comments

Comments
 (0)