KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > Error


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30
31 /**
32  *
33  *
34  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
35  */

36 public final class Error
37 {
38     private Throwable JavaDoc throwable;
39     private Throwable JavaDoc cause;
40
41     /**
42      *
43      */

44     public Error(Throwable JavaDoc throwable, Throwable JavaDoc cause)
45     {
46         this.throwable = throwable;
47         this.cause = cause;
48     }
49
50
51
52   /**
53    *
54    */

55   public String JavaDoc getName() {
56     final String JavaDoc fullyQualifiedName = this.throwable.getClass().getName();
57     final int index = (fullyQualifiedName.lastIndexOf(".") == -1) ? 0 : fullyQualifiedName.lastIndexOf(".") + 1;
58     return fullyQualifiedName.substring(index);
59   }
60
61   /**
62    *
63    */

64   public String JavaDoc getMessage() {
65     return this.throwable.getMessage();
66   }
67
68   /**
69    *
70    */

71   public String JavaDoc getStackTrace() {
72     return getStackTrace(this.throwable);
73   }
74
75   /**
76    *
77    */

78   public boolean hasCause() {
79     return this.cause != null;
80   }
81
82   /**
83    *
84    */

85   public String JavaDoc getCauseMessage() {
86     return hasCause() ? this.cause.getMessage() : "";
87   }
88
89   /**
90    *
91    */

92   public String JavaDoc getCauseStackTrace() {
93     return hasCause() ? getStackTrace(this.cause) : "";
94   }
95
96   
97
98   // --- [X implementation] ----------------------------------------------------
99
// --- [X Overrides] ---------------------------------------------------------
100
// --- [Package protected] ---------------------------------------------------
101
// --- [Private] -------------------------------------------------------------
102

103   /**
104    *
105    */

106   private String JavaDoc getStackTrace(Throwable JavaDoc throwable) {
107     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
108     final StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(stackTraceToString(throwable), "\n");
109     
110     while(st.hasMoreTokens()) {
111       sb.append(st.nextToken() + "<br/>");
112     }
113     return sb.toString();
114   }
115
116   /**
117    *
118    */

119   private String JavaDoc stackTraceToString(Throwable JavaDoc throwable) {
120     final StringWriter JavaDoc sw = new StringWriter JavaDoc();
121     final PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
122     
123     throwable.printStackTrace(pw);
124     return sw.getBuffer().toString();
125   }
126
127
128
129   // --- [Protected] -----------------------------------------------------------
130
// --- [Inner classes] -------------------------------------------------------
131
}
Popular Tags