View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.composite;
20  
21  import java.beans.BeanDescriptor;
22  import java.beans.BeanInfo;
23  import java.beans.PropertyDescriptor;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import javax.el.ELException;
33  import javax.el.ValueExpression;
34  import javax.el.VariableMapper;
35  import javax.faces.FacesException;
36  import javax.faces.application.ProjectStage;
37  import javax.faces.application.Resource;
38  import javax.faces.component.ActionSource;
39  import javax.faces.component.EditableValueHolder;
40  import javax.faces.component.UIComponent;
41  import javax.faces.component.UIPanel;
42  import javax.faces.component.UniqueIdVendor;
43  import javax.faces.component.ValueHolder;
44  import javax.faces.context.FacesContext;
45  import javax.faces.view.AttachedObjectHandler;
46  import javax.faces.view.ViewDeclarationLanguage;
47  import javax.faces.view.facelets.ComponentConfig;
48  import javax.faces.view.facelets.ComponentHandler;
49  import javax.faces.view.facelets.FaceletContext;
50  import javax.faces.view.facelets.FaceletException;
51  import javax.faces.view.facelets.FaceletHandler;
52  import javax.faces.view.facelets.MetaRuleset;
53  import javax.faces.view.facelets.Metadata;
54  import javax.faces.view.facelets.TagException;
55  import javax.faces.view.facelets.TextHandler;
56  
57  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
58  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
59  import org.apache.myfaces.view.facelets.TemplateClient;
60  import org.apache.myfaces.view.facelets.TemplateContext;
61  import org.apache.myfaces.view.facelets.el.VariableMapperWrapper;
62  import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler;
63  import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
64  import org.apache.myfaces.view.facelets.tag.jsf.ActionSourceRule;
65  import org.apache.myfaces.view.facelets.tag.jsf.ComponentBuilderHandler;
66  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
67  import org.apache.myfaces.view.facelets.tag.jsf.EditableValueHolderRule;
68  import org.apache.myfaces.view.facelets.tag.jsf.ValueHolderRule;
69  import org.apache.myfaces.view.facelets.tag.jsf.core.AjaxHandler;
70  
71  /**
72   * This handler is responsible for apply composite components. It
73   * is created by CompositeResourceLibrary class when a composite component
74   * is found.
75   * 
76   * @author Leonardo Uribe (latest modification by $Author: lu4242 $)
77   * @version $Revision: 1306829 $ $Date: 2012-03-29 08:03:06 -0500 (Thu, 29 Mar 2012) $
78   */
79  public class CompositeComponentResourceTagHandler extends ComponentHandler
80      implements ComponentBuilderHandler, TemplateClient
81  {
82      private final Resource _resource;
83      
84      private Metadata _mapper;
85      
86      private Class<?> _lastType = Object.class;
87      
88      protected volatile Map<String, FaceletHandler> _facetHandlersMap;
89      
90      protected final Collection<FaceletHandler> _componentHandlers;
91      
92      protected final Collection<FaceletHandler> _facetHandlers;
93      
94      public CompositeComponentResourceTagHandler(ComponentConfig config, Resource resource)
95      {
96          super(config);
97          _resource = resource;
98          _facetHandlers = TagHandlerUtils.findNextByType(nextHandler, javax.faces.view.facelets.FacetHandler.class,
99                                                          InsertFacetHandler.class);
100         _componentHandlers = TagHandlerUtils.findNextByType(nextHandler,
101                 javax.faces.view.facelets.ComponentHandler.class,
102                 ComponentContainerHandler.class, TextHandler.class);
103     }
104 
105     public UIComponent createComponent(FaceletContext ctx)
106     {
107         FacesContext facesContext = ctx.getFacesContext();
108         UIComponent component = facesContext.getApplication().createComponent(facesContext, _resource);
109         
110         // Check required attributes if the app is not on production stage. 
111         // Unfortunately, we can't check it on constructor because we need to call
112         // ViewDeclarationLanguage.getComponentMetadata() and on that point it is possible to not
113         // have a viewId.
114         if (!facesContext.isProjectStage(ProjectStage.Production))
115         {
116             BeanInfo beanInfo = (BeanInfo) component.getAttributes().get(UIComponent.BEANINFO_KEY);
117             for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors())
118             {
119                 ValueExpression ve = (ValueExpression) propertyDescriptor.getValue("required");
120                 if (ve != null)
121                 {
122                     Object value = ve.getValue (facesContext.getELContext());
123                     Boolean required = null;
124                     if (value instanceof Boolean)
125                     {
126                         required = (Boolean) value;
127                     }
128                     else
129                     {
130                         required = Boolean.getBoolean(value.toString());
131                     } 
132                     
133                     if (required != null && required.booleanValue())
134                     {
135                         Object attrValue = this.tag.getAttributes().get (propertyDescriptor.getName());
136                         
137                         if (attrValue == null)
138                         {
139                             throw new TagException(this.tag, "Attribute '" + propertyDescriptor.getName()
140                                                              + "' is required");
141                         }
142                     }
143                 }
144             }
145         }
146         return component;
147     }
148 
149     @SuppressWarnings("unchecked")
150     @Override
151     public void applyNextHandler(FaceletContext ctx, UIComponent c)
152             throws IOException
153     {
154         //super.applyNextHandler(ctx, c);
155         
156         applyNextHandlerIfNotApplied(ctx, c);
157         
158         applyCompositeComponentFacelet(ctx,c);
159         
160         if (ComponentHandler.isNew(c))
161         {
162             FacesContext facesContext = ctx.getFacesContext();
163             
164             ViewDeclarationLanguage vdl = facesContext.getApplication().getViewHandler().
165                 getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
166 
167             FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
168             List<AttachedObjectHandler> handlers = mctx.getAttachedObjectHandlers(c);
169             
170             if (handlers != null)
171             {
172                 vdl.retargetAttachedObjects(facesContext, c, handlers);
173                 
174                 // remove the list of handlers, as it is no longer necessary
175                 mctx.removeAttachedObjectHandlers(c);
176             }
177             
178             vdl.retargetMethodExpressions(facesContext, c);
179             
180             if ( FaceletCompositionContext.getCurrentInstance(ctx).isMarkInitialState())
181             {
182                 // Call it only if we are using partial state saving
183                 c.markInitialState();
184                 // Call it to other components created not bound by a tag handler
185                 c.getFacet(UIComponent.COMPOSITE_FACET_NAME).markInitialState();
186             }            
187         }
188     }
189     
190     @SuppressWarnings("unchecked")
191     protected void applyNextHandlerIfNotApplied(FaceletContext ctx, UIComponent c)
192         throws IOException
193     {
194         //Apply all facelets not applied yet.
195         
196         CompositeComponentBeanInfo beanInfo = 
197             (CompositeComponentBeanInfo) c.getAttributes().get(UIComponent.BEANINFO_KEY);
198         
199         BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
200 
201         boolean insertChildrenUsed = (beanDescriptor.getValue(InsertChildrenHandler.INSERT_CHILDREN_USED) != null);
202         
203         List<String> insertFacetList = (List<String>) beanDescriptor.getValue(InsertFacetHandler.INSERT_FACET_USED);
204         
205         if (nextHandler instanceof javax.faces.view.facelets.CompositeFaceletHandler)
206         {
207             for (FaceletHandler handler :
208                     ((javax.faces.view.facelets.CompositeFaceletHandler)nextHandler).getHandlers())
209             {
210                 if (handler instanceof javax.faces.view.facelets.FacetHandler)
211                 {
212                     if (insertFacetList == null ||
213                         !insertFacetList.contains(((javax.faces.view.facelets.FacetHandler)handler).getFacetName(ctx)))
214                     {
215                         handler.apply(ctx, c);
216                     }
217                 }
218                 else if (handler instanceof InsertFacetHandler)
219                 {
220                     if (insertFacetList == null ||
221                         !insertFacetList.contains( ((InsertFacetHandler)handler).getFacetName(ctx)))
222                     {
223                         handler.apply(ctx, c);
224                     }
225                 }
226                 else if (insertChildrenUsed)
227                 {
228                     if (!(handler instanceof javax.faces.view.facelets.ComponentHandler ||
229                             handler instanceof ComponentContainerHandler ||
230                             handler instanceof TextHandler))
231                     {
232                         handler.apply(ctx, c);
233                     }
234                 }
235                 else
236                 {
237                     handler.apply(ctx, c);
238                 }
239             }
240         }
241         else
242         {
243             if (nextHandler instanceof javax.faces.view.facelets.FacetHandler)
244             {
245                 if (insertFacetList == null ||
246                     !insertFacetList.contains(((javax.faces.view.facelets.FacetHandler)nextHandler).getFacetName(ctx)))
247                 {
248                     nextHandler.apply(ctx, c);
249                 }
250             }
251             else if (nextHandler instanceof InsertFacetHandler)
252             {
253                 if (insertFacetList == null ||
254                     !insertFacetList.contains( ((InsertFacetHandler)nextHandler).getFacetName(ctx)) )
255                 {
256                     nextHandler.apply(ctx, c);
257                 }
258             }
259             else if (insertChildrenUsed)
260             {
261                 if (!(nextHandler instanceof javax.faces.view.facelets.ComponentHandler ||
262                       nextHandler instanceof ComponentContainerHandler ||
263                       nextHandler instanceof TextHandler))
264                 {
265                     nextHandler.apply(ctx, c);
266                 }
267             }
268             else
269             {
270                 nextHandler.apply(ctx, c);
271             }
272         }
273         
274         //Check for required facets
275         Map<String, PropertyDescriptor> facetPropertyDescriptorMap = (Map<String, PropertyDescriptor>)
276             beanDescriptor.getValue(UIComponent.FACETS_KEY);
277         
278         if (facetPropertyDescriptorMap != null)
279         {
280             List<String> facetsRequiredNotFound = null;
281             for (Map.Entry<String, PropertyDescriptor> entry : facetPropertyDescriptorMap.entrySet())
282             {
283                 ValueExpression requiredExpr = (ValueExpression) entry.getValue().getValue("required");
284                 if (requiredExpr != null)
285                 {
286                     Boolean required = (Boolean) requiredExpr.getValue(ctx.getFacesContext().getELContext());
287                     if (Boolean.TRUE.equals(required))
288                     {
289                         initFacetHandlersMap(ctx);
290                         if (!_facetHandlersMap.containsKey(entry.getKey()))
291                         {
292                             if (facetsRequiredNotFound == null)
293                             {
294                                 facetsRequiredNotFound = new ArrayList(facetPropertyDescriptorMap.size());
295                             }
296                             facetsRequiredNotFound.add(entry.getKey());
297                         }
298                         
299                     }
300                 }
301             }
302             if (facetsRequiredNotFound != null && !facetsRequiredNotFound.isEmpty())
303             {
304                 throw new TagException(getTag(), "The following facets are required by the component: "
305                                                  + facetsRequiredNotFound);
306             }
307         }
308     }
309     
310     protected void applyCompositeComponentFacelet(FaceletContext faceletContext, UIComponent compositeComponentBase) 
311         throws IOException
312     {
313         FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(faceletContext);
314         AbstractFaceletContext actx = (AbstractFaceletContext) faceletContext;
315         UIPanel compositeFacetPanel
316                 = (UIPanel) compositeComponentBase.getFacets().get(UIComponent.COMPOSITE_FACET_NAME);
317         if (compositeFacetPanel == null)
318         {
319             compositeFacetPanel = (UIPanel)
320                 faceletContext.getFacesContext().getApplication().createComponent(UIPanel.COMPONENT_TYPE);
321             compositeComponentBase.getFacets().put(UIComponent.COMPOSITE_FACET_NAME, compositeFacetPanel);
322             
323             // Set an id to the created facet component, to prevent id generation and make
324             // partial state saving work without problem.
325             UniqueIdVendor uniqueIdVendor = mctx.getUniqueIdVendorFromStack();
326             if (uniqueIdVendor == null)
327             {
328                 uniqueIdVendor = ComponentSupport.getViewRoot(faceletContext, compositeComponentBase);
329             }
330             if (uniqueIdVendor != null)
331             {
332                 // UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
333                 // and call createUniqueId()
334                 String uid = uniqueIdVendor.createUniqueId(faceletContext.getFacesContext(),
335                         mctx.getSharedStringBuilder()
336                         .append(compositeComponentBase.getId())
337                         .append("__f_")
338                         .append("cc_facet").toString());
339                 compositeFacetPanel.setId(uid);
340             }            
341         }
342         
343         // Before call applyCompositeComponent we need to add ajax behaviors
344         // to the current compositeComponentBase. Note that super.applyNextHandler()
345         // has already been called, but this point is before vdl.retargetAttachedObjects,
346         // so we can't but this on ComponentTagHandlerDelegate, if we want this to be
347         // applied correctly.
348         Iterator<AjaxHandler> it = ((AbstractFaceletContext) faceletContext).getAjaxHandlers();
349         if (it != null)
350         {
351             while(it.hasNext())
352             {
353                 mctx.addAttachedObjectHandler(
354                         compositeComponentBase, it.next());
355             }
356         }    
357         
358         VariableMapper orig = faceletContext.getVariableMapper();
359         try
360         {
361             faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
362             actx.pushCompositeComponentClient(this);
363             Resource resourceForCurrentView = faceletContext.getFacesContext().getApplication().
364                 getResourceHandler().createResource(_resource.getResourceName(), _resource.getLibraryName());
365             if (resourceForCurrentView != null)
366             {
367                 //Wrap it for serialization.
368                 resourceForCurrentView = new CompositeResouceWrapper(resourceForCurrentView);
369             }
370             else
371             {
372                 //If a resource cannot be resolved it means a default for the current 
373                 //composite component does not exists.
374                 throw new TagException(getTag(), "Composite Component " + getTag().getQName() 
375                         + " requires a default instance that can be found by the installed ResourceHandler.");
376             }
377             actx.applyCompositeComponent(compositeFacetPanel, resourceForCurrentView);
378         }
379         finally
380         {
381             actx.popCompositeComponentClient();
382             faceletContext.setVariableMapper(orig);
383         }
384     }
385 
386     @Override
387     public void setAttributes(FaceletContext ctx, Object instance)
388     {
389         if (instance != null)
390         {
391             UIComponent component = (UIComponent) instance;
392 
393             Class<?> type = instance.getClass();
394             if (_mapper == null || !_lastType.equals(type))
395             {
396                 _lastType = type;
397                 BeanInfo beanInfo = (BeanInfo)component.getAttributes().get(UIComponent.BEANINFO_KEY);
398                 _mapper = createMetaRuleset(type , beanInfo).finish();
399             }
400             
401             _mapper.applyMetadata(ctx, instance);
402         }        
403     }
404 
405     protected MetaRuleset createMetaRuleset(Class<?> type, BeanInfo beanInfo)
406     {
407         MetaRuleset m = new CompositeMetaRulesetImpl(this.getTag(), type, beanInfo);
408         // ignore standard component attributes
409         m.ignore("binding").ignore("id");
410 
411         // add auto wiring for attributes
412         m.addRule(CompositeComponentRule.Instance);
413         
414         // add retarget method expression rules
415         m.addRule(RetargetMethodExpressionRule.Instance);
416         
417         if (ActionSource.class.isAssignableFrom(type))
418         {
419             m.addRule(ActionSourceRule.Instance);
420         }
421 
422         if (ValueHolder.class.isAssignableFrom(type))
423         {
424             m.addRule(ValueHolderRule.Instance);
425 
426             if (EditableValueHolder.class.isAssignableFrom(type))
427             {
428                 m.ignore("submittedValue");
429                 m.ignore("valid");
430                 m.addRule(EditableValueHolderRule.Instance);
431             }
432         }
433         
434         return m;
435     }
436     
437     private void initFacetHandlersMap(FaceletContext ctx)
438     {
439         if (_facetHandlersMap == null)
440         {
441             Map<String, FaceletHandler> map = new HashMap<String, FaceletHandler>();
442             
443             for (FaceletHandler handler : _facetHandlers)
444             {
445                 if (handler instanceof javax.faces.view.facelets.FacetHandler )
446                 {
447                     map.put( ((javax.faces.view.facelets.FacetHandler)handler).getFacetName(ctx), handler);
448                 }
449                 else if (handler instanceof InsertFacetHandler)
450                 {
451                     map.put( ((InsertFacetHandler)handler).getFacetName(ctx), handler);
452                 }
453             }
454             _facetHandlersMap = map;
455         }
456     }
457     
458     public boolean apply(FaceletContext ctx, UIComponent parent, String name)
459             throws IOException, FacesException, FaceletException, ELException
460     {        
461         if (name != null)
462         {
463             //1. Initialize map used to retrieve facets
464             if (_facetHandlers == null || _facetHandlers.isEmpty())
465             {
466                 checkFacetRequired(ctx, parent, name);
467                 return true;
468             }
469 
470             initFacetHandlersMap(ctx);
471 
472             FaceletHandler handler = _facetHandlersMap.get(name);
473 
474             if (handler != null)
475             {
476                 AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
477                 // Pop the current composite component on stack, so #{cc} references
478                 // can be resolved correctly, because they are relative to the page 
479                 // that define it.
480                 FaceletCompositionContext fcc = actx.getFaceletCompositionContext(); 
481                 UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack(); 
482                 fcc.popCompositeComponentToStack();
483                 // Pop the template context, so ui:xx tags and nested composite component
484                 // cases could work correctly 
485                 TemplateContext itc = actx.popTemplateContext();
486                 try
487                 {
488                     handler.apply(ctx, parent);
489                 }
490                 finally
491                 {
492                     actx.pushTemplateContext(itc);
493                     fcc.pushCompositeComponentToStack(innerCompositeComponent);
494                 }
495                 return true;
496                 
497             }
498             else
499             {
500                 checkFacetRequired(ctx, parent, name);
501                 return true;
502             }
503         }
504         else
505         {
506             AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
507             // Pop the current composite component on stack, so #{cc} references
508             // can be resolved correctly, because they are relative to the page 
509             // that define it.
510             FaceletCompositionContext fcc = actx.getFaceletCompositionContext(); 
511             UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack(); 
512             fcc.popCompositeComponentToStack();
513             // Pop the template context, so ui:xx tags and nested composite component
514             // cases could work correctly 
515             TemplateContext itc = actx.popTemplateContext();
516             try
517             {
518                 for (FaceletHandler handler : _componentHandlers)
519                 {
520                     handler.apply(ctx, parent);
521                 }
522             }
523             finally
524             {
525                 actx.pushTemplateContext(itc);
526                 fcc.pushCompositeComponentToStack(innerCompositeComponent);
527             }
528             return true;
529         }
530     }
531     
532     private void checkFacetRequired(FaceletContext ctx, UIComponent parent, String name)
533     {
534         AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
535         FaceletCompositionContext fcc = actx.getFaceletCompositionContext(); 
536         UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
537         
538         CompositeComponentBeanInfo beanInfo = 
539             (CompositeComponentBeanInfo) innerCompositeComponent.getAttributes()
540             .get(UIComponent.BEANINFO_KEY);
541         
542         BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
543         
544         Map<String, PropertyDescriptor> insertFacetPropertyDescriptorMap = (Map<String, PropertyDescriptor>)
545             beanDescriptor.getValue(InsertFacetHandler.INSERT_FACET_KEYS);
546 
547         if (insertFacetPropertyDescriptorMap != null && insertFacetPropertyDescriptorMap.containsKey(name))
548         {
549             ValueExpression requiredExpr
550                     = (ValueExpression) insertFacetPropertyDescriptorMap.get(name).getValue("required");
551             
552             if (requiredExpr != null &&
553                 Boolean.TRUE.equals(requiredExpr.getValue(ctx.getFacesContext().getELContext())))
554             {
555                 //Insert facet associated is required, but it was not applied.
556                 throw new TagException(this.tag, "Cannot find facet with name '"+name+"' in composite component");
557             }
558         }
559     }
560 }