| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.logging; |
|---|
| 34 | |
|---|
| 35 | import java.nio.CharBuffer; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * This helper class wraps the information of one stack trace line. |
|---|
| 39 | * |
|---|
| 40 | */ |
|---|
| 41 | public class StackTraceInfo |
|---|
| 42 | { |
|---|
| 43 | private static final String CURRENT_LINE_ERROR_TEXT |
|---|
| 44 | = "The current line is not a "; |
|---|
| 45 | |
|---|
| 46 | private final CharBuffer mStackTraceLine; |
|---|
| 47 | |
|---|
| 48 | /** Flag whether an 'at ...' line. */ |
|---|
| 49 | private final boolean mIsLocationLine; |
|---|
| 50 | /** Flag whether an 'Caused by: ' line. */ |
|---|
| 51 | private final boolean mIsCauseLine; |
|---|
| 52 | /** Flag whether an exception message line. */ |
|---|
| 53 | private final boolean mIsExceptionMessageLine; |
|---|
| 54 | /** Flag whether an '... nnn more ' line. */ |
|---|
| 55 | private final boolean mIsMoreLine; |
|---|
| 56 | |
|---|
| 57 | private final CharBuffer mClassName; |
|---|
| 58 | private final CharBuffer mMethodName; |
|---|
| 59 | private final int mLine; |
|---|
| 60 | private final int mMoreLines; |
|---|
| 61 | |
|---|
| 62 | private final CharBuffer mExceptionText; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Constructs this and sets the information of an 'at ...' stack trace line. |
|---|
| 66 | * |
|---|
| 67 | * @param lineData Contains the original line data. |
|---|
| 68 | * @param className The parsed class name. |
|---|
| 69 | * @param methodName The parsed method name. |
|---|
| 70 | * @param line The parsed line number, is <= 0 if not available. |
|---|
| 71 | */ |
|---|
| 72 | StackTraceInfo ( |
|---|
| 73 | final CharBuffer lineData, |
|---|
| 74 | final CharBuffer className, |
|---|
| 75 | final CharBuffer methodName, |
|---|
| 76 | final int line) |
|---|
| 77 | { |
|---|
| 78 | mStackTraceLine = lineData; |
|---|
| 79 | |
|---|
| 80 | mIsLocationLine = true; |
|---|
| 81 | mIsCauseLine = false; |
|---|
| 82 | mIsExceptionMessageLine = false; |
|---|
| 83 | mIsMoreLine = false; |
|---|
| 84 | mClassName = className; |
|---|
| 85 | mMethodName = methodName; |
|---|
| 86 | mLine = line; |
|---|
| 87 | mMoreLines = -1; |
|---|
| 88 | mExceptionText = null; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Constructs this and sets the information of an '... nnn more' stack trace |
|---|
| 93 | * line. |
|---|
| 94 | * |
|---|
| 95 | * @param lineData Contains the original line data. |
|---|
| 96 | * @param lines The parsed number of lines being omitted. |
|---|
| 97 | */ |
|---|
| 98 | StackTraceInfo ( |
|---|
| 99 | final CharBuffer lineData, |
|---|
| 100 | final int lines) |
|---|
| 101 | { |
|---|
| 102 | mStackTraceLine = lineData; |
|---|
| 103 | |
|---|
| 104 | mIsLocationLine = false; |
|---|
| 105 | mIsCauseLine = false; |
|---|
| 106 | mIsExceptionMessageLine = false; |
|---|
| 107 | mIsMoreLine = true; |
|---|
| 108 | mClassName = null; |
|---|
| 109 | mMethodName = null; |
|---|
| 110 | mLine = -1; |
|---|
| 111 | mMoreLines = lines; |
|---|
| 112 | mExceptionText = null; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Constructs this and sets the information of an stack trace line, which |
|---|
| 117 | * contains the exception message. |
|---|
| 118 | * |
|---|
| 119 | * @param lineData Contains the original line data. |
|---|
| 120 | * @param message The exception message as it is parsed from |
|---|
| 121 | * <code>lineData</code>. |
|---|
| 122 | * @param isCause Flag whether the exception is a cause, i.e. whether the |
|---|
| 123 | * message ahs a 'Caused by: ' prefix. |
|---|
| 124 | */ |
|---|
| 125 | StackTraceInfo ( |
|---|
| 126 | final CharBuffer lineData, |
|---|
| 127 | final CharBuffer message, |
|---|
| 128 | final boolean isCause) |
|---|
| 129 | { |
|---|
| 130 | mStackTraceLine = lineData; |
|---|
| 131 | |
|---|
| 132 | mIsLocationLine = false; |
|---|
| 133 | mIsCauseLine = isCause; |
|---|
| 134 | mIsExceptionMessageLine = ! isCause; |
|---|
| 135 | mIsMoreLine = false; |
|---|
| 136 | mClassName = null; |
|---|
| 137 | mMethodName = null; |
|---|
| 138 | mLine = -1; |
|---|
| 139 | mMoreLines = -1; |
|---|
| 140 | mExceptionText = message; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Returns the wrapped stack trace line as string. |
|---|
| 145 | * |
|---|
| 146 | * @return The stack trace line. |
|---|
| 147 | * |
|---|
| 148 | * @see java.lang.Object#toString() |
|---|
| 149 | */ |
|---|
| 150 | public String toString () |
|---|
| 151 | { |
|---|
| 152 | return String.valueOf(mStackTraceLine); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | * Gets flag whether the current line is a caused-by line. |
|---|
| 157 | * |
|---|
| 158 | * @return true if the current line is a caused-by line; false, else. |
|---|
| 159 | */ |
|---|
| 160 | boolean isCauseLine () |
|---|
| 161 | { |
|---|
| 162 | return mIsCauseLine; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * Gets flag whether the current line contained the exception message with or |
|---|
| 167 | * without the 'Caused by:' prefix. So whenever {@linkplain #isCauseLine()} |
|---|
| 168 | * returns true, this returns true as well. |
|---|
| 169 | * |
|---|
| 170 | * @return true if the current line contained the exception message with or |
|---|
| 171 | * without the cause-by prefix; false, else |
|---|
| 172 | */ |
|---|
| 173 | boolean isExceptionMessageLine () |
|---|
| 174 | { |
|---|
| 175 | return (mIsExceptionMessageLine || mIsCauseLine); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * Gets the exception message if the last line has been a cause line or a |
|---|
| 180 | * exception message line. |
|---|
| 181 | * |
|---|
| 182 | * @return Returns the exception message. |
|---|
| 183 | * |
|---|
| 184 | * @throws IllegalStateException if the recent line was not a cause line or |
|---|
| 185 | * exception message line. |
|---|
| 186 | */ |
|---|
| 187 | String getExceptionMessage () |
|---|
| 188 | { |
|---|
| 189 | if (! (mIsCauseLine || mIsExceptionMessageLine)) |
|---|
| 190 | { |
|---|
| 191 | throw new IllegalStateException("The current line did not contain the " |
|---|
| 192 | + "exception message"); |
|---|
| 193 | } |
|---|
| 194 | return mExceptionText.toString(); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /** |
|---|
| 198 | * Gets flag whether the current line is an at ... line. |
|---|
| 199 | * |
|---|
| 200 | * @return true if the current line is an at ... line; false, else. |
|---|
| 201 | */ |
|---|
| 202 | boolean isLocationLine () |
|---|
| 203 | { |
|---|
| 204 | return mIsLocationLine; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Gets the class name from an at-line. |
|---|
| 209 | * |
|---|
| 210 | * @return class name of an at-line |
|---|
| 211 | * |
|---|
| 212 | * @throws IllegalStateException if the recent line is not an at-line. |
|---|
| 213 | */ |
|---|
| 214 | String getClassName () |
|---|
| 215 | { |
|---|
| 216 | if (! mIsLocationLine) |
|---|
| 217 | { |
|---|
| 218 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT |
|---|
| 219 | + "'at ...' line"); |
|---|
| 220 | } |
|---|
| 221 | return mClassName.toString(); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * Gets the method name from an at-line. |
|---|
| 226 | * |
|---|
| 227 | * @return method of an at-line |
|---|
| 228 | * |
|---|
| 229 | * @throws IllegalStateException if the recent line is not an at-line. |
|---|
| 230 | */ |
|---|
| 231 | String getMethodName () |
|---|
| 232 | { |
|---|
| 233 | if (! mIsLocationLine) |
|---|
| 234 | { |
|---|
| 235 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT |
|---|
| 236 | + "'at ...' line"); |
|---|
| 237 | } |
|---|
| 238 | return mMethodName.toString(); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * Gests the line info of an at-line. This might not be accessible, in which |
|---|
| 243 | * case 0 is returned. |
|---|
| 244 | * |
|---|
| 245 | * @return line information of at-line or 0 if no such. |
|---|
| 246 | * |
|---|
| 247 | * @throws IllegalStateException if the recent line is not an at-line. |
|---|
| 248 | */ |
|---|
| 249 | int getLine () |
|---|
| 250 | { |
|---|
| 251 | if (! mIsLocationLine) |
|---|
| 252 | { |
|---|
| 253 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT |
|---|
| 254 | + "'at ...' line"); |
|---|
| 255 | } |
|---|
| 256 | return mLine; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | /** |
|---|
| 260 | * Gets flag whether the current line is an ... nnn more line. |
|---|
| 261 | * |
|---|
| 262 | * @return true if the current line is an ... more line; false, else. |
|---|
| 263 | */ |
|---|
| 264 | boolean isMoreLine () |
|---|
| 265 | { |
|---|
| 266 | return mIsMoreLine; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | /** |
|---|
| 270 | * Gets the number of stack trace items being concentrated into the more |
|---|
| 271 | * line. |
|---|
| 272 | * |
|---|
| 273 | * @return number of stack trace lines being neglected. |
|---|
| 274 | * |
|---|
| 275 | * @throws IllegalStateException if the recent line was not a more-line. |
|---|
| 276 | */ |
|---|
| 277 | int getMoreLines () |
|---|
| 278 | { |
|---|
| 279 | if (! mIsMoreLine) |
|---|
| 280 | { |
|---|
| 281 | throw new IllegalStateException(CURRENT_LINE_ERROR_TEXT |
|---|
| 282 | + "'... nnn more' line"); |
|---|
| 283 | } |
|---|
| 284 | return mMoreLines; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|