package org.apache.tomcat.util.scan;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.jar.JarEntry;
import java.util.jar.Manifest;
import org.apache.tomcat.Jar;
import org.apache.tomcat.util.compat.JreCompat;
public abstract class AbstractInputStreamJar implements Jar {
private final URL jarFileURL;
private NonClosingJarInputStream jarInputStream = null;
private JarEntry entry = null;
private Boolean multiRelease = null;
private Map<String,String> mrMap = null;
public AbstractInputStreamJar(URL jarFileUrl) {
this.jarFileURL = jarFileUrl;
}
@Override
public URL getJarFileURL() {
return jarFileURL;
}
@Override
public void nextEntry() {
if (jarInputStream == null) {
try {
reset();
} catch (IOException e) {
entry = null;
return;
}
}
try {
entry = jarInputStream.getNextJarEntry();
if (multiRelease.booleanValue()) {
while (entry != null &&
(mrMap.keySet().contains(entry.getName()) ||
entry.getName().startsWith("META-INF/versions/") &&
!mrMap.values().contains(entry.getName()))) {
entry = jarInputStream.getNextJarEntry();
}
} else {
while (entry != null && entry.getName().startsWith("META-INF/versions/")) {
entry = jarInputStream.getNextJarEntry();
}
}
} catch (IOException ioe) {
entry = null;
}
}
@Override
public String getEntryName() {
if (entry == null) {
return null;
} else {
return entry.getName();
}
}
@Override
public InputStream getEntryInputStream() throws IOException {
return jarInputStream;
}
@Override
public InputStream getInputStream(String name) throws IOException {
gotoEntry(name);
if (entry == null) {
return null;
} else {
entry = null;
return jarInputStream;
}
}
@Override
public long getLastModified(String name) throws IOException {
gotoEntry(name);
if (entry == null) {
return -1;
} else {
return entry.getTime();
}
}
@Override
public boolean exists(String name) throws IOException {
gotoEntry(name);
return entry != null;
}
@Override
public String getURL(String entry) {
StringBuilder result = new StringBuilder("jar:");
result.append(getJarFileURL().toExternalForm());
result.append("!/");
result.append(entry);
return result.toString();
}
@Override
public Manifest getManifest() throws IOException {
reset();
return jarInputStream.getManifest();
}
@Override
public void reset() throws IOException {
closeStream();
entry = null;
jarInputStream = createJarInputStream();
if (multiRelease == null) {
if (JreCompat.isJre9Available()) {
Manifest manifest = jarInputStream.getManifest();
if (manifest == null) {
multiRelease = Boolean.FALSE;
} else {
String mrValue = manifest.getMainAttributes().getValue("Multi-Release");
if (mrValue == null) {
multiRelease = Boolean.FALSE;
} else {
multiRelease = Boolean.valueOf(mrValue);
}
}
} else {
multiRelease = Boolean.FALSE;
}
if (multiRelease.booleanValue()) {
if (mrMap == null) {
populateMrMap();
}
}
}
}
protected void closeStream() {
if (jarInputStream != null) {
try {
jarInputStream.reallyClose();
} catch (IOException ioe) {
}
}
}
protected abstract NonClosingJarInputStream createJarInputStream() throws IOException;
private void gotoEntry(String name) throws IOException {
boolean needsReset = true;
if (multiRelease == null) {
reset();
needsReset = false;
}
if (multiRelease.booleanValue()) {
String mrName = mrMap.get(name);
if (mrName != null) {
name = mrName;
}
} else if (name.startsWith("META-INF/versions/")) {
entry = null;
return;
}
if (entry != null && name.equals(entry.getName())) {
return;
}
if (needsReset) {
reset();
}
JarEntry jarEntry = jarInputStream.getNextJarEntry();
while (jarEntry != null) {
if (name.equals(jarEntry.getName())) {
entry = jarEntry;
break;
}
jarEntry = jarInputStream.getNextJarEntry();
}
}
private void populateMrMap() throws IOException {
int targetVersion = JreCompat.getInstance().jarFileRuntimeMajorVersion();
Map<String,Integer> mrVersions = new HashMap<>();
JarEntry jarEntry = jarInputStream.getNextJarEntry();
while (jarEntry != null) {
String name = jarEntry.getName();
if (name.startsWith("META-INF/versions/") && name.endsWith(".class")) {
int i = name.indexOf('/', 18);
if (i > 0) {
String baseName = name.substring(i + 1);
int version = Integer.parseInt(name.substring(18, i));
if (version <= targetVersion) {
Integer mappedVersion = mrVersions.get(baseName);
if (mappedVersion == null) {
mrVersions.put(baseName, Integer.valueOf(version));
} else {
if (version > mappedVersion.intValue()) {
mrVersions.put(baseName, Integer.valueOf(version));
}
}
}
}
}
jarEntry = jarInputStream.getNextJarEntry();
}
mrMap = new HashMap<>();
for (Entry<String,Integer> mrVersion : mrVersions.entrySet()) {
mrMap.put(mrVersion.getKey() , "META-INF/versions/" + mrVersion.getValue().toString() +
"/" + mrVersion.getKey());
}
closeStream();
jarInputStream = createJarInputStream();
}
}