1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.shale.tiger.register.faces; |
21 | |
|
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.lang.reflect.Method; |
24 | |
import javax.faces.FacesException; |
25 | |
import javax.faces.event.PhaseEvent; |
26 | |
import javax.faces.event.PhaseId; |
27 | |
import javax.faces.event.PhaseListener; |
28 | |
import org.apache.shale.tiger.register.AfterPhase; |
29 | |
import org.apache.shale.tiger.register.BeforePhase; |
30 | |
import org.apache.shale.tiger.register.FacesPhaseListener; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public final class PhaseListenerAdapter implements PhaseListener { |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
private static final long serialVersionUID = -8684913975588239869L; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 18 | public PhaseListenerAdapter(Object instance) { |
62 | |
|
63 | |
|
64 | 18 | this.instance = instance; |
65 | |
|
66 | |
|
67 | 18 | FacesPhaseListener fpl = instance.getClass().getAnnotation(FacesPhaseListener.class); |
68 | 18 | if (fpl == null) { |
69 | |
throw new IllegalArgumentException("Implementing class " |
70 | |
+ instance.getClass().getName() |
71 | |
+ " does not have the @FacesPhaseListener annotation"); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | 18 | Method[] methods = instance.getClass().getMethods(); |
77 | 216 | for (int i = 0; i < methods.length; i++) { |
78 | 198 | Method method = methods[i]; |
79 | 198 | Class[] signature = method.getParameterTypes(); |
80 | 198 | if (method.getAnnotation(BeforePhase.class) != null) { |
81 | 18 | if (signature.length != 1) { |
82 | |
throw new IllegalArgumentException("Method " + method.getName() |
83 | |
+ " of class " + instance.getClass().getName() |
84 | |
+ " does not take a exactly one parameter"); |
85 | 18 | } else if (!(signature[0] == PhaseEvent.class)) { |
86 | |
throw new IllegalArgumentException("Method " + method.getName() |
87 | |
+ " of class " + instance.getClass().getName() |
88 | |
+ " does not take a javax.faces.event.PhaaseEvent parameter"); |
89 | |
} else { |
90 | 18 | this.beforePhase = method; |
91 | |
} |
92 | 18 | } else if (method.getAnnotation(AfterPhase.class) != null) { |
93 | 18 | if (signature.length != 1) { |
94 | |
throw new IllegalArgumentException("Method " + method.getName() |
95 | |
+ " of class " + instance.getClass().getName() |
96 | |
+ " does not take a exactly one parameter"); |
97 | 18 | } else if (!(signature[0] == PhaseEvent.class)) { |
98 | |
throw new IllegalArgumentException("Method " + method.getName() |
99 | |
+ " of class " + instance.getClass().getName() |
100 | |
+ " does not take a javax.faces.event.PhaaseEvent parameter"); |
101 | |
} else { |
102 | 18 | this.afterPhase = method; |
103 | |
} |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
|
108 | 18 | FacesPhaseListener.PhaseId pi = fpl.phaseId(); |
109 | 18 | if (pi == FacesPhaseListener.PhaseId.ANY_PHASE) { |
110 | 18 | this.phaseId = PhaseId.ANY_PHASE; |
111 | 18 | } else if (pi == FacesPhaseListener.PhaseId.RESTORE_VIEW) { |
112 | |
this.phaseId = PhaseId.RESTORE_VIEW; |
113 | |
} else if (pi == FacesPhaseListener.PhaseId.APPLY_REQUEST_VALUES) { |
114 | |
this.phaseId = PhaseId.APPLY_REQUEST_VALUES; |
115 | |
} else if (pi == FacesPhaseListener.PhaseId.PROCESS_VALIDATIONS) { |
116 | |
this.phaseId = PhaseId.PROCESS_VALIDATIONS; |
117 | |
} else if (pi == FacesPhaseListener.PhaseId.UPDATE_MODEL_VALUES) { |
118 | |
this.phaseId = PhaseId.UPDATE_MODEL_VALUES; |
119 | |
} else if (pi == FacesPhaseListener.PhaseId.INVOKE_APPLICATION) { |
120 | |
this.phaseId = PhaseId.INVOKE_APPLICATION; |
121 | |
} else if (pi == FacesPhaseListener.PhaseId.RENDER_RESPONSE) { |
122 | |
this.phaseId = PhaseId.RENDER_RESPONSE; |
123 | |
} |
124 | |
|
125 | 18 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private Method afterPhase; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
private Method beforePhase; |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
private Object instance; |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
private PhaseId phaseId; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public void afterPhase(PhaseEvent event) { |
170 | |
|
171 | |
if (afterPhase == null) { |
172 | |
return; |
173 | |
} |
174 | |
try { |
175 | |
afterPhase.invoke(instance, event); |
176 | |
} catch (InvocationTargetException e) { |
177 | |
throw new FacesException(e.getTargetException()); |
178 | |
} catch (Exception e) { |
179 | |
throw new FacesException(e); |
180 | |
} |
181 | |
|
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public void beforePhase(PhaseEvent event) { |
194 | |
|
195 | |
if (beforePhase == null) { |
196 | |
return; |
197 | |
} |
198 | |
try { |
199 | |
beforePhase.invoke(instance, event); |
200 | |
} catch (InvocationTargetException e) { |
201 | |
throw new FacesException(e.getTargetException()); |
202 | |
} catch (Exception e) { |
203 | |
throw new FacesException(e); |
204 | |
} |
205 | |
|
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public PhaseId getPhaseId() { |
213 | |
|
214 | |
return this.phaseId; |
215 | |
|
216 | |
} |
217 | |
|
218 | |
|
219 | |
} |