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.io.IOException;
22  import java.util.Map;
23  import javax.faces.FacesException;
24  import javax.faces.component.StateHolder;
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.faces.event.ComponentSystemEvent;
28  import javax.faces.event.ComponentSystemEventListener;
29  import javax.faces.view.facelets.Facelet;
30  import org.apache.myfaces.config.RuntimeConfig;
31  import org.apache.myfaces.view.facelets.AbstractFacelet;
32  import org.apache.myfaces.view.facelets.DynamicComponentRefreshTransientBuildEvent;
33  import org.apache.myfaces.view.facelets.FaceletCompositionContext;
34  import org.apache.myfaces.view.facelets.FaceletDynamicComponentRefreshTransientBuildEvent;
35  import org.apache.myfaces.view.facelets.FaceletFactory;
36  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
37  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageBase;
38  import org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener;
39  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
40  
41  /**
42   * This listener must be attached to PostAddToViewEvent, so when composite component is
43   * added to the view, the algorithm that create the composite component content is executed.
44   *
45   * @author lu4242
46   */
47  public class CreateDynamicCompositeComponentListener 
48      implements ComponentSystemEventListener, StateHolder
49  {
50      private String taglibURI;
51      private String tagName;
52      private Map<String,Object> attributes;
53      private String baseKey;
54  
55      public CreateDynamicCompositeComponentListener(String taglibURI, String tagName, 
56          Map<String, Object> attributes, String baseKey)
57      {
58          this.taglibURI = taglibURI;
59          this.tagName = tagName;
60          this.attributes = attributes;
61          this.baseKey = baseKey;
62      }
63  
64      public CreateDynamicCompositeComponentListener()
65      {
66      }
67      
68      public void processEvent(ComponentSystemEvent event)
69      {
70          FacesContext facesContext = FacesContext.getCurrentInstance();
71          
72          FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) 
73              facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(
74                  facesContext, facesContext.getViewRoot().getViewId());
75          
76          Facelet componentFacelet;
77          FaceletFactory faceletFactory = vdl.getFaceletFactory();
78              
79          FaceletFactory.setInstance(faceletFactory);
80          try
81          {
82              componentFacelet
83                      = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
84          }
85          finally
86          {
87              FaceletFactory.setInstance(null);
88          }
89          
90          UIComponent component = event.getComponent(); 
91          // The execution of this listener activated another call to PostAddToViewEvent, because
92          // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
93          // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
94          // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
95          // have any side effect (counts as a refresh).
96          Integer step = (Integer) component.getAttributes().get(
97              CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW); 
98          if (step != null && step.intValue() == 0)
99          {
100             component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
101         }
102         else
103         {
104             return;
105         }
106         try
107         {
108             facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD,
109                 Boolean.TRUE);
110             
111             // Detect the relationship between parent and child, to ensure the component is properly created
112             // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a 
113             // dynamic component, we need to do it here before apply the handler
114             UIComponent parent = component.getParent();
115             String facetName = null;
116             if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component))
117             {
118                 facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
119             }
120             
121             
122             try
123             {
124                 if (facetName != null)
125                 {
126                     parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY, 
127                             facetName);
128                 }
129                 // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
130                 // find the component. Then we reset it to exclude it from facelets refresh algorithm.
131                 String markId = (String) component.getAttributes().get("oam.vf.GEN_MARK_ID");
132                 if (markId == null)
133                 {
134                     ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
135                         facesContext, component, baseKey);
136                 }
137                 else
138                 {
139                     try
140                     {
141                         component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
142                         ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
143                             facesContext, component.getParent(), baseKey);
144                     }
145                     finally
146                     {
147                         component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
148                     }
149                 }
150                 
151                 if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext))
152                 {
153                     FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
154                     if (fcc == null)
155                     {
156                         FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
157                         FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
158                         component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new 
159                             RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
160                         component.getAttributes().put(
161                             DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
162 
163                     }
164                     else
165                     {
166                         component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new 
167                             RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
168                     }
169                 }
170             }
171             finally
172             {
173                 if (facetName != null)
174                 {
175                     parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY);
176                 }
177             }
178         }
179         catch (IOException e)
180         {
181             throw new FacesException(e);
182         }
183         finally
184         {
185             facesContext.getAttributes().remove(
186                 FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
187         }
188     }
189 
190     public Object saveState(FacesContext context)
191     {
192         RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
193             context.getExternalContext());
194         Object[] values = new Object[4];
195         Integer tagId = runtimeConfig.getIdByNamespace().get(taglibURI);
196         if (tagId != null)
197         {
198             values[0] = tagId;
199         }
200         else if (taglibURI.startsWith(CompositeResourceLibrary.NAMESPACE_PREFIX))
201         {
202             values[0] = new Object[]{0, taglibURI.substring(35)};
203         }
204         else if(taglibURI.startsWith(CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX))
205         {
206             values[0] = new Object[]{1, taglibURI.substring(34)};
207         }
208         else
209         {
210             values[0] = taglibURI;
211         }
212         values[1] = tagName;
213         values[2] = attributes;
214         values[3] = baseKey;
215         return values;
216     }
217 
218     public void restoreState(FacesContext context, Object state)
219     {
220         Object[] values = (Object[]) state;
221         if (values[0] instanceof String)
222         {
223             taglibURI = (String) values[0];
224         }
225         else if (values[0] instanceof Integer)
226         {
227             RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
228                 context.getExternalContext());
229             taglibURI = runtimeConfig.getNamespaceById().get((Integer)values[0]);
230         }
231         else if (values[0] instanceof Object[])
232         {
233             Object[] def = (Object[])values[0];
234             String ns = ( ((Integer)def[0]).intValue() == 0) ? 
235                 CompositeResourceLibrary.NAMESPACE_PREFIX :
236                 CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX;
237             taglibURI = ns + (String)(((Object[])values[0])[1]);
238         }
239         tagName = (String)values[1];
240         attributes = (Map<String,Object>) values[2];
241         baseKey = (String)values[3];
242     }
243 
244     public boolean isTransient()
245     {
246         return false;
247     }
248 
249     public void setTransient(boolean newTransientValue)
250     {
251     }
252 
253 }