-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRemappingUrlConnection.java
More file actions
99 lines (86 loc) · 3.36 KB
/
Copy pathRemappingUrlConnection.java
File metadata and controls
99 lines (86 loc) · 3.36 KB
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
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.tooling;
import static io.opentelemetry.javaagent.tooling.ShadingRemapper.rule;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.security.Permission;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.annotation.Nullable;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.commons.ClassRemapper;
public class RemappingUrlConnection extends URLConnection {
// We need to prefix the names to prevent the gradle shadowJar relocation rules from touching
// them. It's possible to do this by excluding this class from shading, but it may cause issue
// with transitive dependencies down the line.
private static final ShadingRemapper remapper =
new ShadingRemapper(
rule("#io.opentelemetry.api", "#io.opentelemetry.javaagent.shaded.io.opentelemetry.api"),
rule(
"#io.opentelemetry.context",
"#io.opentelemetry.javaagent.shaded.io.opentelemetry.context"),
rule(
"#io.opentelemetry.common",
"#io.opentelemetry.javaagent.shaded.io.opentelemetry.common"),
rule(
"#io.opentelemetry.instrumentation",
"#io.opentelemetry.javaagent.shaded.instrumentation"),
rule(
"#io.opentelemetry.semconv",
"#io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv"),
rule(
"#io.opentelemetry.contrib.awsxray",
"#io.opentelemetry.javaagent.shaded.io.opentelemetry.contrib.awsxray"),
rule(
"#io.opentelemetry.extension.kotlin",
"#io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin"),
rule("#application.io.opentelemetry", "#io.opentelemetry"),
rule("#java.util.logging.Logger", "#io.opentelemetry.javaagent.bootstrap.PatchLogger"));
private final JarFile delegateJarFile;
private final JarEntry entry;
@Nullable private byte[] cacheClassBytes;
public RemappingUrlConnection(URL url, JarFile delegateJarFile, JarEntry entry) {
super(url);
this.delegateJarFile = delegateJarFile;
this.entry = entry;
}
@Override
public void connect() {
connected = true;
}
@Override
public InputStream getInputStream() throws IOException {
if (cacheClassBytes == null) {
cacheClassBytes = readAndRemap();
}
return new ByteArrayInputStream(cacheClassBytes);
}
private byte[] readAndRemap() throws IOException {
try {
InputStream inputStream = delegateJarFile.getInputStream(entry);
return remapClassBytes(inputStream);
} catch (IOException e) {
throw new IOException(
String.format("Failed to remap bytes for %s: %s%n", url.toString(), e.getMessage()));
}
}
private static byte[] remapClassBytes(InputStream in) throws IOException {
ClassReader cr = new ClassReader(in);
ClassWriter cw = new ClassWriter(cr, 0);
cr.accept(new ClassRemapper(cw, remapper), ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
}
@Nullable
@Override
public Permission getPermission() {
// No permissions needed because all classes are in memory
return null;
}
}