KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > terracotta > modules > struts_1_1 > IncludeTagAdapter


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

5 package org.terracotta.modules.struts_1_1;
6
7 import com.tc.asm.ClassAdapter;
8 import com.tc.asm.ClassVisitor;
9 import com.tc.asm.Label;
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
16 /**
17  * This is a work-around for a problem with the <bean:include> tag in struts. Basically the tag can create a new
18  * response back to the same web application *and* it passes along the session cookie. Given the way we do locking on
19  * the session object, this creates a deadlock. The solution implemented here is to release the session lock before
20  * processing the tag
21  */

22 public class IncludeTagAdapter extends ClassAdapter implements Opcodes, ClassAdapterFactory {
23
24   public IncludeTagAdapter() {
25     super(null);
26   }
27   
28   private IncludeTagAdapter(ClassVisitor cv, ClassLoader JavaDoc caller) {
29     super(cv);
30   }
31   
32   public ClassAdapter create(ClassVisitor visitor, ClassLoader JavaDoc loader) {
33     return new IncludeTagAdapter(visitor, loader);
34   }
35
36   public MethodVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
37     if ("doStartTag".equals(name) && Type.getArgumentTypes(desc).length == 0) {
38       // rename existing method and make it private for good measure
39
return super.visitMethod(ACC_PRIVATE, ByteCodeUtil.METHOD_RENAME_PREFIX + name, desc, signature, exceptions);
40     }
41
42     return super.visitMethod(access, name, desc, signature, exceptions);
43   }
44
45   public void visitEnd() {
46     addSessionLockingSupport();
47     super.visitEnd();
48   }
49
50   private void addSessionLockingSupport() {
51     MethodVisitor mv = super.visitMethod(ACC_PUBLIC, "doStartTag", "()I", null,
52         new String JavaDoc[] { "javax/servlet/jsp/JspException" });
53     mv.visitCode();
54     mv.visitVarInsn(ALOAD, 0);
55     mv.visitFieldInsn(GETFIELD, "org/apache/struts/taglib/bean/IncludeTag", "pageContext",
56         "Ljavax/servlet/jsp/PageContext;");
57     mv.visitMethodInsn(INVOKEVIRTUAL, "javax/servlet/jsp/PageContext", "getSession",
58         "()Ljavax/servlet/http/HttpSession;");
59     mv.visitVarInsn(ASTORE, 1);
60     mv.visitVarInsn(ALOAD, 1);
61     mv.visitTypeInsn(INSTANCEOF, "com/tc/session/SessionSupport");
62     Label l2 = new Label();
63     mv.visitJumpInsn(IFEQ, l2);
64     mv.visitVarInsn(ALOAD, 1);
65     mv.visitTypeInsn(CHECKCAST, "com/tc/session/SessionSupport");
66     mv.visitMethodInsn(INVOKEINTERFACE, "com/tc/session/SessionSupport", "pauseRequest", "()V");
67     mv.visitLabel(l2);
68     mv.visitVarInsn(ALOAD, 0);
69     mv.visitMethodInsn(INVOKESPECIAL, "org/apache/struts/taglib/bean/IncludeTag", ByteCodeUtil.METHOD_RENAME_PREFIX
70         + "doStartTag", "()I");
71     mv.visitVarInsn(ISTORE, 4);
72     Label l4 = new Label();
73     mv.visitJumpInsn(JSR, l4);
74     Label l5 = new Label();
75     mv.visitLabel(l5);
76     mv.visitVarInsn(ILOAD, 4);
77     mv.visitInsn(IRETURN);
78     Label l6 = new Label();
79     mv.visitLabel(l6);
80     mv.visitVarInsn(ASTORE, 3);
81     mv.visitJumpInsn(JSR, l4);
82     mv.visitVarInsn(ALOAD, 3);
83     mv.visitInsn(ATHROW);
84     mv.visitLabel(l4);
85     mv.visitVarInsn(ASTORE, 2);
86     mv.visitVarInsn(ALOAD, 1);
87     mv.visitTypeInsn(INSTANCEOF, "com/tc/session/SessionSupport");
88     Label l9 = new Label();
89     mv.visitJumpInsn(IFEQ, l9);
90     mv.visitVarInsn(ALOAD, 1);
91     mv.visitTypeInsn(CHECKCAST, "com/tc/session/SessionSupport");
92     mv.visitMethodInsn(INVOKEINTERFACE, "com/tc/session/SessionSupport", "resumeRequest", "()V");
93     mv.visitLabel(l9);
94     mv.visitVarInsn(RET, 2);
95     mv.visitTryCatchBlock(l2, l5, l6, null);
96     mv.visitMaxs(0, 0);
97     mv.visitEnd();
98   }
99
100 }
101
Popular Tags