KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > faces > webapp > FacesServlet


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 javax.faces.FactoryFinder;
19 import javax.faces.context.FacesContext;
20 import javax.faces.context.FacesContextFactory;
21 import javax.faces.lifecycle.Lifecycle;
22 import javax.faces.lifecycle.LifecycleFactory;
23 import javax.servlet.*;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * @author Manfred Geiler (latest modification by $Author: manolito $)
28  * @version $Revision: 1.12 $ $Date: 2004/07/13 15:43:34 $
29  * $Log: FacesServlet.java,v $
30  * Revision 1.12 2004/07/13 15:43:34 manolito
31  * Bug #990228
32  *
33  * Revision 1.11 2004/07/01 22:00:54 mwessendorf
34  * ASF switch
35  *
36  * Revision 1.10 2004/06/21 10:57:58 manolito
37  * missing CVS Log keyword
38  *
39  * Revision 1.9 2004/06/16 23:02:20 o_rossmueller
40  * merged confignew_branch
41  *
42  * Revision 1.8 2004/06/14 12:55:23 manolito
43  * Added missing CVS Log comment
44  */

45 public class FacesServlet
46         implements Servlet
47 {
48     public static final String JavaDoc CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
49     public static final String JavaDoc LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";
50
51     private static final String JavaDoc SERVLET_INFO = "FacesServlet of the MyFaces API implementation";
52     private ServletConfig _servletConfig;
53     private FacesContextFactory _facesContextFactory;
54     private Lifecycle _lifecycle;
55
56     public FacesServlet()
57     {
58         super();
59     }
60
61     public void destroy()
62     {
63         _servletConfig = null;
64         _facesContextFactory = null;
65         _lifecycle = null;
66     }
67
68     public ServletConfig getServletConfig()
69     {
70         return _servletConfig;
71     }
72
73     public String JavaDoc getServletInfo()
74     {
75         return SERVLET_INFO;
76     }
77
78     private String JavaDoc getLifecycleId()
79     {
80         String JavaDoc lifecycleId = _servletConfig.getServletContext().getInitParameter(LIFECYCLE_ID_ATTR);
81         return lifecycleId != null ? lifecycleId : LifecycleFactory.DEFAULT_LIFECYCLE;
82     }
83
84     public void init(ServletConfig servletConfig)
85             throws ServletException
86     {
87         _servletConfig = servletConfig;
88         _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
89         //TODO: null-check for Weblogic, that tries to initialize Servlet before ContextListener
90

91         //Javadoc says: Lifecycle instance is shared across multiple simultaneous requests, it must be implemented in a thread-safe manner.
92
//So we can acquire it here once:
93
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
94         _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
95     }
96
97     public void service(ServletRequest request,
98                         ServletResponse response)
99             throws IOException JavaDoc,
100                    ServletException
101     {
102         FacesContext facesContext
103                 = _facesContextFactory.getFacesContext(_servletConfig.getServletContext(),
104                                                        request,
105                                                        response,
106                                                        _lifecycle);
107         try
108         {
109             _lifecycle.execute(facesContext);
110             _lifecycle.render(facesContext);
111         }
112         catch (Throwable JavaDoc e)
113         {
114             logException(e, null);
115             if (e instanceof IOException JavaDoc)
116             {
117                 throw (IOException JavaDoc)e;
118             }
119             else if (e instanceof ServletException)
120             {
121                 throw (ServletException)e;
122             }
123             else if (e.getMessage() != null)
124             {
125                 throw new ServletException(e.getMessage(), e);
126             }
127             else
128             {
129                 throw new ServletException(e);
130             }
131         }
132         finally
133         {
134             facesContext.release();
135         }
136     }
137
138     private void logException(Throwable JavaDoc e, String JavaDoc msgPrefix)
139     {
140         String JavaDoc msg;
141         if (msgPrefix == null)
142         {
143             if (e.getMessage() == null)
144             {
145                 msg = "Exception in FacesServlet";
146             }
147             else
148             {
149                 msg = e.getMessage();
150             }
151         }
152         else
153         {
154             if (e.getMessage() == null)
155             {
156                 msg = msgPrefix;
157             }
158             else
159             {
160                 msg = msgPrefix + ": " + e.getMessage();
161             }
162         }
163
164          _servletConfig.getServletContext().log(msg, e);
165         e.printStackTrace();
166
167         Throwable JavaDoc cause = e.getCause();
168         if (cause != null && cause != e)
169         {
170             logException(cause, "Root cause");
171         }
172
173         if(e instanceof ServletException)
174         {
175             cause = ((ServletException) e).getRootCause();
176
177             if(cause != null && cause != e)
178             {
179                 logException(cause, "Root cause of ServletException");
180             }
181         }
182     }
183
184
185 }
186
Popular Tags