1 package org.apache.velocity.app.event;
2
3 import org.apache.velocity.context.Context;
4 import org.apache.velocity.util.ContextAware;
5
6 /*
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied. See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 */
24
25 /**
26 * Event handler called when a method throws an exception. This gives the
27 * application a chance to deal with it and either
28 * return something nice, or throw.
29 *
30 * Please return what you want rendered into the output stream.
31 *
32 * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
33 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34 * @version $Id: MethodExceptionEventHandler.java 470256 2006-11-02 07:20:36Z wglass $
35 */
36 public interface MethodExceptionEventHandler extends EventHandler
37 {
38 /**
39 * Called when a method throws an exception.
40 * Only the first registered MethodExceptionEventHandler is called. If
41 * none are registered a MethodInvocationException is thrown.
42 *
43 * @param claz the class of the object the method is being applied to
44 * @param method the method
45 * @param e the thrown exception
46 * @return an object to insert in the page
47 * @throws Exception an exception to be thrown instead inserting an object
48 */
49 public Object methodException( Class claz, String method, Exception e )
50 throws Exception;
51
52 /**
53 * Defines the execution strategy for methodException
54 */
55 static class MethodExceptionExecutor implements EventHandlerMethodExecutor
56 {
57 private Context context;
58 private Class claz;
59 private String method;
60 private Exception e;
61
62 private Object result;
63 private boolean executed = false;
64
65 MethodExceptionExecutor(
66 Context context,
67 Class claz,
68 String method,
69 Exception e)
70 {
71 this.context = context;
72 this.claz = claz;
73 this.method = method;
74 this.e = e;
75 }
76
77 /**
78 * Call the method methodException()
79 *
80 * @param handler call the appropriate method on this handler
81 * @exception Exception generic exception thrown by methodException event handler method call
82 */
83 public void execute(EventHandler handler) throws Exception
84 {
85 MethodExceptionEventHandler eh = (MethodExceptionEventHandler) handler;
86
87 if (eh instanceof ContextAware)
88 ((ContextAware) eh).setContext(context);
89
90 executed = true;
91 result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e);
92 }
93
94 public Object getReturnValue()
95 {
96 return result;
97 }
98
99 /**
100 * Only run the first MethodExceptionEventHandler
101 *
102 * @return true after this is executed once.
103 */
104 public boolean isDone()
105 {
106 return executed;
107 }
108
109
110 }
111
112 }