1
2
3
4
5
6
7 package org.mule.registry;
8
9 import org.mule.api.registry.InjectProcessor;
10 import org.mule.config.i18n.CoreMessages;
11 import org.mule.util.annotation.AnnotationMetaData;
12 import org.mule.util.annotation.AnnotationUtils;
13
14 import java.lang.reflect.Method;
15 import java.lang.reflect.Modifier;
16 import java.util.List;
17
18 import javax.annotation.PostConstruct;
19 import javax.annotation.PreDestroy;
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class JSR250ValidatorProcessor implements InjectProcessor
34 {
35 public Object process(Object object)
36 {
37 List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PostConstruct.class);
38 if (annos.size() > 1)
39 {
40 throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePostConstructAnnotation(object.getClass()).getMessage());
41 }
42 else if(annos.size()==1)
43 {
44 validateLifecycleMethod((Method)annos.get(0).getMember());
45 }
46
47 annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PreDestroy.class);
48 if (annos.size() > 1)
49 {
50 throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePreDestroyAnnotation(object.getClass()).getMessage());
51 }
52 else if(annos.size()==1)
53 {
54 validateLifecycleMethod((Method)annos.get(0).getMember());
55 }
56
57 return object;
58 }
59
60 public final void validateLifecycleMethod(Method method)
61 {
62 if(method.getParameterTypes().length != 0)
63 {
64 throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
65 }
66
67 if(!method.getReturnType().equals(Void.TYPE))
68 {
69 throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage());
70 }
71
72 if(Modifier.isStatic(method.getModifiers()))
73 {
74 throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotBeStatic(method).getMessage());
75 }
76
77 for (Class<?> aClass : method.getExceptionTypes())
78 {
79 if(!RuntimeException.class.isAssignableFrom(aClass))
80 {
81 throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotThrowChecked(method).getMessage());
82 }
83 }
84 }
85 }