1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.event;
20
21 import javax.el.ELContext;
22 import javax.el.ELException;
23 import javax.el.ExpressionFactory;
24 import javax.el.MethodExpression;
25 import javax.el.MethodNotFoundException;
26 import javax.faces.component.StateHolder;
27 import javax.faces.context.FacesContext;
28
29
30
31
32
33
34
35 public class MethodExpressionValueChangeListener implements ValueChangeListener, StateHolder
36 {
37
38 private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
39 private static final Object[] EMPTY_PARAMS = new Object[0];
40
41 private MethodExpression methodExpressionOneArg;
42 private MethodExpression methodExpressionZeroArg;
43 private boolean isTransient = false;
44
45
46 public MethodExpressionValueChangeListener()
47 {
48
49 }
50
51 public MethodExpressionValueChangeListener(MethodExpression methodExpressionOneArg)
52 {
53 this.methodExpressionOneArg = methodExpressionOneArg;
54
55 _createZeroArgsMethodExpression(methodExpressionOneArg);
56 }
57
58 public MethodExpressionValueChangeListener(MethodExpression methodExpressionOneArg, MethodExpression methodExpressionZeroArg)
59 {
60 this.methodExpressionOneArg = methodExpressionOneArg;
61 if (methodExpressionZeroArg != null)
62 {
63 this.methodExpressionZeroArg = methodExpressionZeroArg;
64 }
65 else
66 {
67 _createZeroArgsMethodExpression(methodExpressionOneArg);
68 }
69 }
70
71 public void processValueChange(ValueChangeEvent event) throws AbortProcessingException
72 {
73 try
74 {
75 try
76 {
77
78 Object[] params = new Object[] { event };
79 methodExpressionOneArg.invoke(getElContext(), params);
80 }
81 catch (MethodNotFoundException mnfe)
82 {
83
84 methodExpressionZeroArg.invoke(getElContext(), EMPTY_PARAMS);
85 }
86 }
87 catch (ELException e)
88 {
89
90
91
92
93
94
95
96 Throwable cause = e.getCause();
97 AbortProcessingException ape = null;
98 if (cause != null)
99 {
100 do
101 {
102 if (cause != null && cause instanceof AbortProcessingException)
103 {
104 ape = (AbortProcessingException) cause;
105 break;
106 }
107 cause = cause.getCause();
108 }
109 while (cause != null);
110 }
111
112 if (ape != null)
113 {
114
115
116
117
118 throw ape;
119 }
120
121 throw e;
122
123
124
125
126
127
128
129
130
131
132
133
134
135 }
136 }
137
138 public void restoreState(FacesContext context, Object state)
139 {
140 methodExpressionOneArg = (MethodExpression) ((Object[]) state)[0];
141 methodExpressionZeroArg = (MethodExpression) ((Object[]) state)[1];
142 }
143
144 public Object saveState(FacesContext context)
145 {
146 return new Object[] {methodExpressionOneArg, methodExpressionZeroArg};
147 }
148
149 public void setTransient(boolean newTransientValue)
150 {
151 isTransient = newTransientValue;
152 }
153
154 public boolean isTransient()
155 {
156 return isTransient;
157 }
158
159 private ELContext getElContext()
160 {
161 return getFacesContext().getELContext();
162 }
163
164 private FacesContext getFacesContext()
165 {
166 return FacesContext.getCurrentInstance();
167 }
168
169
170
171
172
173
174
175
176 private void _createZeroArgsMethodExpression(MethodExpression methodExpression)
177 {
178 ExpressionFactory expressionFactory = getFacesContext().getApplication().getExpressionFactory();
179
180 this.methodExpressionZeroArg = expressionFactory.createMethodExpression(getElContext(),
181 methodExpression.getExpressionString(), Void.class, EMPTY_CLASS_ARRAY);
182 }
183
184 }