1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.context.servlet;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.el.ELContext;
28 import javax.el.ELContextEvent;
29 import javax.el.ELContextListener;
30 import javax.faces.FactoryFinder;
31 import javax.faces.application.Application;
32 import javax.faces.application.ApplicationFactory;
33 import javax.faces.component.UINamingContainer;
34 import javax.faces.component.UIViewRoot;
35 import javax.faces.context.ExceptionHandler;
36 import javax.faces.context.ExternalContext;
37 import javax.faces.context.FacesContext;
38 import javax.faces.render.RenderKit;
39 import javax.faces.render.RenderKitFactory;
40
41 import org.apache.myfaces.context.ReleaseableExternalContext;
42 import org.apache.myfaces.el.unified.FacesELContext;
43 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
44
45
46
47
48
49
50
51
52 public abstract class FacesContextImplBase extends FacesContext
53 {
54 private Application _application;
55 private ExternalContext _externalContext;
56 private ReleaseableExternalContext _defaultExternalContext;
57 private UIViewRoot _viewRoot;
58 private RenderKitFactory _renderKitFactory;
59 private ELContext _elContext;
60 private Map<Object, Object> _attributes = null;
61 private boolean _processingEvents = true;
62 private ExceptionHandler _exceptionHandler = null;
63
64
65 private RenderKit _cachedRenderKit = null;
66 private String _cachedRenderKitId = null;
67
68 protected boolean _released = false;
69
70 private ApplicationFactory _applicationFactory = null;
71
72 private List<String> _resourceLibraryContracts;
73 private Character _separatorChar;
74 private FacesContext _currentFacesContext;
75
76
77
78
79
80 public FacesContextImplBase(final ExternalContext externalContext,
81 final ReleaseableExternalContext defaultExternalContext)
82 {
83 _externalContext = externalContext;
84 _defaultExternalContext = defaultExternalContext;
85
86
87
88 FacesContext.setCurrentInstance(this);
89 }
90
91 public FacesContextImplBase(final ExternalContext externalContext,
92 final ReleaseableExternalContext defaultExternalContext,
93 final ApplicationFactory applicationFactory,
94 final RenderKitFactory renderKitFactory)
95 {
96 _externalContext = externalContext;
97 _defaultExternalContext = defaultExternalContext;
98
99 _applicationFactory = applicationFactory;
100 _renderKitFactory = renderKitFactory;
101
102
103
104 FacesContext.setCurrentInstance(this);
105 }
106
107
108
109
110
111 @Override
112 public void release()
113 {
114 _applicationFactory = null;
115 _currentFacesContext = null;
116 if (_defaultExternalContext != null)
117 {
118 _defaultExternalContext.release();
119 _defaultExternalContext = null;
120 }
121
122 _application = null;
123 _externalContext = null;
124 _viewRoot = null;
125 _renderKitFactory = null;
126 _elContext = null;
127 _exceptionHandler = null;
128 _cachedRenderKit = null;
129 _cachedRenderKitId = null;
130 _separatorChar = null;
131
132
133
134
135 if (_attributes != null)
136 {
137 _attributes.clear();
138 _attributes = null;
139 }
140
141 _released = true;
142 FacesContext.setCurrentInstance(null);
143 }
144
145 @Override
146 public boolean isReleased()
147 {
148 return _released;
149 }
150
151 @Override
152 public final ExternalContext getExternalContext()
153 {
154 assertNotReleased();
155
156 return _externalContext;
157 }
158
159 @Override
160 public final Application getApplication()
161 {
162 assertNotReleased();
163
164 if (_application == null)
165 {
166 if (_applicationFactory == null)
167 {
168 _applicationFactory = (ApplicationFactory) FactoryFinder.getFactory(
169 FactoryFinder.APPLICATION_FACTORY);
170 }
171 _application = _applicationFactory.getApplication();
172 }
173
174 return _application;
175 }
176
177
178
179
180 public void purgeFacesContext()
181 {
182 _application = null;
183 _renderKitFactory = null;
184 _cachedRenderKit = null;
185 _cachedRenderKitId = null;
186 }
187
188 @Override
189 public final ExceptionHandler getExceptionHandler()
190 {
191 assertNotReleased();
192
193 return _exceptionHandler;
194 }
195
196 @Override
197 public final void setExceptionHandler(ExceptionHandler exceptionHandler)
198 {
199 assertNotReleased();
200
201 _exceptionHandler = exceptionHandler;
202 }
203
204 @Override
205 public final boolean isProcessingEvents()
206 {
207 assertNotReleased();
208
209 return _processingEvents;
210 }
211
212 @Override
213 public final void setProcessingEvents(boolean processingEvents)
214 {
215 assertNotReleased();
216
217 _processingEvents = processingEvents;
218 }
219
220 @Override
221 public final ELContext getELContext()
222 {
223 assertNotReleased();
224
225 if (_elContext != null)
226 {
227 return _elContext;
228 }
229
230 _elContext = new FacesELContext(getApplication().getELResolver(), getCurrentFacesContext());
231
232 ELContextEvent event = new ELContextEvent(_elContext);
233 for (ELContextListener listener : getApplication().getELContextListeners())
234 {
235 listener.contextCreated(event);
236 }
237
238 return _elContext;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 @Override
259 public final Map<Object, Object> getAttributes()
260 {
261 assertNotReleased();
262
263 if (_attributes == null)
264 {
265 _attributes = new HashMap<Object, Object>();
266 }
267 return _attributes;
268 }
269
270 @Override
271 public UIViewRoot getViewRoot()
272 {
273 assertNotReleased();
274
275 return _viewRoot;
276 }
277
278 @Override
279 public final void setViewRoot(final UIViewRoot viewRoot)
280 {
281 assertNotReleased();
282
283 if (viewRoot == null)
284 {
285 throw new NullPointerException("viewRoot");
286 }
287
288
289
290 if (_viewRoot != null && !_viewRoot.equals(viewRoot))
291 {
292
293 if (!Boolean.TRUE.equals(getAttributes().get(FaceletViewDeclarationLanguage.BUILDING_VIEW_METADATA)))
294 {
295
296 Map<String, Object> viewMap = _viewRoot.getViewMap(false);
297 if (viewMap != null)
298 {
299 viewMap.clear();
300 }
301 }
302 }
303 _viewRoot = viewRoot;
304 }
305
306 @Override
307 public final RenderKit getRenderKit()
308 {
309 assertNotReleased();
310
311 if (getViewRoot() == null)
312 {
313 return null;
314 }
315
316 String renderKitId = getViewRoot().getRenderKitId();
317
318 if (renderKitId == null)
319 {
320 return null;
321 }
322
323 if (_cachedRenderKitId == null || !renderKitId.equals(_cachedRenderKitId))
324 {
325 _cachedRenderKitId = renderKitId;
326 if (_renderKitFactory == null)
327 {
328 _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
329 }
330 _cachedRenderKit = _renderKitFactory.getRenderKit(getCurrentFacesContext(), renderKitId);
331 }
332
333 return _cachedRenderKit;
334 }
335
336 @Override
337 public List<String> getResourceLibraryContracts()
338 {
339 assertNotReleased();
340
341 if (_resourceLibraryContracts == null)
342 {
343 return Collections.emptyList();
344 }
345 else
346 {
347 return _resourceLibraryContracts;
348 }
349 }
350
351 @Override
352 public void setResourceLibraryContracts(List<String> contracts)
353 {
354 assertNotReleased();
355
356 if (contracts == null)
357 {
358 _resourceLibraryContracts = null;
359 }
360 else if (contracts.isEmpty())
361 {
362 _resourceLibraryContracts = null;
363 }
364 else
365 {
366 _resourceLibraryContracts = new ArrayList<String>(contracts);
367 }
368 }
369
370 @Override
371 public char getNamingContainerSeparatorChar()
372 {
373 if (_separatorChar == null)
374 {
375 _separatorChar = UINamingContainer.getSeparatorChar(this);
376 }
377 return _separatorChar;
378 }
379
380
381
382
383 protected final void assertNotReleased()
384 {
385 if (_released)
386 {
387 throw new IllegalStateException("Error the FacesContext is already released!");
388 }
389 }
390
391 protected FacesContext getCurrentFacesContext()
392 {
393 if (_currentFacesContext == null)
394 {
395 _currentFacesContext = FacesContext.getCurrentInstance();
396 }
397 return _currentFacesContext;
398 }
399 }