Merge branch '2.2.x' into 2.3.x

Closes gh-22998
This commit is contained in:
Phillip Webb
2020-08-18 14:35:45 -07:00
8 changed files with 385 additions and 76 deletions

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.jar;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Permission;
/**
* Base class for extended variants of {@link java.util.jar.JarFile}.
*
* @author Phillip Webb
*/
abstract class AbstractJarFile extends java.util.jar.JarFile {
/**
* Create a new {@link AbstractJarFile}.
* @param file the root jar file.
* @throws IOException on IO error
*/
AbstractJarFile(File file) throws IOException {
super(file);
}
/**
* Return a URL that can be used to access this JAR file. NOTE: the specified URL
* cannot be serialized and or cloned.
* @return the URL
* @throws MalformedURLException if the URL is malformed
*/
protected abstract URL getUrl() throws MalformedURLException;
/**
* Return the {@link JarFileType} of this instance.
* @return the jar file type
*/
protected abstract JarFileType getType();
/**
* Return the security permission for this JAR.
* @return the security permission.
*/
protected abstract Permission getPermission();
/**
* Return an {@link InputStream} for the entire jar contents.
* @return the contents input stream
* @throws IOException on IO error
*/
protected abstract InputStream getInputStream() throws IOException;
/**
* The type of a {@link JarFile}.
*/
enum JarFileType {
DIRECT, NESTED_DIRECTORY, NESTED_JAR
}
}

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.loader.jar;
import java.io.File;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
@@ -24,6 +25,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.security.Permission;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Spliterator;
@@ -52,7 +54,7 @@ import org.springframework.boot.loader.data.RandomAccessDataFile;
* @author Andy Wilkinson
* @since 1.0.0
*/
public class JarFile extends java.util.jar.JarFile implements Iterable<java.util.jar.JarEntry> {
public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.JarEntry> {
private static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
@@ -64,7 +66,7 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
private static final AsciiBytes SIGNATURE_FILE_EXTENSION = new AsciiBytes(".SF");
private final JarFile parent;
private static final String READ_ACTION = "read";
private final RandomAccessDataFile rootFile;
@@ -108,28 +110,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
this(file, "", file, JarFileType.DIRECT);
}
/**
* Create a new JarFile copy based on a given parent.
* @param parent the parent jar
* @throws IOException if the file cannot be read
*/
JarFile(JarFile parent) throws IOException {
super(parent.rootFile.getFile());
super.close();
this.parent = parent;
this.rootFile = parent.rootFile;
this.pathFromRoot = parent.pathFromRoot;
this.data = parent.data;
this.type = parent.type;
this.url = parent.url;
this.urlString = parent.urlString;
this.entries = parent.entries;
this.manifestSupplier = parent.manifestSupplier;
this.manifest = parent.manifest;
this.signed = parent.signed;
this.comment = parent.comment;
}
/**
* Private constructor used to create a new {@link JarFile} either directly or from a
* nested entry.
@@ -141,14 +121,13 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
*/
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarFileType type)
throws IOException {
this(null, rootFile, pathFromRoot, data, null, type, null);
this(rootFile, pathFromRoot, data, null, type, null);
}
private JarFile(JarFile parent, RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data,
JarEntryFilter filter, JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
private JarFile(RandomAccessDataFile rootFile, String pathFromRoot, RandomAccessData data, JarEntryFilter filter,
JarFileType type, Supplier<Manifest> manifestSupplier) throws IOException {
super(rootFile.getFile());
super.close();
this.parent = parent;
this.rootFile = rootFile;
this.pathFromRoot = pathFromRoot;
CentralDirectoryParser parser = new CentralDirectoryParser();
@@ -198,8 +177,9 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
};
}
JarFile getParent() {
return this.parent;
@Override
protected Permission getPermission() {
return new FilePermission(this.rootFile.getFile().getPath(), READ_ACTION);
}
protected final RandomAccessDataFile getRootJarFile() {
@@ -267,6 +247,11 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
return this.entries.getEntry(name);
}
@Override
protected InputStream getInputStream() throws IOException {
return this.data.getInputStream();
}
@Override
public synchronized InputStream getInputStream(ZipEntry entry) throws IOException {
ensureOpen();
@@ -320,9 +305,8 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
}
return null;
};
return new JarFile(this, this.rootFile,
this.pathFromRoot + "!/" + entry.getName().substring(0, name.length() - 1), this.data, filter,
JarFileType.NESTED_DIRECTORY, this.manifestSupplier);
return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName().substring(0, name.length() - 1),
this.data, filter, JarFileType.NESTED_DIRECTORY, this.manifestSupplier);
}
private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException {
@@ -355,7 +339,7 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
return;
}
this.closed = true;
if (this.type == JarFileType.DIRECT && this.parent == null) {
if (this.type == JarFileType.DIRECT) {
this.rootFile.close();
}
}
@@ -377,12 +361,7 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
return this.urlString;
}
/**
* Return a URL that can be used to access this JAR file. NOTE: the specified URL
* cannot be serialized and or cloned.
* @return the URL
* @throws MalformedURLException if the URL is malformed
*/
@Override
public URL getUrl() throws MalformedURLException {
if (this.url == null) {
String file = this.rootFile.getFile().toURI() + this.pathFromRoot + "!/";
@@ -441,7 +420,8 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
return this.pathFromRoot;
}
JarFileType getType() {
@Override
protected JarFileType getType() {
return this.type;
}
@@ -470,15 +450,6 @@ public class JarFile extends java.util.jar.JarFile implements Iterable<java.util
}
}
/**
* The type of a {@link JarFile}.
*/
enum JarFileType {
DIRECT, NESTED_DIRECTORY, NESTED_JAR
}
/**
* An {@link Enumeration} on {@linkplain java.util.jar.JarEntry jar entries}.
*/

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader.jar;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Permission;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
/**
* A wrapper used to create a copy of a {@link JarFile} so that it can be safely closed
* without closing the original.
*
* @author Phillip Webb
*/
class JarFileWrapper extends AbstractJarFile {
private final JarFile parent;
JarFileWrapper(JarFile parent) throws IOException {
super(parent.getRootJarFile().getFile());
this.parent = parent;
super.close();
}
@Override
protected URL getUrl() throws MalformedURLException {
return this.parent.getUrl();
}
@Override
protected JarFileType getType() {
return this.parent.getType();
}
@Override
protected Permission getPermission() {
return this.parent.getPermission();
}
@Override
public Manifest getManifest() throws IOException {
return this.parent.getManifest();
}
@Override
public Enumeration<JarEntry> entries() {
return this.parent.entries();
}
@Override
public JarEntry getJarEntry(String name) {
return this.parent.getJarEntry(name);
}
@Override
public ZipEntry getEntry(String name) {
return this.parent.getEntry(name);
}
@Override
protected InputStream getInputStream() throws IOException {
return this.parent.getInputStream();
}
@Override
public synchronized InputStream getInputStream(ZipEntry ze) throws IOException {
return this.parent.getInputStream(ze);
}
@Override
public String getComment() {
return this.parent.getComment();
}
@Override
public int size() {
return this.parent.size();
}
@Override
public String toString() {
return this.parent.toString();
}
@Override
public String getName() {
return this.parent.getName();
}
static JarFile unwrap(java.util.jar.JarFile jarFile) {
if (jarFile instanceof JarFile) {
return (JarFile) jarFile;
}
if (jarFile instanceof JarFileWrapper) {
return unwrap(((JarFileWrapper) jarFile).parent);
}
throw new IllegalStateException("Not a JarFile or Wrapper");
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.boot.loader.jar;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FilePermission;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -68,11 +67,9 @@ final class JarURLConnection extends java.net.JarURLConnection {
private static final JarEntryName EMPTY_JAR_ENTRY_NAME = new JarEntryName(new StringSequence(""));
private static final String READ_ACTION = "read";
private static final JarURLConnection NOT_FOUND_CONNECTION = JarURLConnection.notFound();
private final JarFile jarFile;
private final AbstractJarFile jarFile;
private Permission permission;
@@ -80,9 +77,9 @@ final class JarURLConnection extends java.net.JarURLConnection {
private final JarEntryName jarEntryName;
private JarEntry jarEntry;
private java.util.jar.JarEntry jarEntry;
private JarURLConnection(URL url, JarFile jarFile, JarEntryName jarEntryName) throws IOException {
private JarURLConnection(URL url, AbstractJarFile jarFile, JarEntryName jarEntryName) throws IOException {
// What we pass to super is ultimately ignored
super(EMPTY_JAR_URL);
this.url = url;
@@ -105,7 +102,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
}
@Override
public JarFile getJarFile() throws IOException {
public java.util.jar.JarFile getJarFile() throws IOException {
connect();
return this.jarFile;
}
@@ -138,7 +135,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
}
@Override
public JarEntry getJarEntry() throws IOException {
public java.util.jar.JarEntry getJarEntry() throws IOException {
if (this.jarEntryName == null || this.jarEntryName.isEmpty()) {
return null;
}
@@ -163,7 +160,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
throw new IOException("no entry name specified");
}
connect();
InputStream inputStream = (this.jarEntryName.isEmpty() ? this.jarFile.getData().getInputStream()
InputStream inputStream = (this.jarEntryName.isEmpty() ? this.jarFile.getInputStream()
: this.jarFile.getInputStream(this.jarEntry));
if (inputStream == null) {
throwFileNotFound(this.jarEntryName, this.jarFile);
@@ -171,7 +168,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
return inputStream;
}
private void throwFileNotFound(Object entry, JarFile jarFile) throws FileNotFoundException {
private void throwFileNotFound(Object entry, AbstractJarFile jarFile) throws FileNotFoundException {
if (Boolean.TRUE.equals(useFastExceptions.get())) {
throw FILE_NOT_FOUND_EXCEPTION;
}
@@ -196,7 +193,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
if (this.jarEntryName.isEmpty()) {
return this.jarFile.size();
}
JarEntry entry = getJarEntry();
java.util.jar.JarEntry entry = getJarEntry();
return (entry != null) ? (int) entry.getSize() : -1;
}
catch (IOException ex) {
@@ -221,7 +218,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
throw FILE_NOT_FOUND_EXCEPTION;
}
if (this.permission == null) {
this.permission = new FilePermission(this.jarFile.getRootJarFile().getFile().getPath(), READ_ACTION);
this.permission = this.jarFile.getPermission();
}
return this.permission;
}
@@ -232,7 +229,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
return 0;
}
try {
JarEntry entry = getJarEntry();
java.util.jar.JarEntry entry = getJarEntry();
return (entry != null) ? entry.getTime() : 0;
}
catch (IOException ex) {
@@ -266,7 +263,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
&& !jarFile.containsEntry(jarEntryName.toString())) {
return NOT_FOUND_CONNECTION;
}
return new JarURLConnection(url, new JarFile(jarFile), jarEntryName);
return new JarURLConnection(url, new JarFileWrapper(jarFile), jarEntryName);
}
private static int indexOfRootSpec(StringSequence file, String pathFromRoot) {