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