All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.monitor.metrics.MetricsAdviceBase Maven / Gradle / Ivy

package org.zodiac.monitor.metrics;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Map;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.Logger;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.sdk.toolkit.util.ClassUtil;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;

public abstract class MetricsAdviceBase implements MethodInterceptor {

    protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());

    private final Map methodsCache = CollUtil.concurrentMap(32);

    private volatile Object metricNameLock = new Object();

    protected MetricsAdviceBase() {
    }

    /**
     * Production unique name based on method name.
     * 
     * @param invoc {@link MethodInvocation}
     * @return name
     */
    protected String getMetricName(MethodInvocation invoc) {
        String metricName = methodsCache.get(invoc.getMethod());
        if (null == metricName) {
            synchronized (metricNameLock) {
                if (null == metricName) {
                    Method m = invoc.getMethod();
                    StringBuffer sign = new StringBuffer(ClassUtil.getAbbreviatedClassName(invoc.getThis().getClass().getName()));
                    sign.append(".");
                    sign.append(m.getName());
                    sign.append("(");
                    Parameter[] params = m.getParameters();
                    if (params != null) {
                        for (Parameter p : params) {
                            sign.append(ClassUtil.getAbbreviatedParameterType(p.getType().getSimpleName()));
                            sign.append(" ");
                            sign.append(p.getName());
                            sign.append(",");
                        }
                        if (sign.length() > 1) {
                            sign.delete(sign.length() - 1, sign.length());
                        }
                        sign.append(")");
                    }
                    methodsCache.put(m, (metricName = sign.toString()));
                }
            }
        }
        return metricName;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy