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