001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.modules.lucene_2_0_0;
006:
007: import com.tc.asm.ClassAdapter;
008: import com.tc.asm.ClassVisitor;
009: import com.tc.asm.Label;
010: import com.tc.asm.MethodVisitor;
011: import com.tc.asm.Opcodes;
012: import com.tc.object.bytecode.ByteCodeUtil;
013: import com.tc.object.bytecode.ClassAdapterFactory;
014:
015: public class RAMDirectoryLockAdapter extends ClassAdapter implements
016: ClassAdapterFactory, Opcodes {
017:
018: private static final String IS_SHARED_METHOD = ByteCodeUtil.TC_METHOD_PREFIX
019: + "isShared";
020: private static final String DSO_LOCK_NAME_METHOD = ByteCodeUtil.TC_METHOD_PREFIX
021: + "dsoLockName";
022:
023: private String className;
024:
025: public RAMDirectoryLockAdapter(ClassVisitor cv) {
026: super (cv);
027: }
028:
029: public RAMDirectoryLockAdapter() {
030: super (null);
031: }
032:
033: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
034: return new RAMDirectoryLockAdapter(visitor);
035: }
036:
037: public void visit(int version, int access, String name,
038: String signature, String super Name, String[] interfaces) {
039: super .visit(version, access, name, signature, super Name,
040: interfaces);
041: this .className = name;
042: }
043:
044: public MethodVisitor visitMethod(int access, String name,
045: String desc, String signature, String[] exceptions) {
046: if ("obtain".equals(name) || "release".equals(name)
047: || "isLocked".equals(name)) {
048: name = ByteCodeUtil.TC_METHOD_PREFIX + name;
049: }
050:
051: return super .visitMethod(access, name, desc, signature,
052: exceptions);
053: }
054:
055: public void visitEnd() {
056: addIsShared();
057: addDsoLockName();
058: addObtainWrapper();
059: addReleaseWrapper();
060: addIsLockedWrapper();
061:
062: super .visitEnd();
063: }
064:
065: private void addIsLockedWrapper() {
066: MethodVisitor mv = super .visitMethod(ACC_PUBLIC, "isLocked",
067: "()Z", null, null);
068: mv.visitCode();
069: mv.visitVarInsn(ALOAD, 0);
070: mv.visitMethodInsn(INVOKESPECIAL, className, IS_SHARED_METHOD,
071: "()Z");
072: Label notShared = new Label();
073: mv.visitJumpInsn(IFEQ, notShared);
074: mv.visitVarInsn(ALOAD, 0);
075: mv.visitMethodInsn(INVOKESPECIAL, className,
076: DSO_LOCK_NAME_METHOD, "()Ljava/lang/String;");
077: mv.visitInsn(ICONST_2);
078: mv.visitMethodInsn(INVOKESTATIC,
079: "com/tc/object/bytecode/ManagerUtil", "isLocked",
080: "(Ljava/lang/Object;I)Z");
081: mv.visitInsn(IRETURN);
082: mv.visitLabel(notShared);
083: mv.visitVarInsn(ALOAD, 0);
084: mv.visitMethodInsn(INVOKEVIRTUAL, className, "__tc_isLocked",
085: "()Z");
086: mv.visitInsn(IRETURN);
087: mv.visitMaxs(0, 0);
088: mv.visitEnd();
089: }
090:
091: private void addReleaseWrapper() {
092: MethodVisitor mv = super .visitMethod(ACC_PUBLIC, "release",
093: "()V", null, null);
094: mv.visitCode();
095: mv.visitVarInsn(ALOAD, 0);
096: mv.visitMethodInsn(INVOKESPECIAL, className, IS_SHARED_METHOD,
097: "()Z");
098: Label notShared = new Label();
099: mv.visitJumpInsn(IFEQ, notShared);
100: mv.visitVarInsn(ALOAD, 0);
101: mv.visitMethodInsn(INVOKESPECIAL, className,
102: DSO_LOCK_NAME_METHOD, "()Ljava/lang/String;");
103: mv.visitMethodInsn(INVOKESTATIC,
104: "com/tc/object/bytecode/ManagerUtil", "commitLock",
105: "(Ljava/lang/String;)V");
106: mv.visitInsn(RETURN);
107: mv.visitLabel(notShared);
108: mv.visitVarInsn(ALOAD, 0);
109: mv.visitMethodInsn(INVOKEVIRTUAL, className, "__tc_release",
110: "()V");
111: mv.visitInsn(RETURN);
112: mv.visitMaxs(0, 0);
113: mv.visitEnd();
114: }
115:
116: private void addDsoLockName() {
117: MethodVisitor mv = super .visitMethod(ACC_PRIVATE,
118: DSO_LOCK_NAME_METHOD, "()Ljava/lang/String;", null,
119: null);
120: mv.visitCode();
121: mv.visitVarInsn(ALOAD, 0);
122: mv.visitFieldInsn(GETFIELD, className, "this$0",
123: "Lorg/apache/lucene/store/RAMDirectory;");
124: mv.visitMethodInsn(INVOKEVIRTUAL,
125: "org/apache/lucene/store/RAMDirectory", ByteCodeUtil
126: .fieldGetterMethod("files"),
127: "()Ljava/util/Hashtable;");
128: mv
129: .visitTypeInsn(CHECKCAST,
130: "com/tc/object/bytecode/Manageable");
131: mv.visitMethodInsn(INVOKEINTERFACE,
132: "com/tc/object/bytecode/Manageable", "__tc_managed",
133: "()Lcom/tc/object/TCObject;");
134: mv.visitVarInsn(ASTORE, 1);
135: mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
136: mv.visitInsn(DUP);
137: mv.visitVarInsn(ALOAD, 0);
138: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object",
139: "getClass", "()Ljava/lang/Class;");
140: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName",
141: "()Ljava/lang/String;");
142: mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf",
143: "(Ljava/lang/Object;)Ljava/lang/String;");
144: mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer",
145: "<init>", "(Ljava/lang/String;)V");
146: mv.visitLdcInsn("_");
147: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
148: "append",
149: "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
150: mv.visitVarInsn(ALOAD, 1);
151: mv.visitMethodInsn(INVOKEINTERFACE, "com/tc/object/TCObject",
152: "getObjectID", "()Lcom/tc/object/ObjectID;");
153: mv.visitMethodInsn(INVOKEVIRTUAL, "com/tc/object/ObjectID",
154: "toLong", "()J");
155: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
156: "append", "(J)Ljava/lang/StringBuffer;");
157: mv.visitLdcInsn("_");
158: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
159: "append",
160: "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
161: mv.visitVarInsn(ALOAD, 0);
162: mv.visitFieldInsn(GETFIELD, className, "val$name",
163: "Ljava/lang/String;");
164: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
165: "append",
166: "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
167: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer",
168: "toString", "()Ljava/lang/String;");
169: mv.visitInsn(ARETURN);
170: mv.visitMaxs(0, 0);
171: mv.visitEnd();
172: }
173:
174: private void addIsShared() {
175: MethodVisitor mv = super .visitMethod(ACC_PRIVATE,
176: IS_SHARED_METHOD, "()Z", null, null);
177: mv.visitCode();
178: mv.visitVarInsn(ALOAD, 0);
179: mv.visitFieldInsn(GETFIELD, className, "this$0",
180: "Lorg/apache/lucene/store/RAMDirectory;");
181: mv.visitMethodInsn(INVOKEVIRTUAL,
182: "org/apache/lucene/store/RAMDirectory", ByteCodeUtil
183: .fieldGetterMethod("files"),
184: "()Ljava/util/Hashtable;");
185: mv
186: .visitTypeInsn(CHECKCAST,
187: "com/tc/object/bytecode/Manageable");
188: mv.visitMethodInsn(INVOKEINTERFACE,
189: "com/tc/object/bytecode/Manageable", "__tc_isManaged",
190: "()Z");
191: mv.visitInsn(IRETURN);
192: mv.visitMaxs(0, 0);
193: mv.visitEnd();
194: }
195:
196: private void addObtainWrapper() {
197: MethodVisitor mv = super .visitMethod(ACC_PUBLIC, "obtain",
198: "()Z", null, null);
199: mv.visitCode();
200: mv.visitVarInsn(ALOAD, 0);
201: mv.visitMethodInsn(INVOKESPECIAL, className, IS_SHARED_METHOD,
202: "()Z");
203: Label notShared = new Label();
204: mv.visitJumpInsn(IFEQ, notShared);
205: mv.visitVarInsn(ALOAD, 0);
206: mv.visitMethodInsn(INVOKESPECIAL, className,
207: DSO_LOCK_NAME_METHOD, "()Ljava/lang/String;");
208: mv.visitInsn(ICONST_2);
209: mv.visitMethodInsn(INVOKESTATIC,
210: "com/tc/object/bytecode/ManagerUtil", "beginLock",
211: "(Ljava/lang/String;I)V");
212: mv.visitInsn(ICONST_1);
213: mv.visitInsn(IRETURN);
214: mv.visitLabel(notShared);
215: mv.visitVarInsn(ALOAD, 0);
216: mv.visitMethodInsn(INVOKEVIRTUAL, className, "__tc_obtain",
217: "()Z");
218: mv.visitInsn(IRETURN);
219: mv.visitMaxs(0, 0);
220: mv.visitEnd();
221: }
222:
223: }
|