1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.tests.util.metadata.annotations;
19
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Method;
22 import java.util.Collection;
23
24 import org.aopalliance.intercept.MethodInterceptor;
25 import org.aopalliance.intercept.MethodInvocation;
26 import org.springframework.aop.support.AopUtils;
27 import org.springframework.util.Assert;
28
29 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
30 import ch.elca.el4j.util.metadata.GenericMetaDataSource;
31 import ch.elca.el4j.util.metadata.MetaDataSourceAware;
32
33
34
35
36
37
38
39
40 public class ExampleInterceptor
41 implements MethodInterceptor, MetaDataSourceAware {
42
43
44
45
46 private GenericMetaDataSource m_metaDataSource;
47
48
49
50
51 @SuppressWarnings("unchecked")
52 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
53 Method invokedMethod = methodInvocation.getMethod();
54 Class targetClass = null;
55 if (!AopUtils.isAopProxy(methodInvocation.getThis())) {
56 targetClass = methodInvocation.getThis().getClass();
57 }
58
59 Collection<Annotation> c = m_metaDataSource.getMetaData(
60 invokedMethod, targetClass);
61
62 Assert.isTrue(c != null && !c.isEmpty());
63 Annotation annotation = c.iterator().next();
64 int factor = -1;
65 if (annotation instanceof ExampleAnnotationOne) {
66 factor = ((ExampleAnnotationOne) annotation).factor();
67 } else if (annotation instanceof ExampleAnnotationTwo) {
68 ExampleAnnotationTwo eat = (ExampleAnnotationTwo) annotation;
69 factor = eat.factor() * ExampleAnnotationTwo.CONSTANT_FACTOR;
70 } else {
71 CoreNotificationHelper.notifyMisconfiguration(
72 "There is no annotated annotation of type "
73 + "ExampleAnnotation declared at method "
74 + methodInvocation.getMethod());
75 }
76
77 Object[] param = methodInvocation.getArguments();
78 param[0] = Integer.valueOf(factor);
79
80
81
82
83 return methodInvocation.proceed();
84 }
85
86
87
88
89 public void setMetaDataSource(GenericMetaDataSource metaDataSource) {
90 m_metaDataSource = metaDataSource;
91 }
92 }