-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathDatadogClassLoader.java
More file actions
185 lines (166 loc) · 6.02 KB
/
DatadogClassLoader.java
File metadata and controls
185 lines (166 loc) · 6.02 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
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
package datadog.trace.bootstrap;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.security.CodeSource;
import java.security.SecureClassLoader;
import java.security.cert.Certificate;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** Provides access to Datadog internal classes. */
public final class DatadogClassLoader extends SecureClassLoader {
static {
ClassLoader.registerAsParallelCapable();
}
private static final Logger log = LoggerFactory.getLogger(DatadogClassLoader.class);
private final Set<String> definedPackages = new HashSet<>();
private final JarFile agentJarFile;
private final CodeSource agentCodeSource;
private final String agentResourcePrefix;
private final AgentJarIndex agentJarIndex;
private final Object instrumentationClassLoaderLock = new Object();
private volatile WeakReference<InstrumentationClassLoader> instrumentationClassLoader =
new WeakReference<>(new InstrumentationClassLoader(this));
public DatadogClassLoader(final URL agentJarURL, final ClassLoader parent) throws Exception {
super(parent);
agentJarFile = new JarFile(new File(agentJarURL.toURI()), false);
agentCodeSource = new CodeSource(agentJarURL, (Certificate[]) null);
// Build the resource prefix from the agent jar URL rather than JarFile.getName().
// JarFile.getName() returns the OS-native path, which on Windows looks like
// "C:\Datadog\dd-java-agent.jar" — producing a malformed URL ("jar:file:C:\..."), so
// findResource() returns URLs that openStream() cannot read. See APMS-19624 / #6398.
agentResourcePrefix = "jar:" + agentJarURL + "!/";
agentJarIndex = AgentJarIndex.readIndex(agentJarFile);
}
/** For testing purposes only. */
public DatadogClassLoader() {
super(null);
agentCodeSource = null;
agentJarFile = null;
agentResourcePrefix = null;
agentJarIndex = AgentJarIndex.emptyIndex();
}
@Override
public URL getResource(final String name) {
URL bootstrapResource = BootstrapProxy.INSTANCE.getResource(name);
if (null != bootstrapResource) {
return bootstrapResource;
}
return super.getResource(name);
}
@Override
protected URL findResource(String name) {
String entryName = agentJarIndex.resourceEntryName(name);
if (null != entryName) {
JarEntry jarEntry = agentJarFile.getJarEntry(entryName);
if (null != jarEntry) {
String location = agentResourcePrefix + entryName;
try {
return new URL(location);
} catch (Exception e) {
log.warn("Malformed location {}", location);
}
}
}
return null;
}
@Override
protected Enumeration<URL> findResources(String name) {
URL resource = findResource(name);
if (null != resource) {
return Collections.enumeration(Collections.singleton(resource));
}
return Collections.emptyEnumeration();
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith("datadog.trace.instrumentation.")
&& (name.endsWith("$Muzzle")
|| name.endsWith("Instrumentation")
|| name.endsWith("Module"))) {
InstrumentationClassLoader cl;
if (null == (cl = instrumentationClassLoader.get())) {
synchronized (instrumentationClassLoaderLock) {
if (null == (cl = instrumentationClassLoader.get())) {
// previous instance was unloaded, create fresh one
cl = new InstrumentationClassLoader(this);
instrumentationClassLoader = new WeakReference<>(cl);
}
}
}
return cl.loadInstrumentationClass(name, agentCodeSource);
} else if (name.startsWith("com.kenai.jffi")) {
// prefer our embedded JFFI to other versions exposed by the parent class-loader
return loadLocalClass(name, resolve);
} else {
return super.loadClass(name, resolve);
}
}
/** Same as {@link #loadClass(String, boolean)} but it doesn't delegate to the parent. */
private Class<?> loadLocalClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> c = findLoadedClass(name);
if (null == c) {
c = findClass(name);
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] buf = loadClassBytes(name);
return defineClass(name, buf, 0, buf.length, agentCodeSource);
}
byte[] loadClassBytes(String name) throws ClassNotFoundException {
String entryName = agentJarIndex.classEntryName(name);
if (null != entryName) {
JarEntry jarEntry = agentJarFile.getJarEntry(entryName);
if (null != jarEntry) {
byte[] buf = new byte[(int) jarEntry.getSize()];
try (InputStream in = agentJarFile.getInputStream(jarEntry)) {
int bytesRead = in.read(buf);
while (bytesRead < buf.length) {
int delta = in.read(buf, bytesRead, buf.length - bytesRead);
if (delta < 0) {
break;
}
bytesRead += delta;
}
if (bytesRead == buf.length) {
return buf;
} else {
log.warn("Malformed class data at {}", jarEntry);
}
} catch (IOException e) {
log.warn("Problem reading class data at {}", jarEntry, e);
}
}
}
throw new ClassNotFoundException(name);
}
@Override
protected Package getPackage(String name) {
synchronized (definedPackages) {
if (definedPackages.add(name)) {
return definePackage(name, null, null, null, null, null, null, null);
} else {
return super.getPackage(name);
}
}
}
@Override
public String toString() {
return "datadog";
}
}