1 22 package org.jboss.aspects.logging; 23 24 import org.jboss.aop.joinpoint.ConstructorInvocation; 25 import org.jboss.aop.joinpoint.FieldReadInvocation; 26 import org.jboss.aop.joinpoint.FieldWriteInvocation; 27 import org.jboss.aop.joinpoint.MethodInvocation; 28 import org.jboss.logging.Logger; 29 30 import java.util.Arrays ; 31 32 38 public final class CallLoggingInterceptor 39 implements org.jboss.aop.advice.Interceptor, LoggingConstants 40 { 41 protected Logger log = Logger.getLogger(this.getClass()); 42 43 public String getName() { return "CallLoggingInterceptor"; } 44 45 public Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws Throwable 46 { 47 boolean callLogging = log.isDebugEnabled(); 48 if (callLogging) 49 callLogging = Boolean.valueOf((String )invocation.getMetaData(LOGGING, CALL_LOGGING)).booleanValue(); 50 51 if (callLogging) 52 log.debug("Invoking: " + dumpInvocation(invocation)); 53 54 Object response = null; 55 try 56 { 57 response = invocation.invokeNext(); 58 return response; 59 } 60 catch (Throwable t) 61 { 62 if (callLogging) 63 log.debug("Throwing: " + dumpInvocation(invocation), t); 64 throw t; 65 } 66 finally 67 { 68 if (callLogging) 69 log.debug("Response: " + dumpInvocationResponse(response) + " for " + dumpInvocation(invocation)); 70 } 71 } 72 73 78 public String dumpInvocation(org.jboss.aop.joinpoint.Invocation invocation) 79 { 80 StringBuffer buffer = new StringBuffer (); 81 if (invocation instanceof MethodInvocation) 82 { 83 org.jboss.aop.joinpoint.MethodInvocation methodInvocation = (org.jboss.aop.joinpoint.MethodInvocation)invocation; 84 buffer.append("Method@").append(System.identityHashCode(invocation)).append("{"); 85 buffer.append("method=").append(methodInvocation.getMethod()); 86 } 89 else if (invocation instanceof FieldReadInvocation) 90 { 91 org.jboss.aop.joinpoint.FieldReadInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldReadInvocation)invocation; 92 buffer.append("FieldRead@").append(System.identityHashCode(invocation)).append("{"); 93 buffer.append("field=").append(fieldInvocation.getField()); 94 } 95 else if (invocation instanceof FieldWriteInvocation) 96 { 97 org.jboss.aop.joinpoint.FieldWriteInvocation fieldInvocation = (org.jboss.aop.joinpoint.FieldWriteInvocation)invocation; 98 buffer.append("FieldWrite@").append(System.identityHashCode(invocation)).append("{"); 99 buffer.append("field=").append(fieldInvocation.getField()); 100 buffer.append(" value=").append(fieldInvocation.getValue()); 101 } 102 else if (invocation instanceof ConstructorInvocation) 103 { 104 org.jboss.aop.joinpoint.ConstructorInvocation cInvocation = (org.jboss.aop.joinpoint.ConstructorInvocation)invocation; 105 buffer.append("Construct@").append(System.identityHashCode(invocation)).append("{"); 106 buffer.append("constructor=").append(cInvocation.getConstructor()); 107 buffer.append(" args=").append(Arrays.asList(cInvocation.getArguments())); 108 } 109 else 110 { 111 return "Unknown " + invocation; 112 } 113 buffer.append("}"); 114 return buffer.toString(); 115 } 116 117 122 public String dumpInvocationResponse(Object response) 123 { 124 if (response == null) 125 return "null"; 126 StringBuffer buffer = new StringBuffer (); 127 buffer.append(response); 128 return buffer.toString(); 129 } 130 } 131 | Popular Tags |