1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.webapp; |
20 | |
|
21 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
22 | |
import org.apache.myfaces.config.annotation.LifecycleProviderFactory; |
23 | |
import org.apache.myfaces.shared.util.ClassUtils; |
24 | |
|
25 | |
import javax.faces.FactoryFinder; |
26 | |
import javax.faces.context.ExternalContext; |
27 | |
import javax.faces.context.FacesContext; |
28 | |
|
29 | |
import javax.servlet.ServletContext; |
30 | |
import javax.servlet.ServletContextAttributeEvent; |
31 | |
import javax.servlet.ServletContextAttributeListener; |
32 | |
import javax.servlet.ServletContextEvent; |
33 | |
import javax.servlet.ServletContextListener; |
34 | |
import javax.servlet.ServletRequestAttributeEvent; |
35 | |
import javax.servlet.ServletRequestAttributeListener; |
36 | |
import javax.servlet.ServletRequestEvent; |
37 | |
import javax.servlet.ServletRequestListener; |
38 | |
import javax.servlet.http.HttpSessionAttributeListener; |
39 | |
import javax.servlet.http.HttpSessionBindingEvent; |
40 | |
import javax.servlet.http.HttpSessionEvent; |
41 | |
import javax.servlet.http.HttpSessionListener; |
42 | |
import java.lang.reflect.InvocationTargetException; |
43 | |
import java.lang.reflect.Method; |
44 | |
|
45 | |
import java.util.ArrayList; |
46 | |
import java.util.Iterator; |
47 | |
import java.util.LinkedList; |
48 | |
import java.util.List; |
49 | |
import java.util.Map; |
50 | |
import java.util.logging.Level; |
51 | |
import java.util.logging.Logger; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | public class StartupServletContextListener implements ServletContextListener, |
72 | |
HttpSessionAttributeListener, HttpSessionListener, |
73 | |
ServletRequestListener, ServletRequestAttributeListener, |
74 | |
ServletContextAttributeListener |
75 | |
{ |
76 | |
static final String FACES_INIT_DONE = "org.apache.myfaces.webapp.StartupServletContextListener.FACES_INIT_DONE"; |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@JSFWebConfigParam(since = "2.0") |
82 | |
static final String FACES_INIT_PLUGINS = "org.apache.myfaces.FACES_INIT_PLUGINS"; |
83 | |
|
84 | |
private static final byte FACES_INIT_PHASE_PREINIT = 0; |
85 | |
private static final byte FACES_INIT_PHASE_POSTINIT = 1; |
86 | |
private static final byte FACES_INIT_PHASE_PREDESTROY = 2; |
87 | |
private static final byte FACES_INIT_PHASE_POSTDESTROY = 3; |
88 | |
|
89 | |
|
90 | 0 | private static final Logger log = Logger.getLogger(StartupServletContextListener.class.getName()); |
91 | |
|
92 | |
private FacesInitializer _facesInitializer; |
93 | |
private ServletContext _servletContext; |
94 | 0 | private ManagedBeanDestroyerListener _detroyerListener = new ManagedBeanDestroyerListener(); |
95 | |
|
96 | |
public void contextInitialized(ServletContextEvent event) |
97 | |
{ |
98 | 0 | if (_servletContext != null) |
99 | |
{ |
100 | 0 | throw new IllegalStateException("context is already initialized"); |
101 | |
} |
102 | 0 | _servletContext = event.getServletContext(); |
103 | |
|
104 | 0 | Boolean b = (Boolean) _servletContext.getAttribute(FACES_INIT_DONE); |
105 | 0 | if (b == null || b.booleanValue() == false) |
106 | |
{ |
107 | 0 | long start = System.currentTimeMillis(); |
108 | |
|
109 | 0 | if (_facesInitializer == null) |
110 | |
{ |
111 | 0 | _facesInitializer = FacesInitializerFactory.getFacesInitializer(_servletContext); |
112 | |
} |
113 | |
|
114 | |
|
115 | 0 | FacesContext facesContext = _facesInitializer.initStartupFacesContext(_servletContext); |
116 | |
|
117 | |
|
118 | 0 | _publishManagedBeanDestroyerListener(facesContext); |
119 | |
|
120 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_PREINIT); |
121 | 0 | _facesInitializer.initFaces(_servletContext); |
122 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTINIT); |
123 | 0 | _servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE); |
124 | |
|
125 | |
|
126 | 0 | _detroyerListener.contextInitialized(event); |
127 | |
|
128 | |
|
129 | 0 | _facesInitializer.destroyStartupFacesContext(facesContext); |
130 | |
|
131 | 0 | log.log(Level.INFO, "MyFaces Core has started, it took [" |
132 | |
+ (System.currentTimeMillis() - start) |
133 | |
+ "] ms."); |
134 | 0 | } |
135 | |
else |
136 | |
{ |
137 | 0 | log.info("MyFaces already initialized"); |
138 | |
} |
139 | 0 | } |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
private void _publishManagedBeanDestroyerListener(FacesContext facesContext) |
149 | |
{ |
150 | 0 | ExternalContext externalContext = facesContext.getExternalContext(); |
151 | 0 | Map<String, Object> applicationMap = externalContext.getApplicationMap(); |
152 | |
|
153 | 0 | applicationMap.put(ManagedBeanDestroyerListener.APPLICATION_MAP_KEY, _detroyerListener); |
154 | 0 | } |
155 | |
|
156 | |
public void contextDestroyed(ServletContextEvent event) |
157 | |
{ |
158 | 0 | if (_facesInitializer != null && _servletContext != null) |
159 | |
{ |
160 | |
|
161 | 0 | FacesContext facesContext = _facesInitializer.initShutdownFacesContext(_servletContext); |
162 | |
|
163 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_PREDESTROY); |
164 | |
|
165 | 0 | _detroyerListener.contextDestroyed(event); |
166 | |
|
167 | 0 | _facesInitializer.destroyFaces(_servletContext); |
168 | |
|
169 | 0 | LifecycleProviderFactory.getLifecycleProviderFactory().release(); |
170 | |
|
171 | |
|
172 | |
|
173 | 0 | if (facesContext != null) |
174 | |
{ |
175 | 0 | _facesInitializer.destroyShutdownFacesContext(facesContext); |
176 | |
} |
177 | |
|
178 | 0 | FactoryFinder.releaseFactories(); |
179 | |
|
180 | |
|
181 | 0 | dispatchInitializationEvent(event, FACES_INIT_PHASE_POSTDESTROY); |
182 | |
} |
183 | |
|
184 | 0 | _servletContext = null; |
185 | 0 | } |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public void setFacesInitializer(FacesInitializer facesInitializer) |
193 | |
{ |
194 | 0 | if (_facesInitializer != null && _facesInitializer != facesInitializer && _servletContext != null) |
195 | |
{ |
196 | 0 | _facesInitializer.destroyFaces(_servletContext); |
197 | |
} |
198 | 0 | _facesInitializer = facesInitializer; |
199 | 0 | if (_servletContext != null) |
200 | |
{ |
201 | 0 | facesInitializer.initFaces(_servletContext); |
202 | |
} |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
private boolean loadFacesInitPluginsJDK6() |
213 | |
{ |
214 | 0 | String[] pluginEntries = null; |
215 | |
try |
216 | |
{ |
217 | 0 | Class serviceLoader = ClassUtils.getContextClassLoader().loadClass("java.util.ServiceLoader"); |
218 | 0 | Method m = serviceLoader.getDeclaredMethod("load", Class.class, ClassLoader.class); |
219 | 0 | Object loader = m.invoke(serviceLoader, StartupListener.class, ClassUtils.getContextClassLoader()); |
220 | 0 | m = loader.getClass().getDeclaredMethod("iterator"); |
221 | 0 | Iterator<StartupListener> it = (Iterator<StartupListener>) m.invoke(loader); |
222 | 0 | List<StartupListener> listeners = new LinkedList<StartupListener>(); |
223 | 0 | if (!it.hasNext()) |
224 | |
{ |
225 | 0 | return false; |
226 | |
} |
227 | 0 | while (it.hasNext()) |
228 | |
{ |
229 | 0 | listeners.add(it.next()); |
230 | |
} |
231 | |
|
232 | 0 | _servletContext.setAttribute(FACES_INIT_PLUGINS, listeners); |
233 | 0 | return true; |
234 | |
} |
235 | 0 | catch (ClassNotFoundException e) |
236 | |
{ |
237 | |
|
238 | |
} |
239 | 0 | catch (NoSuchMethodException e) |
240 | |
{ |
241 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
242 | |
} |
243 | 0 | catch (InvocationTargetException e) |
244 | |
{ |
245 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
246 | |
} |
247 | 0 | catch (IllegalAccessException e) |
248 | |
{ |
249 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
250 | 0 | } |
251 | 0 | return false; |
252 | |
} |
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
private void loadFacesInitPluginsJDK5() |
259 | |
{ |
260 | |
|
261 | 0 | String plugins = (String) _servletContext.getInitParameter(FACES_INIT_PLUGINS); |
262 | 0 | if (plugins == null) |
263 | |
{ |
264 | 0 | return; |
265 | |
} |
266 | 0 | log.info("MyFaces Plugins found"); |
267 | 0 | String[] pluginEntries = plugins.split(","); |
268 | 0 | List<StartupListener> listeners = new ArrayList<StartupListener>(pluginEntries.length); |
269 | 0 | for (String pluginEntry : pluginEntries) |
270 | |
{ |
271 | |
try |
272 | |
{ |
273 | 0 | Class pluginClass = null; |
274 | 0 | pluginClass = ClassUtils.getContextClassLoader().loadClass(pluginEntry); |
275 | 0 | if (pluginClass == null) |
276 | |
{ |
277 | 0 | pluginClass = this.getClass().getClassLoader().loadClass(pluginEntry); |
278 | |
} |
279 | 0 | listeners.add((StartupListener) pluginClass.newInstance()); |
280 | |
} |
281 | 0 | catch (ClassNotFoundException e) |
282 | |
{ |
283 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
284 | |
} |
285 | 0 | catch (InstantiationException e) |
286 | |
{ |
287 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
288 | |
} |
289 | 0 | catch (IllegalAccessException e) |
290 | |
{ |
291 | 0 | log.log(Level.SEVERE, e.getMessage(), e); |
292 | 0 | } |
293 | |
} |
294 | |
|
295 | 0 | _servletContext.setAttribute(FACES_INIT_PLUGINS, listeners); |
296 | |
|
297 | 0 | } |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
private void dispatchInitializationEvent(ServletContextEvent event, int operation) |
307 | |
{ |
308 | |
|
309 | 0 | if (operation == FACES_INIT_PHASE_PREINIT) |
310 | |
{ |
311 | 0 | if (!loadFacesInitPluginsJDK6()) |
312 | |
{ |
313 | 0 | loadFacesInitPluginsJDK5(); |
314 | |
} |
315 | |
} |
316 | |
|
317 | 0 | List<StartupListener> pluginEntries = (List<StartupListener>) _servletContext.getAttribute(FACES_INIT_PLUGINS); |
318 | 0 | if (pluginEntries == null) |
319 | |
{ |
320 | 0 | return; |
321 | |
} |
322 | |
|
323 | |
|
324 | 0 | for (StartupListener initializer : pluginEntries) |
325 | |
{ |
326 | 0 | log.info("Processing plugin"); |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | 0 | switch (operation) |
332 | |
{ |
333 | |
case FACES_INIT_PHASE_PREINIT: |
334 | 0 | initializer.preInit(event); |
335 | 0 | break; |
336 | |
case FACES_INIT_PHASE_POSTINIT: |
337 | 0 | initializer.postInit(event); |
338 | 0 | break; |
339 | |
case FACES_INIT_PHASE_PREDESTROY: |
340 | 0 | initializer.preDestroy(event); |
341 | 0 | break; |
342 | |
default: |
343 | 0 | initializer.postDestroy(event); |
344 | |
break; |
345 | |
} |
346 | 0 | } |
347 | 0 | log.info("Processing MyFaces plugins done"); |
348 | 0 | } |
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
public void attributeAdded(HttpSessionBindingEvent event) |
354 | |
{ |
355 | 0 | _detroyerListener.attributeAdded(event); |
356 | 0 | } |
357 | |
|
358 | |
public void attributeRemoved(HttpSessionBindingEvent event) |
359 | |
{ |
360 | 0 | _detroyerListener.attributeRemoved(event); |
361 | 0 | } |
362 | |
|
363 | |
public void attributeReplaced(HttpSessionBindingEvent event) |
364 | |
{ |
365 | 0 | _detroyerListener.attributeReplaced(event); |
366 | 0 | } |
367 | |
|
368 | |
public void sessionCreated(HttpSessionEvent event) |
369 | |
{ |
370 | 0 | _detroyerListener.sessionCreated(event); |
371 | 0 | } |
372 | |
|
373 | |
public void sessionDestroyed(HttpSessionEvent event) |
374 | |
{ |
375 | 0 | _detroyerListener.sessionDestroyed(event); |
376 | 0 | } |
377 | |
|
378 | |
|
379 | |
|
380 | |
public void attributeAdded(ServletContextAttributeEvent event) |
381 | |
{ |
382 | 0 | _detroyerListener.attributeAdded(event); |
383 | 0 | } |
384 | |
|
385 | |
public void attributeRemoved(ServletContextAttributeEvent event) |
386 | |
{ |
387 | 0 | _detroyerListener.attributeRemoved(event); |
388 | 0 | } |
389 | |
|
390 | |
public void attributeReplaced(ServletContextAttributeEvent event) |
391 | |
{ |
392 | 0 | _detroyerListener.attributeReplaced(event); |
393 | 0 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
public void attributeAdded(ServletRequestAttributeEvent event) |
398 | |
{ |
399 | 0 | _detroyerListener.attributeAdded(event); |
400 | 0 | } |
401 | |
|
402 | |
public void attributeRemoved(ServletRequestAttributeEvent event) |
403 | |
{ |
404 | 0 | _detroyerListener.attributeRemoved(event); |
405 | 0 | } |
406 | |
|
407 | |
public void attributeReplaced(ServletRequestAttributeEvent event) |
408 | |
{ |
409 | 0 | _detroyerListener.attributeReplaced(event); |
410 | 0 | } |
411 | |
|
412 | |
public void requestInitialized(ServletRequestEvent event) |
413 | |
{ |
414 | 0 | _detroyerListener.requestInitialized(event); |
415 | 0 | } |
416 | |
|
417 | |
public void requestDestroyed(ServletRequestEvent event) |
418 | |
{ |
419 | 0 | _detroyerListener.requestDestroyed(event); |
420 | 0 | } |
421 | |
|
422 | |
} |