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