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 | 0 | public class JSR250ValidatorProcessor implements InjectProcessor |
34 | |
{ |
35 | |
public Object process(Object object) |
36 | |
{ |
37 | 0 | List<AnnotationMetaData> annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PostConstruct.class); |
38 | 0 | if (annos.size() > 1) |
39 | |
{ |
40 | 0 | throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePostConstructAnnotation(object.getClass()).getMessage()); |
41 | |
} |
42 | 0 | else if(annos.size()==1) |
43 | |
{ |
44 | 0 | validateLifecycleMethod((Method)annos.get(0).getMember()); |
45 | |
} |
46 | |
|
47 | 0 | annos = AnnotationUtils.getMethodAnnotations(object.getClass(), PreDestroy.class); |
48 | 0 | if (annos.size() > 1) |
49 | |
{ |
50 | 0 | throw new IllegalArgumentException(CoreMessages.objectHasMoreThanOnePreDestroyAnnotation(object.getClass()).getMessage()); |
51 | |
} |
52 | 0 | else if(annos.size()==1) |
53 | |
{ |
54 | 0 | validateLifecycleMethod((Method)annos.get(0).getMember()); |
55 | |
} |
56 | |
|
57 | 0 | return object; |
58 | |
} |
59 | |
|
60 | |
public final void validateLifecycleMethod(Method method) |
61 | |
{ |
62 | 0 | if(method.getParameterTypes().length != 0) |
63 | |
{ |
64 | 0 | throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage()); |
65 | |
} |
66 | |
|
67 | 0 | if(!method.getReturnType().equals(Void.TYPE)) |
68 | |
{ |
69 | 0 | throw new IllegalArgumentException(CoreMessages.lifecycleMethodNotVoidOrHasParams(method).getMessage()); |
70 | |
} |
71 | |
|
72 | 0 | if(Modifier.isStatic(method.getModifiers())) |
73 | |
{ |
74 | 0 | throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotBeStatic(method).getMessage()); |
75 | |
} |
76 | |
|
77 | 0 | for (Class<?> aClass : method.getExceptionTypes()) |
78 | |
{ |
79 | 0 | if(!RuntimeException.class.isAssignableFrom(aClass)) |
80 | |
{ |
81 | 0 | throw new IllegalArgumentException(CoreMessages.lifecycleMethodCannotThrowChecked(method).getMessage()); |
82 | |
} |
83 | |
} |
84 | 0 | } |
85 | |
} |