Search in sources :

Example 66 with ProceedingJoinPoint

use of org.aspectj.lang.ProceedingJoinPoint in project PhotoNoter by yydcdut.

the class PermissionAspect method aroundCheckAndRequestPermission.

@Around("pointcut2CheckAndRequestPermissions(permission)")
public void aroundCheckAndRequestPermission(ProceedingJoinPoint proceedingJoinPoint, Permission permission) {
    if (permission == null) {
        return;
    }
    IPresenter iPresenter = (IPresenter) proceedingJoinPoint.getTarget();
    int value = permission.value();
    switch(value) {
        case PermissionUtils.CODE_CAMERA:
        case PermissionUtils.CODE_ADJUST_CAMERA:
            {
                if (hasPermission4Camera(PermissionInstance.context)) {
                    try {
                        proceedingJoinPoint.proceed();
                    } catch (Throwable throwable) {
                        YLog.e(throwable);
                    }
                } else {
                    if (iPresenter.getIView().getRequestType().isActivity()) {
                        requestPermissions4Activity(iPresenter.getIView().getRequestType().getActivity(), PermissionInstance.context.getString(R.string.permission_camera_init), PermissionUtils.PERMISSION_CAMERA, value);
                    } else {
                        requestPermissions4Fragment(iPresenter.getIView().getRequestType().getFragment(), PermissionInstance.context.getString(R.string.permission_camera_init), PermissionUtils.PERMISSION_CAMERA, value);
                    }
                }
            }
            break;
        case PermissionUtils.CODE_STORAGE:
            {
                if (hasPermission4Storage(PermissionInstance.context)) {
                    try {
                        proceedingJoinPoint.proceed();
                    } catch (Throwable throwable) {
                        YLog.e(throwable);
                    }
                } else {
                    if (iPresenter.getIView().getRequestType().isActivity()) {
                        requestPermissions4Activity(iPresenter.getIView().getRequestType().getActivity(), PermissionInstance.context.getString(R.string.permission_storage_init), PermissionUtils.PERMISSION_STORAGE, value);
                    } else {
                        requestPermissions4Fragment(iPresenter.getIView().getRequestType().getFragment(), PermissionInstance.context.getString(R.string.permission_storage_init), PermissionUtils.PERMISSION_STORAGE, value);
                    }
                }
            }
            break;
        case PermissionUtils.CODE_LOCATION_AND_CAMERA:
            {
                if (hasPermission4LocationAndCamera(PermissionInstance.context)) {
                    try {
                        proceedingJoinPoint.proceed();
                    } catch (Throwable throwable) {
                        YLog.e(throwable);
                    }
                } else {
                    if (iPresenter.getIView().getRequestType().isActivity()) {
                        requestPermissions4Activity(iPresenter.getIView().getRequestType().getActivity(), PermissionInstance.context.getString(R.string.permission_location), PermissionUtils.PERMISSION_LOCATION_AND_CAMERA, value);
                    } else {
                        requestPermissions4Fragment(iPresenter.getIView().getRequestType().getFragment(), PermissionInstance.context.getString(R.string.permission_location), PermissionUtils.PERMISSION_LOCATION_AND_CAMERA, value);
                    }
                }
            }
            break;
        case PermissionUtils.CODE_AUDIO:
            {
                if (hasPermission4Audio(PermissionInstance.context)) {
                    try {
                        proceedingJoinPoint.proceed();
                    } catch (Throwable throwable) {
                        YLog.e(throwable);
                    }
                } else {
                    if (iPresenter.getIView().getRequestType().isActivity()) {
                        requestPermissions4Activity(iPresenter.getIView().getRequestType().getActivity(), PermissionInstance.context.getString(R.string.permission_audio), PermissionUtils.PERMISSION_AUDIO, value);
                    } else {
                        requestPermissions4Fragment(iPresenter.getIView().getRequestType().getFragment(), PermissionInstance.context.getString(R.string.permission_audio), PermissionUtils.PERMISSION_AUDIO, value);
                    }
                }
            }
            break;
        case PermissionUtils.CODE_PHONE_STATE:
            {
                if (hasPermission4PhoneState(PermissionInstance.context)) {
                    try {
                        proceedingJoinPoint.proceed();
                    } catch (Throwable throwable) {
                        YLog.e(throwable);
                    }
                } else {
                    if (iPresenter.getIView().getRequestType().isActivity()) {
                        requestPermissions4Activity(iPresenter.getIView().getRequestType().getActivity(), PermissionInstance.context.getString(R.string.permission_phone_state), PermissionUtils.PERMISSION_PHONE_STATE, value);
                    } else {
                        requestPermissions4Fragment(iPresenter.getIView().getRequestType().getFragment(), PermissionInstance.context.getString(R.string.permission_phone_state), PermissionUtils.PERMISSION_PHONE_STATE, value);
                    }
                }
            }
            break;
    }
}
Also used : IPresenter(com.yydcdut.note.presenters.IPresenter) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Example 67 with ProceedingJoinPoint

use of org.aspectj.lang.ProceedingJoinPoint in project gocd by gocd.

the class InterceptorInjectorTest method testShouldReturnNullWhenNoHandlerFound.

@Test
public void testShouldReturnNullWhenNoHandlerFound() throws Throwable {
    ProceedingJoinPoint proceedingJoinPoint = mock(ProceedingJoinPoint.class);
    when(proceedingJoinPoint.proceed()).thenReturn(null);
    InterceptorInjector injector = new InterceptorInjector();
    HandlerExecutionChain handlers = injector.mergeInterceptorsToTabs(proceedingJoinPoint);
    assertNull(handlers);
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Example 68 with ProceedingJoinPoint

use of org.aspectj.lang.ProceedingJoinPoint in project JavaForFun by gumartinm.

the class DeadlockRetryAspect method doAround.

@Around(value = "@annotation(deadlockRetry)", argNames = "deadlockRetry")
public Object doAround(final ProceedingJoinPoint pjp, final DeadlockRetry deadlockRetry) throws Throwable {
    final int maxTries = deadlockRetry.maxTries();
    final long interval = deadlockRetry.interval();
    final Object target = pjp.getTarget();
    final MethodSignature signature = (MethodSignature) pjp.getSignature();
    final Method method = signature.getMethod();
    int count = 0;
    Throwable lastException = null;
    do {
        try {
            count++;
            logger.info("Attempting to invoke method " + method.getName() + " on " + target.getClass() + " count " + count);
            // Calling real method
            final Object result = pjp.proceed();
            logger.info("Completed invocation of method " + method.getName() + " on " + target.getClass());
            return result;
        } catch (final Throwable ex) {
            if (!isDeadLock(ex)) {
                throw ex;
            }
            lastException = ex;
            if (interval > 0) {
                try {
                    Thread.sleep(interval);
                } catch (final InterruptedException exi) {
                    logger.warn("Deadlock retry thread interrupt", exi);
                    // Restore interrupt status.
                    Thread.currentThread().interrupt();
                }
            }
        }
    } while (count < maxTries);
    // The exception being thrown will depend on the API that you are using.
    throw lastException;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Example 69 with ProceedingJoinPoint

use of org.aspectj.lang.ProceedingJoinPoint in project irida by phac-nml.

the class RunAsUserAspect method setSecurityContextFromAnalysisSubmission.

/**
 * Advice around a method annotated with {@link RunAsUser}. This method will
 * set the {@link User} specified in the {@link RunAsUser#value()} using
 * SpEL in the security context before the method is run, then reset the
 * original user after the method completes.
 *
 * @param jp
 *            {@link ProceedingJoinPoint} for the called method
 * @param userAnnotation
 *            {@link RunAsUser} annotation specifying the user
 * @return Return value of the method called
 * @throws Throwable
 *             if the method throws an exception
 */
@Around(value = "execution(* *(..)) && @annotation(userAnnotation)")
public Object setSecurityContextFromAnalysisSubmission(ProceedingJoinPoint jp, RunAsUser userAnnotation) throws Throwable {
    // Get the method arguments and apply them to an evaluation context
    MethodSignature signature = (MethodSignature) jp.getSignature();
    String[] parameterNames = signature.getParameterNames();
    Object[] args = jp.getArgs();
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    for (int i = 0; i < args.length; i++) {
        String name = parameterNames[i];
        Object val = args[i];
        evaluationContext.setVariable(name, val);
    }
    // get the expression from the annotation and apply it to the evaluation
    // context
    String expression = userAnnotation.value();
    ExpressionParser parser = new SpelExpressionParser();
    Expression parseExpression = parser.parseExpression(expression);
    Object expressionValue = parseExpression.getValue(evaluationContext);
    if (!(expressionValue instanceof User)) {
        throw new IllegalArgumentException("RunAsUser value must refer to a User");
    }
    User submitter = (User) expressionValue;
    // get the original security context
    logger.trace("Updating user authentication");
    SecurityContext originalConext = SecurityContextHolder.getContext();
    logger.trace("Original user: " + originalConext.getAuthentication().getName());
    logger.trace("Setting user " + submitter.getUsername());
    Object returnValue = null;
    try {
        // set the new user authentication
        PreAuthenticatedAuthenticationToken submitterAuthenticationToken = new PreAuthenticatedAuthenticationToken(submitter, null, Lists.newArrayList(submitter.getSystemRole()));
        SecurityContext newContext = SecurityContextHolder.createEmptyContext();
        newContext.setAuthentication(submitterAuthenticationToken);
        SecurityContextHolder.setContext(newContext);
        // run the method
        returnValue = jp.proceed();
    } finally {
        // return the old authentication
        logger.trace("Resetting authentication to " + originalConext.getAuthentication().getName());
        SecurityContextHolder.setContext(originalConext);
    }
    return returnValue;
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) User(ca.corefacility.bioinformatics.irida.model.user.User) MethodSignature(org.aspectj.lang.reflect.MethodSignature) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) SecurityContext(org.springframework.security.core.context.SecurityContext) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Around(org.aspectj.lang.annotation.Around)

Example 70 with ProceedingJoinPoint

use of org.aspectj.lang.ProceedingJoinPoint in project onebusaway-application-modules by camsys.

the class CacheableMethodKeyFactoryManager method getMatchingMethodsForJoinPoint.

public List<Method> getMatchingMethodsForJoinPoint(ProceedingJoinPoint pjp) {
    Signature sig = pjp.getSignature();
    Object target = pjp.getTarget();
    Class<?> type = target.getClass();
    List<Method> matches = new ArrayList<Method>();
    for (Method m : type.getDeclaredMethods()) {
        if (!m.getName().equals(sig.getName()))
            continue;
        // if (m.getModifiers() != sig.getModifiers())
        // continue;
        Object[] args = pjp.getArgs();
        Class<?>[] types = m.getParameterTypes();
        if (args.length != types.length)
            continue;
        boolean miss = false;
        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            Class<?> argType = types[i];
            if (argType.isPrimitive()) {
                if (argType.equals(Double.TYPE) && !arg.getClass().equals(Double.class))
                    miss = true;
            } else {
                if (arg != null && !argType.isInstance(arg))
                    miss = true;
            }
        }
        if (miss)
            continue;
        matches.add(m);
    }
    return matches;
}
Also used : Signature(org.aspectj.lang.Signature) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Aggregations

ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)77 Around (org.aspectj.lang.annotation.Around)32 MethodSignature (org.aspectj.lang.reflect.MethodSignature)16 Method (java.lang.reflect.Method)14 Test (org.junit.Test)14 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)10 SimpleDateFormat (java.text.SimpleDateFormat)8 ArrayList (java.util.ArrayList)8 JoinPoint (org.aspectj.lang.JoinPoint)8 FaseDTO (com.tomasio.projects.trainning.dto.FaseDTO)7 OrganizacaoDTO (com.tomasio.projects.trainning.dto.OrganizacaoDTO)7 PessoaDTO (com.tomasio.projects.trainning.dto.PessoaDTO)7 MockProceedingJoinPoint (org.finra.herd.core.MockProceedingJoinPoint)7 Annotation (java.lang.annotation.Annotation)5 CancelamentoMatriculaDTO (com.tomasio.projects.trainning.dto.CancelamentoMatriculaDTO)4 MatriculaDTO (com.tomasio.projects.trainning.dto.MatriculaDTO)4 Date (java.util.Date)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Action (org.apache.nifi.action.Action)4 MessageHeader (org.finra.herd.model.dto.MessageHeader)4