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