Example usage for org.aspectj.lang ProceedingJoinPoint getSignature

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

Introduction

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

Prototype

Signature getSignature();

Source Link

Document

getStaticPart().getSignature() returns the same object

Usage

From source file:org.finra.herd.service.advice.PublishJmsMessagesAdviceTest.java

License:Apache License

/**
 * Creates and returns a mocked join point of the method call.
 *
 * @param methodName the name of the method
 *
 * @return the mocked ProceedingJoinPoint
 *///from  w w w  .j  av a2 s.c  o m
private ProceedingJoinPoint getMockedProceedingJoinPoint(String methodName) throws Exception {
    ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = PublishJmsMessagesAdviceTest.class.getDeclaredMethod("mockMethod");
    when(joinPoint.getTarget()).thenReturn(new PublishJmsMessagesAdviceTest());
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(methodSignature.getMethod()).thenReturn(method);
    when(methodSignature.getName()).thenReturn(methodName);

    return joinPoint;
}

From source file:org.finra.herd.service.advice.PublishNotificationMessagesAdvice.java

License:Apache License

/**
 * Publishes all notification messages stored in the "in-memory" notification message queue.
 *
 * @param joinPoint the join point/*from  w w w . j  a  v a 2s  .c  o  m*/
 *
 * @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();
        }
    }
}

From source file:org.finra.herd.service.advice.PublishNotificationMessagesAdviceTest.java

License:Apache License

/**
 * Creates and returns a mocked join point of the method call.
 *
 * @param methodName the name of the method
 *
 * @return the mocked ProceedingJoinPoint
 *///from  www  . j  a  v a  2  s. c o  m
private ProceedingJoinPoint getMockedProceedingJoinPoint(String methodName) throws Exception {
    ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = PublishNotificationMessagesAdviceTest.class.getDeclaredMethod("mockMethod");
    when(joinPoint.getTarget()).thenReturn(new PublishNotificationMessagesAdviceTest());
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(methodSignature.getMethod()).thenReturn(method);
    when(methodSignature.getName()).thenReturn(methodName);

    return joinPoint;
}

From source file:org.finra.herd.service.advice.StopWatchAdvice.java

License:Apache License

/**
 * 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.
 *//* w w w  .  j av a2  s .  c  om*/
@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();
    }
}

From source file:org.finra.herd.service.advice.StopWatchAdviceTest.java

License:Apache License

/**
 * Creates and returns a mocked join point of the method call.
 *
 * @param targetObject the target object
 * @param method the method/*from  w w w  . jav  a2  s.c o  m*/
 *
 * @return the mocked ProceedingJoinPoint
 */
private ProceedingJoinPoint getMockedProceedingJoinPoint(Object targetObject, Method method) throws Exception {
    ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    when(joinPoint.getTarget()).thenReturn(targetObject);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(methodSignature.getMethod()).thenReturn(method);
    when(methodSignature.getName()).thenReturn(method.getName());

    return joinPoint;
}

From source file:org.finra.herd.service.helper.CheckAllowedMethodAdvice.java

License:Apache License

/**
 * Checks whether the requested operation is permitted.
 *
 * @param pjp the join point./*  www. j  av  a 2 s .c  o  m*/
 *
 * @return the return value of the method at the join point.
 * @throws Throwable if any errors were encountered.
 */
@SuppressWarnings("rawtypes")
public Object checkNotAllowedMethods(ProceedingJoinPoint pjp) throws Throwable {
    // Get the method name being invoked.
    Class targetClass = pjp.getTarget().getClass();
    MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature();
    String methodName = targetClass.getName() + "." + targetMethodSignature.getName();

    configurationDaoHelper.checkNotAllowedMethod(methodName);
    return pjp.proceed();
}

From source file:org.fishwife.jrugged.aspects.CircuitBreakerAspect.java

License:Apache License

/** Runs a method call through the configured
 * {@link org.fishwife.jrugged.CircuitBreaker}.
 * @param pjp a {@link ProceedingJoinPoint} representing an annotated
 * method call./*w  ww .j av a 2  s  .  c  om*/
 * @param circuitBreakerAnnotation the {@link org.fishwife.jrugged.CircuitBreaker} annotation
 * that wrapped the method.
 * @throws Throwable if the method invocation itself or the wrapping
 * {@link org.fishwife.jrugged.CircuitBreaker} throws one during execution.
 * @return The return value from the method call.
 */
@Around("@annotation(circuitBreakerAnnotation)")
public Object monitor(final ProceedingJoinPoint pjp, CircuitBreaker circuitBreakerAnnotation) throws Throwable {
    final String name = circuitBreakerAnnotation.name();

    org.fishwife.jrugged.CircuitBreaker circuitBreaker = circuitBreakerFactory.findCircuitBreaker(name);

    if (circuitBreaker == null) {
        DefaultFailureInterpreter dfi = new DefaultFailureInterpreter(circuitBreakerAnnotation.ignore(),
                circuitBreakerAnnotation.limit(), circuitBreakerAnnotation.windowMillis());

        CircuitBreakerConfig config = new CircuitBreakerConfig(circuitBreakerAnnotation.resetMillis(), dfi);

        circuitBreaker = circuitBreakerFactory.createCircuitBreaker(name, config);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(
                "Have @CircuitBreaker method with breaker name {}, "
                        + "wrapping call on method {} of target object {} with status {}",
                new Object[] { name, pjp.getSignature().getName(), pjp.getTarget(),
                        circuitBreaker.getStatus() });
    }

    return circuitBreaker.invoke(new Callable<Object>() {
        public Object call() throws Exception {
            try {
                return pjp.proceed();
            } catch (Throwable e) {
                if (e instanceof Exception) {
                    throw (Exception) e;
                } else if (e instanceof Error) {
                    throw (Error) e;
                } else {
                    throw new RuntimeException(e);
                }
            }
        }
    });
}

From source file:org.fishwife.jrugged.aspects.PerformanceMonitorAspect.java

License:Apache License

/**
 * Wraps a method annotated with the {@link org.fishwife.jrugged.PerformanceMonitor} annotation
 * with a {@link org.fishwife.jrugged.PerformanceMonitor}.
 * /*from   w  ww  .j a v  a 2 s  . c o  m*/
 * @param pjp Represents the method that is being executed.
 * @param performanceMonitorAnnotation The PerformanceMonitor annotation
 * associated with the method being execute.
 * @return Value returned by the method that is being wrapped.
 * @throws Throwable Whatever the wrapped method throws will be thrown by
 * this method.
 */
@Around("@annotation(performanceMonitorAnnotation)")
public Object monitor(final ProceedingJoinPoint pjp, PerformanceMonitor performanceMonitorAnnotation)
        throws Throwable {
    String monitorName = performanceMonitorAnnotation.value();

    if (logger.isDebugEnabled()) {
        logger.debug(
                "Have @PerformanceMonitor method with monitor name {}, "
                        + "wrapping call on method {} of target object {}",
                new Object[] { monitorName, pjp.getSignature().getName(), pjp.getTarget() });
    }

    org.fishwife.jrugged.PerformanceMonitor performanceMonitor = performanceMonitorFactory
            .findPerformanceMonitor(monitorName);

    if (performanceMonitor == null) {
        performanceMonitor = performanceMonitorFactory.createPerformanceMonitor(monitorName);
    }

    return performanceMonitor.invoke(new Callable<Object>() {
        public Object call() throws Exception {
            Object retval;
            try {
                retval = pjp.proceed();
            } catch (Throwable e) {
                if (e instanceof Exception) {
                    throw (Exception) e;
                } else {
                    throw (Error) e;
                }
            }
            return retval;
        }
    });
}

From source file:org.fishwife.jrugged.aspects.RetryableAspect.java

License:Apache License

/**
 * Runs a method call with retries.//from   w  w w .  j  a v a  2  s.  c om
 * @param pjp a {@link ProceedingJoinPoint} representing an annotated
 *            method call.
 * @param retryableAnnotation the {@link org.fishwife.jrugged.aspects.Retryable}
 *                            annotation that wrapped the method.
 * @throws Throwable if the method invocation itself throws one during execution.
 * @return The return value from the method call.
 */
@Around("@annotation(retryableAnnotation)")
public Object call(final ProceedingJoinPoint pjp, Retryable retryableAnnotation) throws Throwable {
    final int maxTries = retryableAnnotation.maxTries();
    final int retryDelayMillies = retryableAnnotation.retryDelayMillis();
    final Class<? extends Throwable>[] retryOn = retryableAnnotation.retryOn();
    final boolean doubleDelay = retryableAnnotation.doubleDelay();
    final boolean throwCauseException = retryableAnnotation.throwCauseException();

    if (logger.isDebugEnabled()) {
        logger.debug("Have @Retryable method wrapping call on method {} of target object {}",
                new Object[] { pjp.getSignature().getName(), pjp.getTarget() });
    }

    ServiceRetrier serviceRetrier = new ServiceRetrier(retryDelayMillies, maxTries, doubleDelay,
            throwCauseException, retryOn);

    return serviceRetrier.invoke(new Callable<Object>() {
        public Object call() throws Exception {
            try {
                return pjp.proceed();
            } catch (Exception e) {
                throw e;
            } catch (Error e) {
                throw e;
            } catch (Throwable t) {
                throw new RuntimeException(t);
            }
        }
    });
}

From source file:org.fishwife.jrugged.aspects.TestCircuitBreakerAspect.java

License:Apache License

private static ProceedingJoinPoint createPjpMock(Signature mockSignature, int times) {
    ProceedingJoinPoint mockPjp = createMock(ProceedingJoinPoint.class);
    // XXX: the following two interactions are for logging, so they may happen
    //      0 or n times, pending logging configuration
    expect(mockPjp.getTarget()).andReturn("Target").times(0, times);
    expect(mockPjp.getSignature()).andReturn(mockSignature).times(0, times);
    return mockPjp;
}