KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > webapp > StartupServletContextListener


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 JavaDoc;
21 import javax.servlet.ServletContextEvent JavaDoc;
22 import javax.servlet.ServletContextListener JavaDoc;
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: matze $)
34  * @version $Revision: 1.28 $ $Date: 2004/10/13 11:51:01 $
35  * $Log: StartupServletContextListener.java,v $
36  * Revision 1.28 2004/10/13 11:51:01 matze
37  * renamed packages to org.apache
38  *
39  * Revision 1.27 2004/07/16 17:46:26 royalts
40  * moved org.apache.myfaces.webapp.webxml and org.apache.util.xml to share src-tree (needed WebXml for JspTilesViewHandlerImpl)
41  *
42  * Revision 1.26 2004/07/16 15:16:10 royalts
43  * moved org.apache.myfaces.webapp.webxml and org.apache.util.xml to share src-tree (needed WebXml for JspTilesViewHandlerImpl)
44  *
45  * Revision 1.25 2004/07/07 08:34:57 mwessendorf
46  * removed unused import-statements
47  *
48  * Revision 1.24 2004/07/07 00:25:08 o_rossmueller
49  * tidy up config/confignew package (moved confignew classes to package config)
50  *
51  * Revision 1.23 2004/07/06 23:46:01 o_rossmueller
52  * tidy up config/confignew package
53  *
54  * Revision 1.22 2004/07/01 22:05:11 mwessendorf
55  * ASF switch
56  *
57  * Revision 1.21 2004/06/16 23:02:25 o_rossmueller
58  * merged confignew_branch
59  *
60  * Revision 1.20.2.2 2004/06/16 02:07:24 o_rossmueller
61  * get navigation rules from RuntimeConfig
62  * refactored all remaining usages of MyFacesFactoryFinder to use RuntimeConfig
63  *
64  * Revision 1.20.2.1 2004/06/13 15:59:08 o_rossmueller
65  * started integration of new config mechanism:
66  * - factories
67  * - components
68  * - render kits
69  * - managed beans + managed properties (no list/map initialization)
70  *
71  * Revision 1.20 2004/04/26 11:28:17 manolito
72  * global navigation-rule with no from-view-id NPE bug
73  *
74  * Revision 1.19 2004/04/16 13:21:39 manolito
75  * Weblogic startup issue
76  *
77  */

78 public class StartupServletContextListener
79         implements ServletContextListener JavaDoc
80 {
81     private static final Log log = LogFactory.getLog(StartupServletContextListener.class);
82
83     static final String JavaDoc FACES_INIT_DONE
84             = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE";
85
86     public void contextInitialized(ServletContextEvent JavaDoc event)
87     {
88         initFaces(event.getServletContext());
89     }
90
91     public static void initFaces(ServletContext JavaDoc servletContext)
92     {
93         try
94         {
95             Boolean JavaDoc b = (Boolean JavaDoc)servletContext.getAttribute(FACES_INIT_DONE);
96
97             if (b == null || b.booleanValue() == false)
98             {
99                 log.trace("Initializing MyFaces");
100
101                 //Load the configuration
102
ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null);
103
104                 //And configure everything
105
new FacesConfigurator(externalContext).configure();
106
107                 // parse web.xml
108
WebXml.init(externalContext);
109
110                 servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE);
111             }
112             else
113             {
114                 log.info("MyFaces already initialized");
115             }
116         }
117         catch (Exception JavaDoc ex)
118         {
119             log.error("Error initializing ServletContext", ex);
120             ex.printStackTrace();
121         }
122         log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized.");
123     }
124
125
126     public void contextDestroyed(ServletContextEvent JavaDoc e)
127     {
128         FactoryFinder.releaseFactories();
129     }
130 }
131
Popular Tags