aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/main/java/org/apache/velocity/app/event/EventCartridge.java
blob: 1bbb49f31439897a70b62f9998385aad80325ccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package org.apache.velocity.app.event;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.context.InternalEventContext;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeServices;
import org.apache.velocity.util.RuntimeServicesAware;
import org.apache.velocity.util.introspection.Info;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>Stores the event handlers. Event handlers can be assigned on a per
 * VelocityEngine instance basis by specifying the class names in the
 * velocity.properties file. Event handlers may also be assigned on a per-page
 * basis by creating a new instance of EventCartridge, adding the event
 * handlers, and then calling attachToContext. For clarity, it's recommended
 * that one approach or the other be followed, as the second method is primarily
 * presented for backwards compatibility.</p>
 * <p>Note that Event Handlers follow a filter pattern, with multiple event
 * handlers allowed for each event. When the appropriate event occurs, all the
 * appropriate event handlers are called in the sequence they were added to the
 * Event Cartridge. See the javadocs of the specific event handler interfaces
 * for more details.</p>
 *
 * @author <a href="mailto:wglass@wglass@forio.com">Will Glass-Husain </a>
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr. </a>
 * @author <a href="mailto:j_a_fernandez@yahoo.com">Jose Alberto Fernandez </a>
 * @version $Id$
 */
public class EventCartridge
{
    private List<ReferenceInsertionEventHandler> referenceHandlers = new ArrayList<>();
    private MethodExceptionEventHandler methodExceptionHandler = null;
    private List<IncludeEventHandler> includeHandlers = new ArrayList<>();
    private List<InvalidReferenceEventHandler> invalidReferenceHandlers = new ArrayList<>();

    /**
     * Ensure that handlers are not initialized more than once.
     */
    Set<EventHandler> initializedHandlers = new HashSet<>();

    protected RuntimeServices rsvc = null;

    protected Logger getLog()
    {
        return rsvc == null ? LoggerFactory.getLogger(EventCartridge.class) : rsvc.getLog();
    }

    /**
     * runtime services setter, called during initialization
     *
     * @param rs runtime services
     * @since 2.0
     */
    public synchronized void setRuntimeServices(RuntimeServices rs)
    {
        if (rsvc == null)
        {
            rsvc = rs;
              /* allow for this method to be called *after* adding event handlers */
            for (EventHandler handler : referenceHandlers)
            {
                if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
                {
                    ((RuntimeServicesAware) handler).setRuntimeServices(rs);
                    initializedHandlers.add(handler);
                }
            }
            if (methodExceptionHandler != null &&
                methodExceptionHandler instanceof RuntimeServicesAware &&
                !initializedHandlers.contains(methodExceptionHandler))
            {
                ((RuntimeServicesAware) methodExceptionHandler).setRuntimeServices(rs);
                initializedHandlers.add(methodExceptionHandler);
            }
            for (EventHandler handler : includeHandlers)
            {
                if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
                {
                    ((RuntimeServicesAware) handler).setRuntimeServices(rs);
                    initializedHandlers.add(handler);
                }
            }
            for (EventHandler handler : invalidReferenceHandlers)
            {
                if (handler instanceof RuntimeServicesAware && !initializedHandlers.contains(handler))
                {
                    ((RuntimeServicesAware) handler).setRuntimeServices(rs);
                    initializedHandlers.add(handler);
                }
            }
        }
        else if (rsvc != rs)
        {
            throw new VelocityException("an event cartridge cannot be used by several different runtime services instances");
        }
    }

    /**
     * Adds an event handler(s) to the Cartridge.  This method
     * will find all possible event handler interfaces supported
     * by the passed in object.
     *
     * @param ev object implementing a valid EventHandler-derived interface
     * @return true if a supported interface, false otherwise or if null
     */
    public boolean addEventHandler(EventHandler ev)
    {
        if (ev == null)
        {
            return false;
        }

        boolean found = false;

        if (ev instanceof ReferenceInsertionEventHandler)
        {
            addReferenceInsertionEventHandler((ReferenceInsertionEventHandler) ev);
            found = true;
        }

        if (ev instanceof MethodExceptionEventHandler)
        {
            addMethodExceptionHandler((MethodExceptionEventHandler) ev);
            found = true;
        }

        if (ev instanceof IncludeEventHandler)
        {
            addIncludeEventHandler((IncludeEventHandler) ev);
            found = true;
        }

        if (ev instanceof InvalidReferenceEventHandler)
        {
            addInvalidReferenceEventHandler((InvalidReferenceEventHandler) ev);
            found = true;
        }

        if (found && rsvc != null && ev instanceof RuntimeServicesAware && !initializedHandlers.contains(ev))
        {
            ((RuntimeServicesAware) ev).setRuntimeServices(rsvc);
            initializedHandlers.add(ev);
        }

        return found;
    }

    /**
     * Add a reference insertion event handler to the Cartridge.
     *
     * @param ev ReferenceInsertionEventHandler
     * @since 1.5
     */
    public void addReferenceInsertionEventHandler(ReferenceInsertionEventHandler ev)
    {
        referenceHandlers.add(ev);
    }

    /**
     * Add a method exception event handler to the Cartridge.
     *
     * @param ev MethodExceptionEventHandler
     * @since 1.5
     */
    public void addMethodExceptionHandler(MethodExceptionEventHandler ev)
    {
        if (methodExceptionHandler == null)
        {
            methodExceptionHandler = ev;
        }
        else
        {
            getLog().warn("ignoring extra method exception handler");
        }
    }

    /**
     * Add an include event handler to the Cartridge.
     *
     * @param ev IncludeEventHandler
     * @since 1.5
     */
    public void addIncludeEventHandler(IncludeEventHandler ev)
    {
        includeHandlers.add(ev);
    }

    /**
     * Add an invalid reference event handler to the Cartridge.
     *
     * @param ev InvalidReferenceEventHandler
     * @since 1.5
     */
    public void addInvalidReferenceEventHandler(InvalidReferenceEventHandler ev)
    {
        invalidReferenceHandlers.add(ev);
    }


    /**
     * Removes an event handler(s) from the Cartridge. This method will find all
     * possible event handler interfaces supported by the passed in object and
     * remove them.
     *
     * @param ev object impementing a valid EventHandler-derived interface
     * @return true if event handler was previously registered, false if not
     * found
     */
    public boolean removeEventHandler(EventHandler ev)
    {
        if (ev == null)
        {
            return false;
        }

        if (ev instanceof ReferenceInsertionEventHandler)
        {
            return referenceHandlers.remove(ev);
        }

        if (ev instanceof MethodExceptionEventHandler)
        {
            if (ev == methodExceptionHandler)
            {
                methodExceptionHandler = null;
                return true;
            }
        }

        if (ev instanceof IncludeEventHandler)
        {
            return includeHandlers.remove(ev);
        }

        if (ev instanceof InvalidReferenceEventHandler)
        {
            return invalidReferenceHandlers.remove(ev);
        }

        return false;
    }

    /**
     * Call reference insertion handlers
     * @param context
     * @param reference
     * @param value
     * @return value returned by handlers
     * @since 2.0
     */
    public Object referenceInsert(InternalContextAdapter context, String reference, Object value)
    {
        for (ReferenceInsertionEventHandler handler : referenceHandlers)
        {
            value = handler.referenceInsert(context, reference, value);
        }
        return value;
    }

    /**
     * Check whether this event cartridge has a method exception event handler
     *
     * @return true if a method exception event handler has been registered
     * @since 2.0
     */
    boolean hasMethodExceptionEventHandler()
    {
        return methodExceptionHandler != null;
    }

    /**
     * Call method exception event handler
     * @param context
     * @param claz
     * @param method
     * @param e exception
     * @param info template name, line and column infos
     * @return value returned by handler
     * @since 2.0
     */
    public Object methodException(Context context, Class<?> claz, String method, Exception e, Info info)
    {
        if (methodExceptionHandler != null)
        {
            return methodExceptionHandler.methodException(context, claz, method, e, info);
        }
        return null;
    }

    /**
     * Call include event handlers
     *
     * @param context
     * @param includeResourcePath
     * @param currentResourcePath
     * @param directiveName
     * @return include path
     * @since 2.0
     */
    public String includeEvent(Context context, String includeResourcePath, String currentResourcePath, String directiveName)
    {
        for (IncludeEventHandler handler : includeHandlers)
        {
            includeResourcePath = handler.includeEvent(context, includeResourcePath, currentResourcePath, directiveName);
            /* reflect 1.x behavior: exit after at least one execution whenever a null include path has been found */
            if (includeResourcePath == null)
            {
                break;
            }
        }
        return includeResourcePath;
    }

    /**
     * Call invalid reference handlers for an invalid getter
     *
     * @param context
     * @param reference
     * @param object
     * @param property
     * @param info
     * @return value returned by handlers
     * @since 2.0
     */
    public Object invalidGetMethod(Context context, String reference, Object object, String property, Info info)
    {
        Object result = null;
        for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
        {
            result = handler.invalidGetMethod(context, reference, object, property, info);
              /* reflect 1.x behavior: exit after at least one execution whenever a non-null value has been found */
            if (result != null)
            {
                break;
            }
        }
        return result;
    }

    /**
     * Call invalid reference handlers for an invalid setter
     *
     * @param context
     * @param leftreference
     * @param rightreference
     * @param info
     * @return whether to stop further chaining in the next cartridge
     * @since 2.0
     */
    public boolean invalidSetMethod(Context context, String leftreference, String rightreference, Info info)
    {
        for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
        {
            if (handler.invalidSetMethod(context, leftreference, rightreference, info))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * Call invalid reference handlers for an invalid method call
     *
     * @param context
     * @param reference
     * @param object
     * @param method
     * @param info
     * @return value returned by handlers
     * @since 2.0
     */
    public Object invalidMethod(Context context, String reference, Object object, String method, Info info)
    {
        Object result = null;
        for (InvalidReferenceEventHandler handler : invalidReferenceHandlers)
        {
            result = handler.invalidMethod(context, reference, object, method, info);
              /* reflect 1.x behavior: exit after at least one execution whenever a non-null value has been found */
            if (result != null)
            {
                break;
            }
        }
        return result;
    }

    /**
     * Attached the EventCartridge to the context
     *
     * Final because not something one should mess with lightly :)
     *
     * @param context context to attach to
     * @return true if successful, false otherwise
     */
    public final boolean attachToContext(Context context)
    {
        if (context instanceof InternalEventContext)
        {
            InternalEventContext iec = (InternalEventContext) context;

            iec.attachEventCartridge(this);

            /*
             * while it's tempting to call setContext on each handler from here,
             * this needs to be done before each method call.  This is
             * because the specific context will change as inner contexts
             * are linked in through macros, foreach, or directly by the user.
             */

            return true;
        }
        else
        {
            return false;
        }
    }
}