1 16 17 package org.springframework.aop.interceptor; 18 19 import java.io.Serializable ; 20 21 import org.aopalliance.intercept.MethodInterceptor; 22 import org.aopalliance.intercept.MethodInvocation; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.aop.support.AopUtils; 27 28 46 public abstract class AbstractTraceInterceptor implements MethodInterceptor, Serializable { 47 48 52 protected transient Log defaultLogger = LogFactory.getLog(getClass()); 53 54 58 private boolean hideProxyClassNames = false; 59 60 61 71 public void setUseDynamicLogger(boolean useDynamicLogger) { 72 this.defaultLogger = (useDynamicLogger ? null : LogFactory.getLog(getClass())); 74 } 75 76 88 public void setLoggerName(String loggerName) { 89 this.defaultLogger = LogFactory.getLog(loggerName); 90 } 91 92 96 public void setHideProxyClassNames(boolean hideProxyClassNames) { 97 this.hideProxyClassNames = hideProxyClassNames; 98 } 99 100 101 107 public Object invoke(MethodInvocation invocation) throws Throwable { 108 Log logger = getLoggerForInvocation(invocation); 109 if (isInterceptorEnabled(invocation, logger)) { 110 return invokeUnderTrace(invocation, logger); 111 } 112 else { 113 return invocation.proceed(); 114 } 115 } 116 117 127 protected Log getLoggerForInvocation(MethodInvocation invocation) { 128 if (this.defaultLogger != null) { 129 return this.defaultLogger; 130 } 131 else { 132 Object target = invocation.getThis(); 133 Class logCategoryClass = target.getClass(); 134 if (this.hideProxyClassNames) { 135 logCategoryClass = AopUtils.getTargetClass(target); 136 } 137 return LogFactory.getLog(logCategoryClass); 138 } 139 } 140 141 152 protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) { 153 return isLogEnabled(logger); 154 } 155 156 162 protected boolean isLogEnabled(Log logger) { 163 return logger.isTraceEnabled(); 164 } 165 166 167 183 protected abstract Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable ; 184 185 } 186 | Popular Tags |