KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > servlet > ServletFacesContextImpl


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 org.apache.myfaces.context.servlet;
17
18 import org.apache.myfaces.util.NullIterator;
19
20 import javax.faces.FactoryFinder;
21 import javax.faces.application.Application;
22 import javax.faces.application.ApplicationFactory;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.component.UIViewRoot;
25 import javax.faces.context.ExternalContext;
26 import javax.faces.context.FacesContext;
27 import javax.faces.context.ResponseStream;
28 import javax.faces.context.ResponseWriter;
29 import javax.faces.render.RenderKit;
30 import javax.faces.render.RenderKitFactory;
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.ServletRequest JavaDoc;
33 import javax.servlet.ServletResponse JavaDoc;
34 import java.util.*;
35 import javax.portlet.PortletContext;
36 import javax.portlet.PortletRequest;
37 import javax.portlet.PortletResponse;
38 import org.apache.myfaces.context.ReleaseableExternalContext;
39 import org.apache.myfaces.context.portlet.PortletExternalContextImpl;
40
41
42 /**
43  * @author Manfred Geiler (latest modification by $Author: matzew $)
44  * @author Anton Koinov
45  * @version $Revision: 1.18 $ $Date: 2005/01/26 17:03:11 $
46  * $Log: ServletFacesContextImpl.java,v $
47  * Revision 1.18 2005/01/26 17:03:11 matzew
48  * MYFACES-86. portlet support provided by Stan Silver (JBoss Group)
49  *
50  * Revision 1.17 2004/10/13 11:51:00 matze
51  * renamed packages to org.apache
52  *
53  * Revision 1.16 2004/07/18 21:44:40 o_rossmueller
54  * fix #992629: implemented FacesContext.getRenderKit()
55  *
56  * Revision 1.15 2004/07/01 22:05:04 mwessendorf
57  * ASF switch
58  *
59  * Revision 1.14 2004/04/16 13:56:59 manolito
60  * Bug #922317 - ClassCastException in action handler after adding message
61  *
62  * Revision 1.13 2004/03/31 11:58:38 manolito
63  * custom component refactoring
64  *
65  */

66 public class ServletFacesContextImpl
67     extends FacesContext
68 {
69     //~ Static fields/initializers -----------------------------------------------------------------
70

71     protected static final Object JavaDoc NULL_DUMMY = new Object JavaDoc();
72
73     //~ Instance fields ----------------------------------------------------------------------------
74

75     List _messageClientIds = null;
76     private List _messages = null;
77     private Application _application;
78     private ReleaseableExternalContext _externalContext;
79     private ResponseStream _responseStream = null;
80     private ResponseWriter _responseWriter = null;
81     private FacesMessage.Severity _maximumSeverity = FacesMessage.SEVERITY_INFO;
82     private UIViewRoot _viewRoot;
83     private boolean _renderResponse = false;
84     private boolean _responseComplete = false;
85     private RenderKitFactory _renderKitFactory;
86
87     //~ Constructors -------------------------------------------------------------------------------
88

89     // TODO: FIXME: the name of this class should be changed.
90
public ServletFacesContextImpl(PortletContext portletContext,
91                                    PortletRequest portletRequest,
92                                    PortletResponse portletResponse)
93     {
94         this(new PortletExternalContextImpl(portletContext,
95                                             portletRequest,
96                                             portletResponse));
97     }
98     
99     public ServletFacesContextImpl(ServletContext JavaDoc servletContext,
100                                    ServletRequest JavaDoc servletRequest,
101                                    ServletResponse JavaDoc servletResponse)
102     {
103         this(new ServletExternalContextImpl(servletContext,
104                                             servletRequest,
105                                             servletResponse));
106     }
107         
108     private ServletFacesContextImpl(ReleaseableExternalContext externalContext)
109     {
110         _application = ((ApplicationFactory)FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY))
111                             .getApplication();
112         _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
113         _externalContext = externalContext;
114         FacesContext.setCurrentInstance(this); //protected method, therefore must be called from here
115
}
116
117     //~ Methods ------------------------------------------------------------------------------------
118

119     public ExternalContext getExternalContext()
120     {
121         return (ExternalContext)_externalContext;
122     }
123
124     public FacesMessage.Severity getMaximumSeverity()
125     {
126         return _maximumSeverity;
127     }
128
129     public Iterator getMessages()
130     {
131         return (_messages != null) ? _messages.iterator() : Collections.EMPTY_LIST.iterator();
132     }
133
134     public Application getApplication()
135     {
136         return _application;
137     }
138
139     public Iterator getClientIdsWithMessages()
140     {
141         if (_messages == null || _messages.isEmpty())
142         {
143             return NullIterator.instance();
144         }
145
146         return new Iterator()
147             {
148                 private int _next;
149                 boolean _nextFound;
150
151                 public void remove()
152                 {
153                     throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
154                 }
155
156                 public boolean hasNext()
157                 {
158                     if (!_nextFound)
159                     {
160                         for (int len = _messageClientIds.size(); _next < len; _next++)
161                         {
162                             if (_messageClientIds.get(_next) != NULL_DUMMY)
163                             {
164                                 _nextFound = true;
165                                 break;
166                             }
167                         }
168                     }
169                     return _nextFound;
170                 }
171
172                 public Object JavaDoc next()
173                 {
174                     if (hasNext())
175                     {
176                         _nextFound = false;
177                         return _messageClientIds.get(_next++);
178                     }
179
180                     throw new NoSuchElementException();
181                 }
182             };
183     }
184
185     public Iterator getMessages(String JavaDoc clientId)
186     {
187         if (_messages == null)
188         {
189             return NullIterator.instance();
190         }
191
192         List lst = new ArrayList();
193         for (int i = 0; i < _messages.size(); i++)
194         {
195             Object JavaDoc savedClientId = _messageClientIds.get(i);
196             if (clientId == null)
197             {
198                 if (savedClientId == NULL_DUMMY) lst.add(_messages.get(i));
199             }
200             else
201             {
202                 if (clientId.equals(savedClientId)) lst.add(_messages.get(i));
203             }
204         }
205         return lst.iterator();
206     }
207
208     public RenderKit getRenderKit()
209     {
210          if (getViewRoot() == null)
211          {
212              return null;
213          }
214
215          String JavaDoc renderKitId = getViewRoot().getRenderKitId();
216         
217          if (renderKitId == null)
218          {
219              return null;
220          }
221
222          return _renderKitFactory.getRenderKit(this, renderKitId);
223     }
224
225     public boolean getRenderResponse()
226     {
227         return _renderResponse;
228     }
229
230     public boolean getResponseComplete()
231     {
232         return _responseComplete;
233     }
234
235     public void setResponseStream(ResponseStream responseStream)
236     {
237         if (responseStream == null)
238         {
239             throw new NullPointerException JavaDoc("responseStream");
240         }
241         _responseStream = responseStream;
242     }
243
244     public ResponseStream getResponseStream()
245     {
246         return _responseStream;
247     }
248
249     public void setResponseWriter(ResponseWriter responseWriter)
250     {
251         if (responseWriter == null)
252         {
253             throw new NullPointerException JavaDoc("responseWriter");
254         }
255         _responseWriter = responseWriter;
256     }
257
258     public ResponseWriter getResponseWriter()
259     {
260         return _responseWriter;
261     }
262
263     public void setViewRoot(UIViewRoot viewRoot)
264     {
265         if (viewRoot == null)
266         {
267             throw new NullPointerException JavaDoc("viewRoot");
268         }
269         _viewRoot = viewRoot;
270     }
271
272     public UIViewRoot getViewRoot()
273     {
274         return _viewRoot;
275     }
276
277     public void addMessage(String JavaDoc clientId, FacesMessage message)
278     {
279         if (message == null)
280         {
281             throw new NullPointerException JavaDoc("message");
282         }
283
284         if (_messages == null)
285         {
286             _messages = new ArrayList();
287             _messageClientIds = new ArrayList();
288         }
289         _messages.add(message);
290         _messageClientIds.add((clientId != null) ? clientId : NULL_DUMMY);
291         FacesMessage.Severity serSeverity = message.getSeverity();
292         if (serSeverity != null && serSeverity.compareTo(_maximumSeverity) > 0)
293         {
294             _maximumSeverity = message.getSeverity();
295         }
296     }
297
298     public void release()
299     {
300         if (_externalContext != null)
301         {
302             _externalContext.release();
303             _externalContext = null;
304         }
305
306         _messageClientIds = null;
307         _messages = null;
308         _application = null;
309         _responseStream = null;
310         _responseWriter = null;
311         _viewRoot = null;
312
313         FacesContext.setCurrentInstance(null);
314     }
315
316     public void renderResponse()
317     {
318         _renderResponse = true;
319     }
320
321     public void responseComplete()
322     {
323         _responseComplete = true;
324     }
325     
326     // Portlet need to do this to change from ActionRequest/Response to
327
// RenderRequest/Response
328
public void setExternalContext(ReleaseableExternalContext extContext)
329     {
330         _externalContext = extContext;
331         FacesContext.setCurrentInstance(this); //TODO: figure out if I really need to do this
332
}
333 }
334
Popular Tags