001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.context;
030:
031: import java.util.*;
032: import java.util.logging.*;
033:
034: import javax.el.*;
035:
036: import javax.faces.*;
037: import javax.faces.application.*;
038: import javax.faces.context.*;
039: import javax.faces.component.*;
040: import javax.faces.render.*;
041:
042: import javax.servlet.*;
043: import javax.servlet.http.*;
044:
045: public class ServletFacesContextImpl extends FacesContext {
046: private static final Logger log = Logger
047: .getLogger(ServletFacesContextImpl.class.getName());
048:
049: private final FacesContextFactoryImpl _factory;
050:
051: private ServletContext _webApp;
052: private HttpServletRequest _request;
053: private HttpServletResponse _response;
054:
055: private ExternalContext _externalContext;
056: private FacesELContext _elContext;
057:
058: private boolean _isResponseComplete;
059: private boolean _isRenderResponse;
060:
061: private UIViewRoot _uiViewRoot;
062:
063: private HashMap<String, ArrayList<FacesMessage>> _messageMap = new HashMap<String, ArrayList<FacesMessage>>();
064:
065: private ResponseWriter _responseWriter;
066: private ResponseStream _responseStream;
067:
068: private boolean _isClosed;
069:
070: protected ServletFacesContextImpl(FacesContextFactoryImpl factory,
071: ServletContext webApp, HttpServletRequest request,
072: HttpServletResponse response) {
073: _factory = factory;
074:
075: _webApp = webApp;
076: _request = request;
077: _response = response;
078: }
079:
080: public Application getApplication() {
081: if (_isClosed)
082: throw new IllegalStateException(getClass().getName()
083: + " is closed");
084:
085: return _factory.getApplication();
086: }
087:
088: public ExternalContext getExternalContext() {
089: if (_isClosed)
090: throw new IllegalStateException(getClass().getName()
091: + " is closed");
092:
093: if (_externalContext == null) {
094: _externalContext = new ServletExternalContext(_webApp,
095: _request, _response);
096: }
097:
098: return _externalContext;
099: }
100:
101: public RenderKit getRenderKit() {
102: if (_isClosed)
103: throw new IllegalStateException(getClass().getName()
104: + " is closed");
105:
106: UIViewRoot viewRoot = getViewRoot();
107:
108: if (viewRoot == null)
109: return null;
110:
111: String renderKitId = viewRoot.getRenderKitId();
112: if (renderKitId == null)
113: return null;
114:
115: RenderKitFactory factory = (RenderKitFactory) FactoryFinder
116: .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
117:
118: return factory.getRenderKit(this , renderKitId);
119: }
120:
121: public ResponseStream getResponseStream() {
122: if (_isClosed)
123: throw new IllegalStateException(getClass().getName()
124: + " is closed");
125:
126: return _responseStream;
127: }
128:
129: public void setResponseStream(ResponseStream responseStream) {
130: if (_isClosed)
131: throw new IllegalStateException(getClass().getName()
132: + " is closed");
133:
134: _responseStream = responseStream;
135: }
136:
137: public ResponseWriter getResponseWriter() {
138: if (_isClosed)
139: throw new IllegalStateException(getClass().getName()
140: + " is closed");
141:
142: if (_responseWriter == null) {
143: try {
144: _responseWriter = new ResponseWriterImpl(_response,
145: _response.getWriter());
146: } catch (RuntimeException e) {
147: throw e;
148: } catch (Exception e) {
149: throw new FacesException(e);
150: }
151: }
152:
153: return _responseWriter;
154: }
155:
156: public void setResponseWriter(ResponseWriter writer) {
157: if (_isClosed)
158: throw new IllegalStateException(getClass().getName()
159: + " is closed");
160:
161: _responseWriter = writer;
162: }
163:
164: /**
165: * Returns the root of the UI component tree.
166: */
167: public UIViewRoot getViewRoot() {
168: if (_isClosed)
169: throw new IllegalStateException(getClass().getName()
170: + " is closed");
171:
172: return _uiViewRoot;
173: /*
174: if (_uiViewRoot == null) {
175: _uiViewRoot = getApplication().getViewHandler().createView(this,
176: null);
177: }
178:
179: return _uiViewRoot;
180: */
181: }
182:
183: /**
184: * Sets the root of the UI component tree.
185: */
186: public void setViewRoot(UIViewRoot root) {
187: if (_isClosed)
188: throw new IllegalStateException(getClass().getName()
189: + " is closed");
190:
191: if (root == null)
192: throw new NullPointerException();
193:
194: _uiViewRoot = root;
195: }
196:
197: /**
198: * If true the facelet will skip to the render phase.
199: */
200: @Override
201: public boolean getRenderResponse() {
202: if (_isClosed)
203: throw new IllegalStateException(getClass().getName()
204: + " is closed");
205:
206: return _isRenderResponse;
207: }
208:
209: /**
210: * Ask the lifecycle to skip to the render phase.
211: */
212: @Override
213: public void renderResponse() {
214: if (_isClosed)
215: throw new IllegalStateException(getClass().getName()
216: + " is closed");
217:
218: _isRenderResponse = true;
219: }
220:
221: /**
222: * Return true if the lifecycle should skip the response phase.
223: */
224: @Override
225: public boolean getResponseComplete() {
226: if (_isClosed)
227: throw new IllegalStateException(getClass().getName()
228: + " is closed");
229:
230: return _isResponseComplete;
231: }
232:
233: /**
234: * Ask the lifecycle to skip the response phase.
235: */
236: @Override
237: public void responseComplete() {
238: if (_isClosed)
239: throw new IllegalStateException(getClass().getName()
240: + " is closed");
241:
242: _isResponseComplete = true;
243: }
244:
245: public void addMessage(String clientId, FacesMessage message) {
246: if (_isClosed)
247: throw new IllegalStateException("FacesContext is closed");
248:
249: if (message == null)
250: throw new NullPointerException();
251:
252: if (log.isLoggable(Level.FINE))
253: log.fine("FacesContext.addMessage " + clientId + " "
254: + message);
255:
256: synchronized (_messageMap) {
257: ArrayList<FacesMessage> messages = _messageMap
258: .get(clientId);
259:
260: if (messages == null) {
261: messages = new ArrayList<FacesMessage>();
262: _messageMap.put(clientId, messages);
263: }
264:
265: messages.add(message);
266: }
267: }
268:
269: public Iterator<String> getClientIdsWithMessages() {
270: if (_isClosed)
271: throw new IllegalStateException(getClass().getName()
272: + " is closed");
273:
274: synchronized (_messageMap) {
275: ArrayList<String> list = new ArrayList<String>(_messageMap
276: .keySet());
277:
278: return list.iterator();
279: }
280: }
281:
282: public FacesMessage.Severity getMaximumSeverity() {
283: if (_isClosed)
284: throw new IllegalStateException(getClass().getName()
285: + " is closed");
286:
287: synchronized (_messageMap) {
288: FacesMessage.Severity severity = null;
289:
290: for (Map.Entry<String, ArrayList<FacesMessage>> entry : _messageMap
291: .entrySet()) {
292: for (FacesMessage msg : entry.getValue()) {
293: if (severity == null)
294: severity = msg.getSeverity();
295: else if (severity.compareTo(msg.getSeverity()) < 0)
296: severity = msg.getSeverity();
297: }
298: }
299:
300: return severity;
301: }
302: }
303:
304: public Iterator<FacesMessage> getMessages() {
305: if (_isClosed)
306: throw new IllegalStateException(getClass().getName()
307: + " is closed");
308:
309: synchronized (_messageMap) {
310: ArrayList<FacesMessage> messages = new ArrayList<FacesMessage>();
311:
312: for (ArrayList<FacesMessage> value : _messageMap.values()) {
313: messages.addAll(value);
314: }
315:
316: return messages.iterator();
317: }
318: }
319:
320: public Iterator<FacesMessage> getMessages(String clientId) {
321: if (_isClosed)
322: throw new IllegalStateException(getClass().getName()
323: + " is closed");
324:
325: synchronized (_messageMap) {
326: ArrayList<FacesMessage> messages = _messageMap
327: .get(clientId);
328:
329: if (messages == null)
330: messages = new ArrayList<FacesMessage>();
331:
332: return messages.iterator();
333: }
334: }
335:
336: /**
337: * @Since 1.2
338: */
339: @Override
340: public ELContext getELContext() {
341: if (_isClosed)
342: throw new IllegalStateException(getClass().getName()
343: + " is closed");
344:
345: if (_elContext == null) {
346: _elContext = new FacesELContext(this , getApplication()
347: .getELResolver());
348: _elContext.putContext(FacesContext.class, this );
349: }
350:
351: return _elContext;
352: }
353:
354: public void release() {
355: _isClosed = true;
356: FacesContext.setCurrentInstance(null);
357: }
358:
359: public String toString() {
360: return "ServletFacesContextImpl[]";
361: }
362: }
|