Created
March 3, 2011 08:16
-
-
Save ketankhairnar/852496 to your computer and use it in GitHub Desktop.
Logging Interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configurable | |
@Aspect | |
public class LoggingInterceptor | |
{ | |
Log log = LogFactory.getLog(LoggingInterceptor.class); | |
@Autowired | |
private AuditData auditData; | |
@Around("execution(* *(..)) && @annotation(com.xyz.portal.util.audit.Auditable)") | |
public Object profile(ProceedingJoinPoint pjp) throws Throwable | |
{ | |
final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName(); | |
try | |
{ | |
Object resultValue = pjp.proceed(); | |
logAudit(currentUser, pjp); | |
return resultValue; | |
} | |
finally | |
{ | |
} | |
} | |
private void logAudit(String currentUser, ProceedingJoinPoint pjp) | |
{ | |
AuditInformation auditInformation = new AuditInformation(); | |
StringBuffer sbfDetails = new StringBuffer(); | |
auditInformation.setDate(new Date()); | |
auditInformation.setUserName(currentUser); | |
auditInformation.setOperation(this.getMethod(pjp).getAnnotation(Auditable.class).operation()); | |
for (Object o : pjp.getArgs()) | |
{ | |
if (o != null) | |
{ | |
sbfDetails.append(" PARAMETER " + o.getClass().getSimpleName() + " : " + o.toString()); | |
} | |
} | |
auditInformation.setMethodName(pjp.getSourceLocation().getWithinType().getSimpleName() + " : " + this.getMethod(pjp).getName()); | |
auditData.save(auditInformation); | |
} | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment