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
123 public void addReferenceInsertionEventHandler( ReferenceInsertionEventHandler ev )
124 {
125 referenceHandlers.add( ev );
126 }
127
128
129
130
131
132
133
134 public void addNullSetEventHandler( NullSetEventHandler ev )
135 {
136 nullSetHandlers.add( ev );
137 }
138
139
140
141
142
143
144
145 public void addMethodExceptionHandler( MethodExceptionEventHandler ev )
146 {
147 methodExceptionHandlers.add( ev );
148 }
149
150
151
152
153
154
155
156 public void addIncludeEventHandler( IncludeEventHandler ev )
157 {
158 includeHandlers.add( ev );
159 }
160
161
162
163
164
165
166
167 public void addInvalidReferenceEventHandler( InvalidReferenceEventHandler ev )
168 {
169 invalidReferenceHandlers.add( ev );
170 }
171
172
173
174
175
176
177
178
179
180
181
182 public boolean removeEventHandler( EventHandler ev )
183 {
184 if ( ev == null )
185 {
186 return false;
187 }
188
189 boolean found = false;
190
191 if ( ev instanceof ReferenceInsertionEventHandler )
192 return referenceHandlers.remove( ev );
193
194 if ( ev instanceof NullSetEventHandler )
195 return nullSetHandlers.remove( ev );
196
197 if ( ev instanceof MethodExceptionEventHandler )
198 return methodExceptionHandlers.remove(ev );
199
200 if ( ev instanceof IncludeEventHandler )
201 return includeHandlers.remove( ev );
202
203 if ( ev instanceof InvalidReferenceEventHandler )
204 return invalidReferenceHandlers.remove( ev );
205
206 return found;
207 }
208
209
210
211
212
213
214
215 public Iterator getReferenceInsertionEventHandlers()
216 {
217 return referenceHandlers.size() == 0 ? null : referenceHandlers.iterator();
218 }
219
220
221
222
223
224
225
226 public Iterator getNullSetEventHandlers()
227 {
228 return nullSetHandlers.iterator();
229 }
230
231
232
233
234
235
236
237 public Iterator getMethodExceptionEventHandlers()
238 {
239 return methodExceptionHandlers.iterator();
240 }
241
242
243
244
245
246
247 public Iterator getIncludeEventHandlers()
248 {
249 return includeHandlers.iterator();
250 }
251
252
253
254
255
256
257
258 public Iterator getInvalidReferenceEventHandlers()
259 {
260 return invalidReferenceHandlers.iterator();
261 }
262
263
264
265
266
267
268
269
270
271 public final boolean attachToContext( Context context )
272 {
273 if ( context instanceof InternalEventContext )
274 {
275 InternalEventContext iec = (InternalEventContext) context;
276
277 iec.attachEventCartridge( this );
278
279
280
281
282
283
284
285
286 return true;
287 }
288 else
289 {
290 return false;
291 }
292 }
293
294
295
296
297
298
299
300
301
302
303 public void initialize (RuntimeServices rs) throws Exception
304 {
305
306 for ( Iterator i = referenceHandlers.iterator(); i.hasNext(); )
307 {
308 EventHandler eh = ( EventHandler ) i.next();
309 if ( (eh instanceof RuntimeServicesAware) &&
310 !initializedHandlers.contains(eh) )
311 {
312 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
313 initializedHandlers.add( eh );
314 }
315 }
316
317 for ( Iterator i = nullSetHandlers.iterator(); i.hasNext(); )
318 {
319 EventHandler eh = ( EventHandler ) i.next();
320 if ( (eh instanceof RuntimeServicesAware) &&
321 !initializedHandlers.contains(eh) )
322 {
323 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
324 initializedHandlers.add( eh );
325 }
326 }
327
328 for ( Iterator i = methodExceptionHandlers.iterator(); i.hasNext(); )
329 {
330 EventHandler eh = ( EventHandler ) i.next();
331 if ( (eh instanceof RuntimeServicesAware) &&
332 !initializedHandlers.contains(eh) )
333 {
334 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
335 initializedHandlers.add( eh );
336 }
337 }
338
339 for ( Iterator i = includeHandlers.iterator(); i.hasNext(); )
340 {
341 EventHandler eh = ( EventHandler ) i.next();
342 if ( (eh instanceof RuntimeServicesAware) &&
343 !initializedHandlers.contains(eh) )
344 {
345 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
346 initializedHandlers.add( eh );
347 }
348 }
349
350 for ( Iterator i = invalidReferenceHandlers.iterator(); i.hasNext(); )
351 {
352 EventHandler eh = ( EventHandler ) i.next();
353 if ( (eh instanceof RuntimeServicesAware) &&
354 !initializedHandlers.contains(eh) )
355 {
356 ((RuntimeServicesAware) eh).setRuntimeServices ( rs );
357 initializedHandlers.add( eh );
358 }
359 }
360
361 }
362
363
364 }