-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
DefaultProfilerPluginContext.java
195 lines (163 loc) · 7.74 KB
/
DefaultProfilerPluginContext.java
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
/*
* Copyright 2014 NAVER Corp.
*
* Licensed 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 com.navercorp.pinpoint.profiler.plugin;
import java.lang.instrument.ClassFileTransformer;
import java.util.ArrayList;
import java.util.List;
import com.navercorp.pinpoint.bootstrap.config.ProfilerConfig;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClass;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentClassPool;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext;
import com.navercorp.pinpoint.bootstrap.instrument.NotFoundInstrumentException;
import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matcher;
import com.navercorp.pinpoint.bootstrap.instrument.matcher.Matchers;
import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback;
import com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScope;
import com.navercorp.pinpoint.bootstrap.plugin.ApplicationTypeDetector;
import com.navercorp.pinpoint.bootstrap.plugin.ProfilerPluginSetupContext;
import com.navercorp.pinpoint.profiler.DefaultAgent;
import com.navercorp.pinpoint.profiler.DynamicTransformService;
import com.navercorp.pinpoint.profiler.context.scope.ConcurrentPool;
import com.navercorp.pinpoint.profiler.context.scope.InterceptorScopeFactory;
import com.navercorp.pinpoint.profiler.context.scope.Pool;
import com.navercorp.pinpoint.profiler.instrument.ClassInjector;
import com.navercorp.pinpoint.profiler.instrument.PluginClassInjector;
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
public class DefaultProfilerPluginContext implements ProfilerPluginSetupContext, InstrumentContext {
private final DefaultAgent agent;
private final ClassInjector classInjector;
private final List<ApplicationTypeDetector> serverTypeDetectors = new ArrayList<ApplicationTypeDetector>();
private final List<ClassFileTransformer> classTransformers = new ArrayList<ClassFileTransformer>();
private final Pool<String, InterceptorScope> interceptorScopePool = new ConcurrentPool<String, InterceptorScope>(new InterceptorScopeFactory());
public DefaultProfilerPluginContext(DefaultAgent agent, ClassInjector classInjector) {
if (agent == null) {
throw new NullPointerException("agent must not be null");
}
if (classInjector == null) {
throw new NullPointerException("classInjector must not be null");
}
this.agent = agent;
this.classInjector = classInjector;
}
@Override
public ProfilerConfig getConfig() {
return agent.getProfilerConfig();
}
public PluginConfig getPluginConfig() {
if (classInjector instanceof PluginClassInjector) {
return ((PluginClassInjector) classInjector).getPluginConfig();
}
return null;
}
@Override
public TraceContext getTraceContext() {
final TraceContext context = agent.getTraceContext();
if (context == null) {
throw new IllegalStateException("TraceContext is not created yet");
}
return context;
}
@Override
public void addApplicationTypeDetector(ApplicationTypeDetector... detectors) {
if (detectors == null) {
return;
}
for (ApplicationTypeDetector detector : detectors) {
serverTypeDetectors.add(detector);
}
}
@Override
public InstrumentClass getInstrumentClass(ClassLoader classLoader, String className, byte[] classFileBuffer) {
if (className == null) {
throw new NullPointerException("className must not be null");
}
try {
final InstrumentClassPool classPool = getClassPool();
return classPool.getClass(this, classLoader, className, classFileBuffer);
} catch (NotFoundInstrumentException e) {
return null;
}
}
@Override
public boolean exist(ClassLoader classLoader, String className) {
if (className == null) {
throw new NullPointerException("className must not be null");
}
final InstrumentClassPool classPool = getClassPool();
return classPool.hasClass(classLoader, className);
}
private InstrumentClassPool getClassPool() {
InstrumentClassPool classPool = agent.getClassPool();
return classPool;
}
@Override
public void addClassFileTransformer(final String targetClassName, final TransformCallback transformCallback) {
if (targetClassName == null) {
throw new NullPointerException("targetClassName must not be null");
}
if (transformCallback == null) {
throw new NullPointerException("transformCallback must not be null");
}
final Matcher matcher = Matchers.newClassNameMatcher(JavaAssistUtils.javaNameToJvmName(targetClassName));
final MatchableClassFileTransformerGuardDelegate guard = new MatchableClassFileTransformerGuardDelegate(this, matcher, transformCallback);
classTransformers.add(guard);
}
@Override
public void addClassFileTransformer(ClassLoader classLoader, String targetClassName, final TransformCallback transformCallback) {
if (targetClassName == null) {
throw new NullPointerException("targetClassName must not be null");
}
if (transformCallback == null) {
throw new NullPointerException("transformCallback must not be null");
}
final ClassFileTransformerGuardDelegate classFileTransformerGuardDelegate = new ClassFileTransformerGuardDelegate(this, transformCallback);
final DynamicTransformService dynamicTransformService = agent.getDynamicTransformService();
dynamicTransformService.addClassFileTransformer(classLoader, targetClassName, classFileTransformerGuardDelegate);
}
@Override
public void retransform(Class<?> target, final TransformCallback transformCallback) {
if (target == null) {
throw new NullPointerException("target must not be null");
}
if (transformCallback == null) {
throw new NullPointerException("transformCallback must not be null");
}
final ClassFileTransformerGuardDelegate classFileTransformerGuardDelegate = new ClassFileTransformerGuardDelegate(this, transformCallback);
final DynamicTransformService dynamicTransformService = agent.getDynamicTransformService();
dynamicTransformService.retransform(target, classFileTransformerGuardDelegate);
}
@Override
public <T> Class<? extends T> injectClass(ClassLoader targetClassLoader, String className) {
if (className == null) {
throw new NullPointerException("className must not be null");
}
return classInjector.injectClass(targetClassLoader, className);
}
public List<ClassFileTransformer> getClassEditors() {
return classTransformers;
}
public List<ApplicationTypeDetector> getApplicationTypeDetectors() {
return serverTypeDetectors;
}
@Override
public InterceptorScope getInterceptorScope(String name) {
if (name == null) {
throw new NullPointerException("name must not be null");
}
return interceptorScopePool.add(name);
}
}