package org.automon.monitors;

import com.jamonapi.MonitorFactory;
import org.aspectj.lang.JoinPoint;

import org.automon.utils.*;
import java.util.Map;

/**
 * Created by stevesouza on 3/2/15.
 */
public abstract class OpenMonBase<T> implements OpenMon<T> {

    private  Map<Throwable, Expirable>  exceptionsMap = Utils.createExceptionMap();

    /**
     * @param jp The pointcut from the @Around advice that was intercepted.
     * @return A label suitable for a monitoring/timer label.  It is a convenience method and need not be
     * used to create the monitor
     */
    protected String getLabel(JoinPoint jp) {
        return jp.getStaticPart().toString();
    }

    /**
     * @param throwable The exception that was thrown
     * @return A label suitable for a monitoring label representing the thrown exception.  It is a convenience method and need not be
     * used to create the monitor
     */
    protected String getLabel(Throwable throwable) {
        return throwable.getClass().getName();
    }

    /**
     * Note the default implementation simply calls {@link #stop(T)} and doesn't do anything with the {@link java.lang.Throwable} argument
     *
     * @param context The object returned by 'start' is passed in.  Typically this would be a timer and should be stopped.
     * @param throwable This argument is ignored in the default implementation.
     */
    @Override
    public void stop(T context, Throwable throwable) {
        put(throwable);
        stop(context);
    }

    /**
     * Override {@link #exceptionImp(org.aspectj.lang.JoinPoint, Throwable)} instead of this method unless the default behavior
     * is not desired
     *
     * @param jp The {@link org.aspectj.lang.JoinPoint} associated with where the exception was thrown.
     * @param throwable The thrown exception
     */
    @Override
    public void exception(JoinPoint jp, Throwable throwable) {
        exceptionImp(jp, throwable);
        put(throwable);
    }


    public void exceptionImp(JoinPoint jp, Throwable throwable) {

    }

    protected void put(Throwable throwable) {
        // note 'get' is used instead of 'containsKey' as we want to update the LRU information for each access.
        if (exceptionsMap.get(throwable)==null) {
            exceptionsMap.put(throwable, new TimeExpirable());
        }
    }

}

@@ -1969,16 +1969,1642 @@
 %0A    %7D%0A%0A
+    /**%0A     * put in this class?%0A     *%0A     * should i change the map order to last is first inserted.  keeping in miind that anything over 2 minutes can go.%0A     * The advantage is that i could abort the loop as soon as i hit the first one that didn't match?%0A     *%0A     *%0A     * When iterating over the map the most recently accessed entry is returned first and the least recently accessed element is returned last.%0A     *%0A   private void trackException(Throwable rootCause, Method method, String sqlMessage) %7B%0A     String detailStackTrace = Misc.getExceptionTrace(rootCause);%0A     MonitorFactory.add(new MonKeyImp(MonitorFactory.EXCEPTIONS_LABEL, detailStackTrace, %22Exception%22), 1); // counts total exceptions from jamon%0A     MonitorFactory.add(new MonKeyImp(%22MonProxy-Exception: InvocationTargetException%22, detailStackTrace, %22Exception%22), 1); //counts total exceptions for MonProxy%0A     MonitorFactory.add(new MonKeyImp(%22MonProxy-Exception: Root cause exception=%22+rootCause.getClass().getName()+sqlMessage,%0A     detailStackTrace, %22Exception%22), 1); // Message for the exception%0A     MonitorFactory.add(new MonKeyImp(labelerInt.getExceptionLabel(method), detailStackTrace,%22Exception%22), 1); // Exception and method that threw it.%0A     %7D%0A%0A     // Add special info if it is a SQLException%0A     if (rootCause instanceof SQLException) %7B%0A       SQLException sqlException = (SQLException) rootCause;%0A       sqlMessage = %22,ErrorCode=%22 + sqlException.getErrorCode()+ %22,SQLState=%22 + sqlException.getSQLState();%0A     %7D%0A%0A     // Add jamon entries for Exceptions%0A     trackException(rootCause, method, sqlMessage);%0A%0A     */%0A%0A    //????
 %0A    pub
@@ -3841,16 +3841,17 @@
     if (
+!
 exceptio
@@ -3856,19 +3856,27 @@
 ionsMap.
-get
+containsKey
 (throwab
@@ -3882,14 +3882,8 @@
 ble)
-==null
 ) %7B%0A

refactor - changed back to using containsKey which is more expressive