PageRenderTime 3440ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/danlu/matrix/controller/manage/TestAOP.java

https://gitlab.com/GavinGJ/MATIRX
Java | 205 lines | 180 code | 23 blank | 2 comment | 26 complexity | bd191a0cefc24fdf6dac7bad0a6ca63d MD5 | raw file
  1. package com.danlu.matrix.controller.manage;
  2. import java.io.IOException;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import javax.annotation.Resource;
  8. import org.apache.http.HttpResponse;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.HttpClient;
  11. import org.apache.http.client.methods.HttpPut;
  12. import org.apache.http.entity.StringEntity;
  13. import org.apache.http.impl.client.HttpClientBuilder;
  14. import org.aspectj.lang.ProceedingJoinPoint;
  15. import org.aspectj.lang.annotation.Around;
  16. import org.aspectj.lang.annotation.Aspect;
  17. import org.aspectj.lang.annotation.Pointcut;
  18. import org.springframework.stereotype.Component;
  19. import com.alibaba.fastjson.JSON;
  20. import com.alibaba.fastjson.JSONObject;
  21. import com.danlu.matrix.dao.appDao;
  22. import com.danlu.matrix.dao.configDao;
  23. import com.danlu.matrix.dao.envDao;
  24. import com.danlu.matrix.dao.log_historyDao;
  25. import com.danlu.matrix.dao.userDao;
  26. import com.danlu.matrix.entity.db.appEntity;
  27. import com.danlu.matrix.entity.db.configEntity;
  28. import com.danlu.matrix.entity.db.envEntity;
  29. import com.danlu.matrix.entity.db.userEntity;
  30. import com.danlu.matrix.model.ParamModel;
  31. import com.danlu.matrix.model.issue;
  32. import com.danlu.matrix.util.constant.DateUtil;
  33. @Component
  34. @Aspect
  35. public class TestAOP {
  36. @Resource
  37. private userDao userDao;
  38. @Resource
  39. private appDao appDao;
  40. @Resource
  41. private envDao envDao;
  42. @Resource
  43. private configDao configDao;
  44. @Resource
  45. private log_historyDao logHistoryDao;
  46. @Pointcut("execution(* com.danlu.matrix.controller.manage.ManageController.*(..))")
  47. private void logMethod() {
  48. }
  49. @Around("logMethod()")
  50. public Object logAround(ProceedingJoinPoint point) {
  51. Map<String, Object> map = new HashMap<String, Object>();
  52. Map<String, Object> configMap = new HashMap<String, Object>();
  53. String desc = null;
  54. String methodName = null; //方法名
  55. Object[] params = null; //入參
  56. Object returnValue = null; //返回值
  57. try {
  58. Date date = new Date();
  59. String descTime = DateUtil.changeDateToString(date);
  60. methodName = "[" + point.getSignature().getDeclaringTypeName() + "."
  61. + point.getSignature().getName() + "]";
  62. System.out.println("请求方法:" + methodName);
  63. params = point.getArgs();
  64. String json = JSONObject.toJSONString(params);
  65. String version = null;
  66. String versionCopySource = null;
  67. String versionCopyTarget = null;
  68. Long envId = 0l;
  69. Long userId = 0l;
  70. Long appId = 0l;
  71. Long appIdCopySource = 0l;
  72. Long appIdCopyTarget = 0l;
  73. Long envIdCopySource = 0l;
  74. Long envIdCopyTarget = 0l;
  75. List<ParamModel> jsons = JSON.parseArray(json, ParamModel.class);
  76. if (jsons.get(0).getUserId() != null) {
  77. userId = Long.parseLong(jsons.get(0).getUserId());
  78. }
  79. if (jsons.get(0).getAppId() != null) {
  80. appId = Long.parseLong(jsons.get(0).getAppId());
  81. }
  82. if (jsons.get(0).getEnvId() != null) {
  83. envId = Long.parseLong(jsons.get(0).getEnvId());
  84. }
  85. if (jsons.get(0).getVersion() != null) {
  86. version = jsons.get(0).getVersion();
  87. }
  88. if (jsons.get(0).getAppIdCopySource() != null) {
  89. appIdCopySource = Long.parseLong(jsons.get(0).getAppIdCopySource());
  90. }
  91. if (jsons.get(0).getAppIdCopyTarget() != null) {
  92. appIdCopyTarget = Long.parseLong(jsons.get(0).getAppIdCopyTarget());
  93. }
  94. if (jsons.get(0).getEnvIdCopySource() != null) {
  95. envIdCopySource = Long.parseLong(jsons.get(0).getEnvIdCopySource());
  96. }
  97. if (jsons.get(0).getEnvIdCopyTarget() != null) {
  98. envIdCopyTarget = Long.parseLong(jsons.get(0).getEnvIdCopyTarget());
  99. }
  100. if (jsons.get(0).getVersionNameCopySource() != null) {
  101. versionCopySource = jsons.get(0).getVersionNameCopySource();
  102. }
  103. if (jsons.get(0).getVersionNameTarget() != null) {
  104. versionCopyTarget = jsons.get(0).getVersionNameTarget();
  105. }
  106. map.put("userId", userId);
  107. map.put("appId", appId);
  108. map.put("envId", envId);
  109. map.put("version", version);
  110. userEntity user = userDao.selectByPrimaryKey(userId);
  111. appEntity app = appDao.selectByPrimaryKey(appId);
  112. envEntity env = envDao.selectByPrimaryKey(envId);
  113. appEntity appSourceEntity = appDao.selectByPrimaryKey(appIdCopySource);
  114. appEntity appTargetEntity = appDao.selectByPrimaryKey(appIdCopyTarget);
  115. envEntity envSourceEntity = envDao.selectByPrimaryKey(envIdCopySource);
  116. envEntity envTargetEntity = envDao.selectByPrimaryKey(envIdCopyTarget);
  117. //configEntity config = configDao.selectConfig(map);
  118. String userName = user.getName();
  119. if (methodName.contains("microServicesDelete")) {//删除微服务
  120. desc = descTime + "," + userName + "删除了" + env.getName() + "环境下的" + app.getName()
  121. + "微服务";
  122. configMap.put("updateTime", descTime);
  123. configMap.put("updateBy", user.getUserId());
  124. configMap.put("envId", env.getEnvId());
  125. configMap.put("appId", app.getAppId());
  126. configMap.put("operation", "删除");
  127. configMap.put("desc", desc);
  128. } else if (methodName.contains("versionDelete")) {//删除版本
  129. desc = descTime + "," + userName + "删除了" + env.getName() + "环境下的" + app.getName()
  130. + "微服务中的" + version + "版本";
  131. configMap.put("updateTime", descTime);
  132. configMap.put("updateBy", user.getUserId());
  133. configMap.put("envId", env.getEnvId());
  134. configMap.put("appId", app.getAppId());
  135. configMap.put("version", version);
  136. configMap.put("operation", "删除");
  137. configMap.put("desc", desc);
  138. } else if (methodName.contains("copyProperties")) {//复制文件
  139. desc = descTime + "," + userName + "从" + envSourceEntity.getName() + "环境下的"
  140. + appSourceEntity.getName() + "微服务中版本号为:" + versionCopySource + "中的所有文件,复制到"
  141. + envTargetEntity.getName() + "环境下的" + appTargetEntity.getName()
  142. + "微服务中版本号为:" + versionCopyTarget + "中的所有文件";
  143. configMap.put("updateTime", descTime);
  144. configMap.put("updateBy", user.getUserId());
  145. configMap.put("envId", envSourceEntity.getEnvId());
  146. configMap.put("appId", appSourceEntity.getAppId());
  147. configMap.put("version", versionCopySource);
  148. configMap.put("operation", "复制");
  149. configMap.put("desc", desc);
  150. }
  151. logHistoryDao.insertSelective(configMap);
  152. send2Redmine(desc);
  153. returnValue = point.proceed(params);
  154. } catch (Throwable e) {
  155. System.out.println(methodName + "调用失败,抛出异常:" + e);
  156. }
  157. return returnValue;
  158. }
  159. public void send2Redmine(String desc) throws ClientProtocolException, IOException {
  160. JSONObject jo = new JSONObject();
  161. issue issue = new issue();
  162. issue.setNotes(desc);
  163. jo.put("issue", issue);
  164. String userKey = "key=1d893519a4674aa4644704c3f8c86003249db597";
  165. String url = "http://182.92.230.201:3000/issues/6186.json?" + userKey;
  166. HttpClient client = HttpClientBuilder.create().build();
  167. HttpPut put = new HttpPut(url);
  168. put.setHeader("Content-type", "application/json;charset=utf-8");
  169. StringEntity params = new StringEntity(jo.toString(), "utf-8");
  170. put.setEntity(params);
  171. HttpResponse response = client.execute(put);
  172. System.out.println("Response Code:" + response.getStatusLine().getStatusCode());
  173. }
  174. public void getMapByJson(String json) {
  175. Map mapTypes = JSON.parseObject(json);
  176. // 最外层解析
  177. System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
  178. for (Object obj : mapTypes.keySet()) {
  179. System.out.println("key为:" + obj + "值为:" + mapTypes.get(obj));
  180. }
  181. }
  182. }