com.lushapp.core.aop.LogAspect.java Source code

Java tutorial

Introduction

Here is the source code for com.lushapp.core.aop.LogAspect.java

Source

/**
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.lushapp.core.aop;

import com.lushapp.common.orm.hibernate.DefaultEntityManager;
import com.lushapp.common.utils.StringUtils;
import com.lushapp.common.utils.browser.BrowserType;
import com.lushapp.common.utils.browser.BrowserUtils;
import com.lushapp.common.web.springmvc.SpringMVCHolder;
import com.lushapp.core.security.SecurityConstants;
import com.lushapp.core.security.SecurityUtils;
import com.lushapp.core.security.SessionInfo;
import com.lushapp.modules.sys._enum.LogType;
import com.lushapp.modules.sys.entity.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 
 */
// @Aspect ?
@Aspect
@Component(value = SecurityConstants.SERVICE_SECURITY_LOGINASPECT)
public class LogAspect {

    private static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Autowired
    private DefaultEntityManager defaultEntityManager;

    /**
     * @param point 
     */

    @Around("execution(* com.lushapp.modules.*.service..*Manager.*(..))")
    public Object logAll(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        // ??
        String methodName = point.getSignature().getName();
        String className = point.getTarget().getClass().getSimpleName();
        String userName = null;
        Long start = 0L;
        Long end = 0L;
        String ip = null;
        // ?
        try {
            // 
            start = System.currentTimeMillis();
            result = point.proceed();
            end = System.currentTimeMillis();

            // ??
            SessionInfo sessionInfo = null;
            try {
                sessionInfo = SecurityUtils.getCurrentSessionInfo();
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
            if (sessionInfo != null) {
                userName = sessionInfo.getLoginName();
                ip = sessionInfo.getIp();
            } else {
                userName = "";
                ip = "127.0.0.1";
                logger.warn("sessionInfo.");
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        String name = null;
        // ?
        if (className.indexOf("Resource") > -1) {
            name = "??";
        } else if (className.indexOf("Role") > -1) {
            name = "?";
        } else if (className.indexOf("User") > -1) {
            name = "?";
        } else if (className.indexOf("Organ") > -1) {
            name = "?";
        } else {
            name = className;
        }
        // ?
        String opertype = methodName;
        if (StringUtils.isNotBlank(opertype) && (opertype.indexOf("save") > -1 || opertype.indexOf("update") > -1
                || opertype.indexOf("delete") > -1 || opertype.indexOf("merge") > -1)) {
            Long time = end - start;
            Log log = new Log();
            log.setType(LogType.operate.getValue());
            log.setLoginName(userName);
            log.setModule(name);
            log.setAction(opertype);
            log.setOperTime(new Date(start));
            log.setActionTime(time.toString());
            log.setIp(ip);
            BrowserType browserType = BrowserUtils.getBrowserType(SpringMVCHolder.getRequest());
            log.setBrowserType(browserType == null ? null : browserType.toString());
            defaultEntityManager.save(log);
        }
        if (logger.isDebugEnabled()) {
            logger.debug(":{},?{},?{},{}ms.",
                    new Object[] { userName, className, methodName, end - start });
        }
        return result;
    }

}