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 java.io.IOException;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import javax.faces.context.FacesContext;
26  import javax.faces.webapp.FacesServlet;
27  import javax.servlet.Servlet;
28  import javax.servlet.ServletConfig;
29  import javax.servlet.ServletContext;
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletRequest;
32  import javax.servlet.ServletResponse;
33  
34  import org.apache.myfaces.shared_impl.webapp.webxml.DelegatedFacesServlet;
35  
36  /**
37   * Derived FacesServlet that can be used for debugging purpose
38   * and to fix the Weblogic startup issue (FacesServlet is initialized before ServletContextListener).
39   *
40   * @author Manfred Geiler (latest modification by $Author$)
41   * @version $Revision$ $Date$
42   */
43  public class MyFacesServlet implements Servlet, DelegatedFacesServlet
44  {
45      //private static final Log log = LogFactory.getLog(MyFacesServlet.class);
46      private static final Logger log = Logger.getLogger(MyFacesServlet.class.getName());
47  
48      private final FacesServlet delegate = new FacesServlet();
49      
50      private FacesInitializer _facesInitializer;
51      
52      
53      public void setFacesInitializer(FacesInitializer facesInitializer) // TODO who uses this method?
54      {
55          _facesInitializer = facesInitializer;
56      }
57  
58      public void destroy()
59      {
60          delegate.destroy();
61      }
62  
63      public ServletConfig getServletConfig()
64      {
65          return delegate.getServletConfig();
66      }
67  
68      public String getServletInfo()
69      {
70          return delegate.getServletInfo();
71      }
72  
73      public void init(ServletConfig servletConfig)
74          throws ServletException
75      {
76          ServletContext servletContext = servletConfig.getServletContext();
77          
78          if (_facesInitializer == null)
79          {
80              _facesInitializer = FacesInitializerFactory.getFacesInitializer(servletContext);
81          }
82          
83          // Create startup FacesContext before initializing
84          FacesContext facesContext = _facesInitializer.initStartupFacesContext(servletContext);
85                
86          // Check, if ServletContextListener was already called
87          Boolean b = (Boolean)servletContext.getAttribute(StartupServletContextListener.FACES_INIT_DONE);
88          if (b == null || b.booleanValue() == false)
89          {
90              if(log.isLoggable(Level.WARNING))
91              {
92                  log.warning("ServletContextListener not yet called");
93              }
94              _facesInitializer.initFaces(servletConfig.getServletContext());
95          }
96          
97          // Destroy startup FacesContext
98          _facesInitializer.destroyStartupFacesContext(facesContext);
99          
100         delegate.init(servletConfig);
101         log.info("MyFacesServlet for context '" + servletConfig.getServletContext().getRealPath("/")
102                  + "' initialized.");
103     }
104     
105     public void service(ServletRequest request, ServletResponse response)
106             throws IOException, ServletException
107     {
108         if (log.isLoggable(Level.FINEST))
109         {
110             log.finest("MyFacesServlet service start");
111         }
112         delegate.service(request, response);
113         if (log.isLoggable(Level.FINEST))
114         {
115             log.finest("MyFacesServlet service finished");
116         }
117     }
118 
119 }