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 685685 2008-08-13 21:43:27Z nbubna $
40   * @since 1.5
41   */
42  public class PrintExceptions implements MethodExceptionEventHandler, RuntimeServicesAware
43  {
44  
45      private static String SHOW_MESSAGE = "eventhandler.methodexception.message";
46      private static String SHOW_STACK_TRACE = "eventhandler.methodexception.stacktrace";
47  
48      /** Reference to the runtime service */
49      private RuntimeServices rs = null;
50  
51      /**
52       * Render the method exception, and optionally the exception message and stack trace.
53       * 
54       * @param claz the class of the object the method is being applied to
55       * @param method the method
56       * @param e the thrown exception
57       * @return an object to insert in the page
58       * @throws Exception an exception to be thrown instead inserting an object
59       */
60      public Object methodException(Class claz, String method, Exception e) throws Exception
61      {
62          boolean showMessage = rs.getBoolean(SHOW_MESSAGE,false);
63          boolean showStackTrace = rs.getBoolean(SHOW_STACK_TRACE,false);
64  
65          StringBuffer st;
66          if (showMessage && showStackTrace)
67          {
68              st = new StringBuffer(200);
69              st.append(e.getClass().getName()).append("\n");
70              st.append(e.getMessage()).append("\n");
71              st.append(getStackTrace(e));
72  
73          } else if (showMessage)
74          {
75              st = new StringBuffer(50);
76              st.append(e.getClass().getName()).append("\n");
77              st.append(e.getMessage()).append("\n");
78  
79          } else if (showStackTrace)
80          {
81              st = new StringBuffer(200);
82              st.append(e.getClass().getName()).append("\n");
83              st.append(getStackTrace(e));
84  
85          } else
86          {
87              st = new StringBuffer(15);
88              st.append(e.getClass().getName()).append("\n");
89          }
90  
91          return st.toString();
92  
93      }
94  
95  
96      private static String getStackTrace(Throwable throwable)
97      {
98          PrintWriter printWriter = null;
99          try
100         {
101             StringWriter stackTraceWriter = new StringWriter();
102             printWriter = new PrintWriter(stackTraceWriter);
103             throwable.printStackTrace(printWriter);
104             printWriter.flush();
105             return stackTraceWriter.toString();
106         }
107         finally
108         {
109             if (printWriter != null)
110             {
111                 printWriter.close();
112             }
113         }
114     }
115 
116 
117     /**
118      * @see org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
119      */
120     public void setRuntimeServices(RuntimeServices rs)
121     {
122         this.rs = rs;
123     }
124 
125 }