aroundAop.java 4.64 KB
package com.example.fuzzControll.aop;

import com.example.fuzzControll.annotion.NeedCutAround;
import com.example.fuzzControll.annotion.NeedCutBefore;
import com.example.fuzzControll.constents.MissionStateEnum;
import com.example.fuzzControll.constents.TableClassEnum;
import com.example.fuzzControll.domain.po.MissionInfo;
import com.example.fuzzControll.exception.mysqlException.MysqlException;
import com.example.fuzzControll.exception.testException.AflnetException;
import com.example.fuzzControll.exception.testException.KittyException;
import com.example.fuzzControll.mapper.MissionInfoMapper;
import com.example.fuzzControll.tools.system.GlobalClass;
import com.example.fuzzControll.tools.system.SystemRunningParams;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class aroundAop implements Ordered {
    @Autowired
    MissionInfoMapper missionInfoMapper;
    @Pointcut(value = "@annotation(com.example.fuzzControll.annotion.NeedCutAround)")
    private void aroundCut() {
    }

    @Around("aroundCut()")
    public Map<String, List<String>> around(ProceedingJoinPoint point) throws Throwable {

        Signature signature = point.getSignature();
        MethodSignature methodSignature = null;
        if (!(signature instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        methodSignature = (MethodSignature) signature;

        Object target = point.getTarget();
        Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());

        NeedCutAround logotype = currentMethod.getAnnotation(NeedCutAround.class);
        /*aflnet相关处理逻辑,负责数据存入*/
        switch (logotype.name()) {
            case "aflnet":
                return null;
//                aflnetAround(logotype.function());
            case "kitty":
                return kittyAround(logotype.function(), point);
            default:
                return null;
        }
    }

    private Map<String, List<String>> kittyAround(String function, ProceedingJoinPoint point) {
        Object result = null;
        if (function == null || "".equals(function)) {
            throw new AflnetException("There is no value in function!");
        }
        if ("generation".equals(function)) {
            /*运行前处理*/
            SystemRunningParams.testTimeMessage.get("kitty").put("start", System.currentTimeMillis());
            /*放行方法*/
            try {
                result = point.proceed();
            } catch (Throwable e) {
                e.printStackTrace();
                throw new KittyException("Kitty run error!");
            }
            /*运行后方法*/
            if (function == null || "".equals(function)) {
                throw new AflnetException("There is no value in function!");
            }
            /*存入kitty结束时的任务信息*/
            try {
                SystemRunningParams.testTimeMessage.get("kitty").put("end", System.currentTimeMillis());
                System.out.println(SystemRunningParams.testTimeMessage.get("kitty").put("end", System.currentTimeMillis()));
                int missionId = SystemRunningParams.kittyMissionId;
                Long runTime = SystemRunningParams.testTimeMessage.get("kitty").get("end") - SystemRunningParams.testTimeMessage.get("kitty").get("start");
                System.out.println("MissionId :" + missionId);
                MissionInfo missionInfo = missionInfoMapper.selectByMissionId(missionId);
                missionInfo.setState(MissionStateEnum.DONE.getStateCode());
                missionInfo.setRunTime(runTime);
                GlobalClass.missionInfoMapper.updateMission(missionInfo);//更新该次任务的执行时间;
            } catch (Exception e) {
                e.printStackTrace();
                throw new MysqlException("Kitty start backup failed!");

            }
        }
        if (result == null) {
            throw new KittyException("Result is null!");
        }
        return (Map<String, List<String>>) result;
    }

    @Override
    public int getOrder() {
        return 2;
    }
}