org.opentestsystem.shared.mna.client.aop.MnaClientLoggingAdvice.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.shared.mna.client.aop.MnaClientLoggingAdvice.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.shared.mna.client.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.opentestsystem.shared.mna.client.aop.LoggingAdvice;
import org.opentestsystem.shared.mna.client.service.MetricClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Implementation of the around advice for performance logging
 */
public class MnaClientLoggingAdvice implements LoggingAdvice {

    /**
     * Hook into a MetricClient to send out the log as a metric
     */
    @Autowired
    private MetricClient mnaMetricClient;

    private static final Logger LOGGER = LoggerFactory.getLogger(MnaClientLoggingAdvice.class);

    /**
     * Simply generates a readable string to identify the join point
     * 
     * @param pjp
     * @return
     */
    protected String getJoinPointLogString(final ProceedingJoinPoint pjp) {
        return pjp.toShortString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object timeMethod(final ProceedingJoinPoint inPjp, final Logger inPerformanceLogger) {
        Object output = null;

        try {

            // only do this if debug is enabled
            if (inPerformanceLogger != null && inPerformanceLogger.isDebugEnabled()) {
                long start = System.currentTimeMillis();
                String jpStr = getJoinPointLogString(inPjp);
                inPerformanceLogger.debug("Begin " + jpStr);
                output = inPjp.proceed();
                long stop = System.currentTimeMillis();
                long elapsedTime = stop - start;
                // log out to the appender that is configured
                inPerformanceLogger.debug("End   " + jpStr + " Elapsed Time: " + elapsedTime + " ms");
                if (mnaMetricClient != null) {
                    String sig = inPjp.getSignature().getDeclaringType().getSimpleName();
                    // allow string concatenation, this isn't a big deal
                    sig = sig + "." + inPjp.getSignature().getName(); // NOPMD

                    // send the performance number to Monitoring and Alerting as a metric
                    mnaMetricClient.sendPerformanceMetricToMna(sig, elapsedTime);
                }
            } else {
                output = inPjp.proceed();
            }
            // allow catch of throwable because the AOP proceed call throws Throwable, also allow rethrowing as a generic
            // RuntimeException
        } catch (Throwable thr) { // NOPMD
            if (thr instanceof RuntimeException) { // NOPMD
                LOGGER.error("Caught RuntimeException in Around Advice, rethrowing", thr);
                throw (RuntimeException) thr;
            } else {
                LOGGER.error("Caught a non-RuntimeException error in AroundAdvice, rethrowing as RuntimeException",
                        thr);
                throw new RuntimeException(
                        "Caught a non-RuntimeException error in AroundAdvice, rethrowing as RuntimeException", thr); // NOPMD
            }
        }
        return output;
    }

}