01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.tomcat.transform;
05:
06: import com.tc.asm.ClassAdapter;
07: import com.tc.asm.ClassVisitor;
08: import com.tc.asm.MethodAdapter;
09: import com.tc.asm.MethodVisitor;
10: import com.tc.asm.Opcodes;
11: import com.tc.object.bytecode.ByteCodeUtil;
12: import com.tc.object.bytecode.ClassAdapterFactory;
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
19: ClassAdapterFactory {
20:
21: public BootstrapAdapter() {
22: super (null);
23: }
24:
25: private BootstrapAdapter(ClassVisitor cv, ClassLoader caller) {
26: super (cv);
27: }
28:
29: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
30: return new BootstrapAdapter(visitor, loader);
31: }
32:
33: public MethodVisitor visitMethod(int access, String name,
34: String desc, String signature, String[] exceptions) {
35: MethodVisitor mv = super .visitMethod(access, name, desc,
36: signature, exceptions);
37:
38: if ("initClassLoaders".equals(name)) {
39: mv = new InitClassLoadersAdatper(mv);
40: }
41:
42: return mv;
43: }
44:
45: private static class InitClassLoadersAdatper extends MethodAdapter
46: implements Opcodes {
47:
48: public InitClassLoadersAdatper(MethodVisitor mv) {
49: super (mv);
50: }
51:
52: private void nameAndRegisterLoader(String fieldName,
53: String loaderName) {
54: mv.visitVarInsn(ALOAD, 0);
55: mv.visitFieldInsn(GETFIELD,
56: "org/apache/catalina/startup/Bootstrap", fieldName,
57: "Ljava/lang/ClassLoader;");
58: mv.visitTypeInsn(CHECKCAST,
59: ByteCodeUtil.NAMEDCLASSLOADER_CLASS);
60: mv.visitInsn(DUP);
61: mv.visitFieldInsn(GETSTATIC,
62: "com/tc/object/loaders/Namespace",
63: "TOMCAT_NAMESPACE", "Ljava/lang/String;");
64: mv.visitLdcInsn(loaderName);
65: mv
66: .visitMethodInsn(INVOKESTATIC,
67: "com/tc/object/loaders/Namespace",
68: "createLoaderName",
69: "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
70: mv.visitMethodInsn(INVOKEINTERFACE,
71: ByteCodeUtil.NAMEDCLASSLOADER_CLASS,
72: "__tc_setClassLoaderName", "(Ljava/lang/String;)V");
73: mv
74: .visitMethodInsn(
75: INVOKESTATIC,
76: "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
77: "registerGlobalLoader",
78: "(" + ByteCodeUtil.NAMEDCLASSLOADER_TYPE
79: + ")V");
80: }
81:
82: public void visitInsn(int opcode) {
83: if (opcode == RETURN) {
84: nameAndRegisterLoader("commonLoader", "common");
85: nameAndRegisterLoader("catalinaLoader", "catalina");
86: nameAndRegisterLoader("sharedLoader", "shared");
87: }
88: super.visitInsn(opcode);
89: }
90: }
91:
92: }
|