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.IntrospectionException;
23  import java.beans.PropertyDescriptor;
24  import java.io.IOException;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.logging.Level;
28  import java.util.logging.Logger;
29  
30  import javax.faces.component.UIComponent;
31  import javax.faces.view.Location;
32  import javax.faces.view.facelets.FaceletContext;
33  import javax.faces.view.facelets.TagConfig;
34  import javax.faces.view.facelets.TagException;
35  import javax.faces.view.facelets.TagHandler;
36  
37  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
38  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
39  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
40  
41  /**
42   * @author Leonardo Uribe (latest modification by $Author: jakobk $)
43   * @version $Revision: 960906 $ $Date: 2010-07-06 09:45:40 -0500 (Tue, 06 Jul 2010) $
44   */
45  @JSFFaceletTag(name="composite:implementation")
46  public class ImplementationHandler extends TagHandler
47  {
48      //private static final Log log = LogFactory.getLog(ImplementationHandler.class);
49      private static final Logger log = Logger.getLogger(ImplementationHandler.class.getName());
50      
51      public final static String NAME = "implementation";
52  
53      public ImplementationHandler(TagConfig config)
54      {
55          super(config);
56      }
57  
58      public void apply(FaceletContext ctx, UIComponent parent)
59              throws IOException
60      {
61          if (!((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata())
62          {
63              // If this tag is found in a facelet, the compiler has trimmed all
64              // tags outside this one excluding composite:interface, so "parent"
65              // is a component used as value for the facet key
66              // UIComponent.COMPOSITE_FACET_NAME in a composite component. 
67              nextHandler.apply(ctx, parent);
68          }
69          else
70          {
71              UIComponent compositeBaseParent = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
72              // Register the facet UIComponent.COMPOSITE_FACET_NAME
73              CompositeComponentBeanInfo beanInfo = 
74                  (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
75                  .get(UIComponent.BEANINFO_KEY);
76              
77              if (beanInfo == null)
78              {
79                  if (log.isLoggable(Level.SEVERE))
80                  {
81                      log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
82                  }
83                  return;
84              }
85              
86              BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
87              
88              Map<String, PropertyDescriptor> facetPropertyDescriptorMap = 
89                  (Map<String, PropertyDescriptor>) beanDescriptor.getValue(UIComponent.FACETS_KEY);
90          
91              if (facetPropertyDescriptorMap == null)
92              {
93                  facetPropertyDescriptorMap = new HashMap<String, PropertyDescriptor>();
94                  beanDescriptor.setValue(UIComponent.FACETS_KEY, facetPropertyDescriptorMap);
95              }
96              
97              if (!facetPropertyDescriptorMap.containsKey(UIComponent.COMPOSITE_FACET_NAME))
98              {
99                  try
100                 {
101                     facetPropertyDescriptorMap.put(UIComponent.COMPOSITE_FACET_NAME, 
102                             new CompositeComponentPropertyDescriptor(UIComponent.COMPOSITE_FACET_NAME));
103                 }
104                 catch (IntrospectionException e)
105                 {
106                     if (log.isLoggable(Level.SEVERE))
107                     {
108                         log.log(Level.SEVERE, "Cannot create PropertyDescriptor for facet ",e);
109                     }
110                     throw new TagException(tag,e);
111                 }
112             }
113             
114             // Pass to nextHandler, to give the chance to cc:insertChildren, cc:insertFacet and cc:renderFacet
115             // to save metadata information.
116             nextHandler.apply(ctx, parent);
117         }
118     }
119     
120     public Location getLocation()
121     {
122         return this.tag.getLocation();
123     }
124 }