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 685685 2008-08-13 21:43:27Z nbubna $
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 * @since 1.5
55 */
56 static class MethodExceptionExecutor implements EventHandlerMethodExecutor
57 {
58 private Context context;
59 private Class claz;
60 private String method;
61 private Exception e;
62
63 private Object result;
64 private boolean executed = false;
65
66 MethodExceptionExecutor(
67 Context context,
68 Class claz,
69 String method,
70 Exception e)
71 {
72 this.context = context;
73 this.claz = claz;
74 this.method = method;
75 this.e = e;
76 }
77
78 /**
79 * Call the method methodException()
80 *
81 * @param handler call the appropriate method on this handler
82 * @exception Exception generic exception thrown by methodException event handler method call
83 */
84 public void execute(EventHandler handler) throws Exception
85 {
86 MethodExceptionEventHandler eh = (MethodExceptionEventHandler) handler;
87
88 if (eh instanceof ContextAware)
89 ((ContextAware) eh).setContext(context);
90
91 executed = true;
92 result = ((MethodExceptionEventHandler) handler).methodException(claz, method, e);
93 }
94
95 public Object getReturnValue()
96 {
97 return result;
98 }
99
100 /**
101 * Only run the first MethodExceptionEventHandler
102 *
103 * @return true after this is executed once.
104 */
105 public boolean isDone()
106 {
107 return executed;
108 }
109
110
111 }
112
113 }