Search in sources :

Example 6 with AfterReturning

use of org.aspectj.lang.annotation.AfterReturning in project spf4j by zolyfarkas.

the class SamplingAllocationMonitorAspect method afterAllocation.

@AfterReturning(pointcut = "call(*.new(..))", returning = "obj", argNames = "jp,obj")
public void afterAllocation(final JoinPoint jp, final Object obj) {
    MutableInteger counter = ThreadLocalCounter.get();
    int value = counter.getValue();
    if (value < SAMPLE_COUNT) {
        counter.setValue(value + 1);
    } else {
        // the stack trace get and the object size method are expensive to be done at every allocation...
        counter.setValue(0);
        StackTrace st = StackTrace.from(Thread.currentThread().getStackTrace(), 2);
        RECORDER.getRecorder(st).record(InstrumentationHelper.getObjectSize(obj));
    }
}
Also used : StackTrace(org.spf4j.stackmonitor.StackTrace) MutableInteger(org.spf4j.base.MutableInteger) JoinPoint(org.aspectj.lang.JoinPoint) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 7 with AfterReturning

use of org.aspectj.lang.annotation.AfterReturning in project topcom-cloud by 545314690.

the class LoginLogService method doAfter.

@AfterReturning("loginMethod()")
public void doAfter(JoinPoint pjp) throws Exception {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    String contextPath = request.getContextPath();
    User user = SubjectUtil.getCurrentUser(request);
    LoginLog loginLog = new LoginLog();
    loginLog.setUserId(user.getId());
    loginLog.setUsername(user.getUsername());
    loginLog.setHost(request.getRemoteHost());
    if (StringUtils.isNotBlank(contextPath) && contextPath.length() > 0) {
        loginLog.setSysName(contextPath.substring(1));
    }
    loginLogManager.save(loginLog);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LoginLog(com.topcom.cms.domain.LoginLog) User(com.topcom.cms.domain.User) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 8 with AfterReturning

use of org.aspectj.lang.annotation.AfterReturning in project topcom-cloud by 545314690.

the class OperationLogService method doBasicProfiling.

@AfterReturning("anyMethod()")
public void doBasicProfiling(JoinPoint pjp) throws Throwable {
    HttpServletRequest request = SubjectUtil.getRequest();
    Object[] args = pjp.getArgs();
    OperationLog ol = new OperationLog();
    Object obj = args[0];
    // 拦截的实体类 Object target = pjp.getTarget();
    Class<?> entityClazz = (Class<?>) ((ParameterizedType) pjp.getTarget().getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    ol.setEntity(entityClazz.getName());
    if (obj instanceof Long) {
        ol.setOperation(OperationLog.Operation.DELETE);
    } else if (obj instanceof BaseEntityModel) {
        BaseEntityModel objModel = (BaseEntityModel) obj;
        if (objModel.getId() == null || objModel.getId() == 0) {
            ol.setOperation(OperationLog.Operation.CREATE);
        } else {
            ol.setOperation(OperationLog.Operation.MODIFY);
        }
        ol.setEntity(objModel.getEntityName());
    } else {
        return;
    }
    // 拦截的方法名称
    String methodName = pjp.getSignature().getName();
    /**
     * request是null,说明是系统调用save,否则是用户调用
     */
    if (request == null) {
        ol.setUsername("系统操作");
    } else {
        User user = SubjectUtil.getCurrentUser();
        ol.setHost(request.getRemoteHost());
        if (user != null) {
            ol.setUserId(user.getId());
            ol.setUsername(user.getUsername());
        }
    }
    ol.setMethod(methodName);
    /**
     * 不使用save方法,更改了操作日志的保存方法,否则会造成无限循环调用
     */
    // TODO:写一个注解,在方法名上标注不被拦截的方法
    operationLogManager.persist(ol);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ParameterizedType(java.lang.reflect.ParameterizedType) User(com.topcom.cms.domain.User) OperationLog(com.topcom.cms.domain.OperationLog) BaseEntityModel(com.topcom.cms.base.model.BaseEntityModel) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 9 with AfterReturning

use of org.aspectj.lang.annotation.AfterReturning in project spring-framework by spring-projects.

the class ReflectiveAspectJAdvisorFactory method getAdvice.

@Override
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
    Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
    validate(candidateAspectClass);
    AspectJAnnotation<?> aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
    if (aspectJAnnotation == null) {
        return null;
    }
    // Check that it's an AspectJ-annotated class
    if (!isAspect(candidateAspectClass)) {
        throw new AopConfigException("Advice must be declared inside an aspect type: " + "Offending method '" + candidateAdviceMethod + "' in class [" + candidateAspectClass.getName() + "]");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Found AspectJ method: " + candidateAdviceMethod);
    }
    AbstractAspectJAdvice springAdvice;
    switch(aspectJAnnotation.getAnnotationType()) {
        case AtBefore:
            springAdvice = new AspectJMethodBeforeAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfter:
            springAdvice = new AspectJAfterAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfterReturning:
            springAdvice = new AspectJAfterReturningAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterReturningAnnotation.returning())) {
                springAdvice.setReturningName(afterReturningAnnotation.returning());
            }
            break;
        case AtAfterThrowing:
            springAdvice = new AspectJAfterThrowingAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
                springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
            }
            break;
        case AtAround:
            springAdvice = new AspectJAroundAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtPointcut:
            if (logger.isDebugEnabled()) {
                logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
            }
            return null;
        default:
            throw new UnsupportedOperationException("Unsupported advice type on method: " + candidateAdviceMethod);
    }
    // Now to configure the advice...
    springAdvice.setAspectName(aspectName);
    springAdvice.setDeclarationOrder(declarationOrder);
    String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
    if (argNames != null) {
        springAdvice.setArgumentNamesFromStringArray(argNames);
    }
    springAdvice.calculateArgumentBindings();
    return springAdvice;
}
Also used : AopConfigException(org.springframework.aop.framework.AopConfigException) AspectJAfterThrowingAdvice(org.springframework.aop.aspectj.AspectJAfterThrowingAdvice) AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJMethodBeforeAdvice(org.springframework.aop.aspectj.AspectJMethodBeforeAdvice) AspectJAfterAdvice(org.springframework.aop.aspectj.AspectJAfterAdvice) AbstractAspectJAdvice(org.springframework.aop.aspectj.AbstractAspectJAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AfterReturning(org.aspectj.lang.annotation.AfterReturning) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Example 10 with AfterReturning

use of org.aspectj.lang.annotation.AfterReturning in project ORCID-Source by ORCID.

the class OrcidApiVisibilitySecurityAspect method simpleVisibilityProfileFilter.

@AfterReturning(pointcut = "@annotation(visibilityAnnotation)", returning = "profile")
public void simpleVisibilityProfileFilter(OrcidProfile profile, VisibilityControl visibilityAnnotation) {
    if (profile != null) {
        OrcidMessage message = new OrcidMessage(profile);
        visibilityFilter.filter(message, visibilityAnnotation.visibilities());
    }
}
Also used : OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Aggregations

AfterReturning (org.aspectj.lang.annotation.AfterReturning)16 UserDetails (com.agiletec.aps.system.services.user.UserDetails)4 AbstractUser (com.agiletec.aps.system.services.user.AbstractUser)2 User (com.topcom.cms.domain.User)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 JoinPoint (org.aspectj.lang.JoinPoint)2 IUserProfile (org.entando.entando.aps.system.services.userprofile.model.IUserProfile)2 OrcidMessage (org.orcid.jaxb.model.message.OrcidMessage)2 ValidateAnnotation (com.paascloud.core.annotation.ValidateAnnotation)1 BaseEntityModel (com.topcom.cms.base.model.BaseEntityModel)1 LoginLog (com.topcom.cms.domain.LoginLog)1 OperationLog (com.topcom.cms.domain.OperationLog)1 OperationRecord (com.weibo.model.OperationRecord)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 HashSet (java.util.HashSet)1 AfterThrowing (org.aspectj.lang.annotation.AfterThrowing)1 Profile (org.craftercms.profile.api.Profile)1 Authentication (org.craftercms.security.authentication.Authentication)1 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)1