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$)
46   * @version $Revision$ $Date$
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
103                 = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
104 
105         CompositeComponentBeanInfo beanInfo = 
106             (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
107             .get(UIComponent.BEANINFO_KEY);
108         
109         if (beanInfo == null)
110         {
111             if (log.isLoggable(Level.SEVERE))
112             {
113                 log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY ");
114             }
115             return;
116         }
117         
118         BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); 
119         
120         //1. Obtain the list mentioned as "targetList" on ViewDeclarationLanguage.retargetAttachedObjects
121         List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>)
122             beanDescriptor.getValue(
123                     AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
124         
125         if (targetList == null)
126         {
127             //2. If not found create it and set
128             targetList = new ArrayList<AttachedObjectTarget>();
129             beanDescriptor.setValue(
130                     AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY,
131                     targetList);
132         }
133         
134         //3. Create the instance of AttachedObjectTarget
135         if (isCacheable())
136         {
137             if (_target == null)
138             {
139                 _target = createAttachedObjectTarget(ctx);
140             }
141             targetList.add(_target);
142         }
143         else
144         {
145             AttachedObjectTarget target = createAttachedObjectTarget(ctx);
146             targetList.add(target);
147         }
148         
149         this.nextHandler.apply(ctx, parent);
150     }
151     
152     public boolean isCacheable()
153     {
154         return _cacheable;
155     }
156     
157     public void setCacheable(boolean cacheable)
158     {
159         _cacheable = cacheable;
160     }
161     
162     //@Override
163     //public FaceletHandler getNextHandler()
164     //{
165     //    return nextHandler;
166     //}
167 
168     /**
169      * Create a new AttachedObjectTarget instance to be added on the 
170      * target list.
171      * 
172      * @return
173      */
174     protected abstract T createAttachedObjectTarget(FaceletContext ctx);    
175 }