001: /*
002: * $Id: FacesContextImpl.java,v 1.2.16.1 2007/03/16 11:05:42 dg154973 Exp $
003: */
004:
005: /*
006: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
007: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
008: */
009:
010: package com.sun.faces.portlet;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017: import javax.faces.FactoryFinder;
018: import javax.faces.application.Application;
019: import javax.faces.application.ApplicationFactory;
020: import javax.faces.application.FacesMessage;
021: import javax.faces.application.FacesMessage.Severity;
022: import javax.faces.component.UIViewRoot;
023: import javax.faces.context.ExternalContext;
024: import javax.faces.context.FacesContext;
025: import javax.faces.context.ResponseWriter;
026: import javax.faces.context.ResponseStream;
027: import javax.faces.lifecycle.Lifecycle;
028: import javax.faces.render.RenderKit;
029: import javax.faces.render.RenderKitFactory;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <p>Concrete implementation of <code>FacesContext</code> for use in
036: * a portlet environment.</p>
037: */
038:
039: public final class FacesContextImpl extends FacesContext {
040:
041: // ------------------------------------------------------------ Constructors
042:
043: public FacesContextImpl(ExternalContext econtext,
044: Lifecycle lifecycle) {
045: if ((econtext == null) || (lifecycle == null)) {
046: throw new NullPointerException();
047: }
048: this .econtext = econtext;
049: this .lifecycle = lifecycle;
050: setCurrentInstance(this );
051: if (log.isTraceEnabled()) {
052: log.trace("Created FacesContext " + this );
053: }
054: }
055:
056: // -------------------------------------------------------- Static Variables
057:
058: // The Log instance for this class
059: private static final Log log = LogFactory
060: .getLog(FacesContextFactoryImpl.class);
061:
062: // ------------------------------------------------------ Instance Variables
063:
064: private Application application;
065: private ExternalContext econtext;
066: private Lifecycle lifecycle;
067: private Map messages = new HashMap();
068: private boolean released = false;
069: private boolean renderResponse = false;
070: private boolean responseComplete = false;
071: private ResponseStream responseStream;
072: private ResponseWriter responseWriter;
073: private UIViewRoot viewRoot;
074:
075: // ------------------------------------------------- FacesContext Properties
076:
077: public Application getApplication() {
078: assertNotReleased();
079: if (application == null) {
080: ApplicationFactory af = (ApplicationFactory) FactoryFinder
081: .getFactory(FactoryFinder.APPLICATION_FACTORY);
082: application = af.getApplication();
083: }
084: return (application);
085: }
086:
087: public Iterator getClientIdsWithMessages() {
088: return (messages.keySet().iterator());
089: }
090:
091: public ExternalContext getExternalContext() {
092: assertNotReleased();
093: return (econtext);
094: }
095:
096: public Severity getMaximumSeverity() {
097: assertNotReleased();
098: Iterator messages = getMessages();
099: Severity maximum = null;
100: while (messages.hasNext()) {
101: Severity severity = ((FacesMessage) messages.next())
102: .getSeverity();
103: if (maximum == null) {
104: maximum = severity;
105: } else if (maximum.getOrdinal() < severity.getOrdinal()) {
106: maximum = severity;
107: }
108: }
109: return (maximum);
110: }
111:
112: public Iterator getMessages() {
113: assertNotReleased();
114: List results = new ArrayList();
115: Iterator clientIds = messages.keySet().iterator();
116: while (clientIds.hasNext()) {
117: String clientId = (String) clientIds.next();
118: results.addAll((List) messages.get(clientId));
119: }
120: return (results.iterator());
121: }
122:
123: public RenderKit getRenderKit() {
124: assertNotReleased();
125: UIViewRoot vr = getViewRoot();
126: if (vr == null) {
127: return (null);
128: }
129: String renderKitId = vr.getRenderKitId();
130: if (renderKitId == null) {
131: return (null);
132: }
133: RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder
134: .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
135: return (rkFactory.getRenderKit(this , renderKitId));
136: }
137:
138: public boolean getRenderResponse() {
139: assertNotReleased();
140: return (renderResponse);
141: }
142:
143: public boolean getResponseComplete() {
144: assertNotReleased();
145: return (responseComplete);
146: }
147:
148: public ResponseStream getResponseStream() {
149: assertNotReleased();
150: return (responseStream);
151: }
152:
153: public void setResponseStream(ResponseStream responseStream) {
154: assertNotReleased();
155: if (responseStream == null) {
156: throw new NullPointerException();
157: }
158: this .responseStream = responseStream;
159: }
160:
161: public ResponseWriter getResponseWriter() {
162: assertNotReleased();
163: return (responseWriter);
164: }
165:
166: public void setResponseWriter(ResponseWriter responseWriter) {
167: assertNotReleased();
168: if (responseWriter == null) {
169: throw new NullPointerException();
170: }
171: this .responseWriter = responseWriter;
172: }
173:
174: public UIViewRoot getViewRoot() {
175: assertNotReleased();
176: return (viewRoot);
177: }
178:
179: public void setViewRoot(UIViewRoot viewRoot) {
180: assertNotReleased();
181: if (viewRoot == null) {
182: throw new NullPointerException();
183: }
184: this .viewRoot = viewRoot;
185: }
186:
187: // ---------------------------------------------------- FacesContext Methods
188:
189: public void addMessage(String clientId, FacesMessage message) {
190: assertNotReleased();
191: if (message == null) {
192: throw new NullPointerException();
193: }
194: List list = (List) messages.get(clientId);
195: if (list == null) {
196: list = new ArrayList();
197: messages.put(clientId, list);
198: }
199: list.add(message);
200: }
201:
202: public Iterator getMessages(String clientId) {
203: assertNotReleased();
204: List list = (List) messages.get(clientId);
205: if (list == null) {
206: list = new ArrayList();
207: }
208: return (list.iterator());
209: }
210:
211: public void release() {
212: assertNotReleased();
213: released = true;
214: econtext = null;
215: responseStream = null;
216: responseWriter = null;
217: messages = null;
218: renderResponse = false;
219: responseComplete = false;
220: viewRoot = null;
221: // Make sure to clear our ThreadLocal instance.
222: setCurrentInstance(null);
223: }
224:
225: public void renderResponse() {
226: assertNotReleased();
227: renderResponse = true;
228: }
229:
230: public void responseComplete() {
231: assertNotReleased();
232: responseComplete = true;
233: }
234:
235: // --------------------------------------------------------- Private Methods
236:
237: /**
238: * <p>Throw an exception if this instance has been released.</p>
239: */
240: private void assertNotReleased() { // FIXME - i18n
241: if (released) {
242: throw new IllegalStateException(
243: "This instance has been released");
244: }
245: }
246:
247: }
|