8264048: Fix caching in Jar URL connections when an entry is missing
Co-authored-by: Daniel Fuchs <dfuchs@openjdk.org> Reviewed-by: bchristi, dfuchs
This commit is contained in:
parent
bf26a2558f
commit
a611c462f9
@ -644,7 +644,7 @@ public class URLClassPath {
|
||||
URLClassPath.check(url);
|
||||
}
|
||||
uc = url.openConnection();
|
||||
InputStream in = uc.getInputStream();
|
||||
|
||||
if (uc instanceof JarURLConnection) {
|
||||
/* Need to remember the jar file so it can be closed
|
||||
* in a hurry.
|
||||
@ -652,6 +652,8 @@ public class URLClassPath {
|
||||
JarURLConnection juc = (JarURLConnection)uc;
|
||||
jarfile = JarLoader.checkJar(juc.getJarFile());
|
||||
}
|
||||
|
||||
InputStream in = uc.getInputStream();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -117,36 +117,56 @@ public class JarURLConnection extends java.net.JarURLConnection {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void connect() throws IOException {
|
||||
if (!connected) {
|
||||
boolean useCaches = getUseCaches();
|
||||
String entryName = this.entryName;
|
||||
|
||||
/* the factory call will do the security checks */
|
||||
jarFile = factory.get(getJarFileURL(), getUseCaches());
|
||||
URL url = getJarFileURL();
|
||||
// if we have an entry name, and the jarfile is local,
|
||||
// don't put the jar into the cache until after we have
|
||||
// validated that the entry name exists
|
||||
jarFile = entryName == null
|
||||
? factory.get(url, useCaches)
|
||||
: factory.getOrCreate(url, useCaches);
|
||||
|
||||
if ((entryName != null)) {
|
||||
jarEntry = (JarEntry) jarFile.getEntry(entryName);
|
||||
if (jarEntry == null) {
|
||||
try {
|
||||
// only close the jar file if it isn't in the
|
||||
// cache. If the jar file is local, it won't be
|
||||
// in the cache yet, and so will be closed here.
|
||||
factory.closeIfNotCached(url, jarFile);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new FileNotFoundException("JAR entry " + entryName +
|
||||
" not found in " +
|
||||
jarFile.getName());
|
||||
}
|
||||
}
|
||||
|
||||
// we have validated that the entry exists.
|
||||
// if useCaches was requested, update the cache now.
|
||||
if (useCaches && entryName != null) {
|
||||
// someone may have beat us and updated the cache
|
||||
// already - in which case - cacheIfAbsent will
|
||||
// return false. cacheIfAbsent returns true if
|
||||
// our jarFile is in the cache when the method
|
||||
// returns, whether because it put it there or
|
||||
// because it found it there.
|
||||
useCaches = factory.cacheIfAbsent(url, jarFile);
|
||||
}
|
||||
|
||||
/* we also ask the factory the permission that was required
|
||||
* to get the jarFile, and set it as our permission.
|
||||
*/
|
||||
if (getUseCaches()) {
|
||||
if (useCaches) {
|
||||
boolean oldUseCaches = jarFileURLConnection.getUseCaches();
|
||||
jarFileURLConnection = factory.getConnection(jarFile);
|
||||
jarFileURLConnection.setUseCaches(oldUseCaches);
|
||||
}
|
||||
|
||||
if ((entryName != null)) {
|
||||
jarEntry = (JarEntry)jarFile.getEntry(entryName);
|
||||
if (jarEntry == null) {
|
||||
try {
|
||||
if (!getUseCaches()) {
|
||||
jarFile.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
throw new FileNotFoundException("JAR entry " + entryName +
|
||||
" not found in " +
|
||||
jarFile.getName());
|
||||
}
|
||||
}
|
||||
connected = true;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -104,7 +104,7 @@ public class URLJarFile extends JarFile {
|
||||
this.closeController = closeController;
|
||||
}
|
||||
|
||||
private static boolean isFileURL(URL url) {
|
||||
static boolean isFileURL(URL url) {
|
||||
if (url.getProtocol().equalsIgnoreCase("file")) {
|
||||
/*
|
||||
* Consider this a 'file' only if it's a LOCAL file, because
|
||||
|
@ -71,6 +71,75 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
|
||||
return get(url, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create a {@code JarFile} for the given {@code url}.
|
||||
* If {@code useCaches} is true, this method attempts to find
|
||||
* a jar file in the cache, and if so, returns it.
|
||||
* If no jar file is found in the cache, or {@code useCaches}
|
||||
* is false, the method creates a new jar file.
|
||||
* If the URL points to a local file, the returned jar file
|
||||
* will not be put in the cache yet.
|
||||
* The caller should then call {@link #cacheIfAbsent(URL, JarFile)}
|
||||
* with the returned jar file, if updating the cache is desired.
|
||||
* @param url the jar file url
|
||||
* @param useCaches whether the cache should be used
|
||||
* @return a new or cached jar file.
|
||||
* @throws IOException if the jar file couldn't be created
|
||||
*/
|
||||
JarFile getOrCreate(URL url, boolean useCaches) throws IOException {
|
||||
if (useCaches == false) {
|
||||
return get(url, false);
|
||||
}
|
||||
|
||||
if (!URLJarFile.isFileURL(url)) {
|
||||
// A temporary file will be created, we can prepopulate
|
||||
// the cache in this case.
|
||||
return get(url, useCaches);
|
||||
}
|
||||
|
||||
// We have a local file. Do not prepopulate the cache.
|
||||
JarFile result;
|
||||
synchronized (instance) {
|
||||
result = getCachedJarFile(url);
|
||||
}
|
||||
if (result == null) {
|
||||
result = URLJarFile.getJarFile(url, this);
|
||||
}
|
||||
if (result == null)
|
||||
throw new FileNotFoundException(url.toString());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the given jar file if it isn't present in the cache.
|
||||
* Otherwise, does nothing.
|
||||
* @param url the jar file URL
|
||||
* @param jarFile the jar file to close
|
||||
* @return true if the jar file has been closed, false otherwise.
|
||||
* @throws IOException if an error occurs while closing the jar file.
|
||||
*/
|
||||
boolean closeIfNotCached(URL url, JarFile jarFile) throws IOException {
|
||||
JarFile result;
|
||||
synchronized (instance) {
|
||||
result = getCachedJarFile(url);
|
||||
}
|
||||
if (result != jarFile) jarFile.close();
|
||||
return result != jarFile;
|
||||
}
|
||||
|
||||
boolean cacheIfAbsent(URL url, JarFile jarFile) {
|
||||
JarFile cached;
|
||||
synchronized (instance) {
|
||||
String key = urlKey(url);
|
||||
cached = fileCache.get(key);
|
||||
if (cached == null) {
|
||||
fileCache.put(key, jarFile);
|
||||
urlCache.put(jarFile, url);
|
||||
}
|
||||
}
|
||||
return cached == null || cached == jarFile;
|
||||
}
|
||||
|
||||
JarFile get(URL url, boolean useCaches) throws IOException {
|
||||
|
||||
JarFile result;
|
||||
@ -106,7 +175,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
|
||||
|
||||
/**
|
||||
* Callback method of the URLJarFileCloseController to
|
||||
* indicate that the JarFile is close. This way we can
|
||||
* indicate that the JarFile is closed. This way we can
|
||||
* remove the JarFile from the cache
|
||||
*/
|
||||
public void close(JarFile jarFile) {
|
||||
|
@ -71,17 +71,99 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
|
||||
return get(url, true);
|
||||
}
|
||||
|
||||
JarFile get(URL url, boolean useCaches) throws IOException {
|
||||
/**
|
||||
* Get or create a {@code JarFile} for the given {@code url}.
|
||||
* If {@code useCaches} is true, this method attempts to find
|
||||
* a jar file in the cache, and if so, returns it.
|
||||
* If no jar file is found in the cache, or {@code useCaches}
|
||||
* is false, the method creates a new jar file.
|
||||
* If the URL points to a local file, the returned jar file
|
||||
* will not be put in the cache yet.
|
||||
* The caller should then call {@link #cacheIfAbsent(URL, JarFile)}
|
||||
* with the returned jar file, if updating the cache is desired.
|
||||
* @param url the jar file url
|
||||
* @param useCaches whether the cache should be used
|
||||
* @return a new or cached jar file.
|
||||
* @throws IOException if the jar file couldn't be created
|
||||
*/
|
||||
JarFile getOrCreate(URL url, boolean useCaches) throws IOException {
|
||||
if (useCaches == false) {
|
||||
return get(url, false);
|
||||
}
|
||||
URL patched = urlFor(url);
|
||||
if (!URLJarFile.isFileURL(patched)) {
|
||||
// A temporary file will be created, we can prepopulate
|
||||
// the cache in this case.
|
||||
return get(url, useCaches);
|
||||
}
|
||||
|
||||
// We have a local file. Do not prepopulate the cache.
|
||||
JarFile result;
|
||||
synchronized (instance) {
|
||||
result = getCachedJarFile(patched);
|
||||
}
|
||||
if (result == null) {
|
||||
result = URLJarFile.getJarFile(patched, this);
|
||||
}
|
||||
if (result == null)
|
||||
throw new FileNotFoundException(url.toString());
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the given jar file if it isn't present in the cache.
|
||||
* Otherwise, does nothing.
|
||||
* @param url the jar file URL
|
||||
* @param jarFile the jar file to close
|
||||
* @return true if the jar file has been closed, false otherwise.
|
||||
* @throws IOException if an error occurs while closing the jar file.
|
||||
*/
|
||||
boolean closeIfNotCached(URL url, JarFile jarFile) throws IOException {
|
||||
url = urlFor(url);
|
||||
JarFile result;
|
||||
synchronized (instance) {
|
||||
result = getCachedJarFile(url);
|
||||
}
|
||||
if (result != jarFile) jarFile.close();
|
||||
return result != jarFile;
|
||||
}
|
||||
|
||||
boolean cacheIfAbsent(URL url, JarFile jarFile) {
|
||||
try {
|
||||
url = urlFor(url);
|
||||
} catch (IOException x) {
|
||||
// should not happen
|
||||
return false;
|
||||
}
|
||||
JarFile cached;
|
||||
synchronized (instance) {
|
||||
String key = urlKey(url);
|
||||
cached = fileCache.get(key);
|
||||
if (cached == null) {
|
||||
fileCache.put(key, jarFile);
|
||||
urlCache.put(jarFile, url);
|
||||
}
|
||||
}
|
||||
return cached == null || cached == jarFile;
|
||||
}
|
||||
|
||||
private URL urlFor(URL url) throws IOException {
|
||||
if (url.getProtocol().equalsIgnoreCase("file")) {
|
||||
// Deal with UNC pathnames specially. See 4180841
|
||||
|
||||
String host = url.getHost();
|
||||
if (host != null && !host.isEmpty() &&
|
||||
!host.equalsIgnoreCase("localhost")) {
|
||||
!host.equalsIgnoreCase("localhost")) {
|
||||
|
||||
url = new URL("file", "", "//" + host + url.getPath());
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
JarFile get(URL url, boolean useCaches) throws IOException {
|
||||
|
||||
url = urlFor(url);
|
||||
|
||||
JarFile result;
|
||||
JarFile local_result;
|
||||
@ -116,7 +198,7 @@ class JarFileFactory implements URLJarFile.URLJarFileCloseController {
|
||||
|
||||
/**
|
||||
* Callback method of the URLJarFileCloseController to
|
||||
* indicate that the JarFile is close. This way we can
|
||||
* indicate that the JarFile is closed. This way we can
|
||||
* remove the JarFile from the cache
|
||||
*/
|
||||
public void close(JarFile jarFile) {
|
||||
|
171
test/jdk/java/net/URLClassLoader/RemoveJar.java
Normal file
171
test/jdk/java/net/URLClassLoader/RemoveJar.java
Normal file
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8264048
|
||||
*
|
||||
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Missing testjar/
|
||||
* @run main/othervm RemoveJar true true testpkg.Missing testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar true true testpkg.Missing testpkg.Missing testjar/
|
||||
*
|
||||
* @run main/othervm RemoveJar true false testpkg.Test testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar true false testpkg.Test testpkg.Missing testjar/
|
||||
* @run main/othervm RemoveJar true false testpkg.Missing testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar true false testpkg.Missing testpkg.Missing testjar/
|
||||
*
|
||||
* @run main/othervm RemoveJar false true testpkg.Test testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar false true testpkg.Test testpkg.Missing testjar/
|
||||
* @run main/othervm RemoveJar false true testpkg.Missing testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar false true testpkg.Missing testpkg.Missing testjar/
|
||||
*
|
||||
* @run main/othervm RemoveJar false false testpkg.Test testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar false false testpkg.Test testpkg.Missing testjar/
|
||||
* @run main/othervm RemoveJar false false testpkg.Missing testpkg.Test testjar/
|
||||
* @run main/othervm RemoveJar false false testpkg.Missing testpkg.Missing testjar/
|
||||
*
|
||||
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Test badpath
|
||||
*
|
||||
* @summary URLClassLoader.close() doesn't close cached JAR file on Windows when load() fails
|
||||
*/
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.spi.ToolProvider;
|
||||
|
||||
public class RemoveJar {
|
||||
private final static String TEST_PKG = "testpkg";
|
||||
private final static String JAR_DIR = "testjar/" + TEST_PKG;
|
||||
private final static String FILE_NAME = "testjar.jar";
|
||||
private final static ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
private final static PrintStream out = new PrintStream(baos);
|
||||
private final static ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
|
||||
.orElseThrow(() ->
|
||||
new RuntimeException("jar tool not found")
|
||||
);
|
||||
|
||||
private static void buildJar() throws IOException {
|
||||
// create dir
|
||||
mkdir(JAR_DIR);
|
||||
// create file
|
||||
Path path = Paths.get(JAR_DIR);
|
||||
String src = "package " + TEST_PKG + ";\n" +
|
||||
"class Test {}\n";
|
||||
Files.write(Paths.get(JAR_DIR + "/Test.java"), src.getBytes());
|
||||
// compile class
|
||||
compile(JAR_DIR + "/Test.java");
|
||||
// package jar
|
||||
jar("-cf testjar.jar " + JAR_DIR);
|
||||
}
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
buildJar();
|
||||
|
||||
URLClassLoader loader = null;
|
||||
URL url = null;
|
||||
Path path = Paths.get(FILE_NAME);
|
||||
|
||||
boolean useCacheFirst = Boolean.parseBoolean(args[0]);
|
||||
boolean useCacheSecond = Boolean.parseBoolean(args[1]);
|
||||
String firstClass = args[2];
|
||||
String secondClass = args[3];
|
||||
String subPath = args[4];
|
||||
|
||||
try {
|
||||
String path_str = path.toUri().toURL().toString();
|
||||
URLConnection.setDefaultUseCaches("jar", useCacheFirst);
|
||||
|
||||
url = new URL("jar", "", path_str + "!/" + subPath);
|
||||
loader = new URLClassLoader(new URL[]{url});
|
||||
|
||||
loader.loadClass(firstClass);
|
||||
} catch (Exception e) {
|
||||
System.err.println("EXCEPTION: " + e);
|
||||
}
|
||||
|
||||
try {
|
||||
URLConnection.setDefaultUseCaches("jar", useCacheSecond);
|
||||
loader.loadClass(secondClass);
|
||||
} catch (Exception e) {
|
||||
System.err.println("EXCEPTION: " + e);
|
||||
} finally {
|
||||
loader.close();
|
||||
Files.delete(path);
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream<Path> mkpath(String... args) {
|
||||
return Arrays.stream(args).map(d -> Paths.get(".", d.split("/")));
|
||||
}
|
||||
|
||||
private static void mkdir(String cmdline) {
|
||||
System.out.println("mkdir -p " + cmdline);
|
||||
mkpath(cmdline.split(" +")).forEach(p -> {
|
||||
try {
|
||||
Files.createDirectories(p);
|
||||
} catch (IOException x) {
|
||||
throw new UncheckedIOException(x);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void jar(String cmdline) throws IOException {
|
||||
System.out.println("jar " + cmdline);
|
||||
baos.reset();
|
||||
|
||||
// the run method catches IOExceptions, we need to expose them
|
||||
ByteArrayOutputStream baes = new ByteArrayOutputStream();
|
||||
PrintStream err = new PrintStream(baes);
|
||||
PrintStream saveErr = System.err;
|
||||
System.setErr(err);
|
||||
int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));
|
||||
System.setErr(saveErr);
|
||||
if (rc != 0) {
|
||||
String s = baes.toString();
|
||||
if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {
|
||||
throw new ZipException(s);
|
||||
}
|
||||
throw new IOException(s);
|
||||
}
|
||||
}
|
||||
|
||||
/* run javac <args> */
|
||||
private static void compile(String... args) {
|
||||
if (com.sun.tools.javac.Main.compile(args) != 0) {
|
||||
throw new RuntimeException("javac failed: args=" + Arrays.toString(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user