KickJava   Java API By Example, From Geeks To Geeks.

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


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.tomcat.transform;
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.Label;
10 import com.tc.asm.MethodAdapter;
11 import com.tc.asm.MethodVisitor;
12 import com.tc.asm.Opcodes;
13 import com.tc.object.bytecode.ClassAdapterFactory;
14
15 public class ContainerBaseAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
16
17   public ContainerBaseAdapter() {
18     super(null);
19   }
20
21   private ContainerBaseAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
22     super(cv);
23   }
24
25   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
26     return new ContainerBaseAdapter(visitor, loader);
27   }
28
29   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
30     MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
31     if ("<init>".equals(name)) {
32       mv = new PipelineAdapter(mv);
33     }
34     return mv;
35   }
36
37   private static class PipelineAdapter extends MethodAdapter implements Opcodes {
38
39     public PipelineAdapter(MethodVisitor mv) {
40       super(mv);
41     }
42
43     public void visitFieldInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
44       if (PUTFIELD == opcode) {
45         if ("org/apache/catalina/core/ContainerBase".equals(owner) && "pipeline".equals(name)) {
46
47           // check if this is an engine (if so, then install the magic TC pipeline)
48
mv.visitVarInsn(ALOAD, 0);
49           mv.visitTypeInsn(INSTANCEOF, "org/apache/catalina/Engine");
50           Label notEngine = new Label();
51           mv.visitJumpInsn(IFEQ, notEngine);
52           mv.visitInsn(POP);
53           mv.visitVarInsn(ALOAD, 0);
54           mv.visitMethodInsn(INVOKESTATIC, "com/tc/tomcat/session/VersionHelper", "createTerracottaPipeline",
55                              "(Lorg/apache/catalina/Container;)Lorg/apache/catalina/Pipeline;");
56           mv.visitLabel(notEngine);
57         }
58       }
59
60       super.visitFieldInsn(opcode, owner, name, desc);
61     }
62
63   }
64
65 }
66
Popular Tags