mirror of
https://github.com/wahyd4/pinpoint.git
synced 2026-08-20 18:25:55 +10:00
[강운덕] [LUCYSUS-1744] interceptor load 코드 수정.
git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@533 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
@@ -5,62 +5,140 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.profiler.interceptor.*;
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtMethod;
|
||||
import javassist.NotFoundException;
|
||||
|
||||
import com.profiler.interceptor.Interceptor;
|
||||
import com.profiler.interceptor.InterceptorRegistry;
|
||||
import com.profiler.interceptor.StaticAroundInterceptor;
|
||||
|
||||
public class JavaAssistClass implements InstrumentClass {
|
||||
|
||||
private final Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
private final Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
private JavaAssistByteCodeInstrumentor instrumentor;
|
||||
private CtClass ctClass;
|
||||
private JavaAssistByteCodeInstrumentor instrumentor;
|
||||
private CtClass ctClass;
|
||||
|
||||
private final AtomicInteger interceptorNo = new AtomicInteger(1);
|
||||
public JavaAssistClass(JavaAssistByteCodeInstrumentor instrumentor, CtClass ctClass) {
|
||||
this.instrumentor = instrumentor;
|
||||
this.ctClass = ctClass;
|
||||
}
|
||||
|
||||
public JavaAssistClass(JavaAssistByteCodeInstrumentor instrumentor, CtClass ctClass) {
|
||||
this.instrumentor = instrumentor;
|
||||
this.ctClass = ctClass;
|
||||
}
|
||||
@Override
|
||||
public void addInterceptor(String methodName, String[] args, Interceptor interceptor) {
|
||||
int id = InterceptorRegistry.addInterceptor(interceptor);
|
||||
try {
|
||||
CtMethod method = getMethod(methodName, args);
|
||||
if (interceptor instanceof StaticAroundInterceptor) {
|
||||
addAroundInterceptor(methodName, id, method);
|
||||
} else if (interceptor instanceof StaticBeforeInterceptor) {
|
||||
addStaticBeforeInterceptor(methodName, id, method);
|
||||
} else if (interceptor instanceof StaticAfterInterceptor) {
|
||||
addStaticAfterInterceptor(methodName, id, method);
|
||||
}
|
||||
} catch (NotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CannotCompileException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptor(String methodName, String[] args, Interceptor interceptor) {
|
||||
int num = interceptorNo.getAndIncrement();
|
||||
InterceptorRegistry.addInterceptor(num, interceptor);
|
||||
private void addAroundInterceptor(String methodName, int id, CtMethod method) throws NotFoundException, CannotCompileException {
|
||||
addStaticBeforeInterceptor(methodName, id, method);
|
||||
addStaticAfterInterceptor(methodName, id, method);
|
||||
}
|
||||
|
||||
try {
|
||||
CtClass[] params = new CtClass[args.length];
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
params[i] = instrumentor.getClassPool().getCtClass(args[i]);
|
||||
}
|
||||
CtMethod method = ctClass.getDeclaredMethod(methodName, params);
|
||||
|
||||
// if (interceptor instanceof StaticAroundInterceptor) {
|
||||
method.insertBefore("System.out.println(\"request=\" + this.getClass().getClassLoader()); ((com.profiler.interceptor.StaticAroundInterceptor) com.profiler.interceptor.InterceptorRegistry.getInterceptor(" + num + ")).before(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args);");
|
||||
// }
|
||||
private void addStaticAfterInterceptor(String methodName, int id, CtMethod method) throws CannotCompileException, NotFoundException {
|
||||
StringBuilder after = new StringBuilder(1024);
|
||||
after.append("{");
|
||||
addGetStaticAfterInterceptor(after, id);
|
||||
after.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, $_);");
|
||||
after.append("}");
|
||||
String buildAfter = after.toString();
|
||||
if (logger.isLoggable(Level.INFO)) {
|
||||
logger.info("addStaticAfterInterceptor after method:" + method.getLongName() + " code:" + buildAfter);
|
||||
}
|
||||
method.insertAfter(buildAfter);
|
||||
|
||||
} catch (NotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (CannotCompileException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
StringBuilder catchCode = new StringBuilder(1024);
|
||||
catchCode.append("{");
|
||||
addGetStaticAfterInterceptor(catchCode, id);
|
||||
catchCode.append(" interceptor.after(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args, $e);");
|
||||
catchCode.append(" throw $e;");
|
||||
catchCode.append("}");
|
||||
String buildCatch = catchCode.toString();
|
||||
if (logger.isLoggable(Level.INFO)) {
|
||||
logger.info("addStaticAfterInterceptor catch method:" + method.getLongName() + " code:" + buildCatch);
|
||||
}
|
||||
CtClass th = instrumentor.getClassPool().get("java.lang.Throwable");
|
||||
method.addCatch(buildCatch, th);
|
||||
|
||||
}
|
||||
|
||||
private void addGetStaticAfterInterceptor(StringBuilder after, int id) {
|
||||
after.append(" com.profiler.interceptor.StaticAfterInterceptor interceptor = "
|
||||
+ "(com.profiler.interceptor.StaticAfterInterceptor) com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
|
||||
after.append(id);
|
||||
after.append(");");
|
||||
}
|
||||
|
||||
private void addStaticBeforeInterceptor(String methodName, int id, CtMethod method) throws CannotCompileException {
|
||||
StringBuilder code = new StringBuilder(1024);
|
||||
code.append("{");
|
||||
addGetBeforeInterceptor(id, code);
|
||||
code.append(" interceptor.before(this, \"" + ctClass.getName() + "\", \"" + methodName + "\", $args);");
|
||||
code.append("}");
|
||||
String buildBefore = code.toString();
|
||||
if (logger.isLoggable(Level.INFO)) {
|
||||
logger.info("addStaticBeforeInterceptor catch method:" + method.getLongName() + " code:" + buildBefore);
|
||||
}
|
||||
method.insertBefore(buildBefore);
|
||||
}
|
||||
|
||||
private void addGetBeforeInterceptor(int id, StringBuilder code) {
|
||||
code.append(" com.profiler.interceptor.StaticBeforeInterceptor interceptor = "
|
||||
+ "(com.profiler.interceptor.StaticBeforeInterceptor)com.profiler.interceptor.InterceptorRegistry.getInterceptor(");
|
||||
code.append(id);
|
||||
code.append(");");
|
||||
}
|
||||
|
||||
|
||||
private CtMethod getMethod(String methodName, String[] args) throws NotFoundException {
|
||||
CtClass[] params = getCtParameter(args);
|
||||
return ctClass.getDeclaredMethod(methodName, params);
|
||||
}
|
||||
|
||||
private CtClass[] getCtParameter(String[] args) throws NotFoundException {
|
||||
if(args == null) {
|
||||
return null;
|
||||
}
|
||||
CtClass[] params = new CtClass[args.length];
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
params[i] = instrumentor.getClassPool().getCtClass(args[i]);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytecode() {
|
||||
try {
|
||||
return ctClass.toBytecode();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.INFO, "IoException class:" + ctClass.getName() + " " + e.getMessage(), e);
|
||||
} catch (CannotCompileException e) {
|
||||
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class toClass() {
|
||||
try {
|
||||
return ctClass.toClass();
|
||||
} catch (CannotCompileException e) {
|
||||
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytecode() {
|
||||
try {
|
||||
return ctClass.toBytecode();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.INFO, "IoException class:" + ctClass.getName() + " " + e.getMessage(), e);
|
||||
} catch (CannotCompileException e) {
|
||||
logger.log(Level.INFO, "CannotCompileException class:" + ctClass.getName() + " " + e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user