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 java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
import java.util.WeakHashMap; |
28 | |
import javax.annotation.PostConstruct; |
29 | |
import javax.annotation.PreDestroy; |
30 | |
import javax.naming.NamingException; |
31 | |
|
32 | |
import org.apache.myfaces.shared.util.ClassUtils; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class NoInjectionAnnotationLifecycleProvider implements LifecycleProvider2 |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | private volatile static WeakHashMap<ClassLoader, Map<Class,Method[]> > declaredMethodBeans = |
51 | |
new WeakHashMap<ClassLoader, Map<Class, Method[]>>(); |
52 | |
|
53 | |
private static Map<Class,Method[]> getDeclaredMethodBeansMap() |
54 | |
{ |
55 | 0 | ClassLoader cl = ClassUtils.getContextClassLoader(); |
56 | |
|
57 | 0 | Map<Class,Method[]> metadata = (Map<Class,Method[]>) |
58 | |
declaredMethodBeans.get(cl); |
59 | |
|
60 | 0 | if (metadata == null) |
61 | |
{ |
62 | |
|
63 | |
|
64 | 0 | synchronized (declaredMethodBeans) |
65 | |
{ |
66 | 0 | metadata = createDeclaredMethodBeansMap(cl, metadata); |
67 | 0 | } |
68 | |
} |
69 | |
|
70 | 0 | return metadata; |
71 | |
} |
72 | |
|
73 | |
private static Map<Class,Method[]> createDeclaredMethodBeansMap( |
74 | |
ClassLoader cl, Map<Class,Method[]> metadata) |
75 | |
{ |
76 | 0 | metadata = (Map<Class,Method[]>) declaredMethodBeans.get(cl); |
77 | 0 | if (metadata == null) |
78 | |
{ |
79 | 0 | metadata = new HashMap<Class,Method[]>(); |
80 | 0 | declaredMethodBeans.put(cl, metadata); |
81 | |
} |
82 | 0 | return metadata; |
83 | |
} |
84 | |
|
85 | |
public Object newInstance(String className) |
86 | |
throws InstantiationException, IllegalAccessException, NamingException, |
87 | |
InvocationTargetException, ClassNotFoundException |
88 | |
{ |
89 | 0 | Class clazz = ClassUtils.classForName(className); |
90 | 0 | Object object = clazz.newInstance(); |
91 | 0 | processAnnotations(object); |
92 | |
|
93 | 0 | return object; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
Method[] getDeclaredMethods(Class clazz) |
98 | |
{ |
99 | 0 | Map<Class,Method[]> declaredMethodBeansMap = getDeclaredMethodBeansMap(); |
100 | 0 | Method[] methods = declaredMethodBeansMap.get(clazz); |
101 | 0 | if (methods == null) |
102 | |
{ |
103 | 0 | methods = clazz.getDeclaredMethods(); |
104 | 0 | synchronized(declaredMethodBeansMap) |
105 | |
{ |
106 | 0 | declaredMethodBeansMap.put(clazz, methods); |
107 | 0 | } |
108 | |
} |
109 | 0 | return methods; |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public void postConstruct(Object instance) |
116 | |
throws IllegalAccessException, InvocationTargetException |
117 | |
{ |
118 | |
|
119 | 0 | Class clazz = instance.getClass(); |
120 | 0 | Method[] methods = getDeclaredMethods(clazz); |
121 | 0 | if (methods == null) |
122 | |
{ |
123 | 0 | methods = clazz.getDeclaredMethods(); |
124 | 0 | Map<Class,Method[]> declaredMethodBeansMap = getDeclaredMethodBeansMap(); |
125 | 0 | synchronized(declaredMethodBeansMap) |
126 | |
{ |
127 | 0 | declaredMethodBeansMap.put(clazz, methods); |
128 | 0 | } |
129 | |
} |
130 | 0 | Method postConstruct = null; |
131 | 0 | for (int i = 0; i < methods.length; i++) |
132 | |
{ |
133 | 0 | Method method = methods[i]; |
134 | 0 | if (method.isAnnotationPresent(PostConstruct.class)) |
135 | |
{ |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | 0 | if ((postConstruct != null) |
143 | |
|| (method.getParameterTypes().length != 0) |
144 | |
|| (Modifier.isStatic(method.getModifiers())) |
145 | |
|| (method.getExceptionTypes().length > 0) |
146 | |
|| (!method.getReturnType().getName().equals("void"))) |
147 | |
{ |
148 | 0 | throw new IllegalArgumentException("Invalid PostConstruct annotation"); |
149 | |
} |
150 | 0 | postConstruct = method; |
151 | |
} |
152 | |
} |
153 | |
|
154 | 0 | invokeAnnotatedMethod(postConstruct, instance); |
155 | |
|
156 | 0 | } |
157 | |
|
158 | |
public void destroyInstance(Object instance) |
159 | |
throws IllegalAccessException, InvocationTargetException |
160 | |
{ |
161 | |
|
162 | |
|
163 | |
|
164 | 0 | Class clazz = instance.getClass(); |
165 | 0 | Method[] methods = getDeclaredMethods(clazz); |
166 | 0 | Method preDestroy = null; |
167 | 0 | for (int i = 0; i < methods.length; i++) |
168 | |
{ |
169 | 0 | Method method = methods[i]; |
170 | 0 | if (method.isAnnotationPresent(PreDestroy.class)) |
171 | |
{ |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 0 | if ((preDestroy != null) |
179 | |
|| (method.getParameterTypes().length != 0) |
180 | |
|| (Modifier.isStatic(method.getModifiers())) |
181 | |
|| (method.getExceptionTypes().length > 0) |
182 | |
|| (!method.getReturnType().getName().equals("void"))) |
183 | |
{ |
184 | 0 | throw new IllegalArgumentException("Invalid PreDestroy annotation"); |
185 | |
} |
186 | 0 | preDestroy = method; |
187 | |
} |
188 | |
} |
189 | |
|
190 | 0 | invokeAnnotatedMethod(preDestroy, instance); |
191 | |
|
192 | 0 | } |
193 | |
|
194 | |
private void invokeAnnotatedMethod(Method method, Object instance) |
195 | |
throws IllegalAccessException, InvocationTargetException |
196 | |
{ |
197 | |
|
198 | |
|
199 | 0 | if (method != null) |
200 | |
{ |
201 | 0 | boolean accessibility = method.isAccessible(); |
202 | 0 | method.setAccessible(true); |
203 | 0 | method.invoke(instance); |
204 | 0 | method.setAccessible(accessibility); |
205 | |
} |
206 | 0 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
protected void processAnnotations(Object instance) |
212 | |
throws IllegalAccessException, InvocationTargetException, NamingException |
213 | |
{ |
214 | |
|
215 | 0 | } |
216 | |
|
217 | |
} |