Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExceptionUtils |
|
| 3.111111111111111;3.111 |
1 | /* | |
2 | * Copyright 2005-2008 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.kns.util; | |
17 | ||
18 | import javax.servlet.ServletException; | |
19 | ||
20 | import org.apache.commons.logging.Log; | |
21 | import org.apache.log4j.Level; | |
22 | import org.apache.log4j.Logger; | |
23 | ||
24 | /** | |
25 | * Adapts Exception.print stack trace to print its output to a Logger. | |
26 | * | |
27 | * | |
28 | */ | |
29 | 0 | public class ExceptionUtils { |
30 | ||
31 | /** | |
32 | * Logs the stack trace of the given throwable to the given logger. | |
33 | * | |
34 | * @param logger | |
35 | * @param t | |
36 | */ | |
37 | public static void logStackTrace(Logger logger, Throwable t) { | |
38 | 0 | if (logger.isEnabledFor(Level.ERROR)) { |
39 | 0 | logger.error(getStackLogMessage(t)); |
40 | } | |
41 | 0 | } |
42 | ||
43 | /** | |
44 | * Logs the stack trace of the given throwable to the given logger. | |
45 | * | |
46 | * @param log | |
47 | * @param t | |
48 | */ | |
49 | public static void logStackTrace(Log log, Throwable t) { | |
50 | 0 | if (log.isErrorEnabled()) { |
51 | 0 | log.error(getStackLogMessage(t)); |
52 | } | |
53 | 0 | } |
54 | ||
55 | /** creates a logging message from a Throwable. */ | |
56 | private static String getStackLogMessage(Throwable t) { | |
57 | 0 | StackTraceElement[] elements = t.getStackTrace(); |
58 | ||
59 | 0 | StringBuffer trace = new StringBuffer(); |
60 | 0 | trace.append(t.getClass().getName()); |
61 | ||
62 | 0 | if (t.getMessage() != null) { |
63 | 0 | trace.append(": "); |
64 | 0 | trace.append(t.getMessage()); |
65 | } | |
66 | ||
67 | 0 | trace.append("\n"); |
68 | ||
69 | 0 | for (int i = 0; i < elements.length; ++i) { |
70 | 0 | StackTraceElement element = elements[i]; |
71 | ||
72 | 0 | trace.append(" at "); |
73 | 0 | trace.append(describeStackTraceElement(element)); |
74 | 0 | trace.append("\n"); |
75 | } | |
76 | ||
77 | 0 | return trace.toString(); |
78 | } | |
79 | ||
80 | /** | |
81 | * @param level | |
82 | * @return String containing the name of the method at the given level from the top of the stack | |
83 | */ | |
84 | public static String describeStackLevel(Throwable t, int level) { | |
85 | 0 | return describeStackLevels(t, level + 1, level + 1); |
86 | } | |
87 | ||
88 | /** | |
89 | * @param fromLevel | |
90 | * @param toLevel | |
91 | * @return String containing the names of the methods at the given levels from the top of the stack | |
92 | */ | |
93 | public static String describeStackLevels(Throwable t, int fromLevel, int toLevel) { | |
94 | 0 | if (fromLevel <= 0) { |
95 | 0 | throw new IllegalArgumentException("invalid fromLevel (" + fromLevel + " < 0)"); |
96 | } | |
97 | 0 | if (fromLevel > toLevel) { |
98 | 0 | throw new IllegalArgumentException("invalid levels (fromLevel " + fromLevel + " > toLevel " + toLevel + ")"); |
99 | } | |
100 | ||
101 | 0 | StackTraceElement[] elements = t.getStackTrace(); |
102 | 0 | int stackHeight = elements.length; |
103 | 0 | if (toLevel >= elements.length) { |
104 | 0 | throw new IllegalArgumentException("invalid toLevel (" + toLevel + " >= " + stackHeight + ")"); |
105 | } | |
106 | ||
107 | 0 | StringBuffer result = new StringBuffer(); |
108 | 0 | for (int level = fromLevel; level <= toLevel; level++) { |
109 | 0 | if (result.length() > 0) { |
110 | 0 | result.append(" from "); |
111 | } | |
112 | 0 | result.append(describeStackTraceElement(elements[level])); |
113 | } | |
114 | 0 | return result.toString(); |
115 | } | |
116 | ||
117 | /** | |
118 | * @param level | |
119 | * @return String containing the name of the method at the given level from the top of the stack (not including this method) | |
120 | */ | |
121 | public static String describeStackLevel(int level) { | |
122 | 0 | return describeStackLevels(level + 1, level + 1); |
123 | } | |
124 | ||
125 | /** | |
126 | * @param fromLevel | |
127 | * @param toLevel | |
128 | * @return String containing the names of the methods at the given levels from the top of the stack (not including this method) | |
129 | */ | |
130 | public static String describeStackLevels(int fromLevel, int toLevel) { | |
131 | 0 | String description = null; |
132 | try { | |
133 | 0 | throw new RuntimeException("hack"); |
134 | } | |
135 | 0 | catch (RuntimeException e) { |
136 | 0 | description = describeStackLevels(e, fromLevel + 1, toLevel + 1); |
137 | } | |
138 | ||
139 | 0 | return description; |
140 | } | |
141 | ||
142 | ||
143 | /** | |
144 | * @param element | |
145 | * @return String describing the given StackTraceElement | |
146 | */ | |
147 | private static String describeStackTraceElement(StackTraceElement element) { | |
148 | 0 | if (element == null) { |
149 | 0 | throw new IllegalArgumentException("invalid (null) element"); |
150 | } | |
151 | ||
152 | 0 | StringBuffer description = new StringBuffer(); |
153 | ||
154 | 0 | description.append(element.getClassName()); |
155 | 0 | description.append("."); |
156 | 0 | description.append(element.getMethodName()); |
157 | 0 | description.append("("); |
158 | 0 | description.append(element.getFileName()); |
159 | 0 | description.append(":"); |
160 | 0 | description.append(element.getLineNumber()); |
161 | 0 | description.append(")"); |
162 | ||
163 | 0 | return description.toString(); |
164 | } | |
165 | ||
166 | /** | |
167 | * Initializes the JDK 1.4 cause of any ServletExceptions in the given cause chain. This is a work-around for the lack of | |
168 | * support in the Servlet 2.4 API and Tomcat 5. It allows anything using Throwable.printStackTrace() (e.g., log4j) to include | |
169 | * the cause chain. If a ServletException has a rootCause but it's JDK 1.4 cause has already been initialized to null, then the | |
170 | * chain ends there. I think Tomcat's exception logging goes beyond this, to follow cause or rootCause depending on the | |
171 | * Exception's type and log each one individually instead of using Throwable.printStackTrace(). But that only helps Tomcat's log | |
172 | * file. | |
173 | * | |
174 | * @param t the head of the cause chain to initialize | |
175 | */ | |
176 | public void initServletExceptionCauses(Throwable t) { | |
177 | 0 | while (t != null) { |
178 | 0 | if (t instanceof ServletException) { |
179 | 0 | ServletException se = (ServletException) t; |
180 | try { | |
181 | // Convert Servlet 2.4 API cause to JDK 1.4 cause. | |
182 | 0 | se.initCause(se.getRootCause()); |
183 | } | |
184 | 0 | catch (IllegalStateException e) { |
185 | // Okay, the cause was already initialized. | |
186 | // (IllegalStateException is the only way to distinguish from being initialized to null.) | |
187 | 0 | } |
188 | } | |
189 | 0 | t = t.getCause(); |
190 | } | |
191 | 0 | } |
192 | } |