Source code: org/apache/myfaces/webapp/StartupServletContextListener.java
1 /*
2 * Copyright 2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.myfaces.webapp;
17
18 import javax.faces.FactoryFinder;
19 import javax.faces.context.ExternalContext;
20 import javax.servlet.ServletContext;
21 import javax.servlet.ServletContextEvent;
22 import javax.servlet.ServletContextListener;
23
24 import org.apache.myfaces.config.FacesConfigurator;
25 import org.apache.myfaces.context.servlet.ServletExternalContextImpl;
26 import org.apache.myfaces.webapp.webxml.WebXml;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31 * TODO: Add listener to myfaces-core.tld instead of web.xml
32 *
33 * @author Manfred Geiler (latest modification by $Author: grantsmith $)
34 * @version $Revision: 169655 $ $Date: 2005-05-11 12:45:06 -0400 (Wed, 11 May 2005) $
35 */
36 public class StartupServletContextListener
37 implements ServletContextListener
38 {
39 private static final Log log = LogFactory.getLog(StartupServletContextListener.class);
40
41 static final String FACES_INIT_DONE
42 = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE";
43
44 public void contextInitialized(ServletContextEvent event)
45 {
46 initFaces(event.getServletContext());
47 }
48
49 public static void initFaces(ServletContext servletContext)
50 {
51 try
52 {
53 Boolean b = (Boolean)servletContext.getAttribute(FACES_INIT_DONE);
54
55 if (b == null || b.booleanValue() == false)
56 {
57 log.trace("Initializing MyFaces");
58
59 //Load the configuration
60 ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null);
61
62 //And configure everything
63 new FacesConfigurator(externalContext).configure();
64
65 // parse web.xml
66 WebXml.init(externalContext);
67
68 servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
69 }
70 else
71 {
72 log.info("MyFaces already initialized");
73 }
74 }
75 catch (Exception ex)
76 {
77 log.error("Error initializing ServletContext", ex);
78 ex.printStackTrace();
79 }
80 log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized.");
81 }
82
83
84 public void contextDestroyed(ServletContextEvent e)
85 {
86 FactoryFinder.releaseFactories();
87 }
88 }