KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > tomcat > transform > WebAppLoaderAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.tomcat.transform;
5
6 import com.tc.asm.ClassAdapter;
7 import com.tc.asm.ClassVisitor;
8 import com.tc.asm.MethodAdapter;
9 import com.tc.asm.MethodVisitor;
10 import com.tc.asm.Opcodes;
11 import com.tc.object.bytecode.ClassAdapterFactory;
12 import com.tc.object.loaders.NamedClassLoader;
13
14 public class WebAppLoaderAdapter extends ClassAdapter implements ClassAdapterFactory {
15
16   public WebAppLoaderAdapter() {
17     super(null);
18   }
19   
20   private WebAppLoaderAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
21     super(cv);
22   }
23   
24   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
25     return new WebAppLoaderAdapter(visitor, loader);
26   }
27
28   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
29     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
30     if ("createClassLoader".equals(name) //
31
&& "()Lorg/apache/catalina/loader/WebappClassLoader;".equals(desc)) { return new CreateClassLoaderAdapter(mv); }
32     return mv;
33   }
34
35   private static class CreateClassLoaderAdapter extends MethodAdapter implements Opcodes {
36
37     public CreateClassLoaderAdapter(MethodVisitor mv) {
38       super(mv);
39     }
40
41     public void visitInsn(int opcode) {
42       if (ARETURN == opcode) {
43         // name the web app loader
44
mv.visitInsn(DUP);
45         mv.visitTypeInsn(CHECKCAST, NamedClassLoader.CLASS);
46         mv.visitFieldInsn(GETSTATIC, "com/tc/object/loaders/Namespace", "TOMCAT_NAMESPACE", "Ljava/lang/String;");
47         mv.visitLdcInsn("context:");
48         mv.visitVarInsn(ALOAD, 0);
49         mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/catalina/Loader", "getContainer",
50                            "()Lorg/apache/catalina/Container;");
51         mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/catalina/Container", "getName", "()Ljava/lang/String;");
52         mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "concat", "(Ljava/lang/String;)Ljava/lang/String;");
53         mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/loaders/Namespace", "createLoaderName",
54                            "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
55         mv.visitMethodInsn(INVOKEINTERFACE, NamedClassLoader.CLASS, "__tc_setClassLoaderName", "(Ljava/lang/String;)V");
56
57         // register the web app loader
58
mv.visitInsn(DUP);
59         mv.visitTypeInsn(CHECKCAST, NamedClassLoader.CLASS);
60         mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
61                            "registerGlobalLoader", "(" + NamedClassLoader.TYPE + ")V");
62       }
63       super.visitInsn(opcode);
64     }
65
66   }
67
68 }
69
Popular Tags