Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javax/faces/webapp/FacesServlet.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 javax.faces.webapp;
17  
18  import java.io.IOException;
19  
20  import javax.faces.FactoryFinder;
21  import javax.faces.context.FacesContext;
22  import javax.faces.context.FacesContextFactory;
23  import javax.faces.lifecycle.Lifecycle;
24  import javax.faces.lifecycle.LifecycleFactory;
25  import javax.servlet.Servlet;
26  import javax.servlet.ServletConfig;
27  import javax.servlet.ServletException;
28  import javax.servlet.ServletRequest;
29  import javax.servlet.ServletResponse;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * @author Manfred Geiler (latest modification by $Author: bdudney $)
36   * @version $Revision: 294841 $ $Date: 2005-10-04 13:13:34 -0400 (Tue, 04 Oct 2005) $
37   */
38  public final class FacesServlet
39          implements Servlet
40  {
41      private static final Log log = LogFactory.getLog(FacesServlet.class);
42      public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
43      public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";
44  
45      private static final String SERVLET_INFO = "FacesServlet of the MyFaces API implementation";
46      private ServletConfig _servletConfig;
47      private FacesContextFactory _facesContextFactory;
48      private Lifecycle _lifecycle;
49  
50      public FacesServlet()
51      {
52          super();
53      }
54  
55      public void destroy()
56      {
57          _servletConfig = null;
58          _facesContextFactory = null;
59          _lifecycle = null;
60      if(log.isTraceEnabled()) log.trace("destroy");
61      }
62  
63      public ServletConfig getServletConfig()
64      {
65          return _servletConfig;
66      }
67  
68      public String getServletInfo()
69      {
70          return SERVLET_INFO;
71      }
72  
73      private String getLifecycleId()
74      {
75          String lifecycleId = _servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
76          return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
77      }
78  
79      public void init(ServletConfig servletConfig)
80              throws ServletException
81      {
82      if(log.isTraceEnabled()) log.trace("init begin");
83          _servletConfig = servletConfig;
84          _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
85          //TODO: null-check for Weblogic, that tries to initialize Servlet before ContextListener
86  
87          //Javadoc says: Lifecycle instance is shared across multiple simultaneous requests, it must be implemented in a thread-safe manner.
88          //So we can acquire it here once:
89          LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
90          _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
91      if(log.isTraceEnabled()) log.trace("init end");
92      }
93  
94      public void service(ServletRequest request,
95                          ServletResponse response)
96              throws IOException,
97                     ServletException
98      {
99      if(log.isTraceEnabled()) log.trace("service begin");
100         FacesContext facesContext
101                 = _facesContextFactory.getFacesContext(_servletConfig.getServletContext(),
102                                                        request,
103                                                        response,
104                                                        _lifecycle);
105         try {
106       _lifecycle.execute(facesContext);
107       _lifecycle.render(facesContext);
108     }
109         catch (Throwable e)
110         {
111             if (e instanceof IOException)
112             {
113                 throw (IOException)e;
114             }
115             else if (e instanceof ServletException)
116             {
117                 throw (ServletException)e;
118             }
119             else if (e.getMessage() != null)
120             {
121                 throw new ServletException(e.getMessage(), e);
122             }
123             else
124             {
125                 throw new ServletException(e);
126             }
127         }
128         finally
129         {
130             facesContext.release();
131         }
132     if(log.isTraceEnabled()) log.trace("service end");
133     }
134 }