001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.weblogic.transform;
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: import java.lang.reflect.Modifier;
016:
017: public class ServerAdapter extends ClassAdapter implements Opcodes,
018: ClassAdapterFactory {
019:
020: public ServerAdapter() {
021: super (null);
022: }
023:
024: private ServerAdapter(ClassVisitor cv, ClassLoader caller) {
025: super (cv);
026: }
027:
028: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
029: return new ServerAdapter(visitor, loader);
030: }
031:
032: public MethodVisitor visitMethod(int access, String name,
033: String desc, String signature, String[] exceptions) {
034: if ("main".equals(name)
035: && "([Ljava/lang/String;)V".equals(desc)
036: && Modifier.isStatic(access)
037: && Modifier.isPublic(access)) {
038: name = ByteCodeUtil.TC_METHOD_PREFIX + name;
039: }
040:
041: return super .visitMethod(access, name, desc, signature,
042: exceptions);
043: }
044:
045: public void visitEnd() {
046: addMainWrapper();
047: super .visitEnd();
048: }
049:
050: private void addMainWrapper() {
051: MethodVisitor mv = super .visitMethod(ACC_PUBLIC + ACC_STATIC,
052: "main", "([Ljava/lang/String;)V", null, null);
053: mv.visitCode();
054:
055: mv.visitLdcInsn("weblogic.Server");
056: mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName",
057: "(Ljava/lang/String;)Ljava/lang/Class;");
058: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class",
059: "getClassLoader", "()Ljava/lang/ClassLoader;");
060: mv.visitMethodInsn(INVOKESTATIC,
061: "com/tc/object/bytecode/hook/impl/SessionsHelper",
062: "injectClasses", "(Ljava/lang/ClassLoader;)V");
063:
064: Label l1 = new Label();
065: mv.visitLabel(l1);
066: mv.visitVarInsn(ALOAD, 0);
067: mv.visitMethodInsn(INVOKESTATIC, "weblogic/Server",
068: ByteCodeUtil.TC_METHOD_PREFIX + "main",
069: "([Ljava/lang/String;)V");
070: Label l2 = new Label();
071: mv.visitJumpInsn(GOTO, l2);
072: Label l3 = new Label();
073: mv.visitLabel(l3);
074: mv.visitVarInsn(ASTORE, 2);
075: Label l4 = new Label();
076: mv.visitJumpInsn(JSR, l4);
077: mv.visitVarInsn(ALOAD, 2);
078: mv.visitInsn(ATHROW);
079: mv.visitLabel(l4);
080: mv.visitVarInsn(ASTORE, 1);
081: Label l6 = new Label();
082: mv.visitLabel(l6);
083: mv
084: .visitMethodInsn(
085: INVOKESTATIC,
086: "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
087: "shutdown", "()V");
088: Label l7 = new Label();
089: mv.visitJumpInsn(GOTO, l7);
090: Label l8 = new Label();
091: mv.visitLabel(l8);
092: mv.visitVarInsn(ASTORE, 3);
093: mv.visitVarInsn(ALOAD, 3);
094: mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Throwable",
095: "printStackTrace", "()V");
096: mv.visitLabel(l7);
097: mv.visitVarInsn(RET, 1);
098: mv.visitLabel(l2);
099: mv.visitJumpInsn(JSR, l4);
100: Label l10 = new Label();
101: mv.visitLabel(l10);
102: mv.visitInsn(RETURN);
103: mv.visitTryCatchBlock(l1, l3, l3, null);
104: mv.visitTryCatchBlock(l2, l10, l3, null);
105: mv.visitTryCatchBlock(l6, l8, l8, "java/lang/Throwable");
106: mv.visitMaxs(0, 0);
107: mv.visitEnd();
108: }
109:
110: }
|