KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > geronimo > transform > TomcatClassLoaderAdapter


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

5 package com.tc.geronimo.transform;
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.MethodAdapter;
10 import com.tc.asm.MethodVisitor;
11 import com.tc.asm.Opcodes;
12 import com.tc.asm.Type;
13 import com.tc.object.bytecode.ByteCodeUtil;
14 import com.tc.object.bytecode.ClassAdapterFactory;
15 import com.tc.object.loaders.NamedClassLoader;
16
17 public class TomcatClassLoaderAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
18
19   public TomcatClassLoaderAdapter() {
20     super(null);
21   }
22
23   private TomcatClassLoaderAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
24     super(cv);
25   }
26
27   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
28     return new TomcatClassLoaderAdapter(visitor, loader);
29   }
30
31   public void visit(int version, int access, String JavaDoc name, String JavaDoc signature, String JavaDoc superName, String JavaDoc[] interfaces) {
32     interfaces = ByteCodeUtil.addInterfaces(interfaces, new String JavaDoc[] { NamedClassLoader.CLASS });
33     super.visit(version, access, name, signature, superName, interfaces);
34   }
35
36   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
37     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
38     if ("<init>".equals(name) && Type.getArgumentTypes(desc).length > 0) {
39       mv = new CstrAdapter(mv);
40     }
41
42     return mv;
43   }
44
45   public void visitEnd() {
46     MethodVisitor mv = super.visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, "__tc_getClassLoaderName",
47                                          "()Ljava/lang/String;", null, null);
48     mv.visitCode();
49     mv.visitFieldInsn(GETSTATIC, "com/tc/object/loaders/Namespace", "GERONIMO_NAMESPACE", "Ljava/lang/String;");
50     mv.visitVarInsn(ALOAD, 0);
51     mv.visitMethodInsn(INVOKEVIRTUAL, "java/net/URLClassLoader", "getParent", "()Ljava/lang/ClassLoader;");
52     mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "toString", "()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(INVOKESTATIC, "com/tc/geronimo/GeronimoLoaderNaming", "adjustName",
56                        "(Ljava/lang/String;)Ljava/lang/String;");
57     mv.visitInsn(ARETURN);
58     mv.visitMaxs(0, 0);
59     mv.visitEnd();
60
61     mv = super.visitMethod(ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, "__tc_setClassLoaderName", "(Ljava/lang/String;)V",
62                            null, null);
63     mv.visitCode();
64     mv.visitTypeInsn(NEW, "java/lang/AssertionError");
65     mv.visitInsn(DUP);
66     mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError", "<init>", "()V");
67     mv.visitInsn(ATHROW);
68     mv.visitMaxs(0, 0);
69     mv.visitEnd();
70
71     super.visitEnd();
72   }
73
74   private static class CstrAdapter extends MethodAdapter implements Opcodes {
75
76     public CstrAdapter(MethodVisitor mv) {
77       super(mv);
78     }
79
80     public void visitInsn(int opcode) {
81       if (opcode == RETURN) {
82         mv.visitVarInsn(ALOAD, 0);
83         mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
84                            "registerGlobalLoader", "(" + NamedClassLoader.TYPE + ")V");
85       }
86       super.visitInsn(opcode);
87     }
88
89   }
90
91 }
92
Popular Tags