001package org.javasimon.javaee;
002
003import java.lang.reflect.Method;
004
005import javax.interceptor.InvocationContext;
006
007import org.javasimon.Manager;
008import org.javasimon.source.AbstractMethodStopwatchSource;
009
010/**
011 * Provide stopwatch source for EJB and CDI invocation context.
012 * Used by {@link SimonInterceptor} as default stopwatch source.
013 * Can be overridden to customize monitored EJB methods and their
014 * related Simon name.
015 *
016 * @author gquintana
017 */
018public class MethodStopwatchSource extends AbstractMethodStopwatchSource<InvocationContext> {
019
020        /** Default prefix for Simon names. */
021        public static final String DEFAULT_PREFIX = "org.javasimon.business";
022
023        /** Simon name prefix - can be overridden in subclasses. */
024        protected String prefix = DEFAULT_PREFIX;
025
026        public MethodStopwatchSource(Manager manager) {
027                super(manager);
028        }
029
030        public String getPrefix() {
031                return prefix;
032        }
033
034        public void setPrefix(String prefix) {
035                this.prefix = prefix;
036        }
037
038        @Override
039        protected final Class<?> getTargetClass(InvocationContext context) {
040                return context.getTarget().getClass();
041        }
042
043        @Override
044        protected final Method getTargetMethod(InvocationContext context) {
045                return context.getMethod();
046        }
047
048        /**
049         * Returns Simon name for the specified Invocation context.
050         * By default it contains the prefix + method name.
051         * This method can be overridden.
052         *
053         * @param context Invocation context
054         * @return fully qualified name of the Simon
055         * @since 3.1
056         */
057        protected String getMonitorName(InvocationContext context) {
058                String className = context.getMethod().getDeclaringClass().getSimpleName();
059                String methodName = context.getMethod().getName();
060                return prefix + Manager.HIERARCHY_DELIMITER + className + Manager.HIERARCHY_DELIMITER + methodName;
061        }
062}