1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.config.annotation; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.lang.reflect.Method; |
23 | |
import java.lang.reflect.Modifier; |
24 | |
|
25 | |
import javax.annotation.PostConstruct; |
26 | |
import javax.annotation.PreDestroy; |
27 | |
import javax.naming.NamingException; |
28 | |
|
29 | |
import org.apache.myfaces.shared_impl.util.ClassUtils; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider2 |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
public Object newInstance(String className) |
42 | |
throws InstantiationException, IllegalAccessException, NamingException, InvocationTargetException, ClassNotFoundException |
43 | |
{ |
44 | 0 | Class clazz = ClassUtils.classForName(className); |
45 | 0 | Object object = clazz.newInstance(); |
46 | 0 | processAnnotations(object); |
47 | |
|
48 | 0 | return object; |
49 | |
} |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public void postConstruct(Object instance) |
55 | |
throws IllegalAccessException, InvocationTargetException |
56 | |
{ |
57 | |
|
58 | |
|
59 | |
|
60 | 0 | Method[] methods = instance.getClass().getDeclaredMethods(); |
61 | 0 | Method postConstruct = null; |
62 | 0 | for (Method method : methods) |
63 | |
{ |
64 | 0 | if (method.isAnnotationPresent(PostConstruct.class)) |
65 | |
{ |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 0 | if ((postConstruct != null) |
73 | |
|| (method.getParameterTypes().length != 0) |
74 | |
|| (Modifier.isStatic(method.getModifiers())) |
75 | |
|| (method.getExceptionTypes().length > 0) |
76 | |
|| (!method.getReturnType().getName().equals("void"))) |
77 | |
{ |
78 | 0 | throw new IllegalArgumentException("Invalid PostConstruct annotation"); |
79 | |
} |
80 | 0 | postConstruct = method; |
81 | |
} |
82 | |
} |
83 | |
|
84 | 0 | invokeAnnotatedMethod(postConstruct, instance); |
85 | |
|
86 | 0 | } |
87 | |
|
88 | |
public void destroyInstance(Object instance) |
89 | |
throws IllegalAccessException, InvocationTargetException |
90 | |
{ |
91 | |
|
92 | |
|
93 | |
|
94 | 0 | Method[] methods = instance.getClass().getDeclaredMethods(); |
95 | 0 | Method preDestroy = null; |
96 | 0 | for (Method method : methods) |
97 | |
{ |
98 | 0 | if (method.isAnnotationPresent(PreDestroy.class)) |
99 | |
{ |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | 0 | if ((preDestroy != null) |
107 | |
|| (method.getParameterTypes().length != 0) |
108 | |
|| (Modifier.isStatic(method.getModifiers())) |
109 | |
|| (method.getExceptionTypes().length > 0) |
110 | |
|| (!method.getReturnType().getName().equals("void"))) |
111 | |
{ |
112 | 0 | throw new IllegalArgumentException("Invalid PreDestroy annotation"); |
113 | |
} |
114 | 0 | preDestroy = method; |
115 | |
} |
116 | |
} |
117 | |
|
118 | 0 | invokeAnnotatedMethod(preDestroy, instance); |
119 | |
|
120 | 0 | } |
121 | |
|
122 | |
private void invokeAnnotatedMethod(Method method, Object instance) |
123 | |
throws IllegalAccessException, InvocationTargetException |
124 | |
{ |
125 | |
|
126 | |
|
127 | 0 | if (method != null) |
128 | |
{ |
129 | 0 | boolean accessibility = method.isAccessible(); |
130 | 0 | method.setAccessible(true); |
131 | 0 | method.invoke(instance); |
132 | 0 | method.setAccessible(accessibility); |
133 | |
} |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
protected void processAnnotations(Object instance) |
140 | |
throws IllegalAccessException, InvocationTargetException, NamingException |
141 | |
{ |
142 | |
|
143 | 0 | } |
144 | |
|
145 | |
} |