Example usage for org.aspectj.lang ProceedingJoinPoint getStaticPart

List of usage examples for org.aspectj.lang ProceedingJoinPoint getStaticPart

Introduction

In this page you can find the example usage for org.aspectj.lang ProceedingJoinPoint getStaticPart.

Prototype

StaticPart getStaticPart();

Source Link

Usage

From source file:net.greencoding.thysdrus.circuitbreaker.aspect.CircuitBreakerAspect.java

License:Apache License

@Around("execution(@net.greencoding.thysdrus.circuitbreaker.annotation.MonitoredByCircuitBreaker * * (..))")
public Object breakCircuit(final ProceedingJoinPoint pjp) throws Throwable {
    Object returnObject = null;//from w  w  w  .j a v a 2s  .c  o  m
    final String method = pjp.getSignature().toLongString();
    CircuitBreakerStatus status = null;
    try {
        final MethodSignature sig = (MethodSignature) pjp.getStaticPart().getSignature();
        final MonitoredByCircuitBreaker cbAnnotation = sig.getMethod()
                .getAnnotation(MonitoredByCircuitBreaker.class);
        registry.registerMehtodIfnecessary(method, cbAnnotation);
        status = registry.getStatusWithHalfOpenExclusiveLockTry(method);
        if (status.equals(CircuitBreakerStatus.OPEN)) {
            logger.info("CIRCUIT STATUS: OPEN. Method {} can not be executed. try later!", method);
            if (cbAnnotation.isSilientMode()) {
                return null;
            } else {
                throw new OpenCircuitException();
            }
        } else if (status.equals(CircuitBreakerStatus.HALF_OPEN)) {
            logger.info(
                    "CIRCUIT STATUS: HALF_OPEN. Another thread has the exclusive lock for half open. Method {} can not be executed.",
                    method);
            if (cbAnnotation.isSilientMode()) {
                return null;
            } else {
                throw new OpenCircuitException();
            }
        } else if (status.equals(CircuitBreakerStatus.CLOSED)) {
            logger.info("CIRCUIT STATUS: CLOSED. execute method {}", method);
            returnObject = proceed(pjp);
        } else if (status.equals(CircuitBreakerStatus.HALF_OPEN_EXCLUSIVE)) {
            logger.info(
                    "CIRCUIT STATUS: HALF_OPEN_EXCLUSIVE. This thread win the exclusive lock for the half open call. execute method: {}",
                    method);
            returnObject = proceed(pjp);
            logger.info(
                    "CIRCUIT STATUS: HALF_OPEN_EXCLUSIVE. method execution was successfull. now close circuit for method {}",
                    method);
            registry.closeAndUnlock(method);
        }

    } catch (CircuitBreakerMethodExecutionException e) {
        Throwable throwable = e.getCause();
        for (Class<? extends Throwable> clazz : registry.getfailureIndications(method)) {
            if (clazz.isAssignableFrom(throwable.getClass())) {
                // detected a failure 
                logger.info("dectected failure. failure indication: {} \nException:", clazz.getCanonicalName(),
                        throwable);
                if (status.equals(CircuitBreakerStatus.CLOSED)
                        && registry.sameClosedCycleInLocalAndGlobaleContext(method)) {
                    logger.info("Valid failure: method call and failure are in the same CLOSED cycle.");
                    registry.addFailureAndOpenCircuitIfThresholdAchived(method);
                } else if (status.equals(CircuitBreakerStatus.HALF_OPEN_EXCLUSIVE)) {
                    registry.keepOpenAndUnlock(method);
                }
                throw throwable;
            }
        }
        // thrown exception is not a failureIndication
        if (status.equals(CircuitBreakerStatus.HALF_OPEN_EXCLUSIVE)) {
            logger.info(
                    "CIRCUIT STATUS: HALF_OPEN_EXCLUSIVE. method execution was successfull. now close circuit for method {}",
                    method);
            registry.closeAndUnlock(method);
        }
        // throw the original method execution exception upper to the method invoker
        throw throwable;
    } finally {
        registry.cleanUp(method);
    }
    return returnObject;
}

From source file:org.automon.aspects.AutomonSpringAspect.java

/**
* _monitor() advice - Wraps the given pointcut and calls the appropriate {@link org.automon.implementations.OpenMon} method
* at the beginning and end of the method call.
*
* @param pjp//  w w w  .  j av  a  2  s . c om
* @return The advised methods value or void.
* @throws Throwable If the method throws a {@link java.lang.Throwable} the advice will rethrow it.
*/

public Object monitor(ProceedingJoinPoint pjp) throws Throwable {
    // Note: context is typically a Timer/Monitor object returned by the monitoring implementation (Jamon, JavaSimon, Metrics,...)
    // though to this advice it is simply an object and the advice doesn't care what the intent of the context/object is.
    Object context = getOpenMon().start(pjp.getStaticPart());
    try {
        Object retVal = pjp.proceed();
        getOpenMon().stop(context);
        return retVal;
    } catch (Throwable throwable) {
        getOpenMon().stop(context, throwable);
        throw throwable;
    }
}

From source file:org.ednovo.gooru.security.MethodAuthorizationAspect.java

License:Open Source License

public boolean hasPartyAuthorization(AuthorizeOperations authorizeOperations, ProceedingJoinPoint pjp,
        RequestMapping requestMapping) {

    GooruAuthenticationToken authenticationContext = (GooruAuthenticationToken) SecurityContextHolder
            .getContext().getAuthentication();

    if (authenticationContext != null) {
        boolean partyOperationCheck = false;
        String apiCallerPermission = null;
        List<String> partyPermissions = authenticationContext.getUserCredential().getPartyOperations();
        List<String> accessblePermissions = Arrays.asList(authorizeOperations.partyOperations());
        if (partyPermissions != null && accessblePermissions != null) {
            for (String partyPermission : partyPermissions) {
                if (accessblePermissions.contains(partyPermission)) {
                    partyOperationCheck = true;
                    apiCallerPermission = partyPermission;
                }//from w  w  w. ja v  a  2  s  .co  m
            }
        }
        final Signature signature = pjp.getStaticPart().getSignature();
        if (signature instanceof MethodSignature) {
            final MethodSignature ms = (MethodSignature) signature;
            String[] paramNames = parameterNameDiscoverer.getParameterNames(ms.getMethod());
            Object[] paramValues = pjp.getArgs();
            String partyUidName = authorizeOperations.partyUId();
            String partyUid = "";
            if (paramNames != null && paramValues != null) {
                for (int paramNameIndex = 0; paramNameIndex < paramNames.length; paramNameIndex++) {
                    String paramName = paramNames[paramNameIndex];
                    if (paramName instanceof String) {
                        if (paramName.equals(partyUidName)) {
                            if (paramValues[paramNameIndex] != null) {
                                partyUid = (String) paramValues[paramNameIndex];
                            }
                        }
                    }
                }
            }
            if (!partyUid.isEmpty()) {
                String[] permittedParties = authenticationContext.getUserCredential().getPartyPermits();
                List<String> permittedPartiesList = Arrays.asList(permittedParties);
                String apiCallerOrgUid = authenticationContext.getUserCredential().getOrganizationUid();
                String userUid = authenticationContext.getUserCredential().getUserUid();
                User user = userService.findByGooruId(partyUid);
                User apiCaller = userService.findByGooruId(userUid);
                RequestMethod[] requestMethods = requestMapping.method();

                if (partyUid.equals(userUid)) {
                    for (RequestMethod requestMethod : requestMethods) {
                        if (requestMethod.equals(RequestMethod.DELETE)) {
                            return false;
                        }
                    }
                    return true;
                } else if (user != null && partyOperationCheck && (permittedPartiesList.contains(partyUid)
                        || permittedPartiesList.contains(user.getOrganization().getPartyUid()))) {
                    if (user.getOrganization().getPartyUid().equals(apiCallerOrgUid)) {
                        if (apiCallerPermission.equalsIgnoreCase(GooruOperationConstants.GROUP_ADMIN)
                                && user.getUserGroup().equals(apiCaller.getUserGroup())) {
                            return true;
                        } else if (apiCallerPermission.equalsIgnoreCase(GooruOperationConstants.ORG_ADMIN)) {
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

From source file:org.escidoc.core.aspects.TraceInterceptor.java

License:Open Source License

@Around("execution(public * de.escidoc.core..*.* (..))" + " && !within(org.escidoc.core.aspects..*)"
        + " && !within(de.escidoc.core.common.util.aop..*)" + " && if(" + "false" + ')')
// enable this aspect only if you need to trace
public Object traceMethod(final ProceedingJoinPoint joinPoint) throws Throwable {
    if (LOGGER.isDebugEnabled()) {
        final StaticPart staticPart = joinPoint.getStaticPart();
        final Signature signature = staticPart.getSignature();
        final String depthString = getDepthString();
        try {//from   w  w  w  . ja  v a  2s  .  c  o  m
            LOGGER.debug(createMessage(true, depthString, signature));
            final Object returnValue = joinPoint.proceed();
            LOGGER.debug(createMessage(false, depthString, signature));
            return returnValue;
        } catch (final Exception e) {
            LOGGER.debug(createExceptionMessage(depthString, e));
            throw e;
        }
    }
    return joinPoint.proceed();
}

From source file:org.mifos.rest.approval.aop.AspectJRESTApprovalInterceptor.java

License:Open Source License

@Around("restMethods() && requestMapping() && excludeAPI() && exludeRestfulServices()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    Signature signature = pjp.getStaticPart().getSignature();
    LOG.debug(this.getClass().getSimpleName() + " staring");

    // FIXME : somehow autowiring is not working
    if (approvalService == null) {
        approvalService = ApplicationContextProvider.getBean(ApprovalService.class);
    }//from  www .  j av  a2  s.com
    if (configurationServiceFacade == null) {
        configurationServiceFacade = ApplicationContextProvider.getBean(ConfigurationServiceFacade.class);
    }
    if (parameterNameDiscoverer == null) {
        parameterNameDiscoverer = ApplicationContextProvider.getBean(ParameterNameDiscoverer.class);
    }

    if (!RESTConfigKey.isApprovalRequired(configurationServiceFacade)) {
        LOG.debug(pjp.getSignature() + " skip approval");
        return pjp.proceed();
    }

    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method m = ms.getMethod();
        RequestMapping mapping = m.getAnnotation(RequestMapping.class);

        if (isReadOnly(mapping)) {
            LOG.debug(m.getName() + " is read only, hence returning control");
            return pjp.proceed();
        }

        Class<?> methodClassType = m.getDeclaringClass();

        if (!methodClassType.getSimpleName().endsWith("RESTController")) {
            LOG.debug(m.getName() + " is not from REST controller, hence returning control");
            return pjp.proceed();
        }

        Object[] argValues = pjp.getArgs();

        Class<?>[] argTypes = m.getParameterTypes();
        String methodName = m.getName();
        String[] names = parameterNameDiscoverer.getParameterNames(m);

        MethodArgHolder args = new MethodArgHolder(argTypes, argValues, names);
        ApprovalMethod method = new ApprovalMethod(methodName, methodClassType, args);
        approvalService.create(method);
    }
    return pjp.proceed();
}

From source file:org.slc.sli.ingestion.aspect.StageTrackingAspect.java

License:Apache License

private Object proceedAndTrackCall(ProceedingJoinPoint pjp) throws Throwable {

    long start = System.currentTimeMillis();
    Object result = pjp.proceed();
    long elapsed = System.currentTimeMillis() - start;

    if (isEnabled()) {
        // class name (sans package) # method name. e.g. "IdNormalizer#resolveInternalIds"
        String statsKey = pjp.getStaticPart().getSignature().getDeclaringType().getSimpleName() + "#"
                + pjp.getStaticPart().getSignature().getName();

        trackCallStatistics(statsKey, elapsed);
    }/*w w  w  . j av a  2s. c  om*/

    return result;
}

From source file:uk.gov.ofwat.fountain.aspect.audit.AuditAdvice.java

License:Open Source License

private HashMap<String, String> getParams(ProceedingJoinPoint pjp) {
    HashMap<String, String> params = new HashMap<String, String>();
    Object[] args = pjp.getArgs();
    Signature signature = pjp.getStaticPart().getSignature();
    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method method = ms.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();

        String[] parameterNames = ms.getParameterNames();
        for (int i = 0; i < args.length; i++) {
            String argsString = "";
            if (args[i] != null) {
                argsString = args[i].toString();
            }//from   w w  w.ja  v  a2  s  . c  o  m
            params.put(parameterNames[i], argsString);
        }
    }
    return params;
}

From source file:uk.gov.ofwat.fountain.aspect.audit.AuditAdvice.java

License:Open Source License

private HashMap<String, Object> getContent(ProceedingJoinPoint pjp) {
    HashMap<String, Object> content = new HashMap<String, Object>();
    Object[] args = pjp.getArgs();
    Signature signature = pjp.getStaticPart().getSignature();
    if (signature instanceof MethodSignature) {
        MethodSignature ms = (MethodSignature) signature;
        Method method = ms.getMethod();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        String[] parameterNames = ms.getParameterNames();
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            if (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput.class == parameterTypes[i]
                    || javax.ws.rs.core.SecurityContext.class == parameterTypes[i]
                    || javax.ws.rs.core.UriInfo.class == parameterTypes[i]
                    || javax.servlet.http.HttpServletRequest.class == parameterTypes[i]) {
                continue;
            }/*from  w w  w.j  a  v  a  2 s .  com*/
            content.put(parameterNames[i], args[i]);
        }
    }
    return content;
}