package es.internna.spring.aop.interceptors;
import es.internna.annotations.LogDebug;
import es.internna.spring.annotations.Bean;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
@Bean(name="logDebug")
public class LogDebugInterceptor
{
protected Method getMethod(JoinPoint jp) {
Method invoked = null;
try {
MethodSignature met = (MethodSignature) jp.getSignature();
invoked = jp.getSourceLocation().getWithinType().getMethod(met.getMethod().getName(), met.getMethod().getParameterTypes());
} finally {
return invoked;
}
}
protected Log getLog(JoinPoint jp) {
Log log = null;
try {
LogDebug logdebug = this.getMethod(jp).getAnnotation(LogDebug.class);
Class clazz = logdebug.loggerClass();
if (clazz == null) clazz = LogDebugInterceptor.class;
log = LogFactory.getLog(clazz);
} finally {
if (log == null) log = LogFactory.getLog(LogDebugInterceptor.class);
}
return log;
}
@Before("@annotation(es.internna.annotations.LogDebug)")
public void beforeLog(JoinPoint jp) {
Log log = this.getLog(jp);
if (log.isDebugEnabled()) {
log.debug(this.getMethod(jp));
for (Object o : jp.getArgs())
{
if (o != null)
{
log.debug("PARAMETER " + o.getClass().getSimpleName());
log.debug(o.toString());
}
}
}
}
@AfterReturning(
pointcut="@annotation(es.internna.annotations.LogDebug)",
returning="retVal"
)
public void afterLog(JoinPoint jp, Object retVal)
{
Log log = this.getLog(jp);
if (log.isDebugEnabled()) log.debug(retVal);
}
}