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.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.myfaces.config.FacesConfigValidator;
24 import org.apache.myfaces.config.FacesConfigurator;
25 import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
26 import org.apache.myfaces.shared_impl.util.StateUtils;
27 import org.apache.myfaces.shared_impl.webapp.webxml.WebXml;
28
29 import javax.faces.FactoryFinder;
30 import javax.faces.context.ExternalContext;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34 import java.util.Iterator;
35 import java.util.List;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class StartupServletContextListener
56 implements ServletContextListener
57 {
58 private static final Log log = LogFactory.getLog(StartupServletContextListener.class);
59
60 static final String FACES_INIT_DONE
61 = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE";
62
63 public void contextInitialized(ServletContextEvent event)
64 {
65 initFaces(event.getServletContext());
66 }
67
68 public static void initFaces(ServletContext servletContext)
69 {
70 try
71 {
72 Boolean b = (Boolean)servletContext.getAttribute(FACES_INIT_DONE);
73
74 if (b == null || b.booleanValue() == false)
75 {
76 log.trace("Initializing MyFaces");
77
78
79 ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null);
80
81
82 new FacesConfigurator(externalContext).configure();
83
84 if ("true".equals(servletContext
85 .getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM)) || "true".equals(servletContext
86 .getInitParameter(FacesConfigValidator.VALIDATE_CONTEXT_PARAM.toLowerCase())))
87 {
88 List list = FacesConfigValidator.validate(externalContext,
89 servletContext.getRealPath("/"));
90
91 Iterator iterator = list.iterator();
92
93 while (iterator.hasNext())
94 log.warn(iterator.next());
95
96 }
97
98
99 WebXml.init(externalContext);
100
101 servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
102 }
103 else
104 {
105 log.info("MyFaces already initialized");
106 }
107 }
108 catch (Exception ex)
109 {
110 log.error("Error initializing ServletContext", ex);
111 ex.printStackTrace();
112 }
113 log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized.");
114
115 String useEncryption = servletContext.getInitParameter(StateUtils.USE_ENCRYPTION);
116 if (!"false".equals(useEncryption)){
117 StateUtils.initSecret(servletContext);
118 }
119
120 }
121
122
123 public void contextDestroyed(ServletContextEvent e)
124 {
125 FactoryFinder.releaseFactories();
126 }
127 }