1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.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;
/**
* Base implementation of Jar for implementations that use a JarInputStream to access the JAR file.
*/
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 ioe) {
entry = null;
return;
}
}
try {
entry = jarInputStream.getNextJarEntry();
if (multiRelease.booleanValue()) {
// Skip base entries where there is a multi-release entry
// Skip multi-release entries that are not being used
while (entry != null && (mrMap.containsKey(entry.getName()) ||
entry.getName().startsWith("META-INF/versions/") && !mrMap.containsValue(entry.getName()))) {
entry = jarInputStream.getNextJarEntry();
}
} else {
// Skip multi-release entries
while (entry != null && entry.getName().startsWith("META-INF/versions/")) {
entry = jarInputStream.getNextJarEntry();
}
}
} catch (IOException ioe) {
entry = null;
}
}
@Override
public String getEntryName() {
// Given how the entry name is used, there is no requirement to convert
// the name for a multi-release entry to the corresponding base name.
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 {
// Clear the entry so that multiple calls to this method for the
// same entry will result in a new InputStream for each call
// (BZ 60798)
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) {
return "jar:" + getJarFileURL().toExternalForm() + "!/" + entry;
}
@Override
public Manifest getManifest() throws IOException {
reset();
return jarInputStream.getManifest();
}
@Override
public void reset() throws IOException {
closeStream();
entry = null;
jarInputStream = createJarInputStream();
// Only perform multi-release processing on first access
if (multiRelease == null) {
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);
}
}
if (multiRelease.booleanValue()) {
if (mrMap == null) {
populateMrMap();
}
}
}
}
protected void closeStream() {
if (jarInputStream != null) {
try {
jarInputStream.reallyClose();
} catch (IOException ioe) {
// Ignore
}
}
}
protected abstract NonClosingJarInputStream createJarInputStream() throws IOException;
private void gotoEntry(String name) throws IOException {
boolean needsReset = true;
if (multiRelease == null) {
reset();
needsReset = false;
}
// Need to convert requested name to multi-release name (if one exists)
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 = Runtime.version().feature();
Map<String,Integer> mrVersions = new HashMap<>();
JarEntry jarEntry = jarInputStream.getNextJarEntry();
// Tracking the base name and the latest valid version found is
// sufficient to be able to create the renaming map required
while (jarEntry != null) {
String name = jarEntry.getName();
if (name.startsWith("META-INF/versions/") && name.endsWith(".class")) {
// Get the base name and version for this versioned entry
int i = name.indexOf('/', 18);
if (i > 0) {
String baseName = name.substring(i + 1);
int version = Integer.parseInt(name.substring(18, i));
// Ignore any entries targeting for a later version than
// the target for this runtime
if (version <= targetVersion) {
Integer mappedVersion = mrVersions.get(baseName);
if (mappedVersion == null) {
// No version found for this name. Create one.
mrVersions.put(baseName, Integer.valueOf(version));
} else {
// Ignore any entry for which we have already found
// a later version
if (version > mappedVersion.intValue()) {
// Replace the earlier version
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());
}
// Reset stream back to the beginning of the JAR
closeStream();
jarInputStream = createJarInputStream();
}
}
|