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.websphere_6_1;
006:
007: import com.tc.asm.ClassAdapter;
008: import com.tc.asm.ClassVisitor;
009: import com.tc.asm.MethodAdapter;
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 ClassGraphAdapter extends ClassAdapter implements
016: ClassAdapterFactory, Opcodes {
017:
018: public ClassGraphAdapter(ClassVisitor cv) {
019: super (cv);
020: }
021:
022: public ClassGraphAdapter() {
023: super (null);
024: }
025:
026: public ClassAdapter create(ClassVisitor visitor, ClassLoader loader) {
027: return new ClassGraphAdapter(visitor);
028: }
029:
030: public MethodVisitor visitMethod(int access, String name,
031: String desc, String signature, String[] exceptions) {
032: if ("processWARModule".equals(name)) {
033: addProcessWARModuleWrapper(access, name, desc, signature,
034: exceptions);
035: return super .visitMethod(access,
036: ByteCodeUtil.METHOD_RENAME_PREFIX + name, desc,
037: signature, exceptions);
038: }
039:
040: MethodVisitor mv = super .visitMethod(access, name, desc,
041: signature, exceptions);
042: if ("createClassLoaders".equals(name)) {
043: return new CreateClassLoadersAdapter(mv);
044: } else if (ACC_STATIC == access && "<clinit>".equals(name)) {
045: return new StaticBlockAdapter(mv);
046: }
047: return mv;
048: }
049:
050: private void addProcessWARModuleWrapper(int access, String name,
051: String desc, String signature, String[] exceptions) {
052: MethodVisitor mv = super .visitMethod(access, name, desc,
053: signature, exceptions);
054: mv.visitCode();
055: mv.visitVarInsn(ALOAD, 0);
056: for (int i = 1; i <= 9; i++) {
057: mv.visitVarInsn(ALOAD, i);
058: }
059: mv.visitMethodInsn(INVOKESPECIAL,
060: "com/ibm/ws/classloader/ClassGraph", //
061: ByteCodeUtil.METHOD_RENAME_PREFIX + name, desc);
062:
063: mv.visitVarInsn(ALOAD, 6); // modulenode
064: mv.visitFieldInsn(GETFIELD,
065: "com/ibm/ws/classloader/ClassGraph$ModuleNode",
066: "classLoader",
067: "Lcom/ibm/ws/classloader/JarClassLoader;");
068: mv.visitVarInsn(ALOAD, 1); // earfile
069: mv.visitVarInsn(ALOAD, 4); // moduleref
070: mv
071: .visitMethodInsn(
072: INVOKESTATIC,
073: "com/tc/websphere/WebsphereLoaderNaming",
074: "registerWebAppLoader",
075: "(Lcom/tc/object/loaders/NamedClassLoader;Ljava/lang/Object;Ljava/lang/Object;)V");
076:
077: mv.visitInsn(RETURN);
078: mv.visitMaxs(0, 0);
079: mv.visitEnd();
080: }
081:
082: private static class CreateClassLoadersAdapter extends
083: MethodAdapter implements Opcodes {
084:
085: public CreateClassLoadersAdapter(MethodVisitor mv) {
086: super (mv);
087: }
088:
089: public void visitMethodInsn(int opcode, String owner,
090: String name, String desc) {
091: super .visitMethodInsn(opcode, owner, name, desc);
092: if (INVOKESPECIAL == opcode
093: && "com/ibm/ws/classloader/JarClassLoader"
094: .equals(owner) && "<init>".equals(name)) {
095: super .visitInsn(DUP);
096: super .visitVarInsn(ALOAD, 1);
097: super
098: .visitMethodInsn(
099: INVOKESTATIC,
100: "com/tc/websphere/WebsphereLoaderNaming",
101: "nameAndRegisterDependencyLoader",
102: "(Lcom/tc/object/loaders/NamedClassLoader;Ljava/lang/Object;)V");
103: }
104: }
105:
106: }
107:
108: /**
109: * This class is in the same OSGi bundle as the MBeanServerBuilder that WebSphere wants to use; we trigger the
110: * creation of the platform default MBeanServer here so that L1Management can pick it up.
111: */
112: private static class StaticBlockAdapter extends MethodAdapter
113: implements Opcodes {
114:
115: public StaticBlockAdapter(MethodVisitor mv) {
116: super (mv);
117: }
118:
119: public void visitInsn(int opcode) {
120: if (opcode == RETURN) {
121: mv.visitMethodInsn(INVOKESTATIC,
122: "java/lang/management/ManagementFactory",
123: "getPlatformMBeanServer",
124: "()Ljavax/management/MBeanServer;");
125: mv.visitInsn(POP);
126: }
127: super.visitInsn(opcode);
128: }
129:
130: }
131:
132: }
|