001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.context.servlet;
017:
018: import org.apache.myfaces.context.ReleaseableExternalContext;
019: import org.apache.myfaces.context.portlet.PortletExternalContextImpl;
020: import org.apache.myfaces.el.unified.FacesELContext;
021: import org.apache.myfaces.shared_impl.util.NullIterator;
022:
023: import javax.el.ELContext;
024: import javax.el.ELContextEvent;
025: import javax.el.ELContextListener;
026: import javax.faces.FactoryFinder;
027: import javax.faces.application.Application;
028: import javax.faces.application.ApplicationFactory;
029: import javax.faces.application.FacesMessage;
030: import javax.faces.component.UIViewRoot;
031: import javax.faces.context.ExternalContext;
032: import javax.faces.context.FacesContext;
033: import javax.faces.context.ResponseStream;
034: import javax.faces.context.ResponseWriter;
035: import javax.faces.render.RenderKit;
036: import javax.faces.render.RenderKitFactory;
037: import javax.portlet.PortletContext;
038: import javax.portlet.PortletRequest;
039: import javax.portlet.PortletResponse;
040: import javax.servlet.ServletContext;
041: import javax.servlet.ServletRequest;
042: import javax.servlet.ServletResponse;
043: import java.util.*;
044:
045: /**
046: * @author Manfred Geiler (latest modification by $Author: baranda $)
047: * @author Anton Koinov
048: * @version $Revision: 534633 $ $Date: 2007-05-03 00:13:29 +0200 (Do, 03 Mai 2007) $
049: */
050: public class FacesContextImpl extends FacesContext {
051: //~ Instance fields ----------------------------------------------------------------------------
052:
053: private List _messageClientIds = null;
054: private List _messages = null;
055: private Application _application;
056: private ReleaseableExternalContext _externalContext;
057: private ResponseStream _responseStream = null;
058: private ResponseWriter _responseWriter = null;
059: private FacesMessage.Severity _maximumSeverity = null;
060: private UIViewRoot _viewRoot;
061: private boolean _renderResponse = false;
062: private boolean _responseComplete = false;
063: private RenderKitFactory _renderKitFactory;
064: private boolean _released = false;
065: private ELContext _elContext;
066:
067: //~ Constructors -------------------------------------------------------------------------------
068:
069: public FacesContextImpl(PortletContext portletContext,
070: PortletRequest portletRequest,
071: PortletResponse portletResponse) {
072: this (new PortletExternalContextImpl(portletContext,
073: portletRequest, portletResponse));
074: }
075:
076: public FacesContextImpl(ServletContext servletContext,
077: ServletRequest servletRequest,
078: ServletResponse servletResponse) {
079: this (new ServletExternalContextImpl(servletContext,
080: servletRequest, servletResponse));
081: }
082:
083: private FacesContextImpl(ReleaseableExternalContext externalContext) {
084: _application = ((ApplicationFactory) FactoryFinder
085: .getFactory(FactoryFinder.APPLICATION_FACTORY))
086: .getApplication();
087: _renderKitFactory = (RenderKitFactory) FactoryFinder
088: .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
089: _externalContext = externalContext;
090: FacesContext.setCurrentInstance(this ); //protected method, therefore must be called from here
091: }
092:
093: //~ Methods ------------------------------------------------------------------------------------
094:
095: public ExternalContext getExternalContext() {
096: if (_released) {
097: throw new IllegalStateException(
098: "FacesContext already released");
099: }
100: return (ExternalContext) _externalContext;
101: }
102:
103: public FacesMessage.Severity getMaximumSeverity() {
104: if (_released) {
105: throw new IllegalStateException(
106: "FacesContext already released");
107: }
108: return _maximumSeverity;
109: }
110:
111: public Iterator getMessages() {
112: if (_released) {
113: throw new IllegalStateException(
114: "FacesContext already released");
115: }
116: return (_messages != null) ? _messages.iterator()
117: : Collections.EMPTY_LIST.iterator();
118: }
119:
120: public Application getApplication() {
121: if (_released) {
122: throw new IllegalStateException(
123: "FacesContext already released");
124: }
125:
126: return _application;
127: }
128:
129: public Iterator getClientIdsWithMessages() {
130: if (_released) {
131: throw new IllegalStateException(
132: "FacesContext already released");
133: }
134: if (_messages == null || _messages.isEmpty()) {
135: return NullIterator.instance();
136: }
137:
138: final Set uniqueClientIds = new LinkedHashSet(_messageClientIds);
139: return uniqueClientIds.iterator();
140: }
141:
142: public Iterator getMessages(String clientId) {
143: if (_released) {
144: throw new IllegalStateException(
145: "FacesContext already released");
146: }
147: if (_messages == null) {
148: return NullIterator.instance();
149: }
150:
151: List lst = new ArrayList();
152: for (int i = 0; i < _messages.size(); i++) {
153: Object savedClientId = _messageClientIds.get(i);
154: if (clientId == null) {
155: if (savedClientId == null)
156: lst.add(_messages.get(i));
157: } else {
158: if (clientId.equals(savedClientId))
159: lst.add(_messages.get(i));
160: }
161: }
162: return lst.iterator();
163: }
164:
165: public RenderKit getRenderKit() {
166: if (getViewRoot() == null) {
167: return null;
168: }
169:
170: String renderKitId = getViewRoot().getRenderKitId();
171:
172: if (renderKitId == null) {
173: return null;
174: }
175:
176: return _renderKitFactory.getRenderKit(this , renderKitId);
177: }
178:
179: public boolean getRenderResponse() {
180: if (_released) {
181: throw new IllegalStateException(
182: "FacesContext already released");
183: }
184: return _renderResponse;
185: }
186:
187: public boolean getResponseComplete() {
188: if (_released) {
189: throw new IllegalStateException(
190: "FacesContext already released");
191: }
192: return _responseComplete;
193: }
194:
195: public void setResponseStream(ResponseStream responseStream) {
196: if (_released) {
197: throw new IllegalStateException(
198: "FacesContext already released");
199: }
200: if (responseStream == null) {
201: throw new NullPointerException("responseStream");
202: }
203: _responseStream = responseStream;
204: }
205:
206: public ResponseStream getResponseStream() {
207: if (_released) {
208: throw new IllegalStateException(
209: "FacesContext already released");
210: }
211: return _responseStream;
212: }
213:
214: public void setResponseWriter(ResponseWriter responseWriter) {
215: if (_released) {
216: throw new IllegalStateException(
217: "FacesContext already released");
218: }
219: if (responseWriter == null) {
220: throw new NullPointerException("responseWriter");
221: }
222: _responseWriter = responseWriter;
223: }
224:
225: public ResponseWriter getResponseWriter() {
226: if (_released) {
227: throw new IllegalStateException(
228: "FacesContext already released");
229: }
230: return _responseWriter;
231: }
232:
233: public void setViewRoot(UIViewRoot viewRoot) {
234: if (_released) {
235: throw new IllegalStateException(
236: "FacesContext already released");
237: }
238: if (viewRoot == null) {
239: throw new NullPointerException("viewRoot");
240: }
241: _viewRoot = viewRoot;
242: }
243:
244: public UIViewRoot getViewRoot() {
245: if (_released) {
246: throw new IllegalStateException(
247: "FacesContext already released");
248: }
249: return _viewRoot;
250: }
251:
252: public void addMessage(String clientId, FacesMessage message) {
253: if (_released) {
254: throw new IllegalStateException(
255: "FacesContext already released");
256: }
257: if (message == null) {
258: throw new NullPointerException("message");
259: }
260:
261: if (_messages == null) {
262: _messages = new ArrayList();
263: _messageClientIds = new ArrayList();
264: }
265: _messages.add(message);
266: _messageClientIds.add((clientId != null) ? clientId : null);
267: FacesMessage.Severity serSeverity = message.getSeverity();
268: if (serSeverity != null) {
269: if (_maximumSeverity == null) {
270: _maximumSeverity = serSeverity;
271: } else if (serSeverity.compareTo(_maximumSeverity) > 0) {
272: _maximumSeverity = serSeverity;
273: }
274: }
275: }
276:
277: public void release() {
278: if (_released) {
279: throw new IllegalStateException(
280: "FacesContext already released");
281: }
282: if (_externalContext != null) {
283: _externalContext.release();
284: _externalContext = null;
285: }
286:
287: _messageClientIds = null;
288: _messages = null;
289: _application = null;
290: _responseStream = null;
291: _responseWriter = null;
292: _viewRoot = null;
293:
294: _released = true;
295: FacesContext.setCurrentInstance(null);
296: }
297:
298: public void renderResponse() {
299: if (_released) {
300: throw new IllegalStateException(
301: "FacesContext already released");
302: }
303: _renderResponse = true;
304: }
305:
306: public void responseComplete() {
307: if (_released) {
308: throw new IllegalStateException(
309: "FacesContext already released");
310: }
311: _responseComplete = true;
312: }
313:
314: // Portlet need to do this to change from ActionRequest/Response to
315: // RenderRequest/Response
316: public void setExternalContext(ReleaseableExternalContext extContext) {
317: _externalContext = extContext;
318: FacesContext.setCurrentInstance(this ); //TODO: figure out if I really need to do this
319: }
320:
321: public ELContext getELContext() {
322: if (_elContext != null)
323: return _elContext;
324:
325: _elContext = new FacesELContext(getApplication()
326: .getELResolver(), this );
327:
328: ELContextEvent event = new ELContextEvent(_elContext);
329: for (ELContextListener listener : getApplication()
330: .getELContextListeners()) {
331: listener.contextCreated(event);
332: }
333:
334: return _elContext;
335: }
336:
337: }
|