001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.faces.context;
018:
019: import javax.faces.FactoryFinder;
020: import javax.faces.application.Application;
021: import javax.faces.application.ApplicationFactory;
022: import javax.faces.application.FacesMessage;
023: import javax.faces.application.FacesMessage.Severity;
024: import javax.faces.component.UIViewRoot;
025: import javax.faces.context.ExternalContext;
026: import javax.faces.context.FacesContext;
027: import javax.faces.context.ResponseStream;
028: import javax.faces.context.ResponseWriter;
029: import javax.faces.render.RenderKit;
030: import javax.faces.render.RenderKitFactory;
031: import java.util.ArrayList;
032: import java.util.Collections;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Map;
037:
038: /**
039: * Implementation of the Java Server Faces Context
040: *
041: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
042: * @version CVS $Id: FacesContextImpl.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public class FacesContextImpl extends FacesContext {
045: private ExternalContextImpl extContext;
046:
047: private boolean released;
048: private boolean renderResponse;
049: private boolean responseComplete;
050:
051: private Application application;
052: private UIViewRoot viewRoot;
053: private Map messages;
054:
055: private ResponseStream responseStream;
056: private ResponseWriter responseWriter;
057:
058: FacesContextImpl(ExternalContextImpl extContext) {
059: this .extContext = extContext;
060: FacesContext.setCurrentInstance(this );
061: }
062:
063: private void checkReleased() {
064: if (released) {
065: throw new IllegalStateException("Context is released.");
066: }
067: }
068:
069: public Application getApplication() {
070: checkReleased();
071:
072: if (application == null) {
073: ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder
074: .getFactory(FactoryFinder.APPLICATION_FACTORY);
075: this .application = aFactory.getApplication();
076: }
077:
078: return this .application;
079: }
080:
081: public Iterator getClientIdsWithMessages() {
082: checkReleased();
083:
084: if (this .messages == null) {
085: return Collections.EMPTY_LIST.iterator();
086: } else {
087: return this .messages.keySet().iterator();
088: }
089: }
090:
091: public ExternalContext getExternalContext() {
092: checkReleased();
093: return this .extContext;
094: }
095:
096: public Severity getMaximumSeverity() {
097: throw new UnsupportedOperationException();
098: }
099:
100: public Iterator getMessages() {
101: checkReleased();
102: if (this .messages == null) {
103: return Collections.EMPTY_LIST.iterator();
104: }
105:
106: List messages = new ArrayList();
107: for (Iterator i = this .messages.values().iterator(); i
108: .hasNext();) {
109: final List list = (List) i.next();
110: messages.addAll(list);
111: }
112:
113: if (messages.size() > 0) {
114: return messages.iterator();
115: }
116:
117: return Collections.EMPTY_LIST.iterator();
118: }
119:
120: public Iterator getMessages(String clientID) {
121: checkReleased();
122: if (this .messages != null) {
123: final List list = (List) this .messages.get(clientID);
124: if (list != null) {
125: return list.iterator();
126: }
127: }
128:
129: return Collections.EMPTY_LIST.iterator();
130: }
131:
132: public RenderKit getRenderKit() {
133: checkReleased();
134:
135: UIViewRoot viewRoot = getViewRoot();
136: if (viewRoot == null) {
137: return null;
138: }
139:
140: String renderKitId = viewRoot.getRenderKitId();
141: if (renderKitId == null) {
142: return null;
143: } else {
144: RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder
145: .getFactory(FactoryFinder.RENDER_KIT_FACTORY);
146: return rkFactory.getRenderKit(this , renderKitId);
147: }
148: }
149:
150: public boolean getRenderResponse() {
151: checkReleased();
152: return this .renderResponse;
153: }
154:
155: public boolean getResponseComplete() {
156: checkReleased();
157: return this .responseComplete;
158: }
159:
160: public ResponseStream getResponseStream() {
161: checkReleased();
162: return this .responseStream;
163: }
164:
165: public void setResponseStream(ResponseStream responseStream) {
166: checkReleased();
167: if (responseStream == null) {
168: throw new NullPointerException(
169: "ResponseStream can't be null.");
170: }
171:
172: this .responseStream = responseStream;
173: }
174:
175: public ResponseWriter getResponseWriter() {
176: checkReleased();
177: return this .responseWriter;
178: }
179:
180: public void setResponseWriter(ResponseWriter responseWriter) {
181: checkReleased();
182: if (responseWriter == null) {
183: throw new NullPointerException(
184: "ResponseWriter can't be null.");
185: }
186:
187: this .responseWriter = responseWriter;
188: }
189:
190: public UIViewRoot getViewRoot() {
191: checkReleased();
192: return this .viewRoot;
193: }
194:
195: public void setViewRoot(UIViewRoot viewRoot) {
196: checkReleased();
197: this .viewRoot = viewRoot;
198: }
199:
200: public void addMessage(String clientID, FacesMessage message) {
201: checkReleased();
202: if (message == null) {
203: throw new NullPointerException("Message can't be null");
204: }
205:
206: if (messages == null) {
207: messages = new HashMap();
208: }
209:
210: List list = (List) messages.get(clientID);
211: if (list == null) {
212: list = new ArrayList();
213: messages.put(clientID, list);
214: }
215:
216: list.add(message);
217: }
218:
219: public void release() {
220: this .released = true;
221: this .extContext = null;
222:
223: FacesContext.setCurrentInstance(null);
224:
225: this .application = null;
226: this .viewRoot = null;
227: this .messages = null;
228:
229: this .responseStream = null;
230: this .responseWriter = null;
231: }
232:
233: public void renderResponse() {
234: checkReleased();
235: this .renderResponse = true;
236: }
237:
238: public void responseComplete() {
239: checkReleased();
240: this .responseComplete = true;
241: }
242: }
|