Search in sources :

Example 51 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project new-cloud by xie-summer.

the class DataSourceAspect method before.

public void before(JoinPoint point) {
    Object target = point.getTarget();
    String method = point.getSignature().getName();
    Class<?>[] classz = target.getClass().getInterfaces();
    Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getParameterTypes();
    // DynamicDataSourceHolders.putDataSource(null);
    try {
        Method m = classz[0].getMethod(method, parameterTypes);
        if (m != null && m.isAnnotationPresent(DataSource.class)) {
            DataSource date = m.getAnnotation(DataSource.class);
            DynamicDataSourceHolders.putDataSource(date.value());
        // System.out.println(date.value()+"----------------");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method)

Example 52 with MethodSignature

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

the class GridifyAspectJAspect method gridify.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * @param joinPnt Join point provided by AspectJ AOP.
 * @return Method execution result.
 * @throws Throwable If execution failed.
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown", "unchecked" })
@Around("execution(@org.apache.ignite.compute.gridify.Gridify * *(..)) && !cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).getMethod();
    Gridify ann = mtd.getAnnotation(Gridify.class);
    assert ann != null : "Intercepted method does not have gridify annotation.";
    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended Ignite instance name.
    // NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
    if (F.isEmpty(igniteInstanceName))
        igniteInstanceName = null;
    if (G.state(igniteInstanceName) != STARTED)
        throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
    // Initialize defaults.
    GridifyArgument arg = new GridifyArgumentAdapter(mtd.getDeclaringClass(), mtd.getName(), mtd.getParameterTypes(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.proceed();
    }
    if (!ann.taskClass().equals(GridifyDefaultTask.class) && !ann.taskName().isEmpty()) {
        throw new IgniteCheckedException("Gridify annotation must specify either Gridify.taskName() or " + "Gridify.taskClass(), but not both: " + ann);
    }
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        // If task class was specified.
        if (!ann.taskClass().equals(GridifyDefaultTask.class)) {
            return ignite.compute().withTimeout(ann.timeout()).execute((Class<? extends ComputeTask<GridifyArgument, Object>>) ann.taskClass(), arg);
        }
        // If task name was not specified.
        if (ann.taskName().isEmpty()) {
            return ignite.compute().withTimeout(ann.timeout()).execute(new GridifyDefaultTask(joinPnt.getSignature().getDeclaringType()), arg);
        }
        // If task name was specified.
        return ignite.compute().withTimeout(ann.timeout()).execute(ann.taskName(), arg);
    } catch (Exception e) {
        for (Class<?> ex : ((MethodSignature) joinPnt.getSignature()).getMethod().getExceptionTypes()) {
            // Descend all levels down.
            Throwable cause = e.getCause();
            while (cause != null) {
                if (ex.isAssignableFrom(cause.getClass()))
                    throw cause;
                cause = cause.getCause();
            }
            if (ex.isAssignableFrom(e.getClass()))
                throw e;
        }
        throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Gridify(org.apache.ignite.compute.gridify.Gridify) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) GridifyArgument(org.apache.ignite.compute.gridify.GridifyArgument) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyDefaultTask(org.apache.ignite.compute.gridify.aop.GridifyDefaultTask) GridifyArgumentAdapter(org.apache.ignite.compute.gridify.aop.GridifyArgumentAdapter) Ignite(org.apache.ignite.Ignite) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 53 with MethodSignature

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

the class GridifySetToValueAspectJAspect method gridify.

/**
 * Aspect implementation which executes grid-enabled methods on remote
 * nodes.
 *
 * @param joinPnt Join point provided by AspectJ AOP.
 * @return Method execution result.
 * @throws Throwable If execution failed.
 */
@SuppressWarnings({ "ProhibitedExceptionDeclared", "ProhibitedExceptionThrown" })
@Around("execution(@org.apache.ignite.compute.gridify.GridifySetToValue * *(..)) && " + "!cflow(call(* org.apache.ignite.compute.ComputeJob.*(..)))")
public Object gridify(ProceedingJoinPoint joinPnt) throws Throwable {
    Method mtd = ((MethodSignature) joinPnt.getSignature()).getMethod();
    GridifySetToValue ann = mtd.getAnnotation(GridifySetToValue.class);
    assert ann != null : "Intercepted method does not have gridify annotation.";
    // Since annotations in Java don't allow 'null' as default value
    // we have accept an empty string and convert it here.
    // NOTE: there's unintended behavior when user specifies an empty
    // string as intended Igninte instance name.
    // NOTE: the 'ann.igniteInstanceName() == null' check is added to mitigate
    // annotation bugs in some scripting languages (e.g. Groovy).
    String igniteInstanceName = F.isEmpty(ann.igniteInstanceName()) ? ann.gridName() : ann.igniteInstanceName();
    if (F.isEmpty(igniteInstanceName))
        igniteInstanceName = null;
    if (G.state(igniteInstanceName) != STARTED)
        throw new IgniteCheckedException("Grid is not locally started: " + igniteInstanceName);
    GridifyNodeFilter nodeFilter = null;
    if (!ann.nodeFilter().equals(GridifyNodeFilter.class))
        nodeFilter = ann.nodeFilter().newInstance();
    // Check is method allowed for gridify.
    checkMethodSignature(mtd);
    GridifyArgumentBuilder argBuilder = new GridifyArgumentBuilder();
    // Creates task argument.
    GridifyRangeArgument arg = argBuilder.createTaskArgument(mtd.getDeclaringClass(), mtd.getName(), mtd.getReturnType(), mtd.getParameterTypes(), mtd.getParameterAnnotations(), joinPnt.getArgs(), joinPnt.getTarget());
    if (!ann.interceptor().equals(GridifyInterceptor.class)) {
        // Check interceptor first.
        if (!ann.interceptor().newInstance().isGridify(ann, arg))
            return joinPnt.proceed();
    }
    // Proceed locally for negative threshold parameter.
    if (ann.threshold() < 0)
        return joinPnt.proceed();
    // Analyse where to execute method (remotely or locally).
    if (arg.getInputSize() != UNKNOWN_SIZE && arg.getInputSize() <= ann.threshold())
        return joinPnt.proceed();
    // Check is split to jobs allowed for input method argument with declared splitSize.
    checkIsSplitToJobsAllowed(arg, ann);
    try {
        Ignite ignite = G.ignite(igniteInstanceName);
        return execute(mtd, ignite.compute(), joinPnt.getSignature().getDeclaringType(), arg, nodeFilter, ann.threshold(), ann.splitSize(), ann.timeout());
    } catch (Exception e) {
        for (Class<?> ex : ((MethodSignature) joinPnt.getSignature()).getMethod().getExceptionTypes()) {
            // Descend all levels down.
            Throwable cause = e.getCause();
            while (cause != null) {
                if (ex.isAssignableFrom(cause.getClass()))
                    throw cause;
                cause = cause.getCause();
            }
            if (ex.isAssignableFrom(e.getClass()))
                throw e;
        }
        throw new GridifyRuntimeException("Undeclared exception thrown: " + e.getMessage(), e);
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) GridifyNodeFilter(org.apache.ignite.compute.gridify.GridifyNodeFilter) GridifyArgumentBuilder(org.apache.ignite.internal.util.gridify.GridifyArgumentBuilder) Method(java.lang.reflect.Method) GridifySetToValue(org.apache.ignite.compute.gridify.GridifySetToValue) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) GridifyInterceptor(org.apache.ignite.compute.gridify.GridifyInterceptor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Ignite(org.apache.ignite.Ignite) GridifyRangeArgument(org.apache.ignite.internal.util.gridify.GridifyRangeArgument) GridifyRuntimeException(org.apache.ignite.compute.gridify.GridifyRuntimeException) Around(org.aspectj.lang.annotation.Around)

Example 54 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project myjdbc by beijing-penguin.

the class TransactionManager method doAround.

// 用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型
public Object doAround(ProceedingJoinPoint call) throws Throwable {
    Signature sig = call.getSignature();
    MethodSignature ms = (MethodSignature) sig;
    Method method = call.getTarget().getClass().getDeclaredMethod(ms.getName(), ms.getParameterTypes());
    Transactional transactional = method.getAnnotation(Transactional.class);
    if (transactional == null) {
        // 方法无注解,查找类上注解,并判断当前调用方法是否为当前类定义的(防止父类方法触发事务边界)
        transactional = method.getDeclaringClass().getAnnotation(Transactional.class);
    }
    if (transactional != null) {
        // 如果不为空,则开启事务
        if (transactional.readOnly() == false) {
            ConnectionManager.setTransaction(true);
        } else {
            ConnectionManager.setReadOnly(true);
        }
    } else {
        ConnectionManager.setTransaction(false);
        ConnectionManager.setReadOnly(false);
    }
    Object invokeObj = null;
    try {
        // 执行目标方法
        invokeObj = call.proceed();
        // invokeObj = method.invoke(call.getTarget(), call.getArgs());
        ConnectionManager.commitAll();
    } catch (Throwable e) {
        ConnectionManager.rollbackAll();
        throw e;
    } finally {
        ConnectionManager.closeConnectionAll();
    }
    return invokeObj;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) Transactional(org.springframework.transaction.annotation.Transactional)

Example 55 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project bamboobsc by billchen198318.

the class ServiceAuthorityCheckAspect method logicServiceProcess.

@Around(AspectConstants.LOGIC_SERVICE_PACKAGE)
public Object logicServiceProcess(ProceedingJoinPoint pjp) throws AuthorityException, ServiceException, Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Annotation[] annotations = pjp.getTarget().getClass().getAnnotations();
    String serviceId = AspectConstants.getServiceId(annotations);
    Subject subject = SecurityUtils.getSubject();
    Method method = signature.getMethod();
    if (subject.hasRole(Constants.SUPER_ROLE_ALL) || subject.hasRole(Constants.SUPER_ROLE_ADMIN)) {
        SysEventLogSupport.log((String) subject.getPrincipal(), Constants.getSystem(), this.getEventId(serviceId, method.getName()), true);
        return pjp.proceed();
    }
    if (StringUtils.isBlank(serviceId)) {
        // 沒有 service id 無法判斷檢查 
        SysEventLogSupport.log((String) subject.getPrincipal(), Constants.getSystem(), this.getEventId(serviceId, method.getName()), true);
        return pjp.proceed();
    }
    if (!this.isServiceAuthorityCheck(annotations)) {
        // 沒有 ServiceAuthority 或 check=false 就不用檢查了 
        SysEventLogSupport.log((String) subject.getPrincipal(), Constants.getSystem(), this.getEventId(serviceId, method.getName()), true);
        return pjp.proceed();
    }
    Annotation[] methodAnnotations = method.getAnnotations();
    if (this.isServiceMethodAuthority(serviceId, methodAnnotations, subject)) {
        SysEventLogSupport.log((String) subject.getPrincipal(), Constants.getSystem(), this.getEventId(serviceId, method.getName()), true);
        return pjp.proceed();
    }
    logger.warn("[decline] user[" + subject.getPrincipal() + "] " + pjp.getTarget().getClass().getName() + " - " + signature.getMethod().getName());
    SysEventLogSupport.log((String) subject.getPrincipal(), Constants.getSystem(), this.getEventId(serviceId, method.getName()), false);
    throw new AuthorityException(SysMessageUtil.get(GreenStepSysMsgConstants.NO_PERMISSION));
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Subject(org.apache.shiro.subject.Subject) AuthorityException(com.netsteadfast.greenstep.base.exception.AuthorityException) 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