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