admin
2020-08-22 813d2cae75cbae3f216de5f493af643c5a560c82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.yeshi.buwan.aspect;
 
import java.lang.reflect.Method;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import net.sf.ehcache.CacheManager;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
 
import com.yeshi.buwan.domain.AdminInfo;
import com.yeshi.buwan.log.LogHelper;
import com.yeshi.buwan.util.Constant;
import com.yeshi.buwan.util.IPUtil;
 
@Component
@Aspect()
public class LogAspect {
 
 
    private String getMethodName(ProceedingJoinPoint pjp) {
        try {
            MethodSignature sig = (MethodSignature) pjp.getSignature();
            Method method = sig.getMethod();
            return method.getDeclaringClass().getName() + "." + sig.getMethod().getName();
        } catch (Exception e) {
            return "";
        }
    }
 
    @Around("execution(public * com.yeshi.buwan.controller.parser.*.*(..))")
    public Object countTime(ProceedingJoinPoint joinPoint) {
 
        Object[] args = joinPoint.getArgs();
        long starttime = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        LogHelper.countTime(getMethodName(joinPoint) + "#" + (System.currentTimeMillis() - starttime) + "");
        return obj;
    }
 
    // @Around("execution(public * com.yeshi.buwan.controller.parser.*(..))")
    public Object statisticEhcache(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        long starttime = System.currentTimeMillis();
        Object obj = null;
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        LogHelper.countTime(getMethodName(joinPoint));
        CacheManager manager = CacheManager.newInstance("src/ehcache.xml");
        // manager.
        // Cache test = manager.getCache("testCache");
        return obj;
    }
    
//    @Around("execution(public * com.yeshi.buwan.controller.admin.api.*.*(..))")
//    public Object verifyLoginState(ProceedingJoinPoint joinPoint) {
//        
//        ServletWebRequest servletContainer = (ServletWebRequest)RequestContextHolder.getRequestAttributes();
//        
//        HttpServletRequest request = servletContainer.getRequest();
////        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        AdminInfo admin = (AdminInfo) request.getSession().getAttribute(Constant.ADMIN);
//        System.out.println("姝e湪妫�祴鏄惁鐧婚檰...");
//        if(admin==null){
//            String ip = IPUtil.getRemotIP(request);
//            System.out.println("鍗遍櫓IP::::"+ip);
//            System.out.println("鏈櫥闄�..");
//            return null;
//        }
//        System.out.println("宸茬櫥闄�..");
//        Object[] args = joinPoint.getArgs();
//        Object obj = null;
//        try {
//            obj = joinPoint.proceed(args);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
//        return obj;
//    }
 
}