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