[유치수] [NOBTS] refactoring

git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-tomcat-profiler/trunk@453 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
Chisu Yu
2012-07-04 10:05:51 +00:00
parent 34cb3fbd1e
commit 08c6f0bd3c
37 changed files with 2257 additions and 1895 deletions
+32 -36
View File
@@ -1,6 +1,7 @@
package com.profiler;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
@@ -14,8 +15,8 @@ public class TomcatTracer implements ClassFileTransformer {
protected String agentArgString = "";
protected Instrumentation instrumentation;
ClassPool classPool;
public static void premain(String agentArgs, Instrumentation inst) {
// TomcatProfiler profiler = new TomcatProfiler(agentArgs, inst);
new TomcatTracer(agentArgs, inst);
}
@@ -27,57 +28,52 @@ public class TomcatTracer implements ClassFileTransformer {
}
@Override
public byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
// System.out.println(className);
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
String javassistClassName = className.replace('/', '.');
if(javassistClassName.startsWith("javax.servlet.") ||
javassistClassName.startsWith("org.apache.tomcat") ) {
if(!javassistClassName.startsWith("org.apache.tomcat.util")) {
byte []result=changeCode(javassistClassName,classfileBuffer);
if(result!=null) return result;
if (javassistClassName.startsWith("javax.servlet.") || javassistClassName.startsWith("org.apache.tomcat")) {
if (!javassistClassName.startsWith("org.apache.tomcat.util")) {
byte[] result = changeCode(javassistClassName, classfileBuffer);
if (result != null)
return result;
}
} else {
// System.out.println("*** "+javassistClassName);
}
return null;
}
private byte[] changeCode(String javassistClassName,byte[] classfileBuffer) {
boolean insertFlag=false;
// System.out.println("*** "+javassistClassName);
// System.out.println("*** ClassPool OK");
private byte[] changeCode(String javassistClassName, byte[] classfileBuffer) {
boolean insertFlag = false;
classPool.insertClassPath(new ByteArrayClassPath(javassistClassName, classfileBuffer));
// System.out.println("*** insertClassPath OK");
try {
if(classPool==null) {
if (classPool == null) {
System.out.println("NULL");
classPool = ClassPool.getDefault();
}
CtClass cc = classPool.get(javassistClassName);
// System.out.println("%%% ClassLoader="+cc.getClass().getClassLoader());
CtMethod[] methodList=cc.getDeclaredMethods();
for(CtMethod method:methodList) {
// System.out.println(method.getLongName());
String methodName=method.getLongName();
if(!method.isEmpty()) {
System.out.println("***inserted instrument code at "+methodName);
// method.insertBefore("{ System.out.println(\"### "+method.getName()+" is called. URL=\"+$1+\" \");}");
method.insertBefore("{ System.out.println(\"--- "+methodName+" is called. \");}");
insertFlag=true;
CtMethod[] methodList = cc.getDeclaredMethods();
for (CtMethod method : methodList) {
String methodName = method.getLongName();
if (!method.isEmpty()) {
System.out.println("***inserted instrument code at " + methodName);
method.insertBefore("{ System.out.println(\"--- " + methodName + " is called. \");}");
insertFlag = true;
}
}
if(insertFlag) {
if (insertFlag) {
byte[] newClassfileBuffer = cc.toBytecode();
System.out.println("@@@"+javassistClassName+"class's new Buffer generated !!!");
System.out.println("@@@" + javassistClassName + "class's new Buffer generated !!!");
return newClassfileBuffer;
}
} catch(Exception e) {
// e.printStackTrace();
System.err.println("!!!!! "+e.getMessage());
} catch (Exception e) {
System.err.println("!!!!! " + e.getMessage());
}
return null;
}