1 package org.apache.velocity.app.event;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.apache.velocity.context.Context;
29 import org.apache.velocity.context.InternalEventContext;
30 import org.apache.velocity.runtime.RuntimeServices;
31 import org.apache.velocity.util.RuntimeServicesAware;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class EventCartridge
55 {
56 private List referenceHandlers = new ArrayList();
57 private List nullSetHandlers = new ArrayList();
58 private List methodExceptionHandlers = new ArrayList();
59 private List includeHandlers = new ArrayList();
60 private List invalidReferenceHandlers = new ArrayList();
61
62
63
64
65 Set initializedHandlers = new HashSet();
66
67
68
69
70
71
72
73
74
75 public boolean addEventHandler( EventHandler ev )
76 {
77 if (ev == null)
78 {
79 return false;
80 }
81
82 boolean found = false;
83
84 if ( ev instanceof ReferenceInsertionEventHandler)
85 {
86 addReferenceInsertionEventHandler( (ReferenceInsertionEventHandler) ev );
87 found = true;
88 }
89
90 if ( ev instanceof NullSetEventHandler )
91 {
92 addNullSetEventHandler( (NullSetEventHandler) ev );
93 found = true;
94 }
95
96 if ( ev instanceof MethodExceptionEventHandler )
97 {
98 addMethodExceptionHandler( (MethodExceptionEventHandler) ev );
99 found = true;
100 }
101
102 if ( ev instanceof IncludeEventHandler )
103 {
104 addIncludeEventHandler( (IncludeEventHandler) ev );
105 found = true;
106 }
107
108 if ( ev instanceof InvalidReferenceEventHandler )
109 {
110 addInvalidReferenceEventHandler( (InvalidReferenceEventHandler) ev );
111 found = true;
112 }
113
114 return found;
115 }
116
117
118
119
120
121
122 public void addReferenceInsertionEventHandler( ReferenceInsertionEventHandler ev )
123 {
124 referenceHandlers.add( ev );
125 }
126
127
128
129
130
131
132 public void addNullSetEventHandler( NullSetEventHandler ev )
133 {
134 nullSetHandlers.add( ev );
135 }
136
137
138
139
140
141
142 public void addMethodExceptionHandler( MethodExceptionEventHandler ev )
143 {
144 methodExceptionHandlers.add( ev );
145 }
146
147
148
149
150
151
152 public void addIncludeEventHandler( IncludeEventHandler ev )
153 {
154 includeHandlers.add( ev );
155 }
156
157
158
159
160
161
162 public void addInvalidReferenceEventHandler( InvalidReferenceEventHandler ev )
163 {
164 invalidReferenceHandlers.add( ev );
165 }
166
167
168
169
170
171
172
173
174
175
176
177 public boolean removeEventHandler( EventHandler ev )
178 {
179 if ( ev == null )
180 {
181 return false;
182 }
183
184 boolean found = false;
185
186 if ( ev instanceof ReferenceInsertionEventHandler )
187 return referenceHandlers.remove( ev );
188
189 if ( ev instanceof NullSetEventHandler )
190 return nullSetHandlers.remove( ev );
191
192 if ( ev instanceof MethodExceptionEventHandler )
193 return methodExceptionHandlers.remove(ev );
194
195 if ( ev instanceof IncludeEventHandler )
196 return includeHandlers.remove( ev );
197
198 if ( ev instanceof InvalidReferenceEventHandler )
199 return invalidReferenceHandlers.remove( ev );
200
201 return found;
202 }
203
204
205
206
207
208
209 public Iterator getReferenceInsertionEventHandlers()
210 {
211 return referenceHandlers.iterator();
212 }
213
214
215
216
217
218
219 public Iterator getNullSetEventHandlers()
220 {
221 return nullSetHandlers.iterator();
222 }
223
224
225
226
227
228
229 public Iterator getMethodExceptionEventHandlers()
230 {
231 return methodExceptionHandlers.iterator();
232 }
233
234
235
236
237
238
239 public Iterator getIncludeEventHandlers()
240 {
241 return includeHandlers.iterator();
242 }
243
244
245
246
247
248
249 public Iterator getInvalidReferenceEventHandlers()
250 {
251 return invalidReferenceHandlers.iterator();
252 }
253
254
255
256
257
258
259
260
261
262 public final boolean attachToContext( Context context )
263 {
264 if ( context instanceof InternalEventContext )
265 {
266 InternalEventContext iec = (InternalEventContext) context;
267
268 iec.attachEventCartridge( this );
269
270
271
272
273
274
275
276
277 return true;
278 }
279 else
280 {
281 return false;
282 }
283 }
284
285
286
287
288
289
290
291
292
293 public void initialize (RuntimeServices rs) throws Exception
294 {
295
296 for ( Iterator i = referenceHandlers.iterator(); i.hasNext(); )
297 {
298 EventHandler eh = ( EventHandler ) i.next();
299 if ( (eh instanceof RuntimeServicesAware) &&
300 !initializedHandlers.contains(eh) )
301 {
302 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
303 initializedHandlers.add( eh );
304 }
305 }
306
307 for ( Iterator i = nullSetHandlers.iterator(); i.hasNext(); )
308 {
309 EventHandler eh = ( EventHandler ) i.next();
310 if ( (eh instanceof RuntimeServicesAware) &&
311 !initializedHandlers.contains(eh) )
312 {
313 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
314 initializedHandlers.add( eh );
315 }
316 }
317
318 for ( Iterator i = methodExceptionHandlers.iterator(); i.hasNext(); )
319 {
320 EventHandler eh = ( EventHandler ) i.next();
321 if ( (eh instanceof RuntimeServicesAware) &&
322 !initializedHandlers.contains(eh) )
323 {
324 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
325 initializedHandlers.add( eh );
326 }
327 }
328
329 for ( Iterator i = includeHandlers.iterator(); i.hasNext(); )
330 {
331 EventHandler eh = ( EventHandler ) i.next();
332 if ( (eh instanceof RuntimeServicesAware) &&
333 !initializedHandlers.contains(eh) )
334 {
335 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
336 initializedHandlers.add( eh );
337 }
338 }
339
340 for ( Iterator i = invalidReferenceHandlers.iterator(); i.hasNext(); )
341 {
342 EventHandler eh = ( EventHandler ) i.next();
343 if ( (eh instanceof RuntimeServicesAware) &&
344 !initializedHandlers.contains(eh) )
345 {
346 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
347 initializedHandlers.add( eh );
348 }
349 }
350
351 }
352
353
354 }