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.tomcat.transform;
006:
007: import com.tc.asm.ClassAdapter;
008: import com.tc.asm.ClassVisitor;
009: import com.tc.asm.MethodVisitor;
010: import com.tc.asm.Opcodes;
011: import com.tc.asm.Type;
012: import com.tc.asm.commons.LocalVariablesSorter;
013: import com.tc.object.bytecode.ByteCodeUtil;
014: import com.tc.object.bytecode.ClassAdapterFactory;
015:
016: public class WebAppLoaderAdapter extends ClassAdapter implements
017: ClassAdapterFactory {
018:
019: public WebAppLoaderAdapter() {
020: super (null);
021: }
022:
023: private WebAppLoaderAdapter(ClassVisitor cv, ClassLoader caller) {
024: super (cv);
025: }
026:
027: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
028: return new WebAppLoaderAdapter(visitor, loader);
029: }
030:
031: public MethodVisitor visitMethod(int access, String name,
032: String desc, String signature, String[] exceptions) {
033: MethodVisitor mv = super .visitMethod(access, name, desc,
034: signature, exceptions);
035: if ("createClassLoader".equals(name) //
036: && "()Lorg/apache/catalina/loader/WebappClassLoader;"
037: .equals(desc)) {
038: return new CreateClassLoaderAdapter(access, desc, mv);
039: }
040: return mv;
041: }
042:
043: private static class CreateClassLoaderAdapter extends
044: LocalVariablesSorter implements Opcodes {
045:
046: public CreateClassLoaderAdapter(int access, String desc,
047: MethodVisitor mv) {
048: super (access, desc, mv);
049: }
050:
051: public void visitInsn(int opcode) {
052: if (ARETURN == opcode) {
053:
054: int slot = newLocal(Type
055: .getObjectType("java/lang/Object"));
056: mv.visitVarInsn(ASTORE, slot);
057:
058: // name the web app loader
059: mv.visitVarInsn(ALOAD, slot);
060: mv.visitFieldInsn(GETSTATIC,
061: "com/tc/object/loaders/Namespace",
062: "TOMCAT_NAMESPACE", "Ljava/lang/String;");
063: mv.visitVarInsn(ALOAD, 0);
064: mv.visitMethodInsn(INVOKEINTERFACE,
065: "org/apache/catalina/Loader", "getContainer",
066: "()Lorg/apache/catalina/Container;");
067: mv.visitMethodInsn(INVOKESTATIC,
068: "com/tc/tomcat/TomcatLoaderNaming",
069: "getFullyQualifiedName",
070: "(Ljava/lang/Object;)Ljava/lang/String;");
071: mv
072: .visitMethodInsn(INVOKESTATIC,
073: "com/tc/object/loaders/Namespace",
074: "createLoaderName",
075: "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
076: mv.visitMethodInsn(INVOKEINTERFACE,
077: "com/tc/object/loaders/NamedClassLoader",
078: "__tc_setClassLoaderName",
079: "(Ljava/lang/String;)V");
080:
081: // register the web app loader
082: mv.visitVarInsn(ALOAD, slot);
083: mv
084: .visitMethodInsn(
085: INVOKESTATIC,
086: "com/tc/object/bytecode/hook/impl/ClassProcessorHelper",
087: "registerGlobalLoader",
088: "("
089: + ByteCodeUtil.NAMEDCLASSLOADER_TYPE
090: + ")V");
091:
092: // prepare for ARETURN
093: mv.visitVarInsn(ALOAD, slot);
094: }
095: super.visitInsn(opcode);
096: }
097:
098: }
099:
100: }
|