Search in sources :

Example 11 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project free-framework by a601942905git.

the class DynamicDataSourceAspect method doAround.

/**
 * 在切入点方法执行前后加入逻辑
 * @param pjp
 */
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) {
    Object obj = null;
    Signature signature = pjp.getSignature();
    MethodSignature methodSignature = (MethodSignature) signature;
    Object target = pjp.getTarget();
    try {
        Method method = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        String methodName = method.getName();
        setDataSourceType(methodName);
        // 执行目标方法
        obj = pjp.proceed();
        // 释放当前线程中的资源
        DataSourceHolder.removeDataSourceKey();
    } catch (NoSuchMethodException e) {
        log.error("======>DynamicDataSourceAspect.doAround,NoSuchMethodException:{}", e);
    } catch (Throwable throwable) {
        log.error("======>DynamicDataSourceAspect.doAround,throwable:{}", throwable);
    }
    return obj;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 12 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.

the class AsyncAspect method getExecutionId.

private String getExecutionId(ProceedingJoinPoint pjp) {
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    Method m = ms.getMethod();
    final AsyncOperation asyncOperationAnnotation = m.getAnnotation(AsyncOperation.class);
    final Class<? extends ExecutionIdGenerator> executionIdGeneratorClass = asyncOperationAnnotation.executionIdGeneratorClass();
    try {
        return executionIdGeneratorClass.newInstance().getExecutionId(pjp);
    } catch (Exception e) {
        LOGGER.warn("could not get the async id from {} because {}, fall back to null", pjp.toLongString(), e);
        return null;
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) TDPException(org.talend.dataprep.exception.TDPException) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException)

Example 13 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.

the class AsyncAspect method getResultUrl.

/**
 * Return the URL used to get the result of the asynchronous method
 * @param pjp pjp the proceeding join point.
 * @return the URL used to get the result of the asynchronous method
 */
private AsyncExecutionResult getResultUrl(ProceedingJoinPoint pjp) {
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    Method m = ms.getMethod();
    final AsyncOperation asyncOperationAnnotation = m.getAnnotation(AsyncOperation.class);
    Class<? extends ResultUrlGenerator> resultUrlClass = asyncOperationAnnotation.resultUrlGenerator();
    final ResultUrlGenerator resultUrlGenerator = applicationContext.getBean(resultUrlClass);
    Object[] args = AnnotationUtils.extractAsyncParameter(pjp);
    return resultUrlGenerator.generateResultUrl(args);
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) ResultUrlGenerator(org.talend.dataprep.async.result.ResultUrlGenerator) Method(java.lang.reflect.Method)

Example 14 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.

the class AsyncAspect method executeAsynchronously.

/**
 * Return if we need to execute the method asynchronously by calling the conditionalClass definined on the annotation.
 * By Default call the AlwaysTrueCondtion
 * @param pjp pjp the proceeding join point.
 * @return true if we need to execute the method asynchronously. False otherwise
 */
private Boolean executeAsynchronously(ProceedingJoinPoint pjp) {
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    Method m = ms.getMethod();
    final AsyncOperation asyncOperationAnnotation = m.getAnnotation(AsyncOperation.class);
    Class<? extends ConditionalTest> conditionalTestGenerator = asyncOperationAnnotation.conditionalClass();
    final ConditionalTest conditionalTest = applicationContext.getBean(conditionalTestGenerator);
    Object[] args = AnnotationUtils.extractAsyncParameter(pjp);
    return conditionalTest.apply(args);
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) ConditionalTest(org.talend.dataprep.async.conditional.ConditionalTest) Method(java.lang.reflect.Method)

Example 15 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.

the class AnnotationUtils method getAnnotatedParameterIndex.

/**
 * Returns the id of the parameter annotated with <code>annotationClass</code> in all the <code>pjp</code>
 * arguments. If multiple parameters hold the annotation, returns the index of the <b>last</b> parameter.
 *
 * @param pjp The {@link ProceedingJoinPoint} to check for annotation in parameters.
 * @param annotationClass The annotation to look for.
 * @return The index of the annotated parameter or -1 if not found.
 */
public static int getAnnotatedParameterIndex(ProceedingJoinPoint pjp, Class<? extends Annotation> annotationClass) {
    MethodSignature ms = (MethodSignature) pjp.getSignature();
    Method m = ms.getMethod();
    Annotation[][] pa = m.getParameterAnnotations();
    int idParameterIndex = -1;
    int i = 0;
    for (Annotation[] annotations : pa) {
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(annotationClass)) {
                idParameterIndex = i;
            }
        }
        i++;
    }
    return idParameterIndex;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Annotation(java.lang.annotation.Annotation)

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