Skip to content

Commit

Permalink
[JENKINS-38696] - Allow disabling caching of files in ResourceImageInJar
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-nenashev committed Jul 7, 2017
1 parent 3680607 commit 35c651a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/main/java/hudson/remoting/ResourceImageInJar.java
@@ -1,8 +1,10 @@
package hudson.remoting;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;

Expand All @@ -27,6 +29,14 @@ class ResourceImageInJar extends ResourceImageRef {
* the exact path inside the jar file of the resource.
*/
final String path;

/**
* Disables JAR Caching in {@link JarURLConnection}.
* @since TODO
*/
/*package*/ static boolean DISABLE_FILE_CACHING_IN_JAR_CONNECTION =
Boolean.getBoolean(ResourceImageBoth.class.getName() + ".disableFileCachingInJARConnection");


ResourceImageInJar(long sum1, long sum2, String path) {
this.sum1 = sum1;
Expand All @@ -43,11 +53,24 @@ Future<byte[]> resolve(Channel channel, final String resourcePath) throws IOExce
return new FutureAdapter<byte[],URL>(_resolveJarURL(channel)) {
@Override
protected byte[] adapt(URL jar) throws ExecutionException {
final URLConnection c;
try {
return Util.readFully(toResourceURL(jar,resourcePath).openStream());
URL url = toResourceURL(jar,resourcePath);
c = url.openConnection();
if (DISABLE_FILE_CACHING_IN_JAR_CONNECTION) {
// Disable caching if requested
c.setUseCaches(false);
}
} catch (IOException ex) {
throw new ExecutionException(ex);
}

try(InputStream istream = c.getInputStream()) {
return Util.readFully(istream);
} catch (IOException e) {
throw new ExecutionException(e);
}

}
};
}
Expand Down Expand Up @@ -76,6 +99,10 @@ private URL toResourceURL(URL jar, String resourcePath) throws IOException {
open jar file (see sun.net.www.protocol.jar.JarURLConnection.JarURLInputStream.close())
and leave it open. During unit test, this pins the file, and it prevents the test tear down code
from deleting the file.
Oleg Nenashev:
In newer versions there is a system property, which allows disable JAR File caching
in JARUrlConnection. It should not be used in production instances, but it helps tests to pass at least.
See JENKINS-38696 for more info
*/
return new URL("jar:"+ jar +"!/"+resourcePath);
}
Expand Down

0 comments on commit 35c651a

Please sign in to comment.