001: package org.apache.velocity.app.event.implement;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.PrintWriter;
023: import java.io.StringWriter;
024: import org.apache.velocity.app.event.MethodExceptionEventHandler;
025: import org.apache.velocity.runtime.RuntimeServices;
026: import org.apache.velocity.util.RuntimeServicesAware;
027:
028: /**
029: * Simple event handler that renders method exceptions in the page
030: * rather than throwing the exception. Useful for debugging.
031: *
032: * <P>By default this event handler renders the exception name only.
033: * To include both the exception name and the message, set the property
034: * <code>eventhandler.methodexception.message</code> to <code>true</code>. To render
035: * the stack trace, set the property <code>eventhandler.methodexception.stacktrace</code>
036: * to <code>true</code>.
037: *
038: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
039: * @version $Id: PrintExceptions.java 470256 2006-11-02 07:20:36Z wglass $
040: */
041: public class PrintExceptions implements MethodExceptionEventHandler,
042: RuntimeServicesAware {
043:
044: private static String SHOW_MESSAGE = "eventhandler.methodexception.message";
045: private static String SHOW_STACK_TRACE = "eventhandler.methodexception.stacktrace";
046:
047: /** Reference to the runtime service */
048: private RuntimeServices rs = null;
049:
050: /**
051: * Render the method exception, and optionally the exception message and stack trace.
052: *
053: * @param claz the class of the object the method is being applied to
054: * @param method the method
055: * @param e the thrown exception
056: * @return an object to insert in the page
057: * @throws Exception an exception to be thrown instead inserting an object
058: */
059: public Object methodException(Class claz, String method, Exception e)
060: throws Exception {
061: boolean showMessage = rs.getBoolean(SHOW_MESSAGE, false);
062: boolean showStackTrace = rs.getBoolean(SHOW_STACK_TRACE, false);
063:
064: StringBuffer st;
065: if (showMessage && showStackTrace) {
066: st = new StringBuffer(200);
067: st.append(e.getClass().getName()).append("\n");
068: st.append(e.getMessage()).append("\n");
069: st.append(getStackTrace(e));
070:
071: } else if (showMessage) {
072: st = new StringBuffer(50);
073: st.append(e.getClass().getName()).append("\n");
074: st.append(e.getMessage()).append("\n");
075:
076: } else if (showStackTrace) {
077: st = new StringBuffer(200);
078: st.append(e.getClass().getName()).append("\n");
079: st.append(getStackTrace(e));
080:
081: } else {
082: st = new StringBuffer(15);
083: st.append(e.getClass().getName()).append("\n");
084: }
085:
086: return st.toString();
087:
088: }
089:
090: private static String getStackTrace(Throwable throwable) {
091: PrintWriter printWriter = null;
092: try {
093: StringWriter stackTraceWriter = new StringWriter();
094: printWriter = new PrintWriter(stackTraceWriter);
095: throwable.printStackTrace(printWriter);
096: printWriter.flush();
097: return stackTraceWriter.toString();
098: } finally {
099: if (printWriter != null) {
100: printWriter.close();
101: }
102: }
103: }
104:
105: /**
106: * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
107: */
108: public void setRuntimeServices(RuntimeServices rs) {
109: this.rs = rs;
110: }
111:
112: }
|