in tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Transformation.java [91:208]
private void scanJar(final String name, final InputStream inputStream, final OutputStream outputStream) throws IOException {
{
final String jar = new File(name).getName();
final String replacement = replacements.getJars().get(jar);
if (replacement != null) {
final File file = Mvn.mvn(replacement);
if (!file.exists()) {
throw new ReplacementNotFoundException("jar", jar, file.getAbsolutePath());
}
log.info(String.format("Replaced %s", name));
IO.copy(file, outputStream);
IO.copy(inputStream, skipped);
return;
}
}
final Jar oldJar = Jar.enter(name);
final Jar jar = Jar.current();
try {
final ZipInputStream zipInputStream = new ZipInputStream(inputStream);
final ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
ZipEntry oldEntry;
while ((oldEntry = zipInputStream.getNextEntry()) != null) {
// TODO: the name may be changed in transformation
final String path = updatePath(oldEntry.getName());
if (skip(path)) {
IO.copy(zipInputStream, skipped);
continue;
}
/*
* If this entry has been patched, skip it
* We will add the patched version at the end
*/
if (isPatched(path, jar)) {
log.debug("Skipping class " + path);
IO.copy(zipInputStream, skipped);
continue;
}
final ZipEntry newEntry = new ZipEntry(path);
// copyAttributes(oldEntry, newEntry);
zipOutputStream.putNextEntry(newEntry);
try {
if (path.endsWith(".class")) {
scanClass(zipInputStream, zipOutputStream);
} else if (isZip(path)) {
if (isExcludedJar(path)) {
IO.copy(zipInputStream, zipOutputStream);
} else {
scanJar(path, zipInputStream, zipOutputStream);
}
} else if (copyUnmodified(path)) {
IO.copy(zipInputStream, zipOutputStream);
} else {
scanResource(path, zipInputStream, zipOutputStream);
}
} finally {
zipOutputStream.closeEntry();
}
}
// If we skipped any classes, add them now
if (jar.hasPatches()) {
log.info("Patching " + jar.getName());
for (final Clazz clazz : jar.getSkipped()) {
log.debug("Applying patch " + clazz.getName());
final ZipEntry newEntry = new ZipEntry(clazz.getName());
zipOutputStream.putNextEntry(newEntry);
// Run any transformations on these classes as well
IO.copy(IO.read(clazz.getFile()), zipOutputStream);
zipOutputStream.closeEntry();
clazz.applied();
}
}
final String jarName = new File(jar.getName()).getName();
if (additions.getResources().containsKey(jarName) && patchResources.exists()) {
final String regex = additions.getResources().get(jarName);
final Pattern pattern = getPattern(regex);
final Dir dir = Dir.of(Dir.class, patchResources);
final List<Resource> resources = dir.files()
.map(file -> Resource.relative(patchResources, file))
.filter(resource -> resource.matches(pattern))
.collect(Collectors.toList());
for (final Resource resource : resources) {
log.info("Adding " + resource.getPath());
final ZipEntry newEntry = new ZipEntry(resource.getPath());
zipOutputStream.putNextEntry(newEntry);
// Run any transformations on these classes as well
IO.copy(IO.read(resource.getFile()), zipOutputStream);
zipOutputStream.closeEntry();
}
}
zipOutputStream.finish();
} catch (IOException e) {
throw new IOException(jar.getPath() + e.getMessage(), e);
} finally {
Jar.exit(oldJar);
}
}