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