KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
15  * All this adapter does is assign names to the three shared loaders in tomcat (common, catalina, and shared). See
16  * http://tomcat.apache.org/tomcat-5.0-doc/class-loader-howto.html for more info about these loaders
17  */

18 public class BootstrapAdapter extends ClassAdapter implements ClassAdapterFactory {
19
20   public BootstrapAdapter() {
21     super(null);
22   }
23   
24   private BootstrapAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
25     super(cv);
26   }
27   
28   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
29     return new BootstrapAdapter(visitor, loader);
30   }
31
32   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
33     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
34
35     if ("initClassLoaders".equals(name)) {
36       mv = new InitClassLoadersAdatper(mv);
37     }
38
39     return mv;
40   }
41
42   private static class InitClassLoadersAdatper extends MethodAdapter implements Opcodes {
43
44     public InitClassLoadersAdatper(MethodVisitor mv) {
45       super(mv);
46     }
47
48     private void nameAndRegisterLoader(String JavaDoc fieldName, String JavaDoc loaderName) {
49       mv.visitVarInsn(ALOAD, 0);
50       mv.visitFieldInsn(GETFIELD, "org/apache/catalina/startup/Bootstrap", fieldName, "Ljava/lang/ClassLoader;");
51       mv.visitTypeInsn(CHECKCAST, NamedClassLoader.CLASS);
52       mv.visitInsn(DUP);
53       mv.visitFieldInsn(GETSTATIC, "com/tc/object/loaders/Namespace", "TOMCAT_NAMESPACE", "Ljava/lang/String;");
54       mv.visitLdcInsn(loaderName);
55       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/loaders/Namespace", "createLoaderName",
56                          "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
57       mv.visitMethodInsn(INVOKEINTERFACE, NamedClassLoader.CLASS, "__tc_setClassLoaderName", "(Ljava/lang/String;)V");
58       mv.visitMethodInsn(INVOKESTATIC, "com/tc/object/bytecode/hook/impl/ClassProcessorHelper", "registerGlobalLoader",
59                          "(" + NamedClassLoader.TYPE + ")V");
60     }
61
62     public void visitInsn(int opcode) {
63       if (opcode == RETURN) {
64         nameAndRegisterLoader("commonLoader", "common");
65         nameAndRegisterLoader("catalinaLoader", "catalina");
66         nameAndRegisterLoader("sharedLoader", "shared");
67       }
68       super.visitInsn(opcode);
69     }
70   }
71
72 }
73
Popular Tags