Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RestoreStateUtils |
|
| 4.0;4 |
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 | 0 | public class RestoreStateUtils |
31 | { | |
32 | //private static Log log = LogFactory.getLog(RestoreStateUtils.class); | |
33 | 0 | 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 | 0 | recursivelyHandleComponentReferencesAndSetValid(facesContext, parent, false); |
52 | 0 | } |
53 | ||
54 | public static void recursivelyHandleComponentReferencesAndSetValid(FacesContext facesContext, | |
55 | UIComponent parent, boolean forceHandle) | |
56 | { | |
57 | 0 | Method handleBindingsMethod = getBindingMethod(parent); |
58 | ||
59 | 0 | if(handleBindingsMethod!=null && !forceHandle) |
60 | { | |
61 | try | |
62 | { | |
63 | 0 | handleBindingsMethod.invoke(parent,new Object[]{}); |
64 | } | |
65 | 0 | catch (Throwable th) |
66 | { | |
67 | 0 | log.log(Level.SEVERE, "Exception while invoking handleBindings on component with client-id:" |
68 | +parent.getClientId(facesContext),th); | |
69 | 0 | } |
70 | } | |
71 | else | |
72 | { | |
73 | 0 | for (Iterator it = parent.getFacetsAndChildren(); it.hasNext(); ) |
74 | { | |
75 | 0 | UIComponent component = (UIComponent)it.next(); |
76 | ||
77 | 0 | ValueExpression binding = component.getValueExpression("binding"); //TODO: constant |
78 | 0 | if (binding != null) |
79 | { | |
80 | 0 | 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 | 0 | recursivelyHandleComponentReferencesAndSetValid(facesContext, component); |
90 | 0 | } |
91 | } | |
92 | 0 | } |
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 | 0 | Class[] clazzes = parent.getClass().getInterfaces(); |
102 | ||
103 | 0 | for (int i = 0; i < clazzes.length; i++) |
104 | { | |
105 | 0 | Class clazz = clazzes[i]; |
106 | ||
107 | 0 | if(clazz.getName().indexOf("BindingAware")!=-1) |
108 | { | |
109 | try | |
110 | { | |
111 | 0 | return parent.getClass().getMethod("handleBindings",new Class[]{}); |
112 | } | |
113 | 0 | catch (NoSuchMethodException e) |
114 | { | |
115 | // return | |
116 | } | |
117 | } | |
118 | } | |
119 | ||
120 | 0 | return null; |
121 | } | |
122 | } |