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.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.view.AttachedObjectTarget;
30  import javax.faces.view.facelets.FaceletContext;
31  import javax.faces.view.facelets.TagAttribute;
32  import javax.faces.view.facelets.TagConfig;
33  import javax.faces.view.facelets.TagHandler;
34  
35  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
36  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
37  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
38  
39  /**
40   * composite:actionSource, composite:valueHolder and composite:editableValueHolder
41   * do the same: register an AttachedObjectTarget on the "targetList" mentioned on
42   * ViewDeclarationLanguage.retargetAttachedObjects. AttachedObjectTargetHandler group the
43   * common behavior
44   * 
45   * @author Leonardo Uribe (latest modification by $Author: jakobk $)
46   * @version $Revision: 960906 $ $Date: 2010-07-06 09:45:40 -0500 (Tue, 06 Jul 2010) $
47   */
48  @JSFFaceletTag
49  public abstract class AttachedObjectTargetHandler<T extends AttachedObjectTarget> 
50      extends TagHandler implements InterfaceDescriptorCreator
51  {
52  
53      //private static final Log log = LogFactory.getLog(AttachedObjectTargetHandler.class);
54      private static final Logger log = Logger.getLogger(AttachedObjectTargetHandler.class.getName());
55      
56      /**
57       * Indicate the name of the attribute that the component should expose
58       * to page authors.
59       * 
60       */
61      @JSFFaceletAttribute(name="name",
62              className="javax.el.ValueExpression",
63              deferredValueType="java.lang.String",
64              required=true)
65      protected final TagAttribute _name;
66  
67      /**
68       * 
69       */
70      @JSFFaceletAttribute(name="targets",
71              className="javax.el.ValueExpression",
72              deferredValueType="java.lang.String")
73      protected final TagAttribute _targets;
74  
75      /**
76       * Check if the PropertyDescriptor instance created by this handler
77       * can be cacheable or not. 
78       */
79      private boolean _cacheable;
80      
81      private AttachedObjectTarget _target;
82  
83      public AttachedObjectTargetHandler(TagConfig config)
84      {
85          super(config);
86          _name = getRequiredAttribute("name");
87          _targets = getAttribute("targets");
88          if (_name.isLiteral())
89          {
90              _cacheable = true;
91          }
92          else
93          {
94              _cacheable = false;
95          }
96      }
97  
98      @SuppressWarnings("unchecked")
99      public void apply(FaceletContext ctx, UIComponent parent)
100             throws IOException
101     {
102         UIComponent compositeBaseParent = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
103 
104         CompositeComponentBeanInfo beanInfo = 
105             (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
106             .get(UIComponent.BEANINFO_KEY);
107         
108         if (beanInfo == null)
109         {
110             if (log.isLoggable(Level.SEVERE))
111             {
112                 log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
113             }
114             return;
115         }
116         
117         BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); 
118         
119         //1. Obtain the list mentioned as "targetList" on ViewDeclarationLanguage.retargetAttachedObjects
120         List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>)
121             beanDescriptor.getValue(
122                     AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
123         
124         if (targetList == null)
125         {
126             //2. If not found create it and set
127             targetList = new ArrayList<AttachedObjectTarget>();
128             beanDescriptor.setValue(
129                     AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY,
130                     targetList);
131         }
132         
133         //3. Create the instance of AttachedObjectTarget
134         if (isCacheable())
135         {
136             if (_target == null)
137             {
138                 _target = createAttachedObjectTarget(ctx);
139             }
140             targetList.add(_target);
141         }
142         else
143         {
144             AttachedObjectTarget target = createAttachedObjectTarget(ctx);
145             targetList.add(target);
146         }
147         
148         this.nextHandler.apply(ctx, parent);
149     }
150     
151     public boolean isCacheable()
152     {
153         return _cacheable;
154     }
155     
156     public void setCacheable(boolean cacheable)
157     {
158         _cacheable = cacheable;
159     }
160     
161     //@Override
162     //public FaceletHandler getNextHandler()
163     //{
164     //    return nextHandler;
165     //}
166 
167     /**
168      * Create a new AttachedObjectTarget instance to be added on the 
169      * target list.
170      * 
171      * @return
172      */
173     protected abstract T createAttachedObjectTarget(FaceletContext ctx);    
174 }