1
18 package net.bull.javamelody;
19
20 import java.io.Serializable;
21
22 import org.aopalliance.intercept.MethodInterceptor;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.springframework.aop.support.AopUtils;
25
26 import net.bull.javamelody.internal.common.LOG;
27 import net.bull.javamelody.internal.common.Parameters;
28 import net.bull.javamelody.internal.model.Counter;
29
30
36 public class MonitoringSpringInterceptor implements MethodInterceptor, Serializable {
37 private static final long serialVersionUID = -6594338383847482623L;
38 private static final Counter SPRING_COUNTER = MonitoringProxy.getSpringCounter();
39 private static final boolean COUNTER_HIDDEN = Parameters
40 .isCounterHidden(SPRING_COUNTER.getName());
41 private static final boolean DISABLED = Parameter.DISABLED.getValueAsBoolean();
42
43
46 public MonitoringSpringInterceptor() {
47 super();
48
49
50 SPRING_COUNTER.setDisplayed(!COUNTER_HIDDEN);
51
52
53 SPRING_COUNTER.setUsed(true);
54 LOG.debug("spring interceptor initialized");
55 }
56
57
64 @Override
65 public Object invoke(MethodInvocation invocation) throws Throwable {
66
67 if (DISABLED || !SPRING_COUNTER.isDisplayed()) {
68 return invocation.proceed();
69 }
70
71 final String requestName = getRequestName(invocation);
72
73 boolean systemError = false;
74 try {
75 SPRING_COUNTER.bindContextIncludingCpu(requestName);
76 return invocation.proceed();
77 } catch (final Error e) {
78
79
80 systemError = true;
81 throw e;
82 } finally {
83
84 SPRING_COUNTER.addRequestForCurrentContext(systemError);
85 }
86 }
87
88
94 protected String getRequestName(MethodInvocation invocation) {
95 final String classPart = getClassPart(invocation);
96 final String methodPart = getMethodPart(invocation);
97 return classPart + '.' + methodPart;
98 }
99
100 private static String getClassPart(MethodInvocation invocation) {
101
102
103
104 final Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
105 final MonitoredWithSpring classAnnotation = targetClass
106 .getAnnotation(MonitoredWithSpring.class);
107 if (classAnnotation == null || classAnnotation.name() == null
108 || classAnnotation.name().isEmpty()) {
109 final Class<?> declaringClass = invocation.getMethod().getDeclaringClass();
110 final MonitoredWithSpring declaringClassAnnotation = declaringClass
111 .getAnnotation(MonitoredWithSpring.class);
112 if (declaringClassAnnotation == null || declaringClassAnnotation.name() == null
113 || declaringClassAnnotation.name().isEmpty()) {
114 return targetClass.getSimpleName();
115 }
116 return declaringClassAnnotation.name();
117 }
118 return classAnnotation.name();
119 }
120
121 private static String getMethodPart(MethodInvocation invocation) {
122 final MonitoredWithSpring methodAnnotation = invocation.getMethod()
123 .getAnnotation(MonitoredWithSpring.class);
124 if (methodAnnotation == null || methodAnnotation.name() == null
125 || methodAnnotation.name().isEmpty()) {
126 return invocation.getMethod().getName();
127 }
128 return methodAnnotation.name();
129 }
130 }
131