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.OutputStream; 58 import java.io.PrintStream; 59 import java.io.PrintWriter; 60 import java.io.StringWriter; 61 import java.io.Writer; 62 import java.util.LinkedList; 63 import java.util.StringTokenizer; 64 65 /*** 66 * This is a base class of runtime exeptions thrown by Turbine. 67 * 68 * This class represents a non-checked type exception (see 69 * {@see java.lang.RuntimeException}). It has the nested stack trace 70 * functionality found in the {@see TurbineException} class. 71 * 72 * It's sad that this class is a straight copy/paste of Turbine exception. 73 * I wish that Java supported NonCheckedException marker interface... 74 * 75 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 76 */ 77 public class TurbineRuntimeException extends RuntimeException 78 { 79 /*** 80 * Holds the reference to the exception or error that caused 81 * this exception to be thrown. 82 */ 83 private Throwable nested = null; 84 85 /*** 86 * Constructs a new <code>TurbineRuntimeException</code> without specified 87 * detail message. 88 */ 89 public TurbineRuntimeException() 90 { 91 super(); 92 } 93 94 /*** 95 * Constructs a new <code>TurbineRuntimeException</code> with specified 96 * detail message. 97 * 98 * @param msg the error message. 99 */ 100 public TurbineRuntimeException(String msg) 101 { 102 super(msg); 103 } 104 105 /*** 106 * Constructs a new <code>TurbineRuntimeException</code> with specified 107 * nested <code>Throwable</code>. 108 * 109 * @param nested the exception or error that caused this exception 110 * to be thrown. 111 */ 112 public TurbineRuntimeException(Throwable nested) 113 { 114 super(); 115 this.nested = nested; 116 } 117 118 /*** 119 * Constructs a new <code>TurbineRuntimeException</code> with specified 120 * detail message and nested <code>Throwable</code>. 121 * 122 * @param msg the error message. 123 * @param nested the exception or error that caused this exception 124 * to be thrown. 125 */ 126 public TurbineRuntimeException(String msg, Throwable nested) 127 { 128 super(msg); 129 this.nested = nested; 130 } 131 132 /*** 133 * Prints the stack trace of this exception the the standar error 134 * stream. 135 */ 136 public void printStackTrace() 137 { 138 synchronized(System.err) 139 { 140 printStackTrace(System.err); 141 } 142 } 143 144 /*** 145 * Prints the stack trace of this exception to the specified print stream. 146 * 147 * @param out <code>PrintStream</code> to use for output 148 */ 149 public void printStackTrace(PrintStream out) 150 { 151 synchronized(out) 152 { 153 PrintWriter pw=new PrintWriter(out, false); 154 printStackTrace(pw); 155 // flush the PrintWriter before it's GCed 156 pw.flush(); 157 } 158 } 159 160 /*** 161 * Prints the stack trace of this exception to the specified print writer. 162 * 163 * @param out <code>PrintWriter</code> to use for output. 164 */ 165 public void printStackTrace(PrintWriter out) 166 { 167 synchronized(out) 168 { 169 printStackTrace(out, 0); 170 } 171 } 172 173 /*** 174 * Prints the stack trace of this exception skiping a specified number 175 * of stack frames. 176 * 177 * @param out <code>PrintWriter</code> to use for output. 178 * @param skip the numbere of stack frames to skip. 179 */ 180 public void printStackTrace(PrintWriter out, int skip) 181 { 182 String[] st = captureStackTrace(); 183 if(nested != null) 184 { 185 if(nested instanceof TurbineRuntimeException) 186 { 187 ((TurbineRuntimeException)nested).printStackTrace(out, st.length - 2); 188 } 189 else if(nested instanceof TurbineException) 190 { 191 ((TurbineException)nested).printStackTrace(out, st.length - 2); 192 } 193 else 194 { 195 String[] nst = captureStackTrace(nested); 196 for(int i = 0; i<nst.length - st.length + 2; i++) 197 { 198 out.println(nst[i]); 199 } 200 } 201 out.print("rethrown as "); 202 } 203 for(int i=0; i<st.length - skip; i++) 204 { 205 out.println(st[i]); 206 } 207 } 208 209 /*** 210 * Captures the stack trace associated with this exception. 211 * 212 * @return an array of Strings describing stack frames. 213 */ 214 private String[] captureStackTrace() 215 { 216 StringWriter sw = new StringWriter(); 217 super.printStackTrace(new PrintWriter(sw, true)); 218 return splitStackTrace(sw.getBuffer().toString()); 219 } 220 221 /*** 222 * Captures the stack trace associated with a <code>Throwable</code> 223 * object. 224 * 225 * @param t the <code>Throwable</code>. 226 * @return an array of Strings describing stack frames. 227 */ 228 private String[] captureStackTrace(Throwable t) 229 { 230 StringWriter sw = new StringWriter(); 231 t.printStackTrace(new PrintWriter(sw, true)); 232 return splitStackTrace(sw.getBuffer().toString()); 233 } 234 235 /*** 236 * Splits the stack trace given as a newline separated string 237 * into an array of stack frames. 238 * 239 * @param stackTrace the stack trace. 240 * @return an array of Strings describing stack frames. 241 */ 242 private String[] splitStackTrace(String stackTrace) 243 { 244 String linebreak = System.getProperty("line.separator"); 245 StringTokenizer st = new StringTokenizer(stackTrace, linebreak); 246 LinkedList list = new LinkedList(); 247 while(st.hasMoreTokens()) 248 { 249 list.add(st.nextToken()); 250 } 251 return (String [])list.toArray(new String[] {}); 252 } 253 }

This page was automatically generated by Maven