1 50 package net.sf.just4log.transform; 51 52 import java.util.HashMap ; 53 54 import org.apache.bcel.Constants; 55 import org.apache.bcel.generic.ConstantPoolGen; 56 import org.apache.bcel.generic.IFEQ; 57 import org.apache.bcel.generic.InstructionHandle; 58 import org.apache.bcel.generic.InstructionList; 59 import org.apache.bcel.generic.InvokeInstruction; 60 import org.apache.bcel.generic.MethodGen; 61 import org.apache.bcel.generic.ObjectType; 62 import org.apache.bcel.generic.PUSH; 63 import org.apache.bcel.generic.Type; 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 67 70 71 public class Log4jTransform extends Transform { 72 73 private static final String INTERFACE = "org.apache.log4j.Logger"; 74 77 private static final ObjectType logType = new ObjectType(INTERFACE); 78 public ObjectType getLogType() { 79 80 return logType; 81 } 82 private static final String LEVEL = "org.apache.log4j.Level"; 83 private static final String PRIORITY = "org.apache.log4j.Priority"; 84 private static final ObjectType LevelType = new ObjectType(LEVEL); 85 private static final ObjectType[] ARGUMENTS_ISENABLEDFOR = 86 new ObjectType[] { new ObjectType(PRIORITY)}; 87 88 private static Log logger = LogFactory.getLog(Log4jTransform.class); 89 private static String CLASSID = 90 "$Id: Log4jTransform.java,v 1.8 2003/07/22 23:38:37 lbruand Exp $"; 91 92 public static void register() { 93 Transform.register(new Log4jTransform()); 94 } 95 96 99 public Log4jTransform() { 100 super(); 101 } 102 103 106 public InstructionHandle insertFork( 107 InstructionList il, 108 InstructionHandle getStaticHandle, 109 InstructionHandle invokeHandle, 110 ConstantPoolGen cp) { 111 String level = 112 getIsEnabled( 113 ((InvokeInstruction) invokeHandle.getInstruction()), 114 cp); 115 if (level == null) { 116 return null; 118 } 119 logger.info("the level of enabling is: " + level); 120 logger.info("Inserting GETSTATIC"); 121 InstructionHandle insertHandle = 122 il.insert(getStaticHandle, getStaticHandle.getInstruction().copy()); 123 il.insert( 124 getStaticHandle, 125 instFact.createGetStatic(LEVEL, level, LevelType)); 126 127 logger.info("Inserting INVOKE call to isEnabledFor"); 128 129 il.insert( 130 getStaticHandle, 131 instFact.createInvoke( 132 INTERFACE, 133 "isEnabledFor", 134 Type.BOOLEAN, 135 ARGUMENTS_ISENABLEDFOR, 136 Constants.INVOKEVIRTUAL)); 137 logger.info("Inserting IFEQ"); 138 il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext())); 139 return insertHandle; 140 } 141 private static HashMap mapping = new HashMap (); 142 static { 143 mapping.put("debug", "DEBUG"); 144 mapping.put("error", "ERROR"); 145 mapping.put("fatal", "FATAL"); 146 mapping.put("info", "INFO"); 147 mapping.put("warn", "WARN"); 148 } 149 public String getIsEnabled( 150 InvokeInstruction invokeInstruction, 151 ConstantPoolGen cp) { 152 return (String ) mapping.get(invokeInstruction.getMethodName(cp)); 153 } 154 155 158 public String getClassname() { 159 return INTERFACE; 160 } 161 162 165 public InstructionHandle insertEnter( 166 MethodGen orig, 167 InstructionList il, 168 InstructionHandle firstInstructionHandle, 169 ConstantPoolGen cp) { 170 logger.info("Inserting Enter code."); 171 InstructionHandle backup = il.insert( 172 firstInstructionHandle, 173 instFact.createGetStatic( 174 clazz.getClassName(), 175 loggerAttribute.getName(), 176 loggerAttribute.getType())); 177 178 il.insert(firstInstructionHandle, 179 new PUSH(cp, Transform.ENTER_STRING+ getMethodRepr(orig)) 180 ); 181 182 183 il.insert( 184 firstInstructionHandle, 185 instFact.createInvoke( 186 INTERFACE, 187 "debug", 188 Type.VOID, 189 new Type[] {Type.OBJECT}, 190 Constants.INVOKEVIRTUAL)); 191 192 return backup; 193 194 } 195 196 199 public InstructionHandle insertExit( 200 MethodGen orig, 201 InstructionList il, 202 InstructionHandle returnInstructionHandle, 203 ConstantPoolGen cp) { 204 logger.info("Inserting Exit code."); 205 InstructionHandle backup = il.insert( 206 returnInstructionHandle, 207 instFact.createGetStatic( 208 clazz.getClassName(), 209 loggerAttribute.getName(), 210 loggerAttribute.getType())); 211 212 il.insert(returnInstructionHandle, 213 new PUSH(cp, Transform.EXIT_STRING+ getMethodRepr(orig)) 214 ); 215 216 217 il.insert( 218 returnInstructionHandle, 219 instFact.createInvoke( 220 INTERFACE, 221 "debug", 222 Type.VOID, 223 new Type[] {Type.OBJECT}, 224 Constants.INVOKEVIRTUAL)); 225 226 return backup; 227 228 } 229 230 } 231 | Popular Tags |