1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets;
20
21 import java.beans.BeanDescriptor;
22 import java.beans.BeanInfo;
23 import java.beans.PropertyDescriptor;
24 import java.io.FileNotFoundException;
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.lang.reflect.Array;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.logging.Level;
36 import java.util.logging.Logger;
37
38 import javax.el.ELContext;
39 import javax.el.ELException;
40 import javax.el.MethodExpression;
41 import javax.el.ValueExpression;
42 import javax.el.VariableMapper;
43 import javax.faces.FacesException;
44 import javax.faces.FacesWrapper;
45 import javax.faces.application.ProjectStage;
46 import javax.faces.application.Resource;
47 import javax.faces.application.StateManager;
48 import javax.faces.application.ViewHandler;
49 import javax.faces.component.ActionSource2;
50 import javax.faces.component.EditableValueHolder;
51 import javax.faces.component.UIComponent;
52 import javax.faces.component.UINamingContainer;
53 import javax.faces.component.UIViewRoot;
54 import javax.faces.context.ExternalContext;
55 import javax.faces.context.FacesContext;
56 import javax.faces.context.ResponseWriter;
57 import javax.faces.event.ActionEvent;
58 import javax.faces.event.ActionListener;
59 import javax.faces.event.MethodExpressionActionListener;
60 import javax.faces.event.MethodExpressionValueChangeListener;
61 import javax.faces.event.PhaseId;
62 import javax.faces.event.PostAddToViewEvent;
63 import javax.faces.event.PreRemoveFromViewEvent;
64 import javax.faces.event.ValueChangeEvent;
65 import javax.faces.event.ValueChangeListener;
66 import javax.faces.render.RenderKit;
67 import javax.faces.validator.MethodExpressionValidator;
68 import javax.faces.validator.Validator;
69 import javax.faces.view.ActionSource2AttachedObjectHandler;
70 import javax.faces.view.ActionSource2AttachedObjectTarget;
71 import javax.faces.view.AttachedObjectHandler;
72 import javax.faces.view.AttachedObjectTarget;
73 import javax.faces.view.BehaviorHolderAttachedObjectHandler;
74 import javax.faces.view.BehaviorHolderAttachedObjectTarget;
75 import javax.faces.view.EditableValueHolderAttachedObjectHandler;
76 import javax.faces.view.EditableValueHolderAttachedObjectTarget;
77 import javax.faces.view.StateManagementStrategy;
78 import javax.faces.view.ValueHolderAttachedObjectHandler;
79 import javax.faces.view.ValueHolderAttachedObjectTarget;
80 import javax.faces.view.ViewDeclarationLanguage;
81 import javax.faces.view.ViewMetadata;
82 import javax.faces.view.facelets.FaceletContext;
83 import javax.faces.view.facelets.ResourceResolver;
84 import javax.faces.view.facelets.TagDecorator;
85 import javax.servlet.http.HttpServletResponse;
86
87 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
88 import org.apache.myfaces.config.RuntimeConfig;
89 import org.apache.myfaces.shared.application.DefaultViewHandlerSupport;
90 import org.apache.myfaces.shared.application.ViewHandlerSupport;
91 import org.apache.myfaces.shared.config.MyfacesConfig;
92 import org.apache.myfaces.shared.util.ClassUtils;
93 import org.apache.myfaces.shared.util.StringUtils;
94 import org.apache.myfaces.shared.util.WebConfigParamUtils;
95 import org.apache.myfaces.shared.view.ViewDeclarationLanguageBase;
96 import org.apache.myfaces.view.ViewDeclarationLanguageStrategy;
97 import org.apache.myfaces.view.ViewMetadataBase;
98 import org.apache.myfaces.view.facelets.FaceletViewHandler.NullWriter;
99 import org.apache.myfaces.view.facelets.compiler.Compiler;
100 import org.apache.myfaces.view.facelets.compiler.SAXCompiler;
101 import org.apache.myfaces.view.facelets.compiler.TagLibraryConfig;
102 import org.apache.myfaces.view.facelets.el.CompositeComponentELUtils;
103 import org.apache.myfaces.view.facelets.el.LocationMethodExpression;
104 import org.apache.myfaces.view.facelets.el.LocationValueExpression;
105 import org.apache.myfaces.view.facelets.el.MethodExpressionMethodExpression;
106 import org.apache.myfaces.view.facelets.el.RedirectMethodExpressionValueExpressionActionListener;
107 import org.apache.myfaces.view.facelets.el.RedirectMethodExpressionValueExpressionValidator;
108 import org.apache.myfaces.view.facelets.el.RedirectMethodExpressionValueExpressionValueChangeListener;
109 import org.apache.myfaces.view.facelets.el.ValueExpressionMethodExpression;
110 import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
111 import org.apache.myfaces.view.facelets.impl.DefaultFaceletFactory;
112 import org.apache.myfaces.view.facelets.impl.DefaultResourceResolver;
113 import org.apache.myfaces.view.facelets.tag.TagLibrary;
114 import org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorAttachedObjectTarget;
115 import org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper;
116 import org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper;
117 import org.apache.myfaces.view.facelets.tag.composite.CompositeLibrary;
118 import org.apache.myfaces.view.facelets.tag.composite.CompositeResourceLibrary;
119 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
120 import org.apache.myfaces.view.facelets.tag.jsf.core.AjaxHandler;
121 import org.apache.myfaces.view.facelets.tag.jsf.core.CoreLibrary;
122 import org.apache.myfaces.view.facelets.tag.jsf.html.HtmlLibrary;
123 import org.apache.myfaces.view.facelets.tag.jstl.core.JstlCoreLibrary;
124 import org.apache.myfaces.view.facelets.tag.jstl.fn.JstlFnLibrary;
125 import org.apache.myfaces.view.facelets.tag.ui.UIDebug;
126 import org.apache.myfaces.view.facelets.tag.ui.UILibrary;
127 import org.apache.myfaces.view.facelets.util.ReflectionUtil;
128
129 import static org.apache.myfaces.view.facelets.DefaultFaceletsStateManagementStrategy.*;
130 import org.apache.myfaces.view.facelets.impl.SectionUniqueIdCounter;
131 import org.apache.myfaces.view.facelets.tag.jsf.PartialMethodExpressionActionListener;
132 import org.apache.myfaces.view.facelets.tag.jsf.PartialMethodExpressionValidator;
133 import org.apache.myfaces.view.facelets.tag.jsf.PartialMethodExpressionValueChangeListener;
134
135
136
137
138
139
140
141
142
143 public class FaceletViewDeclarationLanguage extends ViewDeclarationLanguageBase
144 {
145
146 private static final Logger log = Logger.getLogger(FaceletViewDeclarationLanguage.class.getName());
147
148 private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
149
150 private static final Class<?>[] VALUE_CHANGE_LISTENER_SIGNATURE = new Class[]{ValueChangeEvent.class};
151
152 private static final Class<?>[] ACTION_LISTENER_SIGNATURE = new Class[]{ActionEvent.class};
153
154 private static final Class<?>[] VALIDATOR_SIGNATURE
155 = new Class[]{FacesContext.class, UIComponent.class, Object.class};
156
157 public static final String CHARACTER_ENCODING_KEY = "javax.faces.request.charset";
158
159 public final static long DEFAULT_REFRESH_PERIOD = 2;
160 public final static long DEFAULT_REFRESH_PERIOD_PRODUCTION = -1;
161
162 public final static String DEFAULT_CHARACTER_ENCODING = "UTF-8";
163
164
165
166
167
168
169
170 @JSFWebConfigParam(since = "2.0", alias = "facelets.BUFFER_SIZE", classType = "java.lang.Integer",
171 tags = "performance",
172 desc = "Define the default buffer size value passed to ExternalContext.setResponseBufferResponse() and in "
173 + "a servlet environment to HttpServletResponse.setBufferSize()")
174 public final static String PARAM_BUFFER_SIZE = "javax.faces.FACELETS_BUFFER_SIZE";
175
176
177
178
179
180 @JSFWebConfigParam(since = "2.0", deprecated = true, classType = "java.lang.Integer")
181 private final static String PARAM_BUFFER_SIZE_DEPRECATED = "facelets.BUFFER_SIZE";
182
183 private final static String[] PARAMS_BUFFER_SIZE = {PARAM_BUFFER_SIZE, PARAM_BUFFER_SIZE_DEPRECATED};
184
185
186
187
188
189
190
191 @JSFWebConfigParam(since = "2.0", alias = "facelets.DECORATORS")
192 public final static String PARAM_DECORATORS = "javax.faces.FACELETS_DECORATORS";
193
194
195
196
197
198 @JSFWebConfigParam(since = "2.0", deprecated = true)
199 private final static String PARAM_DECORATORS_DEPRECATED = "facelets.DECORATORS";
200
201 private final static String[] PARAMS_DECORATORS = {PARAM_DECORATORS, PARAM_DECORATORS_DEPRECATED};
202
203
204
205
206
207 public final static String PARAM_ENCODING = "facelets.Encoding";
208
209
210
211
212 @JSFWebConfigParam(since = "2.0",
213 desc = "Set of .taglib.xml files, separated by ';' that should be loaded by facelet engine.",
214 alias = "facelets.LIBRARIES")
215 public final static String PARAM_LIBRARIES = "javax.faces.FACELETS_LIBRARIES";
216
217
218
219
220 @JSFWebConfigParam(since = "2.0",
221 desc = "Set of .taglib.xml files, separated by ';' that should be loaded by facelet engine.",
222 deprecated = true)
223 private final static String PARAM_LIBRARIES_DEPRECATED = "facelets.LIBRARIES";
224
225 private final static String[] PARAMS_LIBRARIES = {PARAM_LIBRARIES, PARAM_LIBRARIES_DEPRECATED};
226
227
228
229
230
231
232 @JSFWebConfigParam(since = "2.0", defaultValue = "-1", alias = "facelets.REFRESH_PERIOD",
233 classType = "java.lang.Long", tags = "performance")
234 public final static String PARAM_REFRESH_PERIOD = "javax.faces.FACELETS_REFRESH_PERIOD";
235
236
237
238
239
240
241 @JSFWebConfigParam(since = "2.0", defaultValue = "-1", deprecated = true)
242 private final static String PARAM_REFRESH_PERIOD_DEPRECATED = "facelets.REFRESH_PERIOD";
243
244 private final static String[] PARAMS_REFRESH_PERIOD = {PARAM_REFRESH_PERIOD, PARAM_REFRESH_PERIOD_DEPRECATED};
245
246
247
248
249 @JSFWebConfigParam(since = "2.0", alias = "facelets.RESOURCE_RESOLVER")
250 public final static String PARAM_RESOURCE_RESOLVER = "javax.faces.FACELETS_RESOURCE_RESOLVER";
251
252
253
254
255 @JSFWebConfigParam(since = "2.0", deprecated = true)
256 private final static String PARAM_RESOURCE_RESOLVER_DEPRECATED = "facelets.RESOURCE_RESOLVER";
257
258 private final static String[] PARAMS_RESOURCE_RESOLVER
259 = {PARAM_RESOURCE_RESOLVER, PARAM_RESOURCE_RESOLVER_DEPRECATED};
260
261
262
263
264 @JSFWebConfigParam(since = "2.0", alias = "facelets.SKIP_COMMENTS")
265 public final static String PARAM_SKIP_COMMENTS = "javax.faces.FACELETS_SKIP_COMMENTS";
266
267
268
269
270 @JSFWebConfigParam(since = "2.0", deprecated = true)
271 private final static String PARAM_SKIP_COMMENTS_DEPRECATED = "facelets.SKIP_COMMENTS";
272
273 @JSFWebConfigParam(since = "2.1", defaultValue = "false", expectedValues = "true, false", tags = "performance")
274 private final static String PARAM_MARK_INITIAL_STATE_WHEN_APPLY_BUILD_VIEW
275 = "org.apache.myfaces.MARK_INITIAL_STATE_WHEN_APPLY_BUILD_VIEW";
276
277 private final static String[] PARAMS_SKIP_COMMENTS = {PARAM_SKIP_COMMENTS, PARAM_SKIP_COMMENTS_DEPRECATED};
278
279
280 public final static String FILLED_VIEW = "org.apache.myfaces.FILLED_VIEW";
281
282
283
284 public final static String BUILDING_VIEW_METADATA = "org.apache.myfaces.BUILDING_VIEW_METADATA";
285
286 public final static String REFRESHING_TRANSIENT_BUILD = "org.apache.myfaces.REFRESHING_TRANSIENT_BUILD";
287
288 public final static String REFRESH_TRANSIENT_BUILD_ON_PSS = "org.apache.myfaces.REFRESH_TRANSIENT_BUILD_ON_PSS";
289
290 public final static String USING_PSS_ON_THIS_VIEW = "org.apache.myfaces.USING_PSS_ON_THIS_VIEW";
291
292 public final static String REMOVING_COMPONENTS_BUILD = "org.apache.myfaces.REMOVING_COMPONENTS_BUILD";
293
294
295
296
297
298
299
300 public final static String MARK_INITIAL_STATE_KEY = "org.apache.myfaces.MARK_INITIAL_STATE";
301
302 public final static String IS_BUILDING_INITIAL_STATE_KEY_ALIAS
303 = "javax.faces.view.ViewDeclarationLanguage.IS_BUILDING_INITIAL_STATE";
304
305 public final static String CLEAN_TRANSIENT_BUILD_ON_RESTORE
306 = "org.apache.myfaces.CLEAN_TRANSIENT_BUILD_ON_RESTORE";
307
308 private final static String STATE_KEY = "<!--@@JSF_FORM_STATE_MARKER@@-->";
309
310 private final static int STATE_KEY_LEN = STATE_KEY.length();
311
312
313
314
315 public final static String CACHED_COMPONENT_IDS = "oam.CACHED_COMPONENT_IDS";
316
317 private int _bufferSize;
318
319
320
321
322 private ViewHandlerSupport _cachedViewHandlerSupport;
323
324 private String _defaultSuffix;
325
326 private FaceletFactory _faceletFactory;
327
328 private StateManagementStrategy _stateMgmtStrategy;
329
330 private boolean _partialStateSaving;
331
332 private boolean _refreshTransientBuildOnPSS;
333
334 private boolean _refreshTransientBuildOnPSSAuto;
335
336 private Set<String> _viewIds;
337
338 private boolean _markInitialStateWhenApplyBuildView;
339
340 private final ViewDeclarationLanguageStrategy _strategy;
341
342 private ResourceResolver _resourceResolver;
343
344
345
346
347 public FaceletViewDeclarationLanguage(FacesContext context)
348 {
349 initialize(context);
350 _strategy = new FaceletViewDeclarationLanguageStrategy();
351 }
352
353 public FaceletViewDeclarationLanguage(FacesContext context, ViewDeclarationLanguageStrategy strategy)
354 {
355 initialize(context);
356 _strategy = strategy;
357 }
358
359
360 @Override
361 public String getId()
362 {
363 return ViewDeclarationLanguage.FACELETS_VIEW_DECLARATION_LANGUAGE_ID;
364 }
365
366 @Override
367 public boolean viewExists(FacesContext facesContext, String viewId)
368 {
369 if (_strategy.handles(viewId))
370 {
371 return _resourceResolver.resolveUrl(viewId) != null;
372 }
373 return false;
374 }
375
376
377
378
379 @Override
380 public void buildView(FacesContext context, UIViewRoot view) throws IOException
381 {
382 if (isFilledView(context, view))
383 {
384 return;
385 }
386
387
388 String previousViewId = view.getViewId();
389 String renderedViewId = getRenderedViewId(context, previousViewId);
390
391 if (renderedViewId == null)
392 {
393 view.setViewId(renderedViewId);
394 }
395 else if (!renderedViewId.equals(previousViewId))
396 {
397 view.setViewId(renderedViewId);
398 }
399
400 if (log.isLoggable(Level.FINEST))
401 {
402 log.finest("Building View: " + renderedViewId);
403 }
404
405 boolean usePartialStateSavingOnThisView = _usePartialStateSavingOnThisView(renderedViewId);
406 boolean refreshTransientBuild = (view.getChildCount() > 0);
407 boolean refreshTransientBuildOnPSS = (usePartialStateSavingOnThisView && _refreshTransientBuildOnPSS);
408
409 if (usePartialStateSavingOnThisView)
410 {
411
412
413
414
415 if (view.getId() == null)
416 {
417 view.setId(view.createUniqueId(context, null));
418 }
419
420 context.getAttributes().put(USING_PSS_ON_THIS_VIEW, Boolean.TRUE);
421
422
423 if (!refreshTransientBuild)
424 {
425 context.getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
426 context.getAttributes().put(IS_BUILDING_INITIAL_STATE_KEY_ALIAS, Boolean.TRUE);
427 }
428 if (!refreshTransientBuild && _markInitialStateWhenApplyBuildView)
429 {
430 context.getAttributes().put(MARK_INITIAL_STATE_KEY, Boolean.TRUE);
431 }
432 if (refreshTransientBuildOnPSS)
433 {
434
435 context.getAttributes().put(REFRESH_TRANSIENT_BUILD_ON_PSS,
436 _refreshTransientBuildOnPSSAuto ? "auto" : "true");
437 }
438 }
439
440 try
441 {
442 if (refreshTransientBuild)
443 {
444 context.getAttributes().put(REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
445
446
447
448
449
450 }
451
452 _getFacelet(renderedViewId).apply(context, view);
453 }
454 finally
455 {
456 if (refreshTransientBuildOnPSS)
457 {
458 context.getAttributes().remove(REFRESH_TRANSIENT_BUILD_ON_PSS);
459 }
460 if (refreshTransientBuild)
461 {
462
463
464 if (!usePartialStateSavingOnThisView || refreshTransientBuildOnPSS)
465 {
466
467
468
469
470
471
472
473
474 FaceletViewDeclarationLanguage._publishPostBuildComponentTreeOnRestoreViewEvent(context, view);
475 }
476
477 context.getAttributes().remove(REFRESHING_TRANSIENT_BUILD);
478 }
479 else
480 {
481
482 context.getApplication().publishEvent(context, PostAddToViewEvent.class, UIViewRoot.class, view);
483 }
484 }
485
486
487 if (refreshTransientBuild)
488 {
489
490
491
492 setFilledView(context, view);
493 }
494 else if (!refreshTransientBuildOnPSS)
495 {
496
497
498 setFilledView(context, view);
499 }
500
501 else if (_refreshTransientBuildOnPSSAuto &&
502 !context.getAttributes().containsKey(CLEAN_TRANSIENT_BUILD_ON_RESTORE))
503 {
504 setFilledView(context, view);
505 }
506
507
508 if (usePartialStateSavingOnThisView)
509 {
510
511
512
513 if (!refreshTransientBuild)
514 {
515 if (_markInitialStateWhenApplyBuildView)
516 {
517 if (!refreshTransientBuildOnPSS ||
518 !view.getAttributes().containsKey(COMPONENT_ADDED_AFTER_BUILD_VIEW))
519 {
520 view.markInitialState();
521 }
522
523
524
525 context.getAttributes().remove(MARK_INITIAL_STATE_KEY);
526 }
527 else
528 {
529 context.getAttributes().put(MARK_INITIAL_STATE_KEY, Boolean.TRUE);
530 _markInitialStateOnView(view, refreshTransientBuildOnPSS);
531 context.getAttributes().remove(MARK_INITIAL_STATE_KEY);
532 }
533 context.getAttributes().remove(StateManager.IS_BUILDING_INITIAL_STATE);
534 context.getAttributes().remove(IS_BUILDING_INITIAL_STATE_KEY_ALIAS);
535 }
536
537
538
539
540
541
542 if (!(refreshTransientBuild && PhaseId.RESTORE_VIEW.equals(context.getCurrentPhaseId())))
543 {
544 ((DefaultFaceletsStateManagementStrategy) getStateManagementStrategy(context, view.getViewId())).
545 suscribeListeners(view);
546 }
547
548 context.getAttributes().remove(USING_PSS_ON_THIS_VIEW);
549 }
550
551
552
553
554 context.getAttributes().remove(AjaxHandler.STANDARD_JSF_AJAX_LIBRARY_LOADED);
555 }
556
557 private void _markInitialStateOnView(final UIViewRoot view, final boolean refreshTransientBuildOnPSS)
558 {
559 if (!refreshTransientBuildOnPSS ||
560 !view.getAttributes().containsKey(COMPONENT_ADDED_AFTER_BUILD_VIEW))
561 {
562 if (!view.isTransient())
563 {
564 view.markInitialState();
565 }
566 }
567
568 int childCount = view.getChildCount();
569 if (childCount > 0)
570 {
571 for (int i = 0; i < childCount; i++)
572 {
573 UIComponent child = view.getChildren().get(i);
574 if (!child.isTransient())
575 {
576 _markInitialState(child);
577 }
578 }
579 }
580 if (view.getFacetCount() > 0)
581 {
582 Map<String, UIComponent> facetMap = view.getFacets();
583 for (Map.Entry<String, UIComponent> entry : facetMap.entrySet())
584 {
585 UIComponent child = entry.getValue();
586 if (!child.isTransient())
587 {
588 _markInitialState(child);
589 }
590 }
591
592 }
593 }
594
595 private void _markInitialState(final UIComponent component)
596 {
597 component.markInitialState();
598
599 final int childCount = component.getChildCount();
600 if (childCount > 0)
601 {
602 for (int i = 0; i < childCount; i++)
603 {
604 UIComponent child = component.getChildren().get(i);
605 if (!child.isTransient())
606 {
607 _markInitialState(child);
608 }
609 }
610 }
611 if (component.getFacetCount() > 0)
612 {
613 Map<String, UIComponent> facetMap = component.getFacets();
614 for (Map.Entry<String, UIComponent> entry : facetMap.entrySet())
615 {
616 UIComponent child = entry.getValue();
617 if (!child.isTransient())
618 {
619 _markInitialState(child);
620 }
621 }
622
623 }
624 }
625
626 private static void _publishPreRemoveFromViewEvent(FacesContext context, UIComponent component)
627 {
628 context.getApplication().publishEvent(context, PreRemoveFromViewEvent.class, component.getClass(), component);
629
630 if (component.getChildCount() > 0)
631 {
632 for (int j = 0, childCount = component.getChildCount(); j < childCount; j++)
633 {
634 UIComponent child = component.getChildren().get(j);
635 _publishPreRemoveFromViewEvent(context, child);
636 }
637 }
638 if (component.getFacetCount() > 0)
639 {
640 for (UIComponent child : component.getFacets().values())
641 {
642 _publishPreRemoveFromViewEvent(context, child);
643 }
644 }
645 }
646
647 public static void _publishPostBuildComponentTreeOnRestoreViewEvent(FacesContext context, UIComponent component)
648 {
649 context.getApplication().publishEvent(context, PostBuildComponentTreeOnRestoreViewEvent.class,
650 component.getClass(), component);
651
652 if (component.getChildCount() > 0)
653 {
654
655
656
657 List<UIComponent> children = component.getChildren();
658 UIComponent child = null;
659 UIComponent currentChild = null;
660 int i = 0;
661 while (i < children.size())
662 {
663 child = children.get(i);
664
665
666 do
667 {
668 _publishPostBuildComponentTreeOnRestoreViewEvent(context, child);
669 currentChild = child;
670 child = children.get(i);
671 }
672 while ((i < children.size()) && child != currentChild);
673 i++;
674 }
675 }
676 if (component.getFacetCount() > 0)
677 {
678 for (UIComponent child : component.getFacets().values())
679 {
680 _publishPostBuildComponentTreeOnRestoreViewEvent(context, child);
681 }
682 }
683 }
684
685 private boolean isFilledView(FacesContext context, UIViewRoot view)
686 {
687
688
689
690 return context.getAttributes().containsKey(view);
691
692
693
694
695
696 }
697
698 private void setFilledView(FacesContext context, UIViewRoot view)
699 {
700 context.getAttributes().put(view, Boolean.TRUE);
701
702
703
704
705
706 }
707
708
709
710
711
712
713 @Override
714 public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource)
715 {
716 BeanInfo beanInfo = null;
717
718 checkNull(context, "context");
719
720 try
721 {
722 Facelet compositeComponentFacelet;
723 FaceletFactory.setInstance(_faceletFactory);
724 try
725 {
726 compositeComponentFacelet
727 = _faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
728 }
729 finally
730 {
731 FaceletFactory.setInstance(null);
732 }
733
734
735
736
737 UINamingContainer compositeComponentBase
738 = (UINamingContainer) context.getApplication().createComponent(
739 context, UINamingContainer.COMPONENT_TYPE, null);
740
741
742
743
744
745
746 compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
747
748
749
750
751 FaceletContext faceletContext = (FaceletContext) context.
752 getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
753 VariableMapper orig = faceletContext.getVariableMapper();
754 try
755 {
756 faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
757
758 compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
759
760 compositeComponentFacelet.apply(context, compositeComponentBase);
761
762 compositeComponentBase.popComponentFromEL(context);
763 }
764 finally
765 {
766 faceletContext.setVariableMapper(orig);
767 }
768
769 beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
770 }
771 catch (IOException e)
772 {
773 throw new FacesException(e);
774 }
775
776
777
778
779
780 return beanInfo;
781 }
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800 public static boolean isBuildingViewMetadata(FacesContext context)
801 {
802 return context.getAttributes().containsKey(BUILDING_VIEW_METADATA);
803 }
804
805 public static boolean isRefreshingTransientBuild(FacesContext context)
806 {
807 return context.getAttributes().containsKey(REFRESHING_TRANSIENT_BUILD);
808 }
809
810 public static boolean isRemovingComponentBuild(FacesContext context)
811 {
812 return context.getAttributes().containsKey(REMOVING_COMPONENTS_BUILD);
813 }
814
815 public static boolean isMarkInitialState(FacesContext context)
816 {
817 return Boolean.TRUE.equals(context.getAttributes().get(MARK_INITIAL_STATE_KEY));
818 }
819
820 public static boolean isRefreshTransientBuildOnPSS(FacesContext context)
821 {
822
823 return context.getAttributes().containsKey(REFRESH_TRANSIENT_BUILD_ON_PSS);
824 }
825
826 public static boolean isRefreshTransientBuildOnPSSAuto(FacesContext context)
827 {
828 return "auto".equalsIgnoreCase((String) context.getAttributes().get(REFRESH_TRANSIENT_BUILD_ON_PSS));
829 }
830
831 public static boolean isCleanTransientBuildOnRestore(FacesContext context)
832 {
833 return context.getAttributes().containsKey(CLEAN_TRANSIENT_BUILD_ON_RESTORE);
834 }
835
836 public static void cleanTransientBuildOnRestore(FacesContext context)
837 {
838 context.getAttributes().put(CLEAN_TRANSIENT_BUILD_ON_RESTORE, Boolean.TRUE);
839 }
840
841 public static boolean isUsingPSSOnThisView(FacesContext context)
842 {
843 return context.getAttributes().containsKey(USING_PSS_ON_THIS_VIEW);
844 }
845
846
847
848
849
850
851
852
853
854
855 @Override
856 @SuppressWarnings("unchecked")
857 public void retargetAttachedObjects(FacesContext context,
858 UIComponent topLevelComponent, List<AttachedObjectHandler> handlerList)
859 {
860 checkNull(context, "context");
861 checkNull(topLevelComponent, "topLevelComponent");
862 checkNull(handlerList, "handlerList");
863
864 BeanInfo compositeComponentMetadata
865 = (BeanInfo) topLevelComponent.getAttributes().get(UIComponent.BEANINFO_KEY);
866
867 if (compositeComponentMetadata == null)
868 {
869 log.severe("Composite component metadata not found for: " + topLevelComponent.getClientId(context));
870 return;
871 }
872
873 BeanDescriptor compositeComponentDescriptor = compositeComponentMetadata.getBeanDescriptor();
874
875 List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>)
876 compositeComponentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
877
878 if (targetList == null || targetList.isEmpty())
879 {
880 return;
881 }
882
883 for (int i = 0, size = handlerList.size(); i < size; i++)
884 {
885 AttachedObjectHandler currentHandler = handlerList.get(i);
886
887
888 String forValue = currentHandler.getFor();
889
890
891
892 for (int k = 0, targetsSize = targetList.size(); k < targetsSize; k++)
893 {
894 AttachedObjectTarget currentTarget = targetList.get(k);
895 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance();
896
897 if ((forValue != null && forValue.equals(currentTarget.getName())) &&
898 ((currentTarget instanceof ActionSource2AttachedObjectTarget &&
899 currentHandler instanceof ActionSource2AttachedObjectHandler) ||
900 (currentTarget instanceof EditableValueHolderAttachedObjectTarget &&
901 currentHandler instanceof EditableValueHolderAttachedObjectHandler) ||
902 (currentTarget instanceof ValueHolderAttachedObjectTarget &&
903 currentHandler instanceof ValueHolderAttachedObjectHandler)))
904 {
905
906 List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
907 for (int l = 0, targetsCount = targets.size(); l < targetsCount; l++)
908 {
909 UIComponent component = targets.get(l);
910
911
912
913
914 if (UIComponent.isCompositeComponent(component))
915 {
916
917
918
919
920
921
922
923
924
925
926
927
928 mctx.addAttachedObjectHandler(component, currentHandler);
929
930 List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
931
932 retargetAttachedObjects(context, component, handlers);
933
934 handlers.remove(currentHandler);
935 }
936 else
937 {
938 currentHandler.applyAttachedObject(context, component);
939 }
940 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
941 {
942 component.markInitialState();
943 }
944 }
945 }
946 else if ((currentTarget instanceof BehaviorHolderAttachedObjectTarget &&
947 currentHandler instanceof BehaviorHolderAttachedObjectHandler))
948 {
949 String eventName = ((BehaviorHolderAttachedObjectHandler) currentHandler).getEventName();
950 boolean isDefaultEvent = ((BehaviorHolderAttachedObjectTarget) currentTarget).isDefaultEvent();
951
952 if ((eventName != null && eventName.equals(currentTarget.getName())) ||
953 (eventName == null && isDefaultEvent))
954 {
955 List<UIComponent> targets = currentTarget.getTargets(topLevelComponent);
956 for (int j = 0, targetssize = targets.size(); j < targetssize; j++)
957 {
958 UIComponent component = targets.get(j);
959
960
961
962
963 if (UIComponent.isCompositeComponent(component))
964 {
965 if (currentTarget instanceof ClientBehaviorAttachedObjectTarget)
966 {
967 mctx.addAttachedObjectHandler(component,
968 new ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper(
969 (BehaviorHolderAttachedObjectHandler) currentHandler,
970 ((ClientBehaviorAttachedObjectTarget) currentTarget).getEvent()));
971 }
972 else
973 {
974 mctx.addAttachedObjectHandler(component, currentHandler);
975 }
976
977 List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(component);
978
979 retargetAttachedObjects(context, component, handlers);
980
981 handlers.remove(currentHandler);
982 }
983 else
984 {
985 if (currentHandler instanceof
986 ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper)
987 {
988 currentHandler.applyAttachedObject(context,
989 new ClientBehaviorRedirectEventComponentWrapper(component,
990 ((ClientBehaviorRedirectBehaviorAttachedObjectHandlerWrapper)
991 currentHandler).getWrappedEventName(), eventName));
992 }
993 else
994 {
995 currentHandler.applyAttachedObject(context, component);
996 }
997 }
998 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
999 {
1000 component.markInitialState();
1001 }
1002 }
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1009 @Override
1010 public void retargetMethodExpressions(FacesContext context, UIComponent topLevelComponent)
1011 {
1012 checkNull(context, "context");
1013
1014 BeanInfo compositeComponentMetadata
1015 = (BeanInfo) topLevelComponent.getAttributes().get(UIComponent.BEANINFO_KEY);
1016
1017 if (compositeComponentMetadata == null)
1018 {
1019 log.severe("Composite component metadata not found for: " + topLevelComponent.getClientId(context));
1020 return;
1021 }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 PropertyDescriptor[] propertyDescriptors = compositeComponentMetadata.getPropertyDescriptors();
1032
1033 ELContext elContext = (ELContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
1034
1035 for (PropertyDescriptor propertyDescriptor : propertyDescriptors)
1036 {
1037 if (propertyDescriptor.getValue("type") != null)
1038 {
1039
1040
1041 continue;
1042 }
1043
1044 String attributeName = propertyDescriptor.getName();
1045
1046
1047
1048
1049
1050 ValueExpression methodSignatureExpression
1051 = (ValueExpression) propertyDescriptor.getValue("method-signature");
1052 String methodSignature = null;
1053 if (methodSignatureExpression != null)
1054 {
1055
1056
1057 methodSignature = (String) methodSignatureExpression.getValue(elContext);
1058 }
1059
1060 String targetAttributeName = null;
1061 ValueExpression targetAttributeNameVE
1062 = (ValueExpression) propertyDescriptor.getValue("targetAttributeName");
1063 if (targetAttributeNameVE != null)
1064 {
1065 targetAttributeName = (String) targetAttributeNameVE.getValue(context.getELContext());
1066 if (targetAttributeName == null)
1067 {
1068 targetAttributeName = attributeName;
1069 }
1070 }
1071 else
1072 {
1073 targetAttributeName = attributeName;
1074 }
1075
1076 boolean isKnownTargetAttributeMethod
1077 = "action".equals(targetAttributeName) || "actionListener".equals(targetAttributeName)
1078 || "validator".equals(targetAttributeName) || "valueChangeListener".equals(targetAttributeName);
1079
1080
1081 if (isKnownTargetAttributeMethod || methodSignature != null)
1082 {
1083 ValueExpression targetsExpression =
1084 (ValueExpression) propertyDescriptor.getValue("targets");
1085
1086 String targets = null;
1087
1088
1089 if (targetsExpression != null)
1090 {
1091 targets = (String) targetsExpression.getValue(elContext);
1092 }
1093
1094 if (targets == null)
1095 {
1096
1097
1098 targets = attributeName;
1099 }
1100
1101 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance();
1102
1103
1104
1105
1106 if (!mctx.isMethodExpressionAttributeApplied(topLevelComponent, attributeName))
1107 {
1108
1109 ValueExpression attributeNameValueExpression =
1110 (ValueExpression) topLevelComponent.getAttributes().get(attributeName);
1111
1112 if (attributeNameValueExpression == null)
1113 {
1114
1115
1116
1117 attributeNameValueExpression = (ValueExpression) propertyDescriptor.getValue("default");
1118 if (attributeNameValueExpression == null)
1119 {
1120
1121 ValueExpression ve = (ValueExpression) propertyDescriptor.getValue("required");
1122 if (ve != null)
1123 {
1124 Object requiredValue = ve.getValue(elContext);
1125 Boolean required = null;
1126 if (requiredValue instanceof Boolean)
1127 {
1128 required = (Boolean) requiredValue;
1129 }
1130 else
1131 {
1132 required = Boolean.getBoolean(requiredValue.toString());
1133 }
1134
1135 if (required != null && required.booleanValue())
1136 {
1137 if (log.isLoggable(Level.SEVERE))
1138 {
1139 log.severe("attributeValueExpression not found under the key \""
1140 + attributeName
1141 + "\". Looking for the next attribute");
1142 }
1143 }
1144 }
1145 continue;
1146 }
1147 }
1148
1149 String[] targetsArray = StringUtils.splitShortString(targets, ' ');
1150 String attributeExpressionString = attributeNameValueExpression.getExpressionString();
1151
1152
1153 boolean ccAttrMeRedirection =
1154 attributeNameValueExpression instanceof LocationValueExpression &&
1155 CompositeComponentELUtils.isCompositeComponentAttrsMethodExpression(
1156 attributeNameValueExpression.getExpressionString());
1157
1158 if (isKnownTargetAttributeMethod)
1159 {
1160
1161
1162
1163
1164
1165
1166 if ("action".equals(targetAttributeName))
1167 {
1168 applyActionMethodExpressionEL(context, elContext,
1169 topLevelComponent, attributeName,
1170 attributeExpressionString, attributeNameValueExpression,
1171 ccAttrMeRedirection);
1172 }
1173 else if ("actionListener".equals(targetAttributeName))
1174 {
1175 applyActionListenerMethodExpressionEL(context, elContext,
1176 topLevelComponent, attributeName,
1177 attributeExpressionString, attributeNameValueExpression,
1178 ccAttrMeRedirection);
1179 }
1180 else if ("validator".equals(targetAttributeName))
1181 {
1182 applyValidatorMethodExpressionEL(context, elContext,
1183 topLevelComponent, attributeName,
1184 attributeExpressionString, attributeNameValueExpression,
1185 ccAttrMeRedirection);
1186 }
1187 else if ("valueChangeListener".equals(targetAttributeName))
1188 {
1189 applyValueChangeListenerMethodExpressionEL(context, elContext,
1190 topLevelComponent, attributeName,
1191 attributeExpressionString, attributeNameValueExpression,
1192 ccAttrMeRedirection);
1193 }
1194
1195 UIComponent topLevelComponentBase =
1196 topLevelComponent.getFacet(UIComponent.COMPOSITE_FACET_NAME);
1197
1198 for (String target : targetsArray)
1199 {
1200 UIComponent innerComponent
1201 = ComponentSupport.findComponentChildOrFacetFrom(context, topLevelComponentBase,
1202 target);
1203
1204 if (innerComponent == null)
1205 {
1206 continue;
1207 }
1208
1209 if (isCompositeComponentRetarget(context, innerComponent, targetAttributeName))
1210 {
1211 innerComponent.getAttributes().put(targetAttributeName, attributeNameValueExpression);
1212
1213 mctx.clearMethodExpressionAttribute(innerComponent, targetAttributeName);
1214
1215 retargetMethodExpressions(context, innerComponent);
1216 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1217 {
1218 innerComponent.markInitialState();
1219 }
1220 }
1221 else
1222 {
1223 if ("action".equals(targetAttributeName))
1224 {
1225 applyActionMethodExpressionTarget(context, mctx, elContext,
1226 topLevelComponentBase, innerComponent,
1227 attributeName, targetAttributeName,
1228 attributeExpressionString, attributeNameValueExpression,
1229 ccAttrMeRedirection);
1230 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1231 {
1232 innerComponent.markInitialState();
1233 }
1234 }
1235 else if ("actionListener".equals(targetAttributeName))
1236 {
1237 applyActionListenerMethodExpressionTarget(context, mctx, elContext,
1238 topLevelComponentBase, innerComponent,
1239 attributeName, targetAttributeName,
1240 attributeExpressionString, attributeNameValueExpression,
1241 ccAttrMeRedirection);
1242 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1243 {
1244 innerComponent.markInitialState();
1245 }
1246 }
1247 else if ("validator".equals(targetAttributeName))
1248 {
1249 applyValidatorMethodExpressionTarget(context, mctx, elContext,
1250 topLevelComponentBase, innerComponent,
1251 attributeName, targetAttributeName,
1252 attributeExpressionString, attributeNameValueExpression,
1253 ccAttrMeRedirection);
1254 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1255 {
1256 innerComponent.markInitialState();
1257 }
1258 }
1259 else if ("valueChangeListener".equals(targetAttributeName))
1260 {
1261 applyValueChangeListenerMethodExpressionTarget(context, mctx, elContext,
1262 topLevelComponentBase, innerComponent,
1263 attributeName, targetAttributeName,
1264 attributeExpressionString, attributeNameValueExpression,
1265 ccAttrMeRedirection);
1266 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1267 {
1268 innerComponent.markInitialState();
1269 }
1270 }
1271 }
1272 }
1273 }
1274 else
1275 {
1276 MethodExpression methodExpression = null;
1277
1278
1279
1280
1281
1282
1283 methodSignature = methodSignature.trim();
1284 methodExpression = context.getApplication().getExpressionFactory().
1285 createMethodExpression(elContext,
1286 attributeExpressionString, _getReturnType(methodSignature),
1287 _getParameters(methodSignature));
1288
1289 methodExpression = reWrapMethodExpression(methodExpression, attributeNameValueExpression);
1290
1291 applyMethodExpression(context, mctx, elContext, topLevelComponent, attributeName,
1292 targetAttributeName, attributeNameValueExpression, methodExpression,
1293 ccAttrMeRedirection, targetsArray);
1294 }
1295 mctx.markMethodExpressionAttribute(topLevelComponent, attributeName);
1296 }
1297
1298
1299
1300 topLevelComponent.setValueExpression(attributeName, null);
1301 }
1302 }
1303 }
1304
1305 private void applyActionMethodExpressionEL(FacesContext context,
1306 ELContext elContext,
1307 UIComponent topLevelComponent,
1308 String attributeName,
1309 String attributeExpressionString,
1310 ValueExpression attributeNameValueExpression,
1311 boolean ccAttrMeRedirection)
1312 {
1313
1314 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1315 createMethodExpression(elContext,
1316 attributeExpressionString, null,
1317 EMPTY_CLASS_ARRAY), attributeNameValueExpression);
1318
1319
1320 if (!ccAttrMeRedirection)
1321 {
1322
1323 topLevelComponent.getAttributes().put(attributeName, methodExpression);
1324 }
1325
1326
1327 }
1328
1329 private void applyActionListenerMethodExpressionEL(FacesContext context,
1330 ELContext elContext,
1331 UIComponent topLevelComponent,
1332 String attributeName,
1333 String attributeExpressionString,
1334 ValueExpression attributeNameValueExpression,
1335 boolean ccAttrMeRedirection)
1336 {
1337
1338 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1339 createMethodExpression(elContext,
1340 attributeExpressionString, Void.TYPE, ACTION_LISTENER_SIGNATURE),
1341 attributeNameValueExpression);
1342
1343 MethodExpression methodExpression2 = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1344 createMethodExpression(elContext,
1345 attributeExpressionString, Void.TYPE, EMPTY_CLASS_ARRAY),
1346 attributeNameValueExpression);
1347
1348
1349 if (!ccAttrMeRedirection)
1350 {
1351
1352 topLevelComponent.getAttributes().put(attributeName,
1353 new MethodExpressionMethodExpression(methodExpression, methodExpression2));
1354 }
1355
1356
1357 }
1358
1359 private void applyValidatorMethodExpressionEL(FacesContext context,
1360 ELContext elContext,
1361 UIComponent topLevelComponent,
1362 String attributeName,
1363 String attributeExpressionString,
1364 ValueExpression attributeNameValueExpression,
1365 boolean ccAttrMeRedirection)
1366 {
1367
1368 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1369 createMethodExpression(elContext,
1370 attributeExpressionString, Void.TYPE,
1371 VALIDATOR_SIGNATURE), attributeNameValueExpression);
1372
1373
1374 if (!ccAttrMeRedirection)
1375 {
1376
1377 topLevelComponent.getAttributes().put(attributeName, methodExpression);
1378 }
1379
1380
1381 }
1382
1383 private void applyValueChangeListenerMethodExpressionEL(FacesContext context,
1384 ELContext elContext,
1385 UIComponent topLevelComponent,
1386 String attributeName,
1387 String attributeExpressionString,
1388 ValueExpression attributeNameValueExpression,
1389 boolean ccAttrMeRedirection)
1390 {
1391
1392 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1393 createMethodExpression(elContext,
1394 attributeExpressionString, Void.TYPE,
1395 VALUE_CHANGE_LISTENER_SIGNATURE), attributeNameValueExpression);
1396
1397 MethodExpression methodExpression2 = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1398 createMethodExpression(elContext,
1399 attributeExpressionString, Void.TYPE,
1400 EMPTY_CLASS_ARRAY), attributeNameValueExpression);
1401
1402
1403 if (!ccAttrMeRedirection)
1404 {
1405
1406 topLevelComponent.getAttributes().put(attributeName,
1407 new MethodExpressionMethodExpression(methodExpression, methodExpression2));
1408 }
1409
1410 }
1411
1412 private void applyActionMethodExpressionTarget(FacesContext context, FaceletCompositionContext mctx,
1413 ELContext elContext,
1414 UIComponent topLevelComponent,
1415 UIComponent innerComponent,
1416 String attributeName,
1417 String targetAttributeName,
1418 String attributeExpressionString,
1419 ValueExpression attributeNameValueExpression,
1420 boolean ccAttrMeRedirection)
1421 {
1422
1423 MethodExpression methodExpression
1424 = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1425 createMethodExpression(elContext,
1426 attributeExpressionString, null, EMPTY_CLASS_ARRAY),
1427 attributeNameValueExpression);
1428
1429
1430
1431 if (ccAttrMeRedirection)
1432 {
1433 ((ActionSource2) innerComponent).setActionExpression(
1434 new ValueExpressionMethodExpression(attributeNameValueExpression));
1435 }
1436 else
1437 {
1438 ((ActionSource2) innerComponent).setActionExpression(methodExpression);
1439 }
1440 }
1441
1442 private void applyActionListenerMethodExpressionTarget(FacesContext context, FaceletCompositionContext mctx,
1443 ELContext elContext,
1444 UIComponent topLevelComponent,
1445 UIComponent innerComponent,
1446 String attributeName,
1447 String targetAttributeName,
1448 String attributeExpressionString,
1449 ValueExpression attributeNameValueExpression,
1450 boolean ccAttrMeRedirection)
1451 {
1452
1453 ActionListener o = (ActionListener)
1454 mctx.removeMethodExpressionTargeted(innerComponent, targetAttributeName);
1455 if (o != null)
1456 {
1457 ((ActionSource2) innerComponent).removeActionListener(o);
1458 }
1459
1460
1461 ActionListener actionListener = null;
1462
1463
1464 if (ccAttrMeRedirection)
1465 {
1466 actionListener = new RedirectMethodExpressionValueExpressionActionListener(
1467 attributeNameValueExpression);
1468 }
1469 else
1470 {
1471 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1472 createMethodExpression(elContext,
1473 attributeExpressionString, Void.TYPE, ACTION_LISTENER_SIGNATURE), attributeNameValueExpression);
1474
1475 MethodExpression methodExpression2 = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1476 createMethodExpression(elContext,
1477 attributeExpressionString, Void.TYPE, EMPTY_CLASS_ARRAY), attributeNameValueExpression);
1478
1479 if (mctx.isUsingPSSOnThisView())
1480 {
1481 actionListener = new PartialMethodExpressionActionListener(methodExpression, methodExpression2);
1482 }
1483 else
1484 {
1485 actionListener = new MethodExpressionActionListener(methodExpression, methodExpression2);
1486 }
1487 }
1488 ((ActionSource2) innerComponent).addActionListener(actionListener);
1489 mctx.addMethodExpressionTargeted(innerComponent, targetAttributeName, actionListener);
1490 }
1491
1492 private void applyValidatorMethodExpressionTarget(FacesContext context, FaceletCompositionContext mctx,
1493 ELContext elContext,
1494 UIComponent topLevelComponent,
1495 UIComponent innerComponent,
1496 String attributeName,
1497 String targetAttributeName,
1498 String attributeExpressionString,
1499 ValueExpression attributeNameValueExpression,
1500 boolean ccAttrMeRedirection)
1501 {
1502
1503 Validator o = (Validator) mctx.removeMethodExpressionTargeted(innerComponent, targetAttributeName);
1504 if (o != null)
1505 {
1506 ((EditableValueHolder) innerComponent).removeValidator(o);
1507 }
1508
1509
1510 Validator validator = null;
1511
1512 if (ccAttrMeRedirection)
1513 {
1514 validator = new RedirectMethodExpressionValueExpressionValidator(attributeNameValueExpression);
1515 }
1516 else
1517 {
1518 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1519 createMethodExpression(elContext,
1520 attributeExpressionString, Void.TYPE,
1521 VALIDATOR_SIGNATURE), attributeNameValueExpression);
1522
1523 if (mctx.isUsingPSSOnThisView())
1524 {
1525 validator = new PartialMethodExpressionValidator(methodExpression);
1526 }
1527 else
1528 {
1529 validator = new MethodExpressionValidator(methodExpression);
1530 }
1531 }
1532 ((EditableValueHolder) innerComponent).addValidator(validator);
1533 mctx.addMethodExpressionTargeted(innerComponent, targetAttributeName, validator);
1534 }
1535
1536 private void applyValueChangeListenerMethodExpressionTarget(FacesContext context, FaceletCompositionContext mctx,
1537 ELContext elContext,
1538 UIComponent topLevelComponent,
1539 UIComponent innerComponent,
1540 String attributeName,
1541 String targetAttributeName,
1542 String attributeExpressionString,
1543 ValueExpression attributeNameValueExpression,
1544 boolean ccAttrMeRedirection)
1545 {
1546 ValueChangeListener o = (ValueChangeListener) mctx.removeMethodExpressionTargeted(
1547 innerComponent, targetAttributeName);
1548 if (o != null)
1549 {
1550 ((EditableValueHolder) innerComponent).removeValueChangeListener(o);
1551 }
1552
1553
1554 ValueChangeListener valueChangeListener = null;
1555
1556 if (ccAttrMeRedirection)
1557 {
1558 valueChangeListener = new RedirectMethodExpressionValueExpressionValueChangeListener(
1559 attributeNameValueExpression);
1560 }
1561 else
1562 {
1563 MethodExpression methodExpression = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1564 createMethodExpression(elContext,
1565 attributeExpressionString, Void.TYPE,
1566 VALUE_CHANGE_LISTENER_SIGNATURE), attributeNameValueExpression);
1567
1568 MethodExpression methodExpression2 = reWrapMethodExpression(context.getApplication().getExpressionFactory().
1569 createMethodExpression(elContext,
1570 attributeExpressionString, Void.TYPE,
1571 EMPTY_CLASS_ARRAY), attributeNameValueExpression);
1572
1573 if (mctx.isUsingPSSOnThisView())
1574 {
1575 valueChangeListener = new PartialMethodExpressionValueChangeListener(
1576 methodExpression, methodExpression2);
1577 }
1578 else
1579 {
1580 valueChangeListener = new MethodExpressionValueChangeListener(methodExpression, methodExpression2);
1581 }
1582 }
1583 ((EditableValueHolder) innerComponent).addValueChangeListener(valueChangeListener);
1584 mctx.addMethodExpressionTargeted(innerComponent, targetAttributeName, valueChangeListener);
1585 }
1586
1587 private void applyMethodExpression(FacesContext context, FaceletCompositionContext mctx,
1588 ELContext elContext,
1589 UIComponent topLevelComponent,
1590 String attributeName,
1591 String targetAttributeName,
1592 ValueExpression attributeNameValueExpression,
1593 MethodExpression methodExpression,
1594 boolean ccAttrMeRedirection,
1595 String[] targetsArray)
1596 {
1597 UIComponent topLevelComponentBase = topLevelComponent.getFacet(
1598 UIComponent.COMPOSITE_FACET_NAME);
1599
1600 for (String target : targetsArray)
1601 {
1602 UIComponent innerComponent = ComponentSupport.findComponentChildOrFacetFrom(context,
1603 topLevelComponentBase, target);
1604
1605 if (innerComponent == null)
1606 {
1607 continue;
1608 }
1609
1610
1611
1612 if (isCompositeComponentRetarget(context, innerComponent, targetAttributeName))
1613 {
1614 innerComponent.getAttributes().put(targetAttributeName, attributeNameValueExpression);
1615
1616 mctx.clearMethodExpressionAttribute(innerComponent, targetAttributeName);
1617
1618 retargetMethodExpressions(context, innerComponent);
1619
1620 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1621 {
1622
1623 innerComponent.markInitialState();
1624 }
1625 }
1626 else
1627 {
1628
1629 if (ccAttrMeRedirection)
1630 {
1631
1632
1633 innerComponent.getAttributes().put(targetAttributeName,
1634 new ValueExpressionMethodExpression(attributeNameValueExpression));
1635 }
1636 else
1637 {
1638 innerComponent.getAttributes().put(targetAttributeName, methodExpression);
1639 }
1640 if (mctx.isUsingPSSOnThisView() && mctx.isMarkInitialState())
1641 {
1642 innerComponent.markInitialState();
1643 }
1644 }
1645 }
1646
1647 if (!ccAttrMeRedirection)
1648 {
1649
1650 topLevelComponent.getAttributes().put(attributeName, methodExpression);
1651 }
1652
1653
1654 }
1655
1656
1657 private boolean isCompositeComponentRetarget(FacesContext context, UIComponent component, String attributeName)
1658 {
1659 if (UIComponent.isCompositeComponent(component))
1660 {
1661 BeanInfo compositeComponentMetadata = (BeanInfo) component.getAttributes().get(UIComponent.BEANINFO_KEY);
1662
1663 PropertyDescriptor[] propertyDescriptors = compositeComponentMetadata.getPropertyDescriptors();
1664
1665 ELContext elContext = (ELContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
1666
1667 for (PropertyDescriptor propertyDescriptor : propertyDescriptors)
1668 {
1669 if (propertyDescriptor.getValue("type") != null)
1670 {
1671
1672
1673 continue;
1674 }
1675
1676 if (attributeName.equals(propertyDescriptor.getName()))
1677 {
1678
1679
1680
1681
1682
1683 ValueExpression methodSignatureExpression
1684 = (ValueExpression) propertyDescriptor.getValue("method-signature");
1685 String methodSignature = null;
1686 if (methodSignatureExpression != null)
1687 {
1688
1689
1690 methodSignature = (String) methodSignatureExpression.getValue(elContext);
1691 }
1692
1693 String targetAttributeName = null;
1694 ValueExpression targetAttributeNameVE = (ValueExpression)
1695 propertyDescriptor.getValue("targetAttributeName");
1696 if (targetAttributeNameVE != null)
1697 {
1698 targetAttributeName = (String) targetAttributeNameVE.getValue(context.getELContext());
1699 if (targetAttributeName == null)
1700 {
1701 targetAttributeName = attributeName;
1702 }
1703 }
1704 else
1705 {
1706 targetAttributeName = attributeName;
1707 }
1708
1709 boolean isKnownTargetAttributeMethod = "action".equals(targetAttributeName)
1710 || "actionListener".equals(targetAttributeName)
1711 || "validator".equals(targetAttributeName)
1712 || "valueChangeListener".equals(targetAttributeName);
1713
1714
1715 if (isKnownTargetAttributeMethod || methodSignature != null)
1716 {
1717 if ("action".equals(targetAttributeName))
1718 {
1719 return !(component instanceof ActionSource2);
1720 }
1721 else if ("actionListener".equals(targetAttributeName))
1722 {
1723 return !(component instanceof ActionSource2);
1724 }
1725 else if ("validator".equals(targetAttributeName))
1726 {
1727 return !(component instanceof EditableValueHolder);
1728 }
1729 else if ("valueChangeListener".equals(targetAttributeName))
1730 {
1731 return !(component instanceof EditableValueHolder);
1732 }
1733 else
1734 {
1735 return true;
1736 }
1737 }
1738 }
1739 }
1740 return false;
1741 }
1742 else
1743 {
1744 return false;
1745 }
1746 }
1747
1748 @SuppressWarnings("unchecked")
1749 private MethodExpression reWrapMethodExpression(MethodExpression createdMethodExpression,
1750 ValueExpression originalValueExpression)
1751 {
1752 if (originalValueExpression instanceof LocationValueExpression)
1753 {
1754 return new LocationMethodExpression(
1755 ((LocationValueExpression) originalValueExpression).getLocation(),
1756 reWrapMethodExpression(createdMethodExpression,
1757 ((LocationValueExpression) originalValueExpression).getWrapped()),
1758 ((LocationValueExpression) originalValueExpression).getCCLevel());
1759 }
1760 else if (originalValueExpression instanceof FacesWrapper &&
1761 ((FacesWrapper) originalValueExpression).getWrapped() instanceof ValueExpression)
1762 {
1763 return reWrapMethodExpression(createdMethodExpression,
1764 (ValueExpression) ((FacesWrapper) originalValueExpression).getWrapped());
1765 }
1766 else
1767 {
1768 return createdMethodExpression;
1769 }
1770 }
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781 public static Class _javaTypeToClass(String type)
1782 throws ClassNotFoundException
1783 {
1784 if (type == null)
1785 {
1786 throw new NullPointerException("type");
1787 }
1788
1789
1790 Class clazz = (Class) ClassUtils.COMMON_TYPES.get(type);
1791 if (clazz != null)
1792 {
1793 return clazz;
1794 }
1795
1796 int len = type.length();
1797 if (len > 2 && type.charAt(len - 1) == ']' && type.charAt(len - 2) == '[')
1798 {
1799 String componentType = type.substring(0, len - 2);
1800 Class componentTypeClass = ClassUtils.classForName(componentType);
1801 return Array.newInstance(componentTypeClass, 0).getClass();
1802 }
1803
1804 if (type.indexOf('.') == -1)
1805 {
1806 type = "java.lang." + type;
1807 }
1808 return ClassUtils.classForName(type);
1809 }
1810
1811 private Class _getReturnType(String signature)
1812 {
1813 int endName = signature.indexOf('(');
1814 if (endName < 0)
1815 {
1816 throw new FacesException("Invalid method signature:" + signature);
1817 }
1818 int end = signature.lastIndexOf(' ', endName);
1819 if (end < 0)
1820 {
1821 throw new FacesException("Invalid method signature:" + signature);
1822 }
1823 try
1824 {
1825 return _javaTypeToClass(signature.substring(0, end));
1826 }
1827 catch (ClassNotFoundException e)
1828 {
1829 throw new FacesException("Invalid method signature:" + signature);
1830 }
1831 }
1832
1833
1834
1835
1836
1837
1838 private Class[] _getParameters(String signature) throws FacesException
1839 {
1840 ArrayList<Class> params = new ArrayList<Class>();
1841
1842
1843
1844 int start = signature.indexOf('(') + 1;
1845 boolean lastArg = false;
1846 while (true)
1847 {
1848 int p = signature.indexOf(',', start);
1849 if (p < 0)
1850 {
1851 p = signature.indexOf(')', start);
1852 if (p < 0)
1853 {
1854 throw new FacesException("Invalid method signature:" + signature);
1855 }
1856 lastArg = true;
1857 }
1858 String arg = signature.substring(start, p).trim();
1859 if (!"".equals(arg))
1860 {
1861 try
1862 {
1863 params.add(_javaTypeToClass(arg));
1864 }
1865 catch (ClassNotFoundException e)
1866 {
1867 throw new FacesException("Invalid method signature:" + signature);
1868 }
1869 }
1870 if (lastArg)
1871 {
1872 break;
1873 }
1874 start = p + 1;
1875 }
1876 return params.toArray(new Class[params.size()]);
1877 }
1878
1879
1880
1881
1882 @Override
1883 public Resource getScriptComponentResource(FacesContext context, Resource componentResource)
1884 {
1885 checkNull(context, "context");
1886 checkNull(componentResource, "componentResource");
1887
1888 return null;
1889 }
1890
1891
1892
1893
1894 @Override
1895 public StateManagementStrategy getStateManagementStrategy(FacesContext context, String viewId)
1896 {
1897
1898
1899 if (_partialStateSaving && _stateMgmtStrategy == null)
1900 {
1901 _stateMgmtStrategy = new DefaultFaceletsStateManagementStrategy();
1902 }
1903
1904 return _usePartialStateSavingOnThisView(viewId) ? _stateMgmtStrategy : null;
1905 }
1906
1907
1908
1909
1910 @Override
1911 public ViewMetadata getViewMetadata(FacesContext context, String viewId)
1912 {
1913 checkNull(viewId, "viewId");
1914 return new FaceletViewMetadata(viewId);
1915 }
1916
1917
1918
1919
1920 @Override
1921 public void renderView(FacesContext context, UIViewRoot view) throws IOException
1922 {
1923 if (!view.isRendered())
1924 {
1925 return;
1926 }
1927
1928
1929 if (log.isLoggable(Level.FINE))
1930 {
1931 log.fine("Rendering View: " + view.getViewId());
1932 }
1933
1934 try
1935 {
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951 ResponseWriter origWriter = createResponseWriter(context);
1952
1953 ExternalContext extContext = context.getExternalContext();
1954 Writer outputWriter = extContext.getResponseOutputWriter();
1955
1956 StateWriter stateWriter = new StateWriter(outputWriter, 1024, context);
1957 try
1958 {
1959 ResponseWriter writer = origWriter.cloneWithWriter(stateWriter);
1960 try
1961 {
1962 context.setResponseWriter(writer);
1963
1964 StateManager stateMgr = context.getApplication().getStateManager();
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 writer.startDocument();
1980
1981 view.encodeAll(context);
1982
1983 writer.endDocument();
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993 if (stateWriter.isStateWritten())
1994 {
1995
1996
1997
1998
1999
2000
2001 writer.flush();
2002
2003
2004
2005
2006
2007 Object stateObj = stateMgr.saveView(context);
2008 String content = stateWriter.getAndResetBuffer();
2009 int end = content.indexOf(STATE_KEY);
2010
2011
2012 if (end >= 0)
2013 {
2014
2015 String stateStr;
2016 if (stateObj == null)
2017 {
2018 stateStr = null;
2019 }
2020 else
2021 {
2022 stateMgr.writeState(context, stateObj);
2023 stateStr = stateWriter.getAndResetBuffer();
2024 }
2025
2026 int start = 0;
2027
2028 while (end != -1)
2029 {
2030 origWriter.write(content, start, end - start);
2031 if (stateStr != null)
2032 {
2033 origWriter.write(stateStr);
2034 }
2035 start = end + STATE_KEY_LEN;
2036 end = content.indexOf(STATE_KEY, start);
2037 }
2038
2039 origWriter.write(content, start, content.length() - start);
2040
2041
2042 }
2043 else
2044 {
2045 origWriter.write(content);
2046 }
2047 }
2048 else if (stateWriter.isStateWrittenWithoutWrapper())
2049 {
2050
2051
2052 stateMgr.saveView(context);
2053 }
2054 }
2055 finally
2056 {
2057
2058 writer.close();
2059 }
2060 }
2061 finally
2062 {
2063 stateWriter.release(context);
2064 }
2065 }
2066 catch (FileNotFoundException fnfe)
2067 {
2068 handleFaceletNotFound(context, view.getViewId());
2069 }
2070 catch (Exception e)
2071 {
2072 handleRenderException(context, e);
2073 }
2074 }
2075
2076
2077
2078
2079 @Override
2080 public UIViewRoot createView(FacesContext context, String viewId)
2081 {
2082 checkNull(viewId, "viewId");
2083
2084 if (UIDebug.debugRequest(context))
2085 {
2086
2087
2088
2089
2090 return null;
2091 }
2092 else
2093 {
2094 return super.createView(context, viewId);
2095 }
2096 }
2097
2098
2099
2100
2101 @Override
2102 public UIViewRoot restoreView(FacesContext context, String viewId)
2103 {
2104 checkNull(viewId, "viewId");
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118 return super.restoreView(context, viewId);
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150 }
2151
2152
2153
2154
2155 @Override
2156 protected String calculateViewId(FacesContext context, String viewId)
2157 {
2158 if (_cachedViewHandlerSupport == null)
2159 {
2160 _cachedViewHandlerSupport = new DefaultViewHandlerSupport();
2161 }
2162
2163 return _cachedViewHandlerSupport.calculateViewId(context, viewId);
2164 }
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174 protected Compiler createCompiler(FacesContext context)
2175 {
2176 Compiler compiler = new SAXCompiler();
2177
2178 compiler.setDevelopmentProjectStage(context.isProjectStage(ProjectStage.Development));
2179
2180 loadLibraries(context, compiler);
2181 loadDecorators(context, compiler);
2182 loadOptions(context, compiler);
2183
2184 compiler.setFaceletsProcessingConfigurations(
2185 RuntimeConfig.getCurrentInstance(
2186 context.getExternalContext()).getFaceletProcessingConfigurations());
2187
2188 return compiler;
2189 }
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201 protected FaceletFactory createFaceletFactory(FacesContext context, Compiler compiler)
2202 {
2203 ExternalContext eContext = context.getExternalContext();
2204
2205
2206 long refreshPeriod;
2207 if (context.isProjectStage(ProjectStage.Production))
2208 {
2209 refreshPeriod = WebConfigParamUtils.getLongInitParameter(eContext, PARAMS_REFRESH_PERIOD,
2210 DEFAULT_REFRESH_PERIOD_PRODUCTION);
2211 }
2212 else
2213 {
2214 refreshPeriod = WebConfigParamUtils.getLongInitParameter(eContext, PARAMS_REFRESH_PERIOD,
2215 DEFAULT_REFRESH_PERIOD);
2216 }
2217
2218
2219 ResourceResolver resolver = new DefaultResourceResolver();
2220 String faceletsResourceResolverClassName = WebConfigParamUtils.getStringInitParameter(eContext,
2221 PARAMS_RESOURCE_RESOLVER, null);
2222 if (faceletsResourceResolverClassName != null)
2223 {
2224 ArrayList<String> classNames = new ArrayList<String>(1);
2225 classNames.add(faceletsResourceResolverClassName);
2226 resolver = ClassUtils.buildApplicationObject(ResourceResolver.class, classNames, resolver);
2227 }
2228
2229 _resourceResolver = resolver;
2230
2231 return new DefaultFaceletFactory(compiler, resolver, refreshPeriod);
2232 }
2233
2234
2235 protected ResponseWriter createResponseWriter(FacesContext context) throws IOException, FacesException
2236 {
2237 ExternalContext extContext = context.getExternalContext();
2238 RenderKit renderKit = context.getRenderKit();
2239
2240
2241 if (renderKit == null)
2242 {
2243 String id = context.getViewRoot().getRenderKitId();
2244 throw new IllegalStateException("No render kit was available for id \"" + id + "\"");
2245 }
2246
2247
2248 if (_bufferSize != -1)
2249 {
2250 extContext.setResponseBufferSize(_bufferSize);
2251 }
2252
2253
2254 String contentType = (String) context.getAttributes().get("facelets.ContentType");
2255
2256
2257 String encoding = (String) context.getAttributes().get("facelets.Encoding");
2258
2259
2260
2261
2262
2263
2264 * to the contentType so createResponseWriter will succeed no matter
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286 contentType = getResponseContentType(context, writer.getContentType());
2287 encoding = getResponseEncoding(context, writer.getCharacterEncoding());
2288
2289
2290 extContext.setResponseContentType(contentType + "; charset=" + encoding);
2291
2292
2293
2294
2295
2296 writer = writer.cloneWithWriter(extContext.getResponseOutputWriter());
2297
2298 return writer;
2299 }
2300
2301
2302
2303
2304 @Deprecated
2305 protected String getDefaultSuffix(FacesContext context) throws FacesException
2306 {
2307 if (_defaultSuffix == null)
2308 {
2309 ExternalContext eContext = context.getExternalContext();
2310
2311 String viewSuffix = eContext.getInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME);
2312
2313 _defaultSuffix = viewSuffix == null ? ViewHandler.DEFAULT_FACELETS_SUFFIX : viewSuffix;
2314 }
2315
2316 return _defaultSuffix;
2317 }
2318
2319
2320
2321
2322 @Deprecated
2323 protected String getRenderedViewId(FacesContext context, String actionId)
2324 {
2325
2326
2327
2328 return actionId;
2329 }
2330
2331
2332
2333
2334
2335
2336
2337
2338 protected String getResponseContentType(FacesContext context, String orig)
2339 {
2340 String contentType = orig;
2341
2342
2343 Map<Object, Object> m = context.getAttributes();
2344 if (m.containsKey("facelets.ContentType"))
2345 {
2346 contentType = (String) m.get("facelets.ContentType");
2347 if (log.isLoggable(Level.FINEST))
2348 {
2349 log.finest("Facelet specified alternate contentType '" + contentType + "'");
2350 }
2351 }
2352
2353
2354 if (contentType == null)
2355 {
2356 contentType = "text/html";
2357 log.finest("ResponseWriter created had a null ContentType, defaulting to text/html");
2358 }
2359
2360 return contentType;
2361 }
2362
2363
2364
2365
2366
2367
2368
2369
2370 protected String getResponseEncoding(FacesContext context, String orig)
2371 {
2372 String encoding = orig;
2373
2374
2375 Map<Object, Object> m = context.getAttributes();
2376 Map<String, Object> sm = context.getExternalContext().getSessionMap();
2377
2378
2379 if (m.containsKey(PARAM_ENCODING))
2380 {
2381 encoding = (String) m.get(PARAM_ENCODING);
2382 if (encoding != null && log.isLoggable(Level.FINEST))
2383 {
2384 log.finest("Facelet specified alternate encoding '" + encoding + "'");
2385 }
2386
2387 sm.put(CHARACTER_ENCODING_KEY, encoding);
2388 }
2389
2390
2391 Object request = context.getExternalContext().getRequest();
2392 if (encoding == null)
2393 {
2394 encoding = context.getExternalContext().getRequestCharacterEncoding();
2395 }
2396
2397
2398 if (encoding == null)
2399 {
2400 encoding = (String) sm.get(CHARACTER_ENCODING_KEY);
2401 if (encoding != null && log.isLoggable(Level.FINEST))
2402 {
2403 log.finest("Session specified alternate encoding '" + encoding + "'");
2404 }
2405 }
2406
2407
2408 if (encoding == null)
2409 {
2410 encoding = DEFAULT_CHARACTER_ENCODING;
2411 if (log.isLoggable(Level.FINEST))
2412 {
2413 log.finest("ResponseWriter created had a null CharacterEncoding, defaulting to " + encoding);
2414 }
2415 }
2416
2417 return encoding;
2418 }
2419
2420 protected void handleFaceletNotFound(FacesContext context, String viewId) throws FacesException, IOException
2421 {
2422 String actualId = context.getApplication().getViewHandler().getActionURL(context, viewId);
2423 context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND, actualId);
2424 context.responseComplete();
2425
2426 }
2427
2428 protected void handleRenderException(FacesContext context, Exception e)
2429 throws IOException, ELException, FacesException
2430 {
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446 if (e instanceof RuntimeException)
2447 {
2448 throw (RuntimeException) e;
2449 }
2450 else if (e instanceof IOException)
2451 {
2452 throw (IOException) e;
2453 }
2454 else
2455 {
2456 throw new FacesException(e.getMessage(), e);
2457 }
2458 }
2459
2460
2461
2462
2463 protected void initialize(FacesContext context)
2464 {
2465 log.finest("Initializing");
2466
2467 Compiler compiler = createCompiler(context);
2468
2469 _faceletFactory = createFaceletFactory(context, compiler);
2470
2471 ExternalContext eContext = context.getExternalContext();
2472 _initializeBuffer(eContext);
2473 _initializeMode(eContext);
2474
2475
2476
2477 MyfacesConfig mfConfig = MyfacesConfig.getCurrentInstance(eContext);
2478 if (mfConfig.getComponentUniqueIdsCacheSize() > 0)
2479 {
2480 String[] componentIdsCached = SectionUniqueIdCounter.generateUniqueIdCache("_",
2481 mfConfig.getComponentUniqueIdsCacheSize());
2482 eContext.getApplicationMap().put(
2483 CACHED_COMPONENT_IDS, componentIdsCached);
2484 }
2485
2486 log.finest("Initialization Successful");
2487 }
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497 protected void loadDecorators(FacesContext context, Compiler compiler)
2498 {
2499 String param = WebConfigParamUtils.getStringInitParameter(context.getExternalContext(), PARAMS_DECORATORS);
2500 if (param != null)
2501 {
2502 for (String decorator : param.split(";"))
2503 {
2504 try
2505 {
2506 compiler.addTagDecorator((TagDecorator) ReflectionUtil.forName(decorator).newInstance());
2507 if (log.isLoggable(Level.FINE))
2508 {
2509 log.fine("Successfully loaded decorator: " + decorator);
2510 }
2511 }
2512 catch (Exception e)
2513 {
2514 log.log(Level.SEVERE, "Error Loading decorator: " + decorator, e);
2515 }
2516 }
2517 }
2518 }
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528 protected void loadLibraries(FacesContext context, Compiler compiler)
2529 {
2530 ExternalContext eContext = context.getExternalContext();
2531
2532 compiler.addTagLibrary(new CoreLibrary());
2533 compiler.addTagLibrary(new HtmlLibrary());
2534 compiler.addTagLibrary(new UILibrary());
2535 compiler.addTagLibrary(new JstlCoreLibrary());
2536 compiler.addTagLibrary(new JstlFnLibrary());
2537 compiler.addTagLibrary(new CompositeLibrary());
2538 compiler.addTagLibrary(new CompositeResourceLibrary(context));
2539
2540 String param = WebConfigParamUtils.getStringInitParameter(eContext, PARAMS_LIBRARIES);
2541 if (param != null)
2542 {
2543 for (String library : param.split(";"))
2544 {
2545 try
2546 {
2547 URL src = eContext.getResource(library.trim());
2548 if (src == null)
2549 {
2550 throw new FileNotFoundException(library);
2551 }
2552
2553 TagLibrary tl = TagLibraryConfig.create(context, src);
2554 if (tl != null)
2555 {
2556 compiler.addTagLibrary(tl);
2557 }
2558 if (log.isLoggable(Level.FINE))
2559 {
2560 log.fine("Successfully loaded library: " + library);
2561 }
2562 }
2563 catch (IOException e)
2564 {
2565 log.log(Level.SEVERE, "Error Loading library: " + library, e);
2566 }
2567 }
2568 }
2569 }
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579 protected void loadOptions(FacesContext context, Compiler compiler)
2580 {
2581 ExternalContext eContext = context.getExternalContext();
2582
2583
2584 compiler.setTrimmingComments(WebConfigParamUtils.getBooleanInitParameter(
2585 eContext, PARAMS_SKIP_COMMENTS, false));
2586 }
2587
2588
2589
2590
2591 @Override
2592 protected void sendSourceNotFound(FacesContext context, String message)
2593 {
2594 try
2595 {
2596 context.responseComplete();
2597 context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND, message);
2598 }
2599 catch (IOException ioe)
2600 {
2601 throw new FacesException(ioe);
2602 }
2603 }
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616 private Facelet _getFacelet(String viewId) throws IOException
2617 {
2618
2619 FaceletFactory.setInstance(_faceletFactory);
2620 try
2621 {
2622 return _faceletFactory.getFacelet(viewId);
2623 }
2624 finally
2625 {
2626 FaceletFactory.setInstance(null);
2627 }
2628 }
2629
2630 private Facelet _getViewMetadataFacelet(String viewId) throws IOException
2631 {
2632
2633 FaceletFactory.setInstance(_faceletFactory);
2634 try
2635 {
2636 return _faceletFactory.getViewMetadataFacelet(viewId);
2637 }
2638 finally
2639 {
2640 FaceletFactory.setInstance(null);
2641 }
2642 }
2643
2644
2645 private void _initializeBuffer(ExternalContext context)
2646 {
2647 _bufferSize = WebConfigParamUtils.getIntegerInitParameter(context, PARAMS_BUFFER_SIZE, -1);
2648 }
2649
2650 private void _initializeMode(ExternalContext context)
2651 {
2652 String facesVersion = RuntimeConfig.getCurrentInstance(context).getFacesVersion();
2653 boolean partialStateSavingDefault;
2654
2655
2656
2657
2658 partialStateSavingDefault = "2.0".equals(facesVersion) || "2.1".equals(facesVersion);
2659
2660
2661
2662 _partialStateSaving = WebConfigParamUtils.getBooleanInitParameter(context,
2663 StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, partialStateSavingDefault);
2664
2665 String[] viewIds = StringUtils.splitShortString(WebConfigParamUtils.getStringInitParameter(context,
2666 StateManager.FULL_STATE_SAVING_VIEW_IDS_PARAM_NAME), ',');
2667
2668 if (viewIds.length > 0)
2669 {
2670 _viewIds = new HashSet<String>(viewIds.length, 1.0f);
2671 Collections.addAll(_viewIds, viewIds);
2672 }
2673 else
2674 {
2675 _viewIds = null;
2676 }
2677
2678 if (_partialStateSaving)
2679 {
2680 _refreshTransientBuildOnPSS = MyfacesConfig.getCurrentInstance(context).isRefreshTransientBuildOnPSS();
2681
2682 _refreshTransientBuildOnPSSAuto
2683 = MyfacesConfig.getCurrentInstance(context).isRefreshTransientBuildOnPSSAuto();
2684
2685 _markInitialStateWhenApplyBuildView = WebConfigParamUtils.getBooleanInitParameter(context,
2686 PARAM_MARK_INITIAL_STATE_WHEN_APPLY_BUILD_VIEW, false);
2687 }
2688 }
2689
2690 private boolean _usePartialStateSavingOnThisView(String viewId)
2691 {
2692 return _partialStateSaving && !(_viewIds != null && _viewIds.contains(viewId));
2693 }
2694
2695 private class FaceletViewMetadata extends ViewMetadataBase
2696 {
2697
2698
2699
2700
2701
2702 public FaceletViewMetadata(String viewId)
2703 {
2704 super(viewId);
2705 }
2706
2707
2708
2709
2710 @Override
2711 public UIViewRoot createMetadataView(FacesContext context)
2712 {
2713 try
2714 {
2715 context.setProcessingEvents(false);
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736 context.getAttributes().put(BUILDING_VIEW_METADATA, Boolean.TRUE);
2737
2738
2739
2740
2741
2742 UIViewRoot view = context.getApplication().getViewHandler().createView(context, getViewId());
2743
2744 if (view != null)
2745 {
2746
2747
2748
2749 Facelet facelet = _getViewMetadataFacelet(view.getViewId());
2750 facelet.apply(context, view);
2751 }
2752
2753 return view;
2754 }
2755 catch (IOException ioe)
2756 {
2757 throw new FacesException(ioe);
2758 }
2759 finally
2760 {
2761 context.getAttributes().remove(BUILDING_VIEW_METADATA);
2762
2763 context.setProcessingEvents(true);
2764 }
2765 }
2766 }
2767
2768 }