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 | 0 | private Map<Object, Object> _attributes = null; |
61 | 0 | private boolean _processingEvents = true; |
62 | 0 | private ExceptionHandler _exceptionHandler = null; |
63 | |
|
64 | |
|
65 | 0 | private RenderKit _cachedRenderKit = null; |
66 | 0 | private String _cachedRenderKitId = null; |
67 | |
|
68 | 0 | protected boolean _released = false; |
69 | |
|
70 | 0 | 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 | 0 | { |
83 | 0 | _externalContext = externalContext; |
84 | 0 | _defaultExternalContext = defaultExternalContext; |
85 | |
|
86 | |
|
87 | |
|
88 | 0 | FacesContext.setCurrentInstance(this); |
89 | 0 | } |
90 | |
|
91 | |
public FacesContextImplBase(final ExternalContext externalContext, |
92 | |
final ReleaseableExternalContext defaultExternalContext, |
93 | |
final ApplicationFactory applicationFactory, |
94 | |
final RenderKitFactory renderKitFactory) |
95 | 0 | { |
96 | 0 | _externalContext = externalContext; |
97 | 0 | _defaultExternalContext = defaultExternalContext; |
98 | |
|
99 | 0 | _applicationFactory = applicationFactory; |
100 | 0 | _renderKitFactory = renderKitFactory; |
101 | |
|
102 | |
|
103 | |
|
104 | 0 | FacesContext.setCurrentInstance(this); |
105 | 0 | } |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
@Override |
112 | |
public void release() |
113 | |
{ |
114 | 0 | _applicationFactory = null; |
115 | 0 | _currentFacesContext = null; |
116 | 0 | if (_defaultExternalContext != null) |
117 | |
{ |
118 | 0 | _defaultExternalContext.release(); |
119 | 0 | _defaultExternalContext = null; |
120 | |
} |
121 | |
|
122 | 0 | _application = null; |
123 | 0 | _externalContext = null; |
124 | 0 | _viewRoot = null; |
125 | 0 | _renderKitFactory = null; |
126 | 0 | _elContext = null; |
127 | 0 | _exceptionHandler = null; |
128 | 0 | _cachedRenderKit = null; |
129 | 0 | _cachedRenderKitId = null; |
130 | 0 | _separatorChar = null; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | 0 | if (_attributes != null) |
136 | |
{ |
137 | 0 | _attributes.clear(); |
138 | 0 | _attributes = null; |
139 | |
} |
140 | |
|
141 | 0 | _released = true; |
142 | 0 | FacesContext.setCurrentInstance(null); |
143 | 0 | } |
144 | |
|
145 | |
@Override |
146 | |
public boolean isReleased() |
147 | |
{ |
148 | 0 | return _released; |
149 | |
} |
150 | |
|
151 | |
@Override |
152 | |
public final ExternalContext getExternalContext() |
153 | |
{ |
154 | 0 | assertNotReleased(); |
155 | |
|
156 | 0 | return _externalContext; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public final Application getApplication() |
161 | |
{ |
162 | 0 | assertNotReleased(); |
163 | |
|
164 | 0 | if (_application == null) |
165 | |
{ |
166 | 0 | if (_applicationFactory == null) |
167 | |
{ |
168 | 0 | _applicationFactory = (ApplicationFactory) FactoryFinder.getFactory( |
169 | |
FactoryFinder.APPLICATION_FACTORY); |
170 | |
} |
171 | 0 | _application = _applicationFactory.getApplication(); |
172 | |
} |
173 | |
|
174 | 0 | return _application; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public void purgeFacesContext() |
181 | |
{ |
182 | 0 | _application = null; |
183 | 0 | _renderKitFactory = null; |
184 | 0 | _cachedRenderKit = null; |
185 | 0 | _cachedRenderKitId = null; |
186 | 0 | } |
187 | |
|
188 | |
@Override |
189 | |
public final ExceptionHandler getExceptionHandler() |
190 | |
{ |
191 | 0 | assertNotReleased(); |
192 | |
|
193 | 0 | return _exceptionHandler; |
194 | |
} |
195 | |
|
196 | |
@Override |
197 | |
public final void setExceptionHandler(ExceptionHandler exceptionHandler) |
198 | |
{ |
199 | 0 | assertNotReleased(); |
200 | |
|
201 | 0 | _exceptionHandler = exceptionHandler; |
202 | 0 | } |
203 | |
|
204 | |
@Override |
205 | |
public final boolean isProcessingEvents() |
206 | |
{ |
207 | 0 | assertNotReleased(); |
208 | |
|
209 | 0 | return _processingEvents; |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
public final void setProcessingEvents(boolean processingEvents) |
214 | |
{ |
215 | 0 | assertNotReleased(); |
216 | |
|
217 | 0 | _processingEvents = processingEvents; |
218 | 0 | } |
219 | |
|
220 | |
@Override |
221 | |
public final ELContext getELContext() |
222 | |
{ |
223 | 0 | assertNotReleased(); |
224 | |
|
225 | 0 | if (_elContext != null) |
226 | |
{ |
227 | 0 | return _elContext; |
228 | |
} |
229 | |
|
230 | 0 | _elContext = new FacesELContext(getApplication().getELResolver(), getCurrentFacesContext()); |
231 | |
|
232 | 0 | ELContextEvent event = new ELContextEvent(_elContext); |
233 | 0 | for (ELContextListener listener : getApplication().getELContextListeners()) |
234 | |
{ |
235 | 0 | listener.contextCreated(event); |
236 | |
} |
237 | |
|
238 | 0 | 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 | 0 | assertNotReleased(); |
262 | |
|
263 | 0 | if (_attributes == null) |
264 | |
{ |
265 | 0 | _attributes = new HashMap<Object, Object>(); |
266 | |
} |
267 | 0 | return _attributes; |
268 | |
} |
269 | |
|
270 | |
@Override |
271 | |
public UIViewRoot getViewRoot() |
272 | |
{ |
273 | 0 | assertNotReleased(); |
274 | |
|
275 | 0 | return _viewRoot; |
276 | |
} |
277 | |
|
278 | |
@Override |
279 | |
public final void setViewRoot(final UIViewRoot viewRoot) |
280 | |
{ |
281 | 0 | assertNotReleased(); |
282 | |
|
283 | 0 | if (viewRoot == null) |
284 | |
{ |
285 | 0 | throw new NullPointerException("viewRoot"); |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | 0 | if (_viewRoot != null && !_viewRoot.equals(viewRoot)) |
291 | |
{ |
292 | |
|
293 | 0 | if (!Boolean.TRUE.equals(getAttributes().get(FaceletViewDeclarationLanguage.BUILDING_VIEW_METADATA))) |
294 | |
{ |
295 | |
|
296 | 0 | Map<String, Object> viewMap = _viewRoot.getViewMap(false); |
297 | 0 | if (viewMap != null) |
298 | |
{ |
299 | 0 | viewMap.clear(); |
300 | |
} |
301 | |
} |
302 | |
} |
303 | 0 | _viewRoot = viewRoot; |
304 | 0 | } |
305 | |
|
306 | |
@Override |
307 | |
public final RenderKit getRenderKit() |
308 | |
{ |
309 | 0 | assertNotReleased(); |
310 | |
|
311 | 0 | if (getViewRoot() == null) |
312 | |
{ |
313 | 0 | return null; |
314 | |
} |
315 | |
|
316 | 0 | String renderKitId = getViewRoot().getRenderKitId(); |
317 | |
|
318 | 0 | if (renderKitId == null) |
319 | |
{ |
320 | 0 | return null; |
321 | |
} |
322 | |
|
323 | 0 | if (_cachedRenderKitId == null || !renderKitId.equals(_cachedRenderKitId)) |
324 | |
{ |
325 | 0 | _cachedRenderKitId = renderKitId; |
326 | 0 | if (_renderKitFactory == null) |
327 | |
{ |
328 | 0 | _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
329 | |
} |
330 | 0 | _cachedRenderKit = _renderKitFactory.getRenderKit(getCurrentFacesContext(), renderKitId); |
331 | |
} |
332 | |
|
333 | 0 | return _cachedRenderKit; |
334 | |
} |
335 | |
|
336 | |
@Override |
337 | |
public List<String> getResourceLibraryContracts() |
338 | |
{ |
339 | 0 | assertNotReleased(); |
340 | |
|
341 | 0 | if (_resourceLibraryContracts == null) |
342 | |
{ |
343 | 0 | return Collections.emptyList(); |
344 | |
} |
345 | |
else |
346 | |
{ |
347 | 0 | return _resourceLibraryContracts; |
348 | |
} |
349 | |
} |
350 | |
|
351 | |
@Override |
352 | |
public void setResourceLibraryContracts(List<String> contracts) |
353 | |
{ |
354 | 0 | assertNotReleased(); |
355 | |
|
356 | 0 | if (contracts == null) |
357 | |
{ |
358 | 0 | _resourceLibraryContracts = null; |
359 | |
} |
360 | 0 | else if (contracts.isEmpty()) |
361 | |
{ |
362 | 0 | _resourceLibraryContracts = null; |
363 | |
} |
364 | |
else |
365 | |
{ |
366 | 0 | _resourceLibraryContracts = new ArrayList<String>(contracts); |
367 | |
} |
368 | 0 | } |
369 | |
|
370 | |
@Override |
371 | |
public char getNamingContainerSeparatorChar() |
372 | |
{ |
373 | 0 | if (_separatorChar == null) |
374 | |
{ |
375 | 0 | _separatorChar = UINamingContainer.getSeparatorChar(this); |
376 | |
} |
377 | 0 | return _separatorChar; |
378 | |
} |
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
protected final void assertNotReleased() |
384 | |
{ |
385 | 0 | if (_released) |
386 | |
{ |
387 | 0 | throw new IllegalStateException("Error the FacesContext is already released!"); |
388 | |
} |
389 | 0 | } |
390 | |
|
391 | |
protected FacesContext getCurrentFacesContext() |
392 | |
{ |
393 | 0 | if (_currentFacesContext == null) |
394 | |
{ |
395 | 0 | _currentFacesContext = FacesContext.getCurrentInstance(); |
396 | |
} |
397 | 0 | return _currentFacesContext; |
398 | |
} |
399 | |
} |