View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.PrintStream; 58 import java.io.PrintWriter; 59 import java.io.StringWriter; 60 import java.util.LinkedList; 61 import java.util.StringTokenizer; 62 63 /*** 64 * This is a base class of runtime exeptions thrown by Turbine. 65 * 66 * This class represents a non-checked type exception (see 67 * {@see java.lang.RuntimeException}). It has the nested stack trace 68 * functionality found in the {@see TurbineException} class. 69 * 70 * It's sad that this class is a straight copy/paste of Turbine exception. 71 * I wish that Java supported NonCheckedException marker interface... 72 * 73 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 74 */ 75 public class TurbineRuntimeException extends RuntimeException 76 { 77 /*** 78 * Holds the reference to the exception or error that caused 79 * this exception to be thrown. 80 */ 81 private Throwable nested = null; 82 83 /*** 84 * Constructs a new <code>TurbineRuntimeException</code> without specified 85 * detail message. 86 */ 87 public TurbineRuntimeException() 88 { 89 super(); 90 } 91 92 /*** 93 * Constructs a new <code>TurbineRuntimeException</code> with specified 94 * detail message. 95 * 96 * @param msg the error message. 97 */ 98 public TurbineRuntimeException(String msg) 99 { 100 super(msg); 101 } 102 103 /*** 104 * Constructs a new <code>TurbineRuntimeException</code> with specified 105 * nested <code>Throwable</code>. 106 * 107 * @param nested the exception or error that caused this exception 108 * to be thrown. 109 */ 110 public TurbineRuntimeException(Throwable nested) 111 { 112 super(); 113 this.nested = nested; 114 } 115 116 /*** 117 * Constructs a new <code>TurbineRuntimeException</code> with specified 118 * detail message and nested <code>Throwable</code>. 119 * 120 * @param msg the error message. 121 * @param nested the exception or error that caused this exception 122 * to be thrown. 123 */ 124 public TurbineRuntimeException(String msg, Throwable nested) 125 { 126 super(msg); 127 this.nested = nested; 128 } 129 130 /*** 131 * Prints the stack trace of this exception the the standar error 132 * stream. 133 */ 134 public void printStackTrace() 135 { 136 synchronized(System.err) 137 { 138 printStackTrace(System.err); 139 } 140 } 141 142 /*** 143 * Prints the stack trace of this exception to the specified print stream. 144 * 145 * @param out <code>PrintStream</code> to use for output 146 */ 147 public void printStackTrace(PrintStream out) 148 { 149 synchronized(out) 150 { 151 PrintWriter pw=new PrintWriter(out, false); 152 printStackTrace(pw); 153 // flush the PrintWriter before it's GCed 154 pw.flush(); 155 } 156 } 157 158 /*** 159 * Prints the stack trace of this exception to the specified print writer. 160 * 161 * @param out <code>PrintWriter</code> to use for output. 162 */ 163 public void printStackTrace(PrintWriter out) 164 { 165 synchronized(out) 166 { 167 printStackTrace(out, 0); 168 } 169 } 170 171 /*** 172 * Prints the stack trace of this exception skiping a specified number 173 * of stack frames. 174 * 175 * @param out <code>PrintWriter</code> to use for output. 176 * @param skip the numbere of stack frames to skip. 177 */ 178 public void printStackTrace(PrintWriter out, int skip) 179 { 180 String[] st = captureStackTrace(); 181 if(nested != null) 182 { 183 if(nested instanceof TurbineRuntimeException) 184 { 185 ((TurbineRuntimeException)nested).printStackTrace(out, st.length - 2); 186 } 187 else if(nested instanceof TurbineException) 188 { 189 ((TurbineException)nested).printStackTrace(out, st.length - 2); 190 } 191 else 192 { 193 String[] nst = captureStackTrace(nested); 194 for(int i = 0; i<nst.length - st.length + 2; i++) 195 { 196 out.println(nst[i]); 197 } 198 } 199 out.print("rethrown as "); 200 } 201 for(int i=0; i<st.length - skip; i++) 202 { 203 out.println(st[i]); 204 } 205 } 206 207 /*** 208 * Captures the stack trace associated with this exception. 209 * 210 * @return an array of Strings describing stack frames. 211 */ 212 private String[] captureStackTrace() 213 { 214 StringWriter sw = new StringWriter(); 215 super.printStackTrace(new PrintWriter(sw, true)); 216 return splitStackTrace(sw.getBuffer().toString()); 217 } 218 219 /*** 220 * Captures the stack trace associated with a <code>Throwable</code> 221 * object. 222 * 223 * @param t the <code>Throwable</code>. 224 * @return an array of Strings describing stack frames. 225 */ 226 private String[] captureStackTrace(Throwable t) 227 { 228 StringWriter sw = new StringWriter(); 229 t.printStackTrace(new PrintWriter(sw, true)); 230 return splitStackTrace(sw.getBuffer().toString()); 231 } 232 233 /*** 234 * Splits the stack trace given as a newline separated string 235 * into an array of stack frames. 236 * 237 * @param stackTrace the stack trace. 238 * @return an array of Strings describing stack frames. 239 */ 240 private String[] splitStackTrace(String stackTrace) 241 { 242 String linebreak = System.getProperty("line.separator"); 243 StringTokenizer st = new StringTokenizer(stackTrace, linebreak); 244 LinkedList list = new LinkedList(); 245 while(st.hasMoreTokens()) 246 { 247 list.add(st.nextToken()); 248 } 249 return (String [])list.toArray(new String[] {}); 250 } 251 }

This page was automatically generated by Maven