Search in sources :

Example 16 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project spring-cloud-sleuth by spring-cloud.

the class TraceAsyncAspectTest method setup.

@Before
public void setup() throws NoSuchMethodException {
    MethodSignature signature = Mockito.mock(MethodSignature.class);
    BDDMockito.given(signature.getName()).willReturn("fooBar");
    BDDMockito.given(signature.getMethod()).willReturn(TraceAsyncAspectTest.class.getMethod("setup"));
    BDDMockito.given(this.point.getSignature()).willReturn(signature);
    BDDMockito.given(this.point.getTarget()).willReturn("");
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Before(org.junit.Before)

Example 17 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project spring-cloud-sleuth by spring-cloud.

the class TraceAsyncAspect method getMethod.

private Method getMethod(ProceedingJoinPoint pjp, Object object) {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = signature.getMethod();
    return ReflectionUtils.findMethod(object.getClass(), method.getName(), method.getParameterTypes());
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method)

Example 18 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project herd by FINRAOS.

the class StopWatchAdvice method logMethodTime.

/**
 * Around advice that logs methods times for all service methods.
 *
 * @param pjp the proceeding join point.
 *
 * @return the return value of the method we are advising.
 * @throws Throwable if there were any problems executing the method.
 */
@Around("serviceMethods()")
public Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable {
    // Get the target class being called.
    Class<?> targetClass = pjp.getTarget().getClass();
    // Get the target method being called.
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSignature.getMethod();
    if (targetMethod.getDeclaringClass().isInterface()) {
        // Get the underlying implementation if we are given an interface.
        targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes());
    }
    // Only keep a stop watch if the class and method aren't suppressing logging and the log level is info.
    if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isInfoEnabled())) {
        // Start the stop watch.
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        // Proceed to the join point (i.e. call the method and let it return).
        Object returnValue = pjp.proceed();
        // Log the duration.
        long durationMilliseconds = stopWatch.getTime();
        LOGGER.info("javaMethod=\"{}.{}\" javaMethodDurationTimeInMilliseconds={} javaMethodDurationTimeFormatted=\"{}\"", targetClass.getName(), targetMethodSignature.getName(), durationMilliseconds, HerdDateUtils.formatDuration(durationMilliseconds));
        // Return the method return value.
        return returnValue;
    } else {
        // Invoke the method normally.
        return pjp.proceed();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) StopWatch(org.apache.commons.lang3.time.StopWatch) Around(org.aspectj.lang.annotation.Around)

Example 19 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project herd by FINRAOS.

the class PublishNotificationMessagesAdvice method publishNotificationMessages.

/**
 * Publishes all notification messages stored in the "in-memory" notification message queue.
 *
 * @param joinPoint the join point
 *
 * @return the return value of the method at the join point
 * @throws Throwable if any errors were encountered
 */
@Around("serviceMethods()")
public Object publishNotificationMessages(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    boolean publishNotificationMessages = method.isAnnotationPresent(PublishNotificationMessages.class);
    // Proceed to the join point (i.e. call the method and let it return).
    try {
        Object returnValue = joinPoint.proceed();
        if (publishNotificationMessages) {
            if (LOGGER.isDebugEnabled()) {
                // Get the target class being called.
                Class<?> targetClass = joinPoint.getTarget().getClass();
                LOGGER.debug("Method is initiating notification message publishing. javaMethod=\"{}.{}\" notificationMessageInMemoryQueueSize={}", targetClass.getName(), methodSignature.getName(), notificationMessageInMemoryQueue.size());
            }
            // Publish all notification messages stored in the "in-memory" notification message queue.
            while (!notificationMessageInMemoryQueue.isEmpty()) {
                // Get notification message from the "in-memory" queue.
                NotificationMessage notificationMessage = notificationMessageInMemoryQueue.remove();
                // Publish the message.
                try {
                    notificationMessagePublishingService.publishNotificationMessage(notificationMessage);
                } catch (Exception sqsException) {
                    // On error, add this notification message to the database queue.
                    try {
                        notificationMessagePublishingService.addNotificationMessageToDatabaseQueue(notificationMessage);
                    } catch (Exception dbException) {
                        // Log the error.
                        LOGGER.error("Failed to add notification message to the database queue. messageType=\"{}\" messageDestination=\"{}\" messageText={}", notificationMessage.getMessageType(), notificationMessage.getMessageDestination(), notificationMessage.getMessageText(), dbException);
                    }
                }
            }
        }
        return returnValue;
    } finally {
        // Removes all of the elements from the queue, since the thread might be reused from the thread pool.
        if (publishNotificationMessages) {
            notificationMessageInMemoryQueue.clear();
        }
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 20 with MethodSignature

use of org.aspectj.lang.reflect.MethodSignature in project herd by FINRAOS.

the class MethodLoggingAdvice method logMethodBeingInvoked.

/**
 * Around advice that logs methods being invoked for all DAO operations methods.
 *
 * @param pjp the proceeding join point.
 *
 * @return the return value of the method we are advising.
 * @throws Throwable if there were any problems executing the method.
 */
@Around("operationsMethods()")
public Object logMethodBeingInvoked(ProceedingJoinPoint pjp) throws Throwable {
    // Get the target class being called.
    Class<?> targetClass = pjp.getTarget().getClass();
    // Get the target method being called.
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    Method targetMethod = targetMethodSignature.getMethod();
    if (targetMethod.getDeclaringClass().isInterface()) {
        // Get the underlying implementation if we are given an interface.
        targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes());
    }
    // Only log the method if the class and method aren't suppressing logging and the log level is debug.
    if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isDebugEnabled())) {
        LOGGER.debug("javaMethod=\"{}.{}\"", targetClass.getName(), targetMethodSignature.getName());
    }
    // Proceed to the join point (i.e. call the method and let it return).
    return pjp.proceed();
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) 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