1515 * limitations under the License.
1616 */
1717package scouter .agent ;
18-
1918import java .lang .instrument .ClassFileTransformer ;
2019import java .lang .instrument .IllegalClassFormatException ;
2120import java .security .ProtectionDomain ;
2221import java .util .ArrayList ;
2322import java .util .List ;
24-
2523import scouter .agent .asm .AddFieldASM ;
2624import scouter .agent .asm .ApicallASM ;
2725import scouter .agent .asm .ApicallInfoASM ;
5452import scouter .org .objectweb .asm .Opcodes ;
5553import scouter .util .FileUtil ;
5654import scouter .util .IntSet ;
57-
5855public class AgentTransformer implements ClassFileTransformer {
5956 public static ThreadLocal <ClassLoader > hookingCtx = new ThreadLocal <ClassLoader >();
6057 private static List <IASM > asms = new ArrayList <IASM >();
61-
6258 // hook 관련 설정이 변경되면 자동으로 변경된다.
6359 private static int hook_signature ;
64-
6560 static {
6661 final Configure conf = Configure .getInstance ();
6762 reload ();
@@ -74,77 +69,61 @@ public void run() {
7469 hook_signature = conf .getHookSignature ();
7570 }
7671 });
77-
7872 }
79-
8073 public static void reload () {
8174 Configure conf = Configure .getInstance ();
82-
8375 List <IASM > temp = new ArrayList <IASM >();
84-
8576 if (conf .enable_hook_service ) {
8677 temp .add (new HttpServiceASM ());
8778 temp .add (new ServiceASM ());
8879 }
89-
9080 if (conf .enable_hook_dbsql ) {
9181 temp .add (new JDBCPreparedStatementASM ());
9282 temp .add (new JDBCResultSetASM ());
9383 temp .add (new JDBCStatementASM ());
9484 temp .add (new SqlMapASM ());
9585 temp .add (new UserTxASM ());
9686 }
97-
9887 if (conf .enable_hook_dbconn ) {
9988 temp .add (new JDBCConnectionOpenASM ());
10089 temp .add (new JDBCDriverASM ());
10190 temp .add (new InitialContextASM ());
10291 }
103-
10492 if (conf .enable_hook_cap ) {
10593 temp .add (new CapArgsASM ());
10694 temp .add (new CapReturnASM ());
10795 temp .add (new CapThisASM ());
10896 }
109-
11097 if (conf .enable_hook_methods ) {
11198 temp .add (new MethodASM ());
11299 temp .add (new ApicallASM ());
113100 temp .add (new ApicallInfoASM ());
114101 temp .add (new SpringReqMapASM ());
115102 }
116-
117103 if (conf .enable_hook_socket ) {
118104 temp .add (new SocketASM ());
119105 }
120-
121106 if (conf .enable_hook_jsp ) {
122107 temp .add (new JspServletASM ());
123108 }
124109 if (conf .enable_hook_async ) {
125110 temp .add (new AddFieldASM ());
126111 }
127-
128112 asms = temp ;
129113 }
130-
131114 // //////////////////////////////////////////////////////////////
132115 // boot class이지만 Hooking되어야하는 클래스를 등록한다.
133116 private static IntSet asynchook = new IntSet ();
134-
135117 static {
136118 asynchook .add ("sun/net/www/protocol/http/HttpURLConnection" .hashCode ());
137119 asynchook .add ("sun/net/www/http/HttpClient" .hashCode ());
138120 asynchook .add ("java/net/Socket" .hashCode ());
139121 asynchook .add ("javax/naming/InitialContext" .hashCode ());
140122 }
141-
142123 private Configure conf = Configure .getInstance ();
143124 private Logger .FileLog bciOut ;
144-
145125 public byte [] transform (ClassLoader loader , String className , Class classBeingRedefined ,
146126 ProtectionDomain protectionDomain , byte [] classfileBuffer ) throws IllegalClassFormatException {
147-
148127 try {
149128 hookingCtx .set (loader );
150129 if (className == null )
@@ -163,31 +142,25 @@ public byte[] transform(ClassLoader loader, String className, Class classBeingRe
163142 }
164143 //
165144 classfileBuffer = DirectPatch .patch (className , classfileBuffer );
166-
167145 ObjTypeDetector .check (className );
168-
169146 final ClassDesc classDesc = new ClassDesc ();
170147 ClassReader cr = new ClassReader (classfileBuffer );
171148 cr .accept (new ClassVisitor (Opcodes .ASM4 ) {
172149 public void visit (int version , int access , String name , String signature , String superName ,
173150 String [] interfaces ) {
174151 classDesc .set (version , access , name , signature , superName , interfaces );
175152 }
176-
177153 @ Override
178154 public AnnotationVisitor visitAnnotation (String desc , boolean visible ) {
179155 classDesc .anotation += desc ;
180156 return super .visitAnnotation (desc , visible );
181157 }
182-
183158 }, 0 );
184-
185159 if (AsmUtil .isInterface (classDesc .access )) {
186160 return null ;
187161 }
188162 classDesc .classBeingRedefined = classBeingRedefined ;
189163 ClassWriter cw = getClassWriter (classDesc );
190-
191164 ClassVisitor cv = cw ;
192165 List <IASM > workAsms = asms ;
193166 for (int i = 0 , max = workAsms .size (); i < max ; i ++) {
@@ -197,7 +170,6 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
197170 cr .accept (cv , ClassReader .EXPAND_FRAMES );
198171 classfileBuffer = cw .toByteArray ();
199172 cv = cw = getClassWriter (classDesc );
200-
201173 if (conf .debug_asm ) {
202174 if (this .bciOut == null ) {
203175 this .bciOut = new Logger .FileLog ("./scouter.bci" );
@@ -213,10 +185,8 @@ public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
213185 } finally {
214186 hookingCtx .set (null );
215187 }
216-
217188 return null ;
218189 }
219-
220190 private ClassWriter getClassWriter (final ClassDesc classDesc ) {
221191 ClassWriter cw ;
222192 switch (classDesc .version ) {
@@ -233,11 +203,8 @@ private ClassWriter getClassWriter(final ClassDesc classDesc) {
233203 }
234204 return cw ;
235205 }
236-
237206 private void dump (String className , byte [] bytes ) {
238207 String fname = "/tmp/" + className .replace ('/' , '_' );
239208 FileUtil .save (fname , bytes );
240-
241209 }
242-
243210}
0 commit comments