001: package org.apache.velocity.app.event;
002:
003: import org.apache.velocity.context.Context;
004: import org.apache.velocity.util.ContextAware;
005:
006: /*
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: */
024:
025: /**
026: * Event handler called when a method throws an exception. This gives the
027: * application a chance to deal with it and either
028: * return something nice, or throw.
029: *
030: * Please return what you want rendered into the output stream.
031: *
032: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
033: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
034: * @version $Id: MethodExceptionEventHandler.java 470256 2006-11-02 07:20:36Z wglass $
035: */
036: public interface MethodExceptionEventHandler extends EventHandler {
037: /**
038: * Called when a method throws an exception.
039: * Only the first registered MethodExceptionEventHandler is called. If
040: * none are registered a MethodInvocationException is thrown.
041: *
042: * @param claz the class of the object the method is being applied to
043: * @param method the method
044: * @param e the thrown exception
045: * @return an object to insert in the page
046: * @throws Exception an exception to be thrown instead inserting an object
047: */
048: public Object methodException(Class claz, String method, Exception e)
049: throws Exception;
050:
051: /**
052: * Defines the execution strategy for methodException
053: */
054: static class MethodExceptionExecutor implements
055: EventHandlerMethodExecutor {
056: private Context context;
057: private Class claz;
058: private String method;
059: private Exception e;
060:
061: private Object result;
062: private boolean executed = false;
063:
064: MethodExceptionExecutor(Context context, Class claz,
065: String method, Exception e) {
066: this .context = context;
067: this .claz = claz;
068: this .method = method;
069: this .e = e;
070: }
071:
072: /**
073: * Call the method methodException()
074: *
075: * @param handler call the appropriate method on this handler
076: * @exception Exception generic exception thrown by methodException event handler method call
077: */
078: public void execute(EventHandler handler) throws Exception {
079: MethodExceptionEventHandler eh = (MethodExceptionEventHandler) handler;
080:
081: if (eh instanceof ContextAware)
082: ((ContextAware) eh).setContext(context);
083:
084: executed = true;
085: result = ((MethodExceptionEventHandler) handler)
086: .methodException(claz, method, e);
087: }
088:
089: public Object getReturnValue() {
090: return result;
091: }
092:
093: /**
094: * Only run the first MethodExceptionEventHandler
095: *
096: * @return true after this is executed once.
097: */
098: public boolean isDone() {
099: return executed;
100: }
101:
102: }
103:
104: }
|