1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.faces.component;
21
22 import javax.faces.FacesException;
23 import javax.faces.component.StateHolder;
24 import javax.faces.context.FacesContext;
25 import javax.faces.el.EvaluationException;
26 import javax.faces.el.MethodBinding;
27 import javax.faces.event.AbortProcessingException;
28 import javax.faces.event.FacesEvent;
29
30
31
32
33
34
35 abstract class _MethodBindingToListener implements StateHolder {
36
37 protected MethodBinding methodBinding;
38
39 public _MethodBindingToListener() {
40 }
41
42
43
44
45 public _MethodBindingToListener(MethodBinding methodBinding) {
46 if (methodBinding == null) throw new NullPointerException("methodBinding can not be null");
47 if (!(methodBinding instanceof StateHolder)) throw new IllegalArgumentException("methodBinding must implement the StateHolder interface");
48
49 this.methodBinding = methodBinding;
50 }
51
52 private FacesContext getFacesContext() {
53 return FacesContext.getCurrentInstance();
54 }
55
56 protected void invokeMethodBinding(FacesEvent event) throws AbortProcessingException {
57 try {
58 methodBinding.invoke(getFacesContext(), new Object[] {event});
59 }
60 catch (EvaluationException e) {
61 Throwable cause = e.getCause();
62 if (cause != null && cause instanceof AbortProcessingException) {
63 throw (AbortProcessingException)cause;
64 }
65
66 throw e;
67 }
68 }
69
70 public MethodBinding getMethodBinding() {
71 return methodBinding;
72 }
73
74 public void restoreState(FacesContext context, Object state) {
75 Object[] stateArray = (Object[])state;
76 try {
77 methodBinding = (MethodBinding)Thread.currentThread()
78 .getContextClassLoader()
79 .loadClass((String)stateArray[0])
80 .newInstance();
81 } catch (Exception e) {
82 throw new FacesException(e);
83 }
84
85 ((StateHolder)methodBinding).restoreState(context, stateArray[1]);
86 }
87
88 public Object saveState(FacesContext context) {
89 Object[] stateArray = new Object[2];
90 stateArray[0] = methodBinding.getClass().getName();
91 stateArray[1] = ((StateHolder)methodBinding).saveState(context);
92 return stateArray;
93 }
94
95 public void setTransient(boolean newTransientValue) {
96 ((StateHolder)methodBinding).setTransient(newTransientValue);
97 }
98
99 public boolean isTransient() {
100 return ((StateHolder)methodBinding).isTransient();
101 }
102
103 }