com.infilos.spring.track.aop.AuditAspect Maven / Gradle / Ivy
The newest version!
package com.infilos.spring.track.aop;
import com.infilos.spring.track.AuditExtractors;
import com.infilos.spring.track.AuditTransfers;
import com.infilos.spring.track.api.*;
import com.infilos.spring.track.config.SpringContextConfigure;
import com.infilos.spring.track.service.AuditRecordJdbcService;
import com.infilos.spring.track.service.AuditRecordMemService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public abstract class AuditAspect {
protected static final Logger log = LoggerFactory.getLogger(AuditAspect.class);
protected static final ConcurrentHashMap, AuditOption> ClassOptions =
new ConcurrentHashMap<>();
protected static final ConcurrentHashMap ActionOptions =
new ConcurrentHashMap<>();
@Autowired(required = false)
protected Optional memService;
@Autowired(required = false)
protected Optional jdbcService;
@Autowired
protected List customServices;
@Pointcut("@annotation(com.infilos.spring.track.api.AuditAction)")
public void isAuditAction() {
}
@Pointcut(
"@annotation(org.springframework.web.bind.annotation.RequestMapping) || "
+ "@annotation(org.springframework.web.bind.annotation.GetMapping) || "
+ "@annotation(org.springframework.web.bind.annotation.PostMapping) || "
+ "@annotation(org.springframework.web.bind.annotation.PutMapping) || "
+ "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
public void isRestController() {
}
protected Optional buildAuditOptions(ProceedingJoinPoint point) {
try {
// class level options
Signature signature = point.getSignature();
Class> classType = signature.getDeclaringType();
String className = signature.getDeclaringTypeName();
if (classType.isAnnotationPresent(AuditOption.class)) {
ClassOptions.computeIfAbsent(classType, c -> classType.getAnnotation(AuditOption.class));
}
// method level options
String pointName = signature.getName();
String pointActionKey = className + "." + pointName;
ActionOptions.computeIfAbsent(
pointActionKey,
k -> {
Method method = ((MethodSignature) signature).getMethod();
AuditAction action = method.getAnnotation(AuditAction.class);
return new AuditOptions(action, ClassOptions.get(classType));
});
return Optional.ofNullable(ActionOptions.get(pointActionKey));
} catch (Throwable ex) {
log.error("Construct audit action options failed.", ex);
return Optional.empty();
}
}
protected Optional buildMethodContext(
ProceedingJoinPoint point, Object result, Throwable failure) {
try {
String method = point.getSignature().getName();
List paramNames =
Arrays.asList(((CodeSignature) point.getSignature()).getParameterNames());
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy