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 Logger log = Logger.getLogger(AttachedObjectTargetHandler.class.getName());
54      
55      /**
56       * Indicate the name of the attribute that the component should expose
57       * to page authors.
58       * 
59       */
60      @JSFFaceletAttribute(name="name",
61              className="javax.el.ValueExpression",
62              deferredValueType="java.lang.String",
63              required=true)
64      protected final TagAttribute _name;
65  
66      /**
67       * 
68       */
69      @JSFFaceletAttribute(name="targets",
70              className="javax.el.ValueExpression",
71              deferredValueType="java.lang.String")
72      protected final TagAttribute _targets;
73  
74      /**
75       * Check if the PropertyDescriptor instance created by this handler
76       * can be cacheable or not. 
77       */
78      private boolean _cacheable;
79      
80      private AttachedObjectTarget _target;
81  
82      public AttachedObjectTargetHandler(TagConfig config)
83      {
84          super(config);
85          _name = getRequiredAttribute("name");
86          _targets = getAttribute("targets");
87          if (_name.isLiteral())
88          {
89              _cacheable = true;
90          }
91          else
92          {
93              _cacheable = false;
94          }
95      }
96  
97      @SuppressWarnings("unchecked")
98      public void apply(FaceletContext ctx, UIComponent parent)
99              throws IOException
100     {
101         UIComponent compositeBaseParent
102                 = 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 }