KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > context > FacesContextImpl


1 /*
2  * Copyright 1999-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.cocoon.faces.context;
17
18 import javax.faces.FactoryFinder;
19 import javax.faces.application.Application;
20 import javax.faces.application.ApplicationFactory;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.application.FacesMessage.Severity;
23 import javax.faces.component.UIViewRoot;
24 import javax.faces.context.ExternalContext;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseStream;
27 import javax.faces.context.ResponseWriter;
28 import javax.faces.render.RenderKit;
29 import javax.faces.render.RenderKitFactory;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Implementation of the Java Server Faces Context
39  *
40  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
41  * @version CVS $Id: FacesContextImpl.java 46253 2004-09-17 14:36:29Z vgritsenko $
42  */

43 public class FacesContextImpl extends FacesContext {
44     private ExternalContextImpl extContext;
45
46     private boolean released;
47     private boolean renderResponse;
48     private boolean responseComplete;
49
50     private Application application;
51     private UIViewRoot viewRoot;
52     private Map JavaDoc messages;
53
54     private ResponseStream responseStream;
55     private ResponseWriter responseWriter;
56
57
58     FacesContextImpl(ExternalContextImpl extContext) {
59         this.extContext = extContext;
60         FacesContext.setCurrentInstance(this);
61     }
62
63     private void checkReleased() {
64         if (released) {
65             throw new IllegalStateException JavaDoc("Context is released.");
66         }
67     }
68
69     public Application getApplication() {
70         checkReleased();
71
72         if (application == null) {
73             ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
74             this.application = aFactory.getApplication();
75         }
76
77         return this.application;
78     }
79
80     public Iterator JavaDoc getClientIdsWithMessages() {
81         checkReleased();
82
83         if (this.messages == null) {
84             return Collections.EMPTY_LIST.iterator();
85         } else {
86             return this.messages.keySet().iterator();
87         }
88     }
89
90     public ExternalContext getExternalContext() {
91         checkReleased();
92         return this.extContext;
93     }
94
95     public Severity getMaximumSeverity() {
96         throw new UnsupportedOperationException JavaDoc();
97     }
98
99     public Iterator JavaDoc getMessages() {
100         checkReleased();
101         if (this.messages == null) {
102             return Collections.EMPTY_LIST.iterator();
103         }
104
105         List JavaDoc messages = new ArrayList JavaDoc();
106         for (Iterator JavaDoc i = this.messages.values().iterator(); i.hasNext();) {
107             final List JavaDoc list = (List JavaDoc) i.next();
108             messages.addAll(list);
109         }
110
111         if (messages.size() > 0) {
112             return messages.iterator();
113         }
114
115         return Collections.EMPTY_LIST.iterator();
116     }
117
118     public Iterator JavaDoc getMessages(String JavaDoc clientID) {
119         checkReleased();
120         if (this.messages != null) {
121             final List JavaDoc list = (List JavaDoc) this.messages.get(clientID);
122             if (list != null) {
123                 return list.iterator();
124             }
125         }
126
127         return Collections.EMPTY_LIST.iterator();
128     }
129
130     public RenderKit getRenderKit() {
131         checkReleased();
132
133         UIViewRoot viewRoot = getViewRoot();
134         if (viewRoot == null) {
135             return null;
136         }
137
138         String JavaDoc renderKitId = viewRoot.getRenderKitId();
139         if (renderKitId == null) {
140             return null;
141         } else {
142             RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
143             return rkFactory.getRenderKit(this, renderKitId);
144         }
145     }
146
147     public boolean getRenderResponse() {
148         checkReleased();
149         return this.renderResponse;
150     }
151
152     public boolean getResponseComplete() {
153         checkReleased();
154         return this.responseComplete;
155     }
156
157     public ResponseStream getResponseStream() {
158         checkReleased();
159         return this.responseStream;
160     }
161
162     public void setResponseStream(ResponseStream responseStream) {
163         checkReleased();
164         if (responseStream == null) {
165             throw new NullPointerException JavaDoc("ResponseStream can't be null.");
166         }
167
168         this.responseStream = responseStream;
169     }
170
171     public ResponseWriter getResponseWriter() {
172         checkReleased();
173         return this.responseWriter;
174     }
175
176     public void setResponseWriter(ResponseWriter responseWriter) {
177         checkReleased();
178         if (responseWriter == null) {
179             throw new NullPointerException JavaDoc("ResponseWriter can't be null.");
180         }
181
182         this.responseWriter = responseWriter;
183     }
184
185     public UIViewRoot getViewRoot() {
186         checkReleased();
187         return this.viewRoot;
188     }
189
190     public void setViewRoot(UIViewRoot viewRoot) {
191         checkReleased();
192         this.viewRoot = viewRoot;
193     }
194
195     public void addMessage(String JavaDoc clientID, FacesMessage message) {
196         checkReleased();
197         if (message == null) {
198             throw new NullPointerException JavaDoc("Message can't be null");
199         }
200
201         if (messages == null) {
202             messages = new HashMap JavaDoc();
203         }
204
205         List JavaDoc list = (List JavaDoc) messages.get(clientID);
206         if (list == null) {
207             list = new ArrayList JavaDoc();
208             messages.put(clientID, list);
209         }
210
211         list.add(message);
212     }
213
214     public void release() {
215         this.released = true;
216         this.extContext = null;
217
218         FacesContext.setCurrentInstance(null);
219
220         this.application = null;
221         this.viewRoot = null;
222         this.messages = null;
223
224         this.responseStream = null;
225         this.responseWriter = null;
226     }
227
228     public void renderResponse() {
229         checkReleased();
230         this.renderResponse = true;
231     }
232
233     public void responseComplete() {
234         checkReleased();
235         this.responseComplete = true;
236     }
237 }
238
Popular Tags