Posted By

jasonhue on 08/13/14


Tagged

spring AOP


Versions (?)

Spring AOP Audit Trail


 / Published in: Java
 

Audit Trail

  1. package net.javabeat.spring.audit;
  2.  
  3. import java.io.IOException;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map.Entry;
  10.  
  11. import javax.annotation.Resource;
  12.  
  13. import org.apache.commons.beanutils.BeanMap;
  14. import org.apache.commons.beanutils.PropertyUtilsBean;
  15. import org.aspectj.lang.JoinPoint;
  16. import org.aspectj.lang.annotation.AfterReturning;
  17. import org.aspectj.lang.annotation.Aspect;
  18. import org.aspectj.lang.annotation.Before;
  19. import org.codehaus.jackson.JsonGenerationException;
  20. import org.codehaus.jackson.map.JsonMappingException;
  21. import org.codehaus.jackson.map.ObjectMapper;
  22. import org.hibernate.SessionFactory;
  23. import org.hibernate.metadata.ClassMetadata;
  24. import org.infinispan.commons.api.BasicCache;
  25. import org.springframework.aop.framework.Advised;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Configurable;
  28. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  29. import org.springframework.stereotype.Component;
  30.  
  31. import net.javabeat.spring.cache.LocalCacheManager;
  32. import net.javabeat.spring.model.AuditTable;
  33. import net.javabeat.spring.service.AuditFlowService;
  34. import net.javabeat.spring.service.AuditTableService;
  35. import net.javabeat.spring.util.JsonUtils;
  36.  
  37. @Aspect
  38. @Component("auditTrailAspect")
  39. @Configurable
  40. @EnableAspectJAutoProxy
  41. public class AuditTrailAspect {
  42.  
  43. @Autowired
  44. private AuditTableService auditTableService;
  45.  
  46. @Resource
  47. private SessionFactory sessionFactory;
  48.  
  49. @Autowired private AuditFlowService auditFlowService;
  50.  
  51.  
  52. private static BasicCache<Object, Object> caches = LocalCacheManager.getCache();
  53.  
  54. @Before("execution(public * net.javabeat.spring.dao..*.genericUpdate(..)) && args(bean)")
  55. public void traceBeforeUpdate(JoinPoint joinPoint, Object bean) {
  56. try {
  57. Advised advised = (Advised) joinPoint.getThis();
  58. Class<?> cls = advised.getTargetSource().getTargetClass();
  59. ClassMetadata metadata = sessionFactory.getClassMetadata(bean.getClass());
  60. String entityPrimaryKey = metadata.getIdentifierPropertyName();
  61. JsonUtils utils = new JsonUtils();
  62. int typeid = Integer.parseInt(utils.getProperty(bean, entityPrimaryKey)) ;
  63.  
  64. Object service = advised.getTargetSource().getTarget();
  65. Method findMethod = cls.getSuperclass().getMethod("genericGetById", new Class[] {int.class});
  66.  
  67. Object oldValue = findMethod.invoke(service, typeid);
  68.  
  69. String key = bean.toString() + typeid;
  70. caches.put(key, oldValue);
  71.  
  72. } catch (Exception e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76.  
  77. }
  78.  
  79.  
  80. @AfterReturning("execution(public * net.javabeat.spring.dao..*.genericUpdate(..)) && args(bean)")
  81. public void traceAfterUpdate(JoinPoint joinPoint, Object bean) {
  82. String key = null;
  83. try {
  84. Advised advised = (Advised) joinPoint.getThis();
  85. Class<?> cls = advised.getTargetSource().getTargetClass();
  86.  
  87. JsonUtils utils = new JsonUtils();
  88. ClassMetadata metadata = sessionFactory.getClassMetadata(bean.getClass());
  89. String entityPrimaryKey = metadata.getIdentifierPropertyName();
  90. int typeid = Integer.parseInt(utils.getProperty(bean,entityPrimaryKey)) ;
  91. key = bean.toString() + typeid;
  92. Object oldValue = (caches.get(key) == null ? null : caches.get(key));
  93.  
  94. ObjectMapper mapper = new ObjectMapper();
  95. ChangeSetRecord changeSetRecord = new ChangeSetRecord ();
  96. changeSetRecord = compareObjects(oldValue, bean);
  97.  
  98. if (changeSetRecord != null){
  99. AuditTable auditRecord = new AuditTable();
  100. auditRecord.setEntityname(bean.getClass().getSimpleName());
  101. auditRecord.setAction("Update");
  102. auditRecord.setChangeset(mapper.writeValueAsString( changeSetRecord ) );
  103. auditTableService.createAudit(auditRecord);
  104. }
  105.  
  106. } catch (Exception e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. }
  111.  
  112. private class ChangeSetRecord extends HashMap<String, List<String>> {
  113. public void put(String key,String index) {
  114. List<String> current = get(key);
  115. if (current == null) {
  116. current = new ArrayList<String>();
  117. super.put(key, current);
  118. }
  119. current.add(index);
  120. }
  121. }
  122.  
  123. public ChangeSetRecord compareObjects(Object oldObject, Object newObject) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
  124. BeanMap map = new BeanMap(oldObject);
  125. PropertyUtilsBean propUtils = new PropertyUtilsBean();
  126. ChangeSetRecord changeSetRecord = new ChangeSetRecord();
  127.  
  128. for (Object propNameObject : map.keySet()) {
  129. String propertyName = (String) propNameObject;
  130. Object property1 = propUtils.getProperty(oldObject, propertyName);
  131. Object property2 = propUtils.getProperty(newObject, propertyName);
  132. if (property1.equals(property2)) {
  133. System.out.println(" " + propertyName + " is equal");
  134. } else {
  135. System.out.println("> " + propertyName + " is different (oldValue=\"" + property1 + "\", newValue=\"" + property2 + "\")");
  136.  
  137.  
  138. changeSetRecord.put(propertyName, (property1 == null ? "" : property1.toString()));
  139. changeSetRecord.put(propertyName, (property2 == null ? "" : property2.toString()));
  140. }
  141. }
  142. return changeSetRecord;
  143. }
  144.  
  145. }

Report this snippet  

You need to login to post a comment.