KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > utils > ExceptionUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.utils;
6
7 /**
8  * Jul 8, 2004
9  * @author: Tuan Nguyen
10  * @email: tuan08@users.sourceforge.net
11  * @version: $Id: ExceptionUtil.java,v 1.2 2004/10/05 14:40:47 tuan08 Exp $
12  */

13 public class ExceptionUtil {
14   private static String JavaDoc LINE_SEPARATOR = System.getProperty("line.separator") ;
15   
16    static public String JavaDoc getExoStackTrace(Throwable JavaDoc t) {
17     StackTraceElement JavaDoc[] elements = t.getStackTrace() ;
18     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
19     b.append(t.getMessage()).append(LINE_SEPARATOR) ;
20     boolean appendDot = true ;
21     for(int i = 0; i < elements.length ; i++) {
22       if(i < 10) {
23         b.append(" at ").append(elements[i].toString()).append(LINE_SEPARATOR) ;
24       } else {
25         if(elements[i].getClassName().startsWith("exo.")) {
26           b.append(" at ").append(elements[i].toString()).append(LINE_SEPARATOR) ;
27           appendDot = true ;
28         } else {
29           if(appendDot) {
30             b.append(" [...................................]").append(LINE_SEPARATOR) ;
31             appendDot = false ;
32           }
33         }
34       }
35     }
36     return b.toString() ;
37    }
38     
39    static public String JavaDoc getStackTrace(Throwable JavaDoc t, int numberOfLine) {
40     StackTraceElement JavaDoc[] elements = t.getStackTrace() ;
41     if(numberOfLine > elements.length) numberOfLine = elements.length ;
42     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
43     b.append(t.getMessage()).append(LINE_SEPARATOR) ;
44     for(int i = 0; i < numberOfLine; i++) {
45       b.append(elements[i].toString()).append(LINE_SEPARATOR) ;
46     }
47     return b.toString() ;
48   }
49    
50   static public Throwable JavaDoc getRootCause(Throwable JavaDoc t) {
51     Throwable JavaDoc root = t ;
52     while(root.getCause() != null) {
53       root = root.getCause() ;
54     }
55     return root ;
56   }
57 }
58
Popular Tags