summaryrefslogtreecommitdiff
path: root/test/jetty/RelocateJettyPackages.java
diff options
context:
space:
mode:
Diffstat (limited to 'test/jetty/RelocateJettyPackages.java')
-rw-r--r--test/jetty/RelocateJettyPackages.java65
1 files changed, 49 insertions, 16 deletions
diff --git a/test/jetty/RelocateJettyPackages.java b/test/jetty/RelocateJettyPackages.java
index 0766388e5..01939f5d2 100644
--- a/test/jetty/RelocateJettyPackages.java
+++ b/test/jetty/RelocateJettyPackages.java
@@ -1,9 +1,10 @@
/*
- * Proof-of-concept bytecode relocator for the Jetty 12 migration.
+ * Build-time bytecode relocator for the Jetty 12 migration.
*
* This is deliberately a small build tool, not YaCy runtime code. It keeps
* Solr's public packages unchanged while moving its private Jetty 9 linkage
- * below net.yacy.solr9.jetty.
+ * below net.yacy.solr9.jetty and its SLF4J 1.7 linkage below
+ * net.yacy.solr9.slf4j.
*/
package net.yacy.test.jetty;
@@ -26,10 +27,12 @@ import org.objectweb.asm.commons.Remapper;
public final class RelocateJettyPackages {
- private static final String SOURCE_INTERNAL = "org/eclipse/jetty";
- private static final String TARGET_INTERNAL = "net/yacy/solr9/jetty";
- private static final String SOURCE_BINARY = "org.eclipse.jetty";
- private static final String TARGET_BINARY = "net.yacy.solr9.jetty";
+ private static final Relocation[] RELOCATIONS = {
+ new Relocation("org/eclipse/jetty", "net/yacy/solr9/jetty",
+ "org.eclipse.jetty", "net.yacy.solr9.jetty"),
+ new Relocation("org/slf4j", "net/yacy/solr9/slf4j",
+ "org.slf4j", "net.yacy.solr9.slf4j")
+ };
private RelocateJettyPackages() {
}
@@ -43,7 +46,7 @@ public final class RelocateJettyPackages {
private static void relocate(final Path input, final Path output) throws IOException {
Files.createDirectories(output.toAbsolutePath().getParent());
- final Remapper remapper = new JettyRemapper();
+ final Remapper remapper = new BridgeRemapper();
try (JarFile source = new JarFile(input.toFile());
JarOutputStream target = new JarOutputStream(Files.newOutputStream(output))) {
@@ -62,8 +65,7 @@ public final class RelocateJettyPackages {
if (entry.getName().endsWith(".class")) {
target.write(relocateClass(content, remapper));
} else if (entry.getName().startsWith("META-INF/services/")) {
- target.write(new String(content, StandardCharsets.UTF_8)
- .replace(SOURCE_BINARY, TARGET_BINARY)
+ target.write(relocateText(new String(content, StandardCharsets.UTF_8))
.getBytes(StandardCharsets.UTF_8));
} else {
target.write(content);
@@ -74,28 +76,42 @@ public final class RelocateJettyPackages {
}
}
- private static final class JettyRemapper extends Remapper {
+ private static final class BridgeRemapper extends Remapper {
- private JettyRemapper() {
+ private BridgeRemapper() {
super(Opcodes.ASM9);
}
@Override
public String map(final String internalName) {
- return replacePrefix(internalName, SOURCE_INTERNAL, TARGET_INTERNAL);
+ return relocateInternalName(internalName);
}
@Override
public Object mapValue(final Object value) {
if (value instanceof String) {
final String text = (String) value;
- return text.replace(SOURCE_BINARY, TARGET_BINARY)
- .replace(SOURCE_INTERNAL, TARGET_INTERNAL);
+ return relocateText(text);
}
return super.mapValue(value);
}
}
+ private static final class Relocation {
+ private final String sourceInternal;
+ private final String targetInternal;
+ private final String sourceBinary;
+ private final String targetBinary;
+
+ private Relocation(final String sourceInternal, final String targetInternal,
+ final String sourceBinary, final String targetBinary) {
+ this.sourceInternal = sourceInternal;
+ this.targetInternal = targetInternal;
+ this.sourceBinary = sourceBinary;
+ this.targetBinary = targetBinary;
+ }
+ }
+
private static byte[] relocateClass(final byte[] content, final Remapper remapper) {
final ClassReader reader = new ClassReader(content);
final ClassWriter writer = new ClassWriter(0);
@@ -120,13 +136,30 @@ public final class RelocateJettyPackages {
}
private static String relocateEntryName(final String name) {
- final String internalName = replacePrefix(name, SOURCE_INTERNAL, TARGET_INTERNAL);
+ final String internalName = relocateInternalName(name);
if (internalName.startsWith("META-INF/services/")) {
- return internalName.replace(SOURCE_BINARY, TARGET_BINARY);
+ return relocateText(internalName);
}
return internalName;
}
+ private static String relocateInternalName(final String name) {
+ String relocated = name;
+ for (final Relocation relocation : RELOCATIONS) {
+ relocated = replacePrefix(relocated, relocation.sourceInternal, relocation.targetInternal);
+ }
+ return relocated;
+ }
+
+ private static String relocateText(final String text) {
+ String relocated = text;
+ for (final Relocation relocation : RELOCATIONS) {
+ relocated = relocated.replace(relocation.sourceBinary, relocation.targetBinary)
+ .replace(relocation.sourceInternal, relocation.targetInternal);
+ }
+ return relocated;
+ }
+
private static byte[] readAll(final InputStream stream) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
stream.transferTo(output);