View Javadoc

1   package org.apache.velocity.app.event.implement;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import org.apache.velocity.app.event.MethodExceptionEventHandler;
25  import org.apache.velocity.runtime.RuntimeServices;
26  import org.apache.velocity.util.RuntimeServicesAware;
27  
28  /**
29   * Simple event handler that renders method exceptions in the page
30   * rather than throwing the exception.  Useful for debugging.
31   *
32   * <P>By default this event handler renders the exception name only.
33   * To include both the exception name and the message, set the property
34   * <code>eventhandler.methodexception.message</code> to <code>true</code>.  To render
35   * the stack trace, set the property <code>eventhandler.methodexception.stacktrace</code>
36   * to <code>true</code>.
37   *
38   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
39   * @version $Id: PrintExceptions.java 470256 2006-11-02 07:20:36Z wglass $
40   */
41  public class PrintExceptions implements MethodExceptionEventHandler, RuntimeServicesAware
42  {
43  
44      private static String SHOW_MESSAGE = "eventhandler.methodexception.message";
45      private static String SHOW_STACK_TRACE = "eventhandler.methodexception.stacktrace";
46  
47      /** Reference to the runtime service */
48      private RuntimeServices rs = null;
49  
50      /**
51       * Render the method exception, and optionally the exception message and stack trace.
52       * 
53       * @param claz the class of the object the method is being applied to
54       * @param method the method
55       * @param e the thrown exception
56       * @return an object to insert in the page
57       * @throws Exception an exception to be thrown instead inserting an object
58       */
59      public Object methodException(Class claz, String method, Exception e) throws Exception
60      {
61          boolean showMessage = rs.getBoolean(SHOW_MESSAGE,false);
62          boolean showStackTrace = rs.getBoolean(SHOW_STACK_TRACE,false);
63  
64          StringBuffer st;
65          if (showMessage && showStackTrace)
66          {
67              st = new StringBuffer(200);
68              st.append(e.getClass().getName()).append("\n");
69              st.append(e.getMessage()).append("\n");
70              st.append(getStackTrace(e));
71  
72          } else if (showMessage)
73          {
74              st = new StringBuffer(50);
75              st.append(e.getClass().getName()).append("\n");
76              st.append(e.getMessage()).append("\n");
77  
78          } else if (showStackTrace)
79          {
80              st = new StringBuffer(200);
81              st.append(e.getClass().getName()).append("\n");
82              st.append(getStackTrace(e));
83  
84          } else
85          {
86              st = new StringBuffer(15);
87              st.append(e.getClass().getName()).append("\n");
88          }
89  
90          return st.toString();
91  
92      }
93  
94  
95      private static String getStackTrace(Throwable throwable)
96      {
97          PrintWriter printWriter = null;
98          try
99          {
100             StringWriter stackTraceWriter = new StringWriter();
101             printWriter = new PrintWriter(stackTraceWriter);
102             throwable.printStackTrace(printWriter);
103             printWriter.flush();
104             return stackTraceWriter.toString();
105         }
106         finally
107         {
108             if (printWriter != null)
109             {
110                 printWriter.close();
111             }
112         }
113     }
114 
115 
116     /**
117      * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
118      */
119     public void setRuntimeServices(RuntimeServices rs)
120     {
121         this.rs = rs;
122     }
123 
124 }