Search in sources :

Example 96 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project syncope by apache.

the class LogicInvocationHandler method around.

@Around("execution(* org.apache.syncope.core.logic.AbstractLogic+.*(..))")
public Object around(final ProceedingJoinPoint joinPoint) throws Throwable {
    Class<?> clazz = joinPoint.getTarget().getClass();
    Object[] input = joinPoint.getArgs();
    String category = clazz.getSimpleName();
    MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    Method method = ms.getMethod();
    String event = joinPoint.getSignature().getName();
    boolean notificationsAvailable = notificationManager.notificationsAvailable(AuditElements.EventCategoryType.LOGIC, category, null, event);
    boolean auditRequested = auditManager.auditRequested(AuditElements.EventCategoryType.LOGIC, category, null, event);
    AuditElements.Result condition = null;
    Object output = null;
    Object before = null;
    try {
        LOG.debug("Before {}.{}({})", clazz.getSimpleName(), event, input == null || input.length == 0 ? StringUtils.EMPTY : Arrays.asList(input));
        if (notificationsAvailable || auditRequested) {
            try {
                before = ((AbstractLogic) joinPoint.getTarget()).resolveBeanReference(method, input);
            } catch (UnresolvedReferenceException ignore) {
                LOG.debug("Unresolved bean reference ...");
            }
        }
        output = joinPoint.proceed();
        condition = AuditElements.Result.SUCCESS;
        LOG.debug("After returning {}.{}: {}", clazz.getSimpleName(), event, output);
        return output;
    } catch (Throwable t) {
        output = t;
        condition = AuditElements.Result.FAILURE;
        LOG.debug("After throwing {}.{}", clazz.getSimpleName(), event);
        throw t;
    } finally {
        if (notificationsAvailable || auditRequested) {
            Map<String, Object> jobMap = new HashMap<>();
            jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new AfterHandlingEvent(AuditElements.EventCategoryType.LOGIC, category, null, event, condition, before, output, input));
            AfterHandlingJob.schedule(scheduler, jobMap);
        }
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) AfterHandlingEvent(org.apache.syncope.core.provisioning.api.event.AfterHandlingEvent) AuditElements(org.apache.syncope.common.lib.types.AuditElements) Around(org.aspectj.lang.annotation.Around)

Example 97 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project vip by guangdada.

the class MultiSourceExAop method around.

@Around("cut()")
public Object 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());
    DataSource datasource = currentMethod.getAnnotation(DataSource.class);
    if (datasource != null) {
        DataSourceContextHolder.setDataSourceType(datasource.name());
        log.debug("设置数据源为:" + datasource.name());
    } else {
        DataSourceContextHolder.setDataSourceType(DSEnum.DATA_SOURCE_GUNS);
        log.debug("设置数据源为:dataSourceCurrent");
    }
    try {
        return point.proceed();
    } finally {
        log.debug("清空数据源信息!");
        DataSourceContextHolder.clearDataSourceType();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) DataSource(com.ikoori.vip.common.annotion.DataSource) Around(org.aspectj.lang.annotation.Around)

Example 98 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project moon by gentoo111.

the class LogAdvice method logSave.

@Around(value = "@annotation(com.moon.admin.common.utils.LogAnnotation)")
public Object logSave(ProceedingJoinPoint joinPoint) throws Throwable {
    SysLogs sysLogs = new SysLogs();
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String module = null;
    LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class);
    module = logAnnotation.module();
    if (StringUtils.isEmpty(module)) {
        ApiOperation apiOperation = methodSignature.getMethod().getDeclaredAnnotation(ApiOperation.class);
        if (apiOperation != null) {
            module = apiOperation.value();
        }
    }
    if (StringUtils.isEmpty(module)) {
        throw new RuntimeException("没有指定日志module");
    }
    sysLogs.setModule(module);
    try {
        Object object = joinPoint.proceed();
        sysLogs.setFlag(true);
        sysLogService.save(sysLogs);
        return object;
    } catch (Exception e) {
        sysLogs.setFlag(false);
        sysLogs.setRemark(e.getMessage());
        sysLogService.save(sysLogs);
        throw e;
    }
}
Also used : SysLogs(com.moon.admin.domain.SysLogs) MethodSignature(org.aspectj.lang.reflect.MethodSignature) ApiOperation(io.swagger.annotations.ApiOperation) Around(org.aspectj.lang.annotation.Around)

Example 99 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project alien4cloud by alien4cloud.

the class ToscaContextualAspect method ensureContext.

@Around("@annotation(alien4cloud.tosca.context.ToscaContextual)")
public Object ensureContext(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature ms = (MethodSignature) joinPoint.getSignature();
    Method m = ms.getMethod();
    boolean requireNew = m.getAnnotation(ToscaContextual.class).requiresNew();
    return execInToscaContext(new Supplier<Object>() {

        @Override
        @SneakyThrows
        public Object get() {
            return joinPoint.proceed();
        }
    }, requireNew, joinPoint.getArgs());
// boolean initContext = false;
// ToscaContext.Context existingContext = ToscaContext.get();
// if (requireNew || existingContext == null) {
// initContext = true;
// }
// try {
// // try to find dependencies from parameters
// if (initContext) {
// Set<CSARDependency> dependencies = findDependencies(joinPoint.getArgs());
// log.debug("Initializing Tosca Context with dependencies {}", dependencies);
// ToscaContext.init(dependencies);
// }
// return joinPoint.proceed();
// } finally {
// if (initContext) {
// log.debug("Destroying Tosca Context");
// ToscaContext.destroy();
// }
// if (existingContext != null) {
// log.debug("Set back the existing context");
// ToscaContext.set(existingContext);
// }
// }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) SneakyThrows(lombok.SneakyThrows) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 100 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project entando-core by entando.

the class CacheInfoManager method aroundCacheableMethod.

@Around("@annotation(cacheableInfo)")
public Object aroundCacheableMethod(ProceedingJoinPoint pjp, CacheableInfo cacheableInfo) throws Throwable {
    Object result = pjp.proceed();
    if (cacheableInfo.expiresInMinute() < 0 && (cacheableInfo.groups() == null || cacheableInfo.groups().trim().length() == 0)) {
        return result;
    }
    try {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method targetMethod = methodSignature.getMethod();
        Class targetClass = pjp.getTarget().getClass();
        Method effectiveTargetMethod = targetClass.getMethod(targetMethod.getName(), targetMethod.getParameterTypes());
        Cacheable cacheable = effectiveTargetMethod.getAnnotation(Cacheable.class);
        if (null == cacheable) {
            CachePut cachePut = effectiveTargetMethod.getAnnotation(CachePut.class);
            if (null == cachePut) {
                return result;
            }
            String[] cacheNames = cachePut.value();
            Object key = this.evaluateExpression(cachePut.key().toString(), targetMethod, pjp.getArgs(), effectiveTargetMethod, targetClass);
            for (String cacheName : cacheNames) {
                if (cacheableInfo.groups() != null && cacheableInfo.groups().trim().length() > 0) {
                    Object groupsCsv = this.evaluateExpression(cacheableInfo.groups().toString(), targetMethod, pjp.getArgs(), effectiveTargetMethod, targetClass);
                    if (null != groupsCsv && groupsCsv.toString().trim().length() > 0) {
                        String[] groups = groupsCsv.toString().split(",");
                        this.putInGroup(cacheName, key.toString(), groups);
                    }
                }
                if (cacheableInfo.expiresInMinute() > 0) {
                    this.setExpirationTime(cacheName, key.toString(), cacheableInfo.expiresInMinute());
                }
            }
        } else {
            String[] cacheNames = cacheable.value();
            Object key = this.evaluateExpression(cacheable.key().toString(), targetMethod, pjp.getArgs(), effectiveTargetMethod, targetClass);
            for (String cacheName : cacheNames) {
                if (cacheableInfo.groups() != null && cacheableInfo.groups().trim().length() > 0) {
                    Object groupsCsv = this.evaluateExpression(cacheableInfo.groups().toString(), targetMethod, pjp.getArgs(), effectiveTargetMethod, targetClass);
                    if (null != groupsCsv && groupsCsv.toString().trim().length() > 0) {
                        String[] groups = groupsCsv.toString().split(",");
                        this.putInGroup(cacheName, key.toString(), groups);
                    }
                }
                if (cacheableInfo.expiresInMinute() > 0) {
                    this.setExpirationTime(cacheName, key.toString(), cacheableInfo.expiresInMinute());
                }
            }
        }
    } catch (Throwable t) {
        logger.error("Error while evaluating cacheableInfo annotation", t);
        throw new ApsSystemException("Error while evaluating cacheableInfo annotation", t);
    }
    return result;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Cacheable(org.springframework.cache.annotation.Cacheable) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) Method(java.lang.reflect.Method) CachePut(org.springframework.cache.annotation.CachePut) Around(org.aspectj.lang.annotation.Around)

Aggregations

MethodSignature (org.aspectj.lang.reflect.MethodSignature)116 Method (java.lang.reflect.Method)95 Around (org.aspectj.lang.annotation.Around)43 JoinPoint (org.aspectj.lang.JoinPoint)30 AccessDeniedException (org.springframework.security.access.AccessDeniedException)26 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)25 Test (org.junit.Test)25 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)24 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)22 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)21 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)14 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)13 Signature (org.aspectj.lang.Signature)13 Annotation (java.lang.annotation.Annotation)10 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)2 List (java.util.List)2 Tick (mysh.util.Tick)2 Ignite (org.apache.ignite.Ignite)2