1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.spi.impl; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.security.AccessController; |
23 | |
import java.security.PrivilegedActionException; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.logging.Level; |
27 | |
import java.util.logging.Logger; |
28 | |
|
29 | |
import javax.faces.FacesException; |
30 | |
import javax.faces.context.ExternalContext; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
import javax.naming.Context; |
33 | |
import javax.naming.InitialContext; |
34 | |
import javax.naming.NamingException; |
35 | |
|
36 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
37 | |
import org.apache.myfaces.shared.util.ClassUtils; |
38 | |
import org.apache.myfaces.spi.InjectionProvider; |
39 | |
import org.apache.myfaces.spi.InjectionProviderFactory; |
40 | |
import org.apache.myfaces.spi.ServiceProviderFinderFactory; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class DefaultInjectionProviderFactory extends InjectionProviderFactory |
47 | |
{ |
48 | |
|
49 | 0 | private static Logger log = Logger.getLogger(DefaultInjectionProviderFactory.class.getName()); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 0 | public static final String INJECTION_PROVIDER_INSTANCE_KEY |
59 | |
= InjectionProvider.class.getName() + ".INJECTION_PROVIDER_INSTANCE"; |
60 | |
|
61 | |
@JSFWebConfigParam(name="org.apache.myfaces.spi.InjectionProvider", since="2.2") |
62 | 0 | public static final String INJECTION_PROVIDER = InjectionProvider.class.getName(); |
63 | |
|
64 | |
|
65 | |
public DefaultInjectionProviderFactory() |
66 | 0 | { |
67 | 0 | } |
68 | |
|
69 | |
@Override |
70 | |
public InjectionProvider getInjectionProvider(ExternalContext externalContext) |
71 | |
{ |
72 | 0 | InjectionProvider lifecycleProvider = null; |
73 | 0 | if (externalContext == null) |
74 | |
{ |
75 | |
|
76 | |
|
77 | |
|
78 | 0 | log.info("No ExternalContext using fallback InjectionProvider."); |
79 | 0 | lifecycleProvider = resolveFallbackInjectionProvider(); |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | lifecycleProvider = (InjectionProvider) |
84 | |
externalContext.getApplicationMap().get(INJECTION_PROVIDER_INSTANCE_KEY); |
85 | |
} |
86 | 0 | if (lifecycleProvider == null) |
87 | |
{ |
88 | 0 | if (!resolveInjectionProviderFromExternalContext(externalContext)) |
89 | |
{ |
90 | 0 | if (!resolveInjectionProviderFromService(externalContext)) |
91 | |
{ |
92 | 0 | lifecycleProvider = resolveFallbackInjectionProvider(); |
93 | 0 | externalContext.getApplicationMap().put(INJECTION_PROVIDER_INSTANCE_KEY, lifecycleProvider); |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | |
|
98 | 0 | lifecycleProvider = (InjectionProvider) |
99 | |
externalContext.getApplicationMap().get(INJECTION_PROVIDER_INSTANCE_KEY); |
100 | |
} |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | |
|
105 | 0 | lifecycleProvider = (InjectionProvider) |
106 | |
externalContext.getApplicationMap().get(INJECTION_PROVIDER_INSTANCE_KEY); |
107 | |
} |
108 | 0 | log.info("Using InjectionProvider "+ lifecycleProvider.getClass().getName()); |
109 | |
} |
110 | 0 | return lifecycleProvider; |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public void release() |
115 | |
{ |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
private boolean resolveInjectionProviderFromExternalContext(ExternalContext externalContext) |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | String lifecycleProvider = externalContext.getInitParameter(INJECTION_PROVIDER); |
125 | 0 | if (lifecycleProvider != null) |
126 | |
{ |
127 | |
|
128 | 0 | Object obj = createClass(lifecycleProvider, externalContext); |
129 | |
|
130 | 0 | if (obj instanceof InjectionProvider) |
131 | |
{ |
132 | 0 | externalContext.getApplicationMap().put(INJECTION_PROVIDER_INSTANCE_KEY, obj); |
133 | 0 | return true; |
134 | |
} |
135 | |
} |
136 | |
} |
137 | 0 | catch (ClassNotFoundException e) |
138 | |
{ |
139 | 0 | log.log(Level.SEVERE, "", e); |
140 | |
} |
141 | 0 | catch (InstantiationException e) |
142 | |
{ |
143 | 0 | log.log(Level.SEVERE, "", e); |
144 | |
} |
145 | 0 | catch (IllegalAccessException e) |
146 | |
{ |
147 | 0 | log.log(Level.SEVERE, "", e); |
148 | |
} |
149 | 0 | catch (InvocationTargetException e) |
150 | |
{ |
151 | 0 | log.log(Level.SEVERE, "", e); |
152 | 0 | } |
153 | 0 | return false; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
private boolean resolveInjectionProviderFromService( |
158 | |
ExternalContext externalContext) |
159 | |
{ |
160 | 0 | boolean returnValue = false; |
161 | 0 | final ExternalContext extContext = externalContext; |
162 | |
try |
163 | |
{ |
164 | 0 | if (System.getSecurityManager() != null) |
165 | |
{ |
166 | 0 | returnValue = AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Boolean>() |
167 | 0 | { |
168 | |
public Boolean run() throws ClassNotFoundException, |
169 | |
NoClassDefFoundError, |
170 | |
InstantiationException, |
171 | |
IllegalAccessException, |
172 | |
InvocationTargetException, |
173 | |
PrivilegedActionException |
174 | |
{ |
175 | 0 | List<String> classList |
176 | |
= ServiceProviderFinderFactory.getServiceProviderFinder(extContext). |
177 | |
getServiceProviderList(INJECTION_PROVIDER); |
178 | 0 | Iterator<String> iter = classList.iterator(); |
179 | 0 | while (iter.hasNext()) |
180 | |
{ |
181 | 0 | String className = iter.next(); |
182 | 0 | Object obj = createClass(className,extContext); |
183 | 0 | if (InjectionProvider.class.isAssignableFrom(obj.getClass())) |
184 | |
{ |
185 | 0 | InjectionProvider discoverableInjectionProvider = |
186 | |
(InjectionProvider) obj; |
187 | 0 | if (discoverableInjectionProvider.isAvailable()) |
188 | |
{ |
189 | 0 | extContext.getApplicationMap().put(INJECTION_PROVIDER_INSTANCE_KEY, |
190 | |
discoverableInjectionProvider); |
191 | 0 | return true; |
192 | |
} |
193 | |
} |
194 | 0 | } |
195 | 0 | return false; |
196 | |
} |
197 | |
}); |
198 | |
} |
199 | |
else |
200 | |
{ |
201 | 0 | List<String> classList = ServiceProviderFinderFactory.getServiceProviderFinder(extContext). |
202 | |
getServiceProviderList(INJECTION_PROVIDER); |
203 | 0 | Iterator<String> iter = classList.iterator(); |
204 | 0 | while (iter.hasNext()) |
205 | |
{ |
206 | 0 | String className = iter.next(); |
207 | 0 | Object obj = createClass(className,extContext); |
208 | 0 | if (InjectionProvider.class.isAssignableFrom(obj.getClass())) |
209 | |
{ |
210 | 0 | InjectionProvider discoverableInjectionProvider |
211 | |
= (InjectionProvider) obj; |
212 | 0 | if (discoverableInjectionProvider.isAvailable()) |
213 | |
{ |
214 | 0 | extContext.getApplicationMap().put(INJECTION_PROVIDER_INSTANCE_KEY, |
215 | |
discoverableInjectionProvider); |
216 | 0 | return (Boolean) true; |
217 | |
} |
218 | |
} |
219 | 0 | } |
220 | |
} |
221 | |
} |
222 | 0 | catch (ClassNotFoundException e) |
223 | |
{ |
224 | |
|
225 | |
} |
226 | 0 | catch (NoClassDefFoundError e) |
227 | |
{ |
228 | |
|
229 | |
} |
230 | 0 | catch (InstantiationException e) |
231 | |
{ |
232 | 0 | log.log(Level.SEVERE, "", e); |
233 | |
} |
234 | 0 | catch (IllegalAccessException e) |
235 | |
{ |
236 | 0 | log.log(Level.SEVERE, "", e); |
237 | |
} |
238 | 0 | catch (InvocationTargetException e) |
239 | |
{ |
240 | 0 | log.log(Level.SEVERE, "", e); |
241 | |
} |
242 | 0 | catch (PrivilegedActionException e) |
243 | |
{ |
244 | 0 | throw new FacesException(e); |
245 | 0 | } |
246 | 0 | return returnValue; |
247 | |
} |
248 | |
|
249 | |
private Object createClass(String className, ExternalContext externalContext) |
250 | |
throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException |
251 | |
{ |
252 | 0 | Class<?> clazz = ClassUtils.classForName(className); |
253 | |
|
254 | |
try |
255 | |
{ |
256 | 0 | return ClassUtils.newInstance(clazz, new Class<?>[]{ ExternalContext.class }, externalContext); |
257 | |
} |
258 | 0 | catch (NoSuchMethodException e) |
259 | |
{ |
260 | 0 | return ClassUtils.newInstance(clazz); |
261 | |
} |
262 | |
} |
263 | |
|
264 | |
private InjectionProvider resolveFallbackInjectionProvider() |
265 | |
{ |
266 | 0 | return resolveFallbackInjectionProvider(FacesContext.getCurrentInstance().getExternalContext()); |
267 | |
} |
268 | |
|
269 | |
private InjectionProvider resolveFallbackInjectionProvider(ExternalContext externalContext) |
270 | |
{ |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
try |
290 | |
{ |
291 | 0 | ClassUtils.classForName("javax.annotation.PreDestroy"); |
292 | |
} |
293 | 0 | catch (ClassNotFoundException e) |
294 | |
{ |
295 | |
|
296 | 0 | return new NoAnnotationInjectionProvider(); |
297 | 0 | } |
298 | |
Context context; |
299 | |
try |
300 | |
{ |
301 | 0 | context = new InitialContext(); |
302 | |
try |
303 | |
{ |
304 | 0 | ClassUtils.classForName("javax.ejb.EJB"); |
305 | |
|
306 | 0 | return new AllAnnotationInjectionProvider(context); |
307 | |
} |
308 | 0 | catch (ClassNotFoundException e) |
309 | |
{ |
310 | |
|
311 | 0 | return new ResourceAnnotationInjectionProvider(context); |
312 | |
} |
313 | |
} |
314 | 0 | catch (NamingException e) |
315 | |
{ |
316 | |
|
317 | 0 | log.log(Level.SEVERE, "No InitialContext found. Using NoInjectionAnnotationProcessor.", e); |
318 | 0 | return new NoInjectionAnnotationInjectionProvider(); |
319 | |
} |
320 | 0 | catch (NoClassDefFoundError e) |
321 | |
{ |
322 | |
|
323 | |
|
324 | |
|
325 | 0 | log.log(Level.SEVERE, "No InitialContext class definition found. Using NoInjectionAnnotationProcessor."); |
326 | 0 | return new NoInjectionAnnotationInjectionProvider(); |
327 | |
} |
328 | |
} |
329 | |
} |