View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Initialise the MyFaces system.
39   * <p>
40   * This context listener is registered by the JSP TLD file for the standard
41   * JSF "f" components. Normally, servlet containers will automatically load
42   * and process .tld files at startup time, and therefore register and run
43   * this class automatically.
44   * <p>
45   * Some very old servlet containers do not do this correctly, so in those
46   * cases this listener may be registered manually in web.xml. Registering
47   * it twice (ie in both .tld and web.xml) will result in a harmless warning
48   * message being generated. Very old versions of MyFaces Core do not register
49   * the listener in the .tld file, so those also need a manual entry in web.xml.
50   * However all versions since at least 1.1.2 have this entry in the tld.
51   * 
52   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
53   * @version $Revision: 952074 $ $Date: 2010-06-06 22:14:18 -0500 (Sun, 06 Jun 2010) $
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                  //Load the configuration
79                  ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null);
80  
81                  //And configure everything
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                  // parse web.xml
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)){ // the default value is true
117             StateUtils.initSecret(servletContext);
118         }
119 
120     }
121 
122 
123     public void contextDestroyed(ServletContextEvent e)
124     {
125         FactoryFinder.releaseFactories();
126     }
127 }