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  
20  package org.apache.myfaces.shared_orchestra.util;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import javax.faces.context.FacesContext;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIInput;
28  import javax.faces.el.ValueBinding;
29  import java.util.Iterator;
30  import java.lang.reflect.Method;
31  
32  /**
33   * @author Martin Marinschek (latest modification by $Author: grantsmith $)
34   * @version $Revision: 169655 $ $Date: 2005-05-11 18:45:06 +0200 (Mi, 11 Mai 2005) $
35   */
36  public class RestoreStateUtils
37  {
38      private static Log log = LogFactory.getLog(RestoreStateUtils.class);
39  
40      /**
41       * Walk the component tree, executing any component-bindings to reattach
42       * components to their backing beans. Also, any UIInput component is
43       * marked as Valid.
44       * <p>
45       *  Note that this method effectively breaks encapsulation; instead of
46       *  asking each component to update itself and its children, this
47       * method just reaches into each component. That makes it impossible
48       * for any component to customise its behaviour at this point.
49       * <p>
50       * This has been filed as an issue against the spec. Until this
51       * issue is resolved, we'll add a new marker-interface for components
52       * to allow them to define their interest in handling children bindings themselves.
53       */
54      public static void recursivelyHandleComponentReferencesAndSetValid(FacesContext facesContext,
55                                                                         UIComponent parent)
56      {
57          recursivelyHandleComponentReferencesAndSetValid(facesContext, parent, false);
58      }
59  
60      public static void recursivelyHandleComponentReferencesAndSetValid(FacesContext facesContext,
61                                                                         UIComponent parent, boolean forceHandle)
62      {
63          Method handleBindingsMethod = getBindingMethod(parent);
64  
65          if(handleBindingsMethod!=null && !forceHandle)
66          {
67              try
68              {
69                  handleBindingsMethod.invoke(parent,new Object[]{});
70              }
71              catch (Throwable th)
72              {
73                  log.error("Exception while invoking handleBindings on component with client-id:"
74                          +parent.getClientId(facesContext),th);
75              }
76          }
77          else
78          {
79              for (Iterator it = parent.getFacetsAndChildren(); it.hasNext(); )
80              {
81                  UIComponent component = (UIComponent)it.next();
82  
83                  ValueBinding binding = component.getValueBinding("binding");    //TODO: constant
84                  if (binding != null && !binding.isReadOnly(facesContext))
85                  {
86                      binding.setValue(facesContext, component);
87                  }
88  
89                  if (component instanceof UIInput)
90                  {
91                      ((UIInput)component).setValid(true);
92                  }
93  
94                  recursivelyHandleComponentReferencesAndSetValid(facesContext, component);
95              }
96          }
97      }
98  
99      /**This is all a hack to work around a spec-bug which will be fixed in JSF2.0
100      *
101      * @param parent
102      * @return true if this component is bindingAware (e.g. aliasBean)
103      */
104     private static Method getBindingMethod(UIComponent parent)
105     {
106         Class[] clazzes = parent.getClass().getInterfaces();
107 
108         for (int i = 0; i < clazzes.length; i++)
109         {
110             Class clazz = clazzes[i];
111 
112             if(clazz.getName().indexOf("BindingAware")!=-1)
113             {
114                 try
115                 {
116                     return  parent.getClass().getMethod("handleBindings",new Class[]{});
117                 }
118                 catch (NoSuchMethodException e)
119                 {
120                     // return
121                 }
122             }
123         }
124 
125         return null;
126     }
127 }